oak-cli/template/scripts/watchServer.js

143 lines
3.8 KiB
JavaScript

/* eslint-disable @typescript-eslint/no-require-imports */
require('module-alias/register');
const chokidar = require('chokidar');
const { join } = require('path');
const ts = require('typescript');
const path = require('path');
const _ = require('lodash');
const { startup } = require('@xuchangzju/oak-cli/lib/server/start');
const projectPath = join(__dirname, '..');
//polyfill console.log 添加时间
const oldLog = console.log;
console.log = function () {
oldLog('[', new Date().toLocaleString(), ']', ...arguments);
};
let shutdown;
const restart = async () => {
if (shutdown) {
await shutdown();
}
const pwd = process.cwd();
const simpleConnector = require('../lib/config/connector').default;
console.warn('----> Starting service......');
shutdown = await startup(pwd, simpleConnector);
};
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(async (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......`);
await restart();
}
} catch (e) {
console.clear();
console.error(e);
// 结束服务
// nodemon.emit('quit');
process.exit(1);
}
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');
}
});
restart();