diff --git a/config/watcher.ts b/config/watcher.ts index 89da62bf6..35e916a35 100644 --- a/config/watcher.ts +++ b/config/watcher.ts @@ -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"], }, }; diff --git a/config/webpackDevServer.config.js b/config/webpackDevServer.config.js index 668433652..b3e6f1578 100644 --- a/config/webpackDevServer.config.js +++ b/config/webpackDevServer.config.js @@ -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}`, }, diff --git a/doczrc.js b/doczrc.js index 19542a6b6..9d7ef1b12 100644 --- a/doczrc.js +++ b/doczrc.js @@ -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$/, diff --git a/package.json b/package.json index fe9a88e41..0bf4530e9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/watcher/bin/watcher.ts b/scripts/watcher/bin/watcher.ts index a3a50e6c3..c8126a2ec 100644 --- a/scripts/watcher/bin/watcher.ts +++ b/scripts/watcher/bin/watcher.ts @@ -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, + options: Record +) { + 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("") - .option("-o, --only ", "only run the specified watcher", list) - .arguments("") + .usage("[watchers or sets]") + .option("-c, --config ", "Use given config file") .description("Run watchers defined in ") - .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()); diff --git a/scripts/watcher/types.ts b/scripts/watcher/types.ts index d55019660..fc099b9b2 100644 --- a/scripts/watcher/types.ts +++ b/scripts/watcher/types.ts @@ -21,15 +21,15 @@ export interface Executor { } export interface Options { - only?: string[]; + only?: ReadonlyArray; } export interface Config { rootDir?: string; backend?: Watcher; - watchers: { - [key: string]: WatchConfig; - }; + watchers: Record; + defaultSet?: string; + sets?: Record>; } 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"); diff --git a/scripts/watcher/watch.ts b/scripts/watcher/watch.ts index d7a477b8e..8302126f9 100644 --- a/scripts/watcher/watch.ts +++ b/scripts/watcher/watch.ts @@ -44,12 +44,33 @@ function setupCleanup(watcher: Watcher, config: Config) { ); } +function resolveSets( + sets: Record>, + value: ReadonlyArray +) { + 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, + sets?: Record> ): 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}"`);