mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
workspace: build: enable ts build cache
This commit is contained in:
1 parent
15b88f00a9
commit
307b26ba4c
1 file changed
+38
-39
+38
-39
@@ -2,6 +2,7 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const dir = path.dirname(path.dirname(require.resolve('@types/node/package.json')));
|
const dir = path.dirname(path.dirname(require.resolve('@types/node/package.json')));
|
||||||
|
const types = fs.readdirSync(dir).filter((i) => !['sharedworker', 'serviceworker'].includes(i));
|
||||||
|
|
||||||
const compilerOptionsBase = {
|
const compilerOptionsBase = {
|
||||||
target: 'es2020',
|
target: 'es2020',
|
||||||
@@ -14,21 +15,19 @@ const compilerOptionsBase = {
|
|||||||
strictBindCallApply: true,
|
strictBindCallApply: true,
|
||||||
experimentalDecorators: true,
|
experimentalDecorators: true,
|
||||||
// emitDecoratorMetadata: true,
|
// emitDecoratorMetadata: true,
|
||||||
noEmit: true,
|
|
||||||
incremental: true,
|
incremental: true,
|
||||||
types: fs.readdirSync(dir).filter((i) => !['sharedworker', 'serviceworker'].includes(i)),
|
types,
|
||||||
};
|
};
|
||||||
|
const baseOutDir = path.resolve(__dirname, '../.cache/ts-out');
|
||||||
const config = {
|
const config = {
|
||||||
compilerOptions: compilerOptionsBase,
|
compilerOptions: compilerOptionsBase,
|
||||||
references: [
|
references: [],
|
||||||
{ path: 'packages/hydrooj' },
|
|
||||||
],
|
|
||||||
files: [],
|
files: [],
|
||||||
};
|
};
|
||||||
const configSrc = {
|
const configSrc = (name) => ({
|
||||||
compilerOptions: {
|
compilerOptions: {
|
||||||
...compilerOptionsBase,
|
...compilerOptionsBase,
|
||||||
outDir: 'src',
|
outDir: path.join(baseOutDir, name),
|
||||||
rootDir: 'src',
|
rootDir: 'src',
|
||||||
},
|
},
|
||||||
include: ['src'],
|
include: ['src'],
|
||||||
@@ -37,18 +36,8 @@ const configSrc = {
|
|||||||
'bin',
|
'bin',
|
||||||
'dist',
|
'dist',
|
||||||
],
|
],
|
||||||
};
|
});
|
||||||
const configFlat = {
|
const configFlat = (name) => (name === 'packages/ui-default' ? {
|
||||||
compilerOptions: {
|
|
||||||
...compilerOptionsBase,
|
|
||||||
outDir: '.',
|
|
||||||
rootDir: '.',
|
|
||||||
},
|
|
||||||
include: ['**/*.ts'],
|
|
||||||
exclude: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
fs.writeFileSync(path.resolve(process.cwd(), 'packages', 'ui-default', 'tsconfig.json'), JSON.stringify({
|
|
||||||
exclude: [
|
exclude: [
|
||||||
'./public',
|
'./public',
|
||||||
],
|
],
|
||||||
@@ -60,40 +49,50 @@ fs.writeFileSync(path.resolve(process.cwd(), 'packages', 'ui-default', 'tsconfig
|
|||||||
target: 'es2020',
|
target: 'es2020',
|
||||||
baseUrl: '.',
|
baseUrl: '.',
|
||||||
moduleResolution: 'node',
|
moduleResolution: 'node',
|
||||||
|
types,
|
||||||
paths: {
|
paths: {
|
||||||
'vj/*': [
|
'vj/*': [
|
||||||
'./*',
|
'./*',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}));
|
} : {
|
||||||
|
compilerOptions: {
|
||||||
|
...compilerOptionsBase,
|
||||||
|
outDir: path.join(baseOutDir, name),
|
||||||
|
rootDir: '.',
|
||||||
|
},
|
||||||
|
include: ['**/*.ts'],
|
||||||
|
exclude: [],
|
||||||
|
});
|
||||||
|
|
||||||
if (!fs.existsSync(path.resolve(process.cwd(), 'plugins'))) {
|
if (!fs.existsSync(path.resolve(process.cwd(), 'plugins'))) {
|
||||||
fs.mkdirSync(path.resolve(process.cwd(), 'plugins'));
|
fs.mkdirSync(path.resolve(process.cwd(), 'plugins'));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const type of ['packages', 'plugins']) {
|
const modules = [
|
||||||
const packages = fs.readdirSync(path.resolve(process.cwd(), type));
|
'packages/hydrooj',
|
||||||
for (const package of packages) {
|
...fs.readdirSync(path.resolve(process.cwd(), 'packages')).map((i) => `packages/${i}`),
|
||||||
if (package === 'ui-default') continue;
|
...fs.readdirSync(path.resolve(process.cwd(), 'plugins')).map((i) => `plugins/${i}`),
|
||||||
const basedir = path.resolve(process.cwd(), type, package);
|
].filter((i) => !i.includes('/.')).filter((i) => fs.statSync(path.resolve(process.cwd(), i)).isDirectory());
|
||||||
if (!fs.statSync(basedir).isDirectory()) continue;
|
|
||||||
|
for (const package of modules) {
|
||||||
|
config.references.push({ path: package });
|
||||||
|
const basedir = path.resolve(process.cwd(), package);
|
||||||
const files = fs.readdirSync(basedir);
|
const files = fs.readdirSync(basedir);
|
||||||
if (!files.includes('src') && !files.map((n) => n.split('.')[1]).includes('ts') && package !== 'utils') continue;
|
if (!files.includes('src') && !files.filter((i) => i.endsWith('.ts')).length && package !== 'packages/utils') continue;
|
||||||
if (package !== 'hydrooj') config.references.push({ path: `${type}/${package}` });
|
const expectedConfig = JSON.stringify((files.includes('src') ? configSrc : configFlat)(package));
|
||||||
fs.writeFileSync(
|
const configPath = path.resolve(basedir, 'tsconfig.json');
|
||||||
path.resolve(basedir, 'tsconfig.json'),
|
const currentConfig = fs.existsSync(configPath) ? fs.readFileSync(configPath, 'utf-8') : '';
|
||||||
files.includes('src') ? JSON.stringify(configSrc) : JSON.stringify(configFlat),
|
if (expectedConfig !== currentConfig) fs.writeFileSync(configPath, expectedConfig);
|
||||||
);
|
if (!files.includes('src')) continue;
|
||||||
if (files.includes('src')) {
|
// Create mapping entry
|
||||||
const inner = fs.readdirSync(path.resolve(basedir, 'src'));
|
for (const file of fs.readdirSync(path.resolve(basedir, 'src'))) {
|
||||||
for (const file of inner) {
|
|
||||||
if (!fs.statSync(path.resolve(basedir, 'src', file)).isFile()) continue;
|
if (!fs.statSync(path.resolve(basedir, 'src', file)).isFile()) continue;
|
||||||
const name = file.split('.')[0];
|
const name = file.split('.')[0];
|
||||||
if (['handler', 'service', 'lib', 'model', 'script'].includes(name)) {
|
const filePath = path.resolve(basedir, `${name}.js`);
|
||||||
fs.writeFileSync(path.resolve(basedir, `${name}.js`), `module.exports = require('./src/${name}');\n`);
|
if (['handler', 'service', 'lib', 'model', 'script', 'index'].includes(name)) {
|
||||||
}
|
if (!fs.existsSync(filePath)) fs.writeFileSync(filePath, `module.exports = require('./src/${name}');\n`);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user