@routineless/nx-aws-cdk
Version:
Nx plugin for AWS CDK
50 lines • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveReexportingName = exports.resolveRelativePath = exports.isRequireExpression = void 0;
const tslib_1 = require("tslib");
const cdk_1 = require("@routineless/cdk");
const ast_types_1 = require("ast-types");
const fs_extra_1 = require("fs-extra");
const path_1 = tslib_1.__importDefault(require("path"));
const extensions = ['.js', '.mjs', '.cjs'];
const isRequireExpression = (node) => {
return ast_types_1.namedTypes.Identifier.check(node.callee) && node.callee.name === 'require';
};
exports.isRequireExpression = isRequireExpression;
const resolveRelativePath = (importingFile, relativePath) => {
if (!relativePath.startsWith('.')) {
return relativePath;
}
const containingDir = path_1.default.dirname(importingFile);
if ((0, fs_extra_1.existsSync)(path_1.default.join(containingDir, relativePath)) &&
!(0, fs_extra_1.statSync)(path_1.default.join(containingDir, relativePath)).isDirectory()) {
// if the file already exists, e.g. .css files, just use it
return relativePath;
}
// strip the file extension if applicable
const extensonlessPath = relativePath.replace(/\.(m|c)?js$/, '');
for (const extension of extensions) {
let candidate = `${extensonlessPath}${extension}`;
if ((0, fs_extra_1.existsSync)(path_1.default.join(containingDir, candidate))) {
return candidate;
}
candidate = `${relativePath}/index${extension}`;
if ((0, fs_extra_1.existsSync)(path_1.default.join(containingDir, candidate))) {
return candidate;
}
}
throw new Error(`Could not resolve relative path ${relativePath} from ${importingFile}`);
};
exports.resolveRelativePath = resolveRelativePath;
const resolveReexportingName = (importedModule, importSpecifier) => {
const normalizedImportedModule = importedModule.replace(/[/.]/g, '-').replaceAll('@', '');
if (importSpecifier.namespace) {
return `${(0, cdk_1.toCamelCase)(normalizedImportedModule)}Namespace`;
}
if (importSpecifier.default) {
return `${(0, cdk_1.toCamelCase)(normalizedImportedModule)}Default`;
}
return (0, cdk_1.toCamelCase)(`${normalizedImportedModule}-${importSpecifier.named}`);
};
exports.resolveReexportingName = resolveReexportingName;
//# sourceMappingURL=modifications-util.js.map