cli/src/build/watch.ts

58 lines
1.4 KiB
TypeScript

import fs from 'fs-extra';
import watch from 'node-watch';
import path from 'path';
import { buildRepository } from './repository/build';
/**
* Create a filesystem watch task for a repository directory
* Equivalent to tsc -w
* @param location Repository path
* @param out Create output files. False - dry run.
* @param doDeploy Use deployment after build success
*/
export async function watchRepository(
location?: string,
out = true,
doDeploy?: string): Promise<void> {
if (!location) {
location = process.cwd();
}
const buildMetaFile = path.join(location, 'squeebot.repo.json');
if (!await fs.pathExists(buildMetaFile)) {
throw new Error(`${location} is not a valid squeebot repository development environment!`);
}
const meta = await fs.readJson(buildMetaFile);
if (!meta.name) {
throw new Error(`${location} is not a valid squeebot repository development environment!`);
}
console.log('Watching repository "%s"!', meta.name);
watch(location, {
recursive: true,
filter(f, skip): any {
if (/\/node_modules/.test(f) ||
/\.out/.test(f) ||
/\.git/.test(f) ||
/\.json$/.test(f)) {
return skip;
}
if (meta.typescript && /\.js$/.test(f)) {
return skip;
}
return true;
},
}, (evt, f) => {
console.log(`\n ==> ${f} changed, rebuilding...`);
buildRepository(location, out, doDeploy).catch((e) => {
console.error(e);
});
});
}