|
| 1 | +import * as glob from 'glob'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as fs from 'fs/promises'; |
| 4 | + |
| 5 | +const requireRegex = (packageName: string) => |
| 6 | + new RegExp(`require\\(['"](@vulcan\/${packageName}[^'"]*)['"]\\)`, 'g'); |
| 7 | +const importRegex = (packageName: string) => |
| 8 | + new RegExp(`(import|from) ['"](@vulcan\\/${packageName}\\/[^'"]*)['"]`, 'g'); |
| 9 | + |
| 10 | +async function getFiles(packageName: string): Promise<string[]> { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + glob( |
| 13 | + path.resolve( |
| 14 | + __dirname, |
| 15 | + '..', |
| 16 | + '..', |
| 17 | + 'dist', |
| 18 | + 'packages', |
| 19 | + packageName, |
| 20 | + '**', |
| 21 | + '*.+(ts|js)' |
| 22 | + ), |
| 23 | + { nodir: true }, |
| 24 | + (err, files) => { |
| 25 | + if (err) return reject(err); |
| 26 | + else return resolve(files); |
| 27 | + } |
| 28 | + ); |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +function getRelativePathFromRoot( |
| 33 | + filePath: string, |
| 34 | + importPath: string, |
| 35 | + packageName: string |
| 36 | +) { |
| 37 | + return path.relative( |
| 38 | + path.dirname(filePath), |
| 39 | + path.resolve( |
| 40 | + __dirname, |
| 41 | + '..', |
| 42 | + '..', |
| 43 | + 'dist', |
| 44 | + 'packages', |
| 45 | + packageName, |
| 46 | + importPath |
| 47 | + ) |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +async function replaceFile( |
| 52 | + filePath: string, |
| 53 | + { |
| 54 | + packageName, |
| 55 | + aliasMappings, |
| 56 | + }: { |
| 57 | + packageName: string; |
| 58 | + aliasMappings: { regex: RegExp; replacePath: string }[]; |
| 59 | + } |
| 60 | +) { |
| 61 | + let content = await fs.readFile(filePath, 'utf8'); |
| 62 | + |
| 63 | + const getReplacePath = (importPath: string) => { |
| 64 | + const aliasMapping = aliasMappings.find((mapping) => |
| 65 | + mapping.regex.test(importPath) |
| 66 | + ); |
| 67 | + if (aliasMapping) { |
| 68 | + return importPath.replace( |
| 69 | + aliasMapping.regex, |
| 70 | + getRelativePathFromRoot(filePath, aliasMapping.replacePath, packageName) |
| 71 | + ); |
| 72 | + } |
| 73 | + return importPath; |
| 74 | + }; |
| 75 | + |
| 76 | + const replacePath = (matched: string, subMatched: string) => { |
| 77 | + const index = matched.indexOf(subMatched); |
| 78 | + return ( |
| 79 | + matched.substring(0, index) + |
| 80 | + getReplacePath(subMatched) + |
| 81 | + matched.substring(index + subMatched.length) |
| 82 | + ); |
| 83 | + }; |
| 84 | + |
| 85 | + content = content.replace(requireRegex(packageName), replacePath); |
| 86 | + content = content.replace(importRegex(packageName), (matched, _, p2) => |
| 87 | + replacePath(matched, p2) |
| 88 | + ); |
| 89 | + await fs.writeFile(filePath, content, 'utf8'); |
| 90 | +} |
| 91 | + |
| 92 | +async function replacePackage(packageName: string) { |
| 93 | + // Read ts config |
| 94 | + const tsConfig = JSON.parse( |
| 95 | + await fs.readFile( |
| 96 | + path.resolve(__dirname, '..', '..', 'tsconfig.base.json'), |
| 97 | + 'utf8' |
| 98 | + ) |
| 99 | + ); |
| 100 | + const alias = tsConfig.compilerOptions.paths; |
| 101 | + const aliasMappings = Object.keys(alias) |
| 102 | + .filter((aliasKey) => aliasKey.startsWith(`@vulcan/${packageName}`)) |
| 103 | + .map((aliasKey) => { |
| 104 | + // There are two kinds of key @vulcan/package/path and @vulcan/package/path/* |
| 105 | + const regex = aliasKey.endsWith('/*') |
| 106 | + ? new RegExp(`${aliasKey.substring(0, aliasKey.length - 2)}`) |
| 107 | + : new RegExp(`${aliasKey}$`); |
| 108 | + // There are two kinds of path @vulcan/package/path and @vulcan/package/path/* |
| 109 | + const absPath = path.resolve( |
| 110 | + ...alias[aliasKey].map((path) => |
| 111 | + path.endsWith('/*') ? path.substring(0, path.length - 2) : path |
| 112 | + ) |
| 113 | + ); |
| 114 | + // Get the relative path from package root |
| 115 | + const replacePath = path.relative( |
| 116 | + path.resolve(__dirname, '..', '..', 'packages', packageName), |
| 117 | + absPath |
| 118 | + ); |
| 119 | + return { |
| 120 | + regex, |
| 121 | + replacePath: replacePath, |
| 122 | + }; |
| 123 | + }); |
| 124 | + const files = await getFiles(packageName); |
| 125 | + for (const file of files) { |
| 126 | + await replaceFile(file, { packageName, aliasMappings }); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +replacePackage(process.argv[2]) |
| 131 | + .then(() => console.log('done')) |
| 132 | + .catch(console.error); |
0 commit comments