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
+21
View File
@@ -40,6 +40,27 @@ const config: Config = {
paths: [],
executor: new LongRunningExecutor("npm run start:webpackDevServer"),
},
runDocz: {
paths: [],
executor: new LongRunningExecutor(
"npm run docz:watch -- --websocketHost 192.168.5.5"
),
},
runJest: {
paths: [],
executor: new LongRunningExecutor("npm run test"),
},
},
defaultSet: "client",
sets: {
server: ["runServer"],
client: [
"runServer",
"runWebpackDevServer",
"compileCSSTypes",
"compileRelayStream",
],
docz: ["runDocz", "compileCSSTypes"],
},
};
+2 -1
View File
@@ -9,6 +9,7 @@ const paths = require("./paths");
const protocol = process.env.HTTPS === "true" ? "https" : "http";
const host = process.env.HOST || "0.0.0.0";
const serverPort = process.env.PORT || 3000;
const doczPort = process.env.DOCZ_PORT || 3030;
module.exports = function(proxy, allowedHost) {
return {
@@ -81,8 +82,8 @@ module.exports = function(proxy, allowedHost) {
disableDotRule: true,
},
public: allowedHost,
// Proxy to the graphql server.
proxy: proxy || {
// Proxy to the graphql server.
"/api": {
target: `http://localhost:${serverPort}`,
},
+1 -1
View File
@@ -14,7 +14,7 @@ export default {
source: "./src",
typescript: true,
host: process.env.HOST || "0.0.0.0",
port: parseInt(process.env.DOCZ_PORT, 10) || 3000,
port: parseInt(process.env.DOCZ_PORT, 10) || 3030,
modifyBundlerConfig: config => {
config.module.rules.push({
test: /\.css$/,
+1 -1
View File
@@ -8,7 +8,7 @@
"build": "npm-run-all compile --parallel build:*",
"build:client": "node ./scripts/build.js",
"build:server": "tsc -p ./src/tsconfig.json",
"watch": "NODE_ENV=development ts-node ./scripts/watcher/bin/watcher.ts ./config/watcher.ts",
"watch": "NODE_ENV=development ts-node ./scripts/watcher/bin/watcher.ts --config ./config/watcher.ts",
"compile": "npm-run-all --parallel compile:*",
"compile:css-types": "tcm src/core/client/",
"compile:relay-stream": "relay-compiler --src ./src/core/client/stream --schema $(ts-node ./scripts/schemaPath.ts tenant) --language typescript --artifactDirectory ./src/core/client/stream/__generated__ --no-watchman",
+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}"`);