watchServer现在支持copyjson和xml等文件

This commit is contained in:
Pan Qiancheng 2024-12-04 16:21:00 +08:00
parent f58d22cb24
commit 9d33e3ce7b
1 changed files with 119 additions and 81 deletions

View File

@ -9,6 +9,7 @@ const _ = require('lodash');
const { startup } = require('@xuchangzju/oak-cli/lib/server/start');
const projectPath = join(__dirname, '..');
const dayjs = require('dayjs');
const fs = require('fs');
const enableTrace = !!process.env.ENABLE_TRACE;
@ -28,11 +29,10 @@ const polyfill = (trace) => {
let callerInfo = null;
try {
const err = new Error();
let currentFile;
Error.prepareStackTrace = (err, stack) => {
return stack;
};
currentFile = err.stack.shift().getFileName();
const currentFile = err.stack.shift().getFileName();
while (err.stack.length) {
callerInfo = err.stack.shift();
if (currentFile !== callerInfo.getFileName()) {
@ -91,10 +91,10 @@ const restart = async () => {
// 清空lib以下目录的缓存
// 删除所有模块的缓存
Object.keys(require.cache).forEach(function (key) {
if (key.includes('node_modules')) {
// 如果不是项目目录下的文件,不删除
if (!key.startsWith(projectPath)) {
return;
}
if (key.includes('lib')) {
} else if (key.includes('lib') && !key.includes('node_modules')) {
console.log('delete module cache:', key);
delete require.cache[key];
}
@ -136,8 +136,6 @@ const watcher = chokidar.watch(watchSourcePath, {
ignored: (file) => {
return (
file.endsWith('.tsx') ||
file.endsWith('.json') ||
file.endsWith('.xml') ||
file.includes('components') ||
file.includes('pages') ||
file.includes('hooks')
@ -166,13 +164,39 @@ watcher.on('ready', () => {
watcher.on('error', (error) => console.log(`Watcher error: ${error}`));
let isProcessing = false;
const onChangeDebounced = _.debounce(async (path, type) => {
if (isProcessing) {
console.log('Processing, please wait...');
const fileChangeHandler = async (path, type) => {
// 判断一下是不是以下扩展名ts,tsx
if (!path.endsWith('.ts')) {
// 如果是json或者xml文件复制或者删除
if (path.endsWith('.json') || path.endsWith('.xml')) {
const targetPath = path.replace('src', 'lib');
if (type === 'remove') {
fs.unlinkSync(targetPath);
console.warn(`File ${targetPath} has been removed.`);
} else if (type === 'add') {
fs.copyFileSync(
path,
targetPath,
fs.constants.COPYFILE_FICLONE
);
console.warn(`File ${path} has been created at ${targetPath}.`);
} else if (type === 'change') {
// 强制覆盖文件
if (fs.existsSync(targetPath)) {
fs.unlinkSync(targetPath);
}
fs.copyFileSync(
path,
targetPath,
fs.constants.COPYFILE_FICLONE
);
console.warn(`File ${path} has been copied to ${targetPath}.`);
}
} else {
console.warn(`File ${path} is not [ts,json,xml] file, skiped.`);
}
return;
}
isProcessing = true;
try {
// 控制台清空
console.clear();
console.warn(`File ${path} has been ${type}d`);
@ -184,6 +208,16 @@ const onChangeDebounced = _.debounce(async (path, type) => {
.replace('.ts', '.js');
let compileOnly = false;
if (!require.cache[libPath]) {
// 如果是删除的话直接尝试删除lib下的文件
if (type === 'remove') {
try {
fs.unlinkSync(libPath);
} catch (e) {
console.error(`Error in delete ${libPath}`, e);
}
console.warn(`File ${libPath} has been removed.`);
return;
}
console.warn(
`File ${libPath} is not in module cache, will compile only.`
);
@ -192,7 +226,6 @@ const onChangeDebounced = _.debounce(async (path, type) => {
// 如果是删除,则需要发出警告,文件正在被进程使用
if (type === 'remove') {
console.error(`File ${libPath} is being used, skiped.`);
isProcessing = false;
return;
}
}
@ -206,8 +239,7 @@ const onChangeDebounced = _.debounce(async (path, type) => {
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
if (diagnostics.length) {
const syntaxErrors = diagnostics.filter(
(diagnostic) =>
diagnostic.category === ts.DiagnosticCategory.Error
(diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error
);
if (syntaxErrors.length) {
@ -221,7 +253,6 @@ const onChangeDebounced = _.debounce(async (path, type) => {
);
});
console.error(`文件存在语法错误,请检查修复后重试!`);
isProcessing = false;
return;
}
}
@ -236,20 +267,27 @@ const onChangeDebounced = _.debounce(async (path, type) => {
`Emit succeeded. ${compileOnly ? '' : 'reload service......'}`
);
if (compileOnly) {
isProcessing = false;
return;
}
await restart();
}
};
const onChangeDebounced = _.debounce(async (path, type) => {
if (isProcessing) {
console.log('Processing, please wait...');
return;
}
try {
isProcessing = true;
await fileChangeHandler(path, type);
} catch (e) {
console.clear();
console.error(e);
// 结束服务
// nodemon.emit('quit');
process.exit(1);
}
} finally {
isProcessing = false;
}, 200);
}
}, 100);
watcher
.on('add', (path) => {