declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
74 lines • 4.16 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.importExportsFromFile = void 0;
const ts_node_1 = require("ts-node");
const UnexpectedCodePathError_1 = require("../../logic/UnexpectedCodePathError");
const nodeModulesToTSNodeTranspileOnImport = [];
const isExplicitImportToNodeModule = (filePath) => new RegExp(/node_modules\//).test(filePath);
const extractNodeModuleNameFromFilePath = (filePath) => { var _a, _b; return (_b = ((_a = new RegExp(/node_modules\/([\w-]+)\//).exec(filePath)) !== null && _a !== void 0 ? _a : [])[1]) !== null && _b !== void 0 ? _b : null; };
const addNodeModuleToListOfModulesToAllowTranspilation = ({ nodeModuleName, }) => {
// check that this is not a redundant op
if (nodeModulesToTSNodeTranspileOnImport.includes(nodeModuleName))
return; // do nothing if its already supported
// add this module to the list, for future reference
nodeModulesToTSNodeTranspileOnImport.push(nodeModuleName);
// now update ts-node registration to include this as a supported module
(0, ts_node_1.register)({
typeCheck: false,
transpileOnly: true,
files: true,
ignore: nodeModulesToTSNodeTranspileOnImport.map((moduleName) => `/node_modules/(?!${moduleName})/`),
skipProject: true,
compilerOptions: {
esModuleInterop: true,
},
});
};
/**
* runs `import(filePath)` on the file path, while also considering whether or not we need to update ts-node to support transpiling it (e.g., if its in `/node_modules/` dir)
*
* note: this fn keeps track of which node modules we've been asked to explicitly import from - and tells tsnode to parse them by adding them to the list of not ignored modules
*/
const importExportsFromFile = (_a) => __awaiter(void 0, [_a], void 0, function* ({ filePath, }) {
if (isExplicitImportToNodeModule(filePath)) {
const nodeModuleName = extractNodeModuleNameFromFilePath(filePath);
if (!nodeModuleName)
throw new UnexpectedCodePathError_1.UnexpectedCodePathError('got a file path attempting to import a node module but could not extract node module name from it');
addNodeModuleToListOfModulesToAllowTranspilation({ nodeModuleName });
}
return Promise.resolve(`${filePath}`).then(s => __importStar(require(s)));
});
exports.importExportsFromFile = importExportsFromFile;
//# sourceMappingURL=importExportsFromFile.js.map