Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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';
Expand Down
7 changes: 6 additions & 1 deletion packages/core/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
132 changes: 132 additions & 0 deletions tools/scripts/replaceAlias.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> {
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);