Support watcher sets

This commit is contained in:
Chi Vinh Le
2018-07-13 14:56:36 -03:00
parent e76740c31f
commit 035d241a72
7 changed files with 98 additions and 29 deletions
+28 -16
View File
@@ -4,24 +4,36 @@ import program from "commander";
import path from "path";
import watch from "../";
function list(val: string) {
return val.split(",");
async function run(
args: ReadonlyArray<string>,
options: Record<string, string>
) {
const only = args;
const { config: configFile = "" } = options;
if (!configFile) {
throw new Error("Config file not specified");
}
// tslint:disable-next-line:no-var-requires
let config: any = require(path.resolve(configFile));
if (config.__esModule) {
config = config.default;
}
try {
await watch(config, { only });
} catch (err) {
// tslint:disable-next-line:no-console
console.error(err);
process.exit(1);
}
}
program
const cmd = program
.version("0.1.0")
.usage("<configFile>")
.option("-o, --only <watcher>", "only run the specified watcher", list)
.arguments("<configFile>")
.usage("[watchers or sets]")
.option("-c, --config <configFile>", "Use given config file")
.description("Run watchers defined in <configFile>")
.action((configFile, cmd) => {
const { only = [] } = cmd;
let config: any = require(path.resolve(configFile));
if (config.__esModule) {
config = config.default;
}
watch(config, { only });
})
.parse(process.argv);
run(cmd.args, cmd.opts());
+14 -5
View File
@@ -21,15 +21,15 @@ export interface Executor {
}
export interface Options {
only?: string[];
only?: ReadonlyArray<string>;
}
export interface Config {
rootDir?: string;
backend?: Watcher;
watchers: {
[key: string]: WatchConfig;
};
watchers: Record<string, WatchConfig>;
defaultSet?: string;
sets?: Record<string, ReadonlyArray<string>>;
}
export interface WatchConfig {
@@ -54,4 +54,13 @@ export const configSchema = Joi.object({
executor: Joi.object(),
})
),
});
defaultSet: Joi.string().optional(),
sets: Joi.object()
.pattern(
/.*/,
Joi.array()
.items(Joi.string())
.unique()
)
.optional(),
}).with("defaultSet", "sets");
+31 -5
View File
@@ -44,12 +44,33 @@ function setupCleanup(watcher: Watcher, config: Config) {
);
}
function resolveSets(
sets: Record<string, ReadonlyArray<string>>,
value: ReadonlyArray<string>
) {
const resolved: string[] = [];
value.forEach(v => {
if (v in sets) {
resolved.push(...sets[v]);
return;
}
resolved.push(v);
});
return resolved;
}
function filterOnly(
watchers: Config["watchers"],
only: string[]
only: ReadonlyArray<string>,
sets?: Record<string, ReadonlyArray<string>>
): Config["watchers"] {
const resolved = sets ? resolveSets(sets, only) : only;
const unknown = resolved.filter(r => !(r in watchers));
if (unknown.length) {
throw new Error(`Watcher Configuration or Set for ${unknown} not found`);
}
return pickBy(watchers, (value, key) => {
if (only.indexOf(key) === -1) {
if (resolved.indexOf(key) === -1) {
// tslint:disable-next-line:no-console
console.log(`Disabled watcher "${key}"`);
return false;
@@ -58,18 +79,23 @@ function filterOnly(
}) as Config["watchers"];
}
export default async function watch(config: Config, options?: Options) {
export default async function watch(config: Config, options: Options = {}) {
Joi.assert(config, configSchema);
const watcher: Watcher = config.backend || new SaneWatcher();
const rootDir = config.rootDir || process.cwd();
const defaultSet = config.defaultSet && [config.defaultSet];
const only = options.only && options.only.length ? options.only : defaultSet;
let watchersConfigs = config.watchers;
if (options && options.only && options.only.length > 0) {
watchersConfigs = filterOnly(watchersConfigs, options.only);
if (only) {
watchersConfigs = filterOnly(watchersConfigs, only, config.sets);
}
setupCleanup(watcher, config);
if (watcher.onInit) {
await watcher.onInit();
}
for (const key of Object.keys(watchersConfigs)) {
// tslint:disable-next-line:no-console
console.log(`Start watcher "${key}"`);