build: sync config

This commit is contained in:
jialin
2026-04-24 14:28:30 +08:00
committed by jialin
parent 48b357b1c6
commit 6c161c6ae7
55 changed files with 731 additions and 90 deletions
+47
View File
@@ -0,0 +1,47 @@
const { spawnSync } = require('node:child_process');
const path = require('node:path');
const syncScript = path.resolve(__dirname, './sync-enterprise-config.cjs');
const runNodeScript = (args) => {
const result = spawnSync(process.execPath, [syncScript, ...args], {
stdio: 'inherit'
});
if (result.error) {
throw result.error;
}
if (typeof result.status === 'number' && result.status !== 0) {
process.exit(result.status);
}
};
const runBuild = () =>
spawnSync('max', ['build'], {
env: {
...process.env,
ENABLE_ENTERPRISE: 'true'
},
shell: true,
stdio: 'inherit'
});
let buildResult;
try {
runNodeScript([]);
buildResult = runBuild();
} finally {
try {
runNodeScript(['clean']);
} catch (error) {
console.error('Failed to clean enterprise config:', error);
}
}
if (buildResult?.error) {
throw buildResult.error;
}
process.exit(buildResult?.status ?? 0);
+69
View File
@@ -0,0 +1,69 @@
const { spawn, spawnSync } = require('node:child_process');
const path = require('node:path');
const syncScript = path.resolve(__dirname, './sync-enterprise-config.cjs');
const runNodeScript = (args) => {
const result = spawnSync(process.execPath, [syncScript, ...args], {
stdio: 'inherit'
});
if (typeof result.status === 'number' && result.status !== 0) {
process.exit(result.status);
}
if (result.error) {
throw result.error;
}
};
runNodeScript([]);
const child = spawn('max', ['dev'], {
env: {
...process.env,
ENABLE_ENTERPRISE: 'true'
},
shell: true,
stdio: 'inherit'
});
let cleaned = false;
const clean = () => {
if (cleaned) {
return;
}
cleaned = true;
try {
runNodeScript(['clean']);
} catch (error) {
console.error('Failed to clean enterprise config:', error);
}
};
const forwardSignal = (signal) => {
if (!child.killed) {
child.kill(signal);
}
};
['SIGINT', 'SIGTERM', 'SIGHUP'].forEach((signal) => {
process.on(signal, () => {
forwardSignal(signal);
});
});
child.on('error', (error) => {
clean();
throw error;
});
child.on('exit', (code, signal) => {
clean();
if (signal) {
process.kill(process.pid, signal);
return;
}
process.exit(code ?? 0);
});
+96
View File
@@ -0,0 +1,96 @@
const fs = require('node:fs');
const path = require('node:path');
const workspaceGlobalPath = path.resolve(__dirname, '../../../global.tsx');
const workspaceDependenciesPath = path.resolve(__dirname, '../../../dependencies.json');
const workspaceTsconfigPath = path.resolve(__dirname, '../../../tsconfig.json');
const targetGlobalPath = path.resolve(__dirname, '../src/global.tsx');
const targetPackageJsonPath = path.resolve(__dirname, '../package.json');
const targetTsconfigPath = path.resolve(__dirname, '../tsconfig.json');
const mode = process.argv[2] || 'append';
const blockStart = '// ENTERPRISE_PLUGIN_BLOCK_START';
const blockEnd = '// ENTERPRISE_PLUGIN_BLOCK_END';
const workspaceSource = fs.readFileSync(workspaceGlobalPath, 'utf8').trim();
const blockContent = `\n${blockStart}\n${workspaceSource}\n${blockEnd}\n`;
const stripBlock = (content) => {
const pattern = new RegExp(`\\n?${blockStart}[\\s\\S]*?${blockEnd}\\n?`, 'g');
return content.replace(pattern, '\n').replace(/\n{3,}/g, '\n\n').trimEnd() + '\n';
};
const currentContent = fs.readFileSync(targetGlobalPath, 'utf8');
const strippedContent = stripBlock(currentContent);
const readJson = (filePath) => JSON.parse(fs.readFileSync(filePath, 'utf8'));
const writeJson = (filePath, data) => {
fs.writeFileSync(filePath, `${JSON.stringify(data, null, 2)}\n`);
};
const syncPackageJson = () => {
const sourceDependencies =
readJson(workspaceDependenciesPath).dependencies || {};
const targetPackageJson = readJson(targetPackageJsonPath);
const currentDependencies = {
...(targetPackageJson.dependencies || {})
};
if (mode === 'clean') {
Object.keys(sourceDependencies).forEach((dependencyName) => {
delete currentDependencies[dependencyName];
});
} else {
Object.assign(currentDependencies, sourceDependencies);
}
targetPackageJson.dependencies = currentDependencies;
writeJson(targetPackageJsonPath, targetPackageJson);
console.log(
`${mode === 'clean' ? 'Cleaned' : 'Synced'} enterprise dependencies in ${targetPackageJsonPath}`,
);
};
const syncTsconfig = () => {
const sourcePaths =
readJson(workspaceTsconfigPath).compilerOptions?.paths || {};
const targetTsconfig = readJson(targetTsconfigPath);
const currentPaths = {
...(targetTsconfig.compilerOptions?.paths || {})
};
if (mode === 'clean') {
Object.keys(sourcePaths).forEach((pathKey) => {
delete currentPaths[pathKey];
});
} else {
Object.assign(currentPaths, sourcePaths);
}
targetTsconfig.compilerOptions = targetTsconfig.compilerOptions || {};
targetTsconfig.compilerOptions.paths = currentPaths;
writeJson(targetTsconfigPath, targetTsconfig);
console.log(
`${mode === 'clean' ? 'Cleaned' : 'Synced'} enterprise tsconfig paths in ${targetTsconfigPath}`,
);
};
if (mode === 'clean') {
if (strippedContent !== currentContent) {
fs.writeFileSync(targetGlobalPath, strippedContent);
console.log(`Cleaned enterprise plugin block from ${targetGlobalPath}`);
} else {
console.log(`Enterprise plugin block already absent: ${targetGlobalPath}`);
}
} else {
const updatedContent = `${strippedContent.trimEnd()}${blockContent}`;
if (updatedContent !== currentContent) {
fs.writeFileSync(targetGlobalPath, updatedContent);
console.log(`Appended enterprise plugin block to ${targetGlobalPath}`);
} else {
console.log(`Enterprise plugin block already up to date: ${targetGlobalPath}`);
}
}
syncPackageJson();
syncTsconfig();