diff --git a/packages/core/src/lib/template-engine/built-in-extensions/index.ts b/packages/core/src/lib/template-engine/built-in-extensions/index.ts deleted file mode 100644 index 1449f292..00000000 --- a/packages/core/src/lib/template-engine/built-in-extensions/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -import './query-builder'; -import './custom-error'; -import './sql-helper'; -import './validator'; diff --git a/packages/core/src/lib/template-engine/extension-loader/loader.ts b/packages/core/src/lib/template-engine/extension-loader/loader.ts index b81543d3..a127e8e0 100644 --- a/packages/core/src/lib/template-engine/extension-loader/loader.ts +++ b/packages/core/src/lib/template-engine/extension-loader/loader.ts @@ -1,5 +1,3 @@ -// Import built in extensions to ensure TypeScript compiler includes them. -import '../built-in-extensions'; import * as fs from 'fs/promises'; import * as path from 'path'; import { flatten } from 'lodash'; diff --git a/packages/core/tsconfig.lib.json b/packages/core/tsconfig.lib.json index 1925baa1..579737f7 100644 --- a/packages/core/tsconfig.lib.json +++ b/packages/core/tsconfig.lib.json @@ -6,5 +6,10 @@ "types": [] }, "include": ["**/*.ts", "../../types/*.d.ts"], - "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"] + "exclude": [ + "jest.config.ts", + "**/*.spec.ts", + "**/*.test.ts", + "./test/**/*.ts" + ] } diff --git a/tools/scripts/replaceAlias.ts b/tools/scripts/replaceAlias.ts new file mode 100644 index 00000000..b0d1d521 --- /dev/null +++ b/tools/scripts/replaceAlias.ts @@ -0,0 +1,132 @@ +import * as glob from 'glob'; +import * as path from 'path'; +import * as fs from 'fs/promises'; + +const requireRegex = (packageName: string) => + new RegExp(`require\\(['"](@vulcan\/${packageName}[^'"]*)['"]\\)`, 'g'); +const importRegex = (packageName: string) => + new RegExp(`(import|from) ['"](@vulcan\\/${packageName}\\/[^'"]*)['"]`, 'g'); + +async function getFiles(packageName: string): Promise { + return new Promise((resolve, reject) => { + glob( + path.resolve( + __dirname, + '..', + '..', + 'dist', + 'packages', + packageName, + '**', + '*.+(ts|js)' + ), + { nodir: true }, + (err, files) => { + if (err) return reject(err); + else return resolve(files); + } + ); + }); +} + +function getRelativePathFromRoot( + filePath: string, + importPath: string, + packageName: string +) { + return path.relative( + path.dirname(filePath), + path.resolve( + __dirname, + '..', + '..', + 'dist', + 'packages', + packageName, + importPath + ) + ); +} + +async function replaceFile( + filePath: string, + { + packageName, + aliasMappings, + }: { + packageName: string; + aliasMappings: { regex: RegExp; replacePath: string }[]; + } +) { + let content = await fs.readFile(filePath, 'utf8'); + + const getReplacePath = (importPath: string) => { + const aliasMapping = aliasMappings.find((mapping) => + mapping.regex.test(importPath) + ); + if (aliasMapping) { + return importPath.replace( + aliasMapping.regex, + getRelativePathFromRoot(filePath, aliasMapping.replacePath, packageName) + ); + } + return importPath; + }; + + const replacePath = (matched: string, subMatched: string) => { + const index = matched.indexOf(subMatched); + return ( + matched.substring(0, index) + + getReplacePath(subMatched) + + matched.substring(index + subMatched.length) + ); + }; + + content = content.replace(requireRegex(packageName), replacePath); + content = content.replace(importRegex(packageName), (matched, _, p2) => + replacePath(matched, p2) + ); + await fs.writeFile(filePath, content, 'utf8'); +} + +async function replacePackage(packageName: string) { + // Read ts config + const tsConfig = JSON.parse( + await fs.readFile( + path.resolve(__dirname, '..', '..', 'tsconfig.base.json'), + 'utf8' + ) + ); + const alias = tsConfig.compilerOptions.paths; + const aliasMappings = Object.keys(alias) + .filter((aliasKey) => aliasKey.startsWith(`@vulcan/${packageName}`)) + .map((aliasKey) => { + // There are two kinds of key @vulcan/package/path and @vulcan/package/path/* + const regex = aliasKey.endsWith('/*') + ? new RegExp(`${aliasKey.substring(0, aliasKey.length - 2)}`) + : new RegExp(`${aliasKey}$`); + // There are two kinds of path @vulcan/package/path and @vulcan/package/path/* + const absPath = path.resolve( + ...alias[aliasKey].map((path) => + path.endsWith('/*') ? path.substring(0, path.length - 2) : path + ) + ); + // Get the relative path from package root + const replacePath = path.relative( + path.resolve(__dirname, '..', '..', 'packages', packageName), + absPath + ); + return { + regex, + replacePath: replacePath, + }; + }); + const files = await getFiles(packageName); + for (const file of files) { + await replaceFile(file, { packageName, aliasMappings }); + } +} + +replacePackage(process.argv[2]) + .then(() => console.log('done')) + .catch(console.error);