add watcher

This commit is contained in:
Evert Prants 2020-11-28 15:59:54 +02:00
parent df17cb9cba
commit 2ed151ac51
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 91 additions and 5 deletions

15
package-lock.json generated
View File

@ -33,6 +33,7 @@
"@squeebot/core": { "@squeebot/core": {
"version": "file:../core", "version": "file:../core",
"requires": { "requires": {
"dateformat": "^4.0.0",
"fs-extra": "^9.0.1" "fs-extra": "^9.0.1"
}, },
"dependencies": { "dependencies": {
@ -145,6 +146,11 @@
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
}, },
"dateformat": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/dateformat/-/dateformat-4.0.0.tgz",
"integrity": "sha512-zpKyDYpeePyYGJp2HhRxLHlA+jZQNjt+MwmcVmLxCIECeC4Ks3TI3yk/CSMKylbnCJ5htonfOugYtRRTpyoHow=="
},
"diff": { "diff": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
@ -399,6 +405,7 @@
"version": "15.0.10", "version": "15.0.10",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.10.tgz", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.10.tgz",
"integrity": "sha512-z8PNtlhrj7eJNLmrAivM7rjBESG6JwC5xP3RVk12i/8HVP7Xnx/sEmERnRImyEuUaJfO942X0qMOYsoupaJbZQ==", "integrity": "sha512-z8PNtlhrj7eJNLmrAivM7rjBESG6JwC5xP3RVk12i/8HVP7Xnx/sEmERnRImyEuUaJfO942X0qMOYsoupaJbZQ==",
"dev": true,
"requires": { "requires": {
"@types/yargs-parser": "*" "@types/yargs-parser": "*"
} }
@ -406,7 +413,8 @@
"@types/yargs-parser": { "@types/yargs-parser": {
"version": "15.0.0", "version": "15.0.0",
"resolved": "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-15.0.0.tgz", "resolved": "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-15.0.0.tgz",
"integrity": "sha512-FA/BWv8t8ZWJ+gEOnLLd8ygxH/2UFbAvgEonyfN6yWGLKc7zVjbpl2Y4CTjid9h2RfgPP6SEt6uHwEOply00yw==" "integrity": "sha512-FA/BWv8t8ZWJ+gEOnLLd8ygxH/2UFbAvgEonyfN6yWGLKc7zVjbpl2Y4CTjid9h2RfgPP6SEt6uHwEOply00yw==",
"dev": true
}, },
"ansi-regex": { "ansi-regex": {
"version": "5.0.0", "version": "5.0.0",
@ -682,6 +690,11 @@
"minimist": "^1.2.5" "minimist": "^1.2.5"
} }
}, },
"node-watch": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/node-watch/-/node-watch-0.7.0.tgz",
"integrity": "sha512-OOBiglke5SlRQT5WYfwXTmYqTfXjcTNBHpalyHLtLxDpQYVpVRkJqabcch1kmwJsjV/J4OZuzEafeb4soqtFZA=="
},
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",

View File

@ -29,6 +29,7 @@
"dependencies": { "dependencies": {
"@squeebot/core": "file:../core", "@squeebot/core": "file:../core",
"fs-extra": "^9.0.1", "fs-extra": "^9.0.1",
"node-watch": "^0.7.0",
"yargs": "^16.1.1" "yargs": "^16.1.1"
} }
} }

View File

@ -1,5 +1,6 @@
import child_process from 'child_process'; import child_process from 'child_process';
import fs from 'fs-extra'; import fs from 'fs-extra';
import watch from 'node-watch';
import path from 'path'; import path from 'path';
import readline from 'readline'; import readline from 'readline';
import yargs from 'yargs'; import yargs from 'yargs';
@ -282,6 +283,52 @@ async function buildRepository(
deploy(meta.name, location, outDir, doDeploy); deploy(meta.name, location, outDir, doDeploy);
} }
async function watchRepository(
location?: string,
out = true,
doDeploy?: string,
onlyDeploy = false): 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, onlyDeploy).catch((e) => {
console.error(e);
});
});
}
const yar = yargs.scriptName('squeebot') const yar = yargs.scriptName('squeebot')
.command('new [name] [path]', 'create a new Squeebot environment', (y) => { .command('new [name] [path]', 'create a new Squeebot environment', (y) => {
y.positional('name', { y.positional('name', {
@ -321,14 +368,39 @@ const yar = yargs.scriptName('squeebot')
nargs: 1, nargs: 1,
type: 'string', type: 'string',
}) })
.option('w', {
alias: 'watch',
describe: 'Watch files for changes',
type: 'boolean',
})
.option('o', { .option('o', {
alias: 'deploy-only', alias: 'deploy-only',
describe: 'Deploy only, without rebuilding', describe: 'Deploy only, without rebuilding',
type: 'boolean', type: 'boolean',
}); });
}, (v) => buildRepository(v.path as string, }, (v) => {
const dargs = [
v.path,
v.p !== true, v.p !== true,
v.d as string, v.d,
v.o === true)); v.o === true,
];
if (v.w) {
return watchRepository(
dargs[0] as string,
dargs[1] as boolean,
dargs[2] as string,
dargs[3] as boolean,
);
}
buildRepository(
dargs[0] as string,
dargs[1] as boolean,
dargs[2] as string,
dargs[3] as boolean,
);
});
}) })
.argv; .argv;