oak-cli/template/scripts/watchServer.js

146 lines
3.8 KiB
JavaScript

/* eslint-disable @typescript-eslint/no-require-imports */
const chokidar = require('chokidar');
const { join } = require('path');
const ts = require('typescript');
const path = require('path');
const _ = require('lodash');
const nodemon = require('nodemon');
const projectPath = join(__dirname, '..');
const watchSLibPath = join(projectPath, 'lib');
// nodemon的配置
nodemon({
script: 'scripts/startServer.js',
ext: 'js',
exec: 'node',
watch: [watchSLibPath],
env: {
//cross-env NODE_ENV=development cross-env OAK_PLATFORM=server node
NODE_ENV: 'development',
OAK_PLATFORM: 'server',
},
});
nodemon
.on('start', function () {
console.log('App has started');
})
.on('quit', function () {
console.log('App has quit');
process.exit();
})
.on('restart', function (files) {
console.log('App restarted due to: ', files);
});
const watchSourcePath = join(projectPath, 'src');
console.log('Watching for changes in', watchSourcePath);
// 查找配置文件
const configFileName = ts.findConfigFile(
/*searchPath*/ projectPath,
ts.sys.fileExists,
'tsconfig.build.json'
);
if (!configFileName) {
throw new Error("Could not find a valid 'tsconfig.build.json'.");
}
// 读取配置文件
const configFile = ts.readConfigFile(configFileName, ts.sys.readFile);
// 解析配置文件内容
const { options, projectReferences } = ts.parseJsonConfigFileContent(
configFile.config,
ts.sys,
path.dirname(configFileName)
);
const watcher = chokidar.watch(watchSourcePath, {
persistent: true,
// ignore render and i18n files
ignored: (file) => {
return (
file.endsWith('.tsx') ||
file.endsWith('.json') ||
file.endsWith('.xml') ||
file.includes('components') ||
file.includes('pages') ||
file.includes('hooks')
);
},
// awaitWriteFinish: true, // emit single event when chunked writes are completed
// atomic: true, // emit proper events when "atomic writes" (mv _tmp file) are used
interval: 100,
binaryInterval: 100,
cwd: projectPath,
depth: 99,
followSymlinks: true,
ignoreInitial: false,
ignorePermissionErrors: false,
usePolling: false,
alwaysStat: false,
});
let startWatching = false;
watcher.on('ready', () => {
console.warn('Initial scan complete. Ready for changes');
startWatching = true;
});
watcher.on('error', (error) => console.log(`Watcher error: ${error}`));
let isProcessing = false;
const onChangeDebounced = _.debounce((path, type) => {
if (isProcessing) {
return;
}
isProcessing = true;
try {
// 控制台清空
console.clear();
console.warn(`File ${path} has been ${type}d`);
const program = ts.createProgram({
rootNames: [path],
options,
projectReferences,
});
const sourceFile = program.getSourceFile(path);
// 只输出单个文件
const emitResult = program.emit(sourceFile);
// 是否成功
const result = emitResult.emitSkipped;
if (result) {
console.error(`Emit failed for ${path}!`);
} else {
console.log(`Emit succeeded. reload service......`);
}
} catch (e) {
console.clear()
console.error(e);
// 结束服务
nodemon.emit('quit');
}
isProcessing = false;
}, 200);
watcher
.on('add', (path) => {
if (startWatching) {
onChangeDebounced(path, 'add');
}
})
.on('change', (path) => {
if (startWatching) {
onChangeDebounced(path, 'change');
}
})
.on('unlink', (path) => {
if (startWatching) {
onChangeDebounced(path, 'remove');
}
});