UNPKG

@wroud/di-tools-codemod

Version:

@wroud/di-tools-codemod is a codemod utility that automates the migration of your codebase from Inversify to @wroud/di. It streamlines the transformation process, allowing for effortless and customizable transitions through configuration support.

117 lines (116 loc) 5.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addServiceToModule = addServiceToModule; const findSrcDir_js_1 = require("./findSrcDir.js"); const node_path_1 = __importDefault(require("node:path")); const node_fs_1 = __importDefault(require("node:fs")); const getModulePath_js_1 = require("./getModulePath.js"); function addServiceToModule(file, j, transformerOptions, classes, replacingPackage) { const srcPath = (0, findSrcDir_js_1.findSrcDir)(file.path); if (!srcPath) { return; } const filePath = (0, getModulePath_js_1.getModulePath)(file, transformerOptions); const packageJsonPath = `${srcPath}/../package.json`; const modulePath = `${srcPath}/module.ts`; let name = node_path_1.default.basename(node_path_1.default.dirname(srcPath)); let moduleRoot = null; if (node_fs_1.default.existsSync(packageJsonPath)) { const packageJson = JSON.parse(node_fs_1.default.readFileSync(packageJsonPath, "utf-8")); if (packageJson.name) { name = packageJson.name; } // if (!packageJson.sideEffects || !Array.isArray(packageJson.sideEffects)) { // packageJson.sideEffects = []; // } // let relativeModulePath = path.relative( // path.dirname(packageJsonPath), // modulePath, // ); // if (!relativeModulePath.startsWith(".")) { // relativeModulePath = "./" + relativeModulePath; // } // if (!packageJson.sideEffects.includes(relativeModulePath)) { // packageJson.sideEffects.push(relativeModulePath); // } // fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); } if (!node_fs_1.default.existsSync(modulePath)) { moduleRoot = j(j.program([ j.importDeclaration.from({ specifiers: [j.importSpecifier(j.identifier("ModuleRegistry"))], source: j.literal(replacingPackage.replace), comments: transformerOptions.copyright ? [j.commentBlock(transformerOptions.copyright, true, true)] : [], }), j.expressionStatement(j.callExpression(j.memberExpression(j.identifier("ModuleRegistry"), j.identifier("add")), [ j.objectExpression([ j.objectProperty(j.identifier("name"), j.literal(name)), j.objectProperty(j.identifier("configure"), j.arrowFunctionExpression([j.identifier("serviceCollection")], j.blockStatement([]))), ]), ])), ])); } if (!moduleRoot) { const moduleSource = node_fs_1.default.readFileSync(modulePath, "utf-8"); moduleRoot = j(moduleSource); } for (const className of classes) { const importPath = "./" + node_path_1.default.relative(node_path_1.default.dirname(modulePath), node_path_1.default.join(filePath)); let classFileImport = moduleRoot.find(j.ImportDeclaration, { source: { value: importPath, }, }); if (!classFileImport.length) { classFileImport = moduleRoot .find(j.ImportDeclaration) .at(0) .insertAfter(j.importDeclaration([], j.literal(importPath))); classFileImport = moduleRoot.find(j.ImportDeclaration, { source: { value: importPath, }, }); } if (!classFileImport.find(j.ImportSpecifier, { imported: { name: className }, }).length) { const freeName = getFreeImportName(j, moduleRoot, className); classFileImport .nodes()[0] ?.specifiers?.push(j.importSpecifier(j.identifier(className), className !== freeName ? j.identifier(freeName) : null)); const block = moduleRoot .find(j.ArrowFunctionExpression, { params: [{ name: "serviceCollection" }], }) .find(j.BlockStatement) .nodes()[0]; if (block) { block.body.push(j.expressionStatement(j.callExpression(j.memberExpression(j.identifier("serviceCollection"), j.identifier("addSingleton")), [j.identifier(freeName)]))); } } } node_fs_1.default.writeFileSync(modulePath, moduleRoot.toSource()); } function getFreeImportName(j, root, baseName) { const importNames = new Set(); root.find(j.ImportDeclaration).forEach((path) => { path.value.specifiers?.forEach((specifier) => { if (specifier.type === "ImportSpecifier") { importNames.add(specifier.local?.name || specifier.imported.name); } }); }); let i = 0; let name = `${baseName}`; while (importNames.has(name)) { i++; name = `${baseName}${i}`; } return name; }