watchServer现在支持copyjson和xml等文件
This commit is contained in:
parent
f58d22cb24
commit
9d33e3ce7b
|
|
@ -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,90 +164,130 @@ watcher.on('ready', () => {
|
|||
watcher.on('error', (error) => console.log(`Watcher error: ${error}`));
|
||||
|
||||
let isProcessing = false;
|
||||
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;
|
||||
}
|
||||
// 控制台清空
|
||||
console.clear();
|
||||
console.warn(`File ${path} has been ${type}d`);
|
||||
// 先判断一下这个文件在不在require.cache里面
|
||||
const modulePath = resolve(path);
|
||||
// 将src替换为lib
|
||||
const libPath = modulePath
|
||||
.replace('\\src\\', '\\lib\\')
|
||||
.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.`
|
||||
);
|
||||
compileOnly = true;
|
||||
} else {
|
||||
// 如果是删除,则需要发出警告,文件正在被进程使用
|
||||
if (type === 'remove') {
|
||||
console.error(`File ${libPath} is being used, skiped.`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
const program = ts.createProgram({
|
||||
rootNames: [path],
|
||||
options,
|
||||
projectReferences,
|
||||
});
|
||||
const sourceFile = program.getSourceFile(path);
|
||||
// 是否有语法错误
|
||||
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
|
||||
if (diagnostics.length) {
|
||||
const syntaxErrors = diagnostics.filter(
|
||||
(diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error
|
||||
);
|
||||
|
||||
if (syntaxErrors.length) {
|
||||
console.error(`Error in ${path}`);
|
||||
syntaxErrors.forEach((diagnostic) => {
|
||||
console.error(
|
||||
`${ts.flattenDiagnosticMessageText(
|
||||
diagnostic.messageText,
|
||||
'\n'
|
||||
)}`
|
||||
);
|
||||
});
|
||||
console.error(`文件存在语法错误,请检查修复后重试!`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 只输出单个文件
|
||||
const emitResult = program.emit(sourceFile);
|
||||
// 是否成功
|
||||
const result = emitResult.emitSkipped;
|
||||
if (result) {
|
||||
console.error(`Emit failed for ${path}!`);
|
||||
} else {
|
||||
console.log(
|
||||
`Emit succeeded. ${compileOnly ? '' : 'reload service......'}`
|
||||
);
|
||||
if (compileOnly) {
|
||||
return;
|
||||
}
|
||||
await restart();
|
||||
}
|
||||
};
|
||||
|
||||
const onChangeDebounced = _.debounce(async (path, type) => {
|
||||
if (isProcessing) {
|
||||
console.log('Processing, please wait...');
|
||||
return;
|
||||
}
|
||||
isProcessing = true;
|
||||
try {
|
||||
// 控制台清空
|
||||
console.clear();
|
||||
console.warn(`File ${path} has been ${type}d`);
|
||||
// 先判断一下这个文件在不在require.cache里面
|
||||
const modulePath = resolve(path);
|
||||
// 将src替换为lib
|
||||
const libPath = modulePath
|
||||
.replace('\\src\\', '\\lib\\')
|
||||
.replace('.ts', '.js');
|
||||
let compileOnly = false;
|
||||
if (!require.cache[libPath]) {
|
||||
console.warn(
|
||||
`File ${libPath} is not in module cache, will compile only.`
|
||||
);
|
||||
compileOnly = true;
|
||||
} else {
|
||||
// 如果是删除,则需要发出警告,文件正在被进程使用
|
||||
if (type === 'remove') {
|
||||
console.error(`File ${libPath} is being used, skiped.`);
|
||||
isProcessing = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
const program = ts.createProgram({
|
||||
rootNames: [path],
|
||||
options,
|
||||
projectReferences,
|
||||
});
|
||||
const sourceFile = program.getSourceFile(path);
|
||||
// 是否有语法错误
|
||||
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
|
||||
if (diagnostics.length) {
|
||||
const syntaxErrors = diagnostics.filter(
|
||||
(diagnostic) =>
|
||||
diagnostic.category === ts.DiagnosticCategory.Error
|
||||
);
|
||||
|
||||
if (syntaxErrors.length) {
|
||||
console.error(`Error in ${path}`);
|
||||
syntaxErrors.forEach((diagnostic) => {
|
||||
console.error(
|
||||
`${ts.flattenDiagnosticMessageText(
|
||||
diagnostic.messageText,
|
||||
'\n'
|
||||
)}`
|
||||
);
|
||||
});
|
||||
console.error(`文件存在语法错误,请检查修复后重试!`);
|
||||
isProcessing = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 只输出单个文件
|
||||
const emitResult = program.emit(sourceFile);
|
||||
// 是否成功
|
||||
const result = emitResult.emitSkipped;
|
||||
if (result) {
|
||||
console.error(`Emit failed for ${path}!`);
|
||||
} else {
|
||||
console.log(
|
||||
`Emit succeeded. ${compileOnly ? '' : 'reload service......'}`
|
||||
);
|
||||
if (compileOnly) {
|
||||
isProcessing = false;
|
||||
return;
|
||||
}
|
||||
await restart();
|
||||
}
|
||||
isProcessing = true;
|
||||
await fileChangeHandler(path, type);
|
||||
} catch (e) {
|
||||
console.clear();
|
||||
console.error(e);
|
||||
// 结束服务
|
||||
// nodemon.emit('quit');
|
||||
process.exit(1);
|
||||
} finally {
|
||||
isProcessing = false;
|
||||
}
|
||||
isProcessing = false;
|
||||
}, 200);
|
||||
}, 100);
|
||||
|
||||
watcher
|
||||
.on('add', (path) => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue