From 46b16243a9d08efd4d8240a389186e3d22e593e6 Mon Sep 17 00:00:00 2001 From: Justin Schwartzenberger Date: Tue, 21 Feb 2017 16:27:14 -0800 Subject: [PATCH] add support for -no-watch and -no-serve flags in cli --- example-app-angular-cli/package.json | 5 ++-- src/cli/cli.ts | 36 +++++++++++++++++++--------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/example-app-angular-cli/package.json b/example-app-angular-cli/package.json index c5a9765f..43734ba1 100644 --- a/example-app-angular-cli/package.json +++ b/example-app-angular-cli/package.json @@ -6,8 +6,9 @@ "scripts": { "playground:fresh": "npm i ../ && npm run playground", "playground": "angular-playground angular-playground.json", - "cli": "ts-node --project ../src/cli ../src/cli/cli.ts angular-playground.json", - "start": "ng serve --progress=false", + "cli": "ts-node --project ../src/cli ../src/cli/cli.ts", + "cli:build": "ts-node --project ../src/cli ../src/cli/cli.ts -no-watch -no-serve", + "start": "ng serve -no-progress", "lint": "tslint \"src/**/*.ts\"", "test": "ng test" }, diff --git a/src/cli/cli.ts b/src/cli/cli.ts index ecac2c91..adbd41ed 100644 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -1,21 +1,35 @@ #! /usr/bin/env node const path = require('path'); -import { build } from './build'; -import { startWatch } from './start-watch'; -import { runAngularCli } from './run-angular-cli'; +import {build} from './build'; +import {startWatch} from './start-watch'; +import {runAngularCli} from './run-angular-cli'; -let configFile = path.resolve(process.argv[2]); +const supportedFlags = ['-no-watch', '-no-serve']; +let args = process.argv.slice(2); +let flags = []; +args = args.reduce((accr, value) => { + supportedFlags.indexOf(value) > -1 ? flags.push(value) : accr.push(value); + return accr; +}, []); + +const runWatch = flags.indexOf('-no-watch') === -1; +const runAngularCliServe = flags.indexOf('-no-serve') === -1; + +const configFilePath = args[0] || 'angular-playground.json'; + +let configFile = path.resolve(configFilePath); let config; -if (configFile) { +try { config = require(configFile.replace(/.json$/, '')); -} else { - config = { - sourceRoot: './' - } +} catch(e) { + process.stdout.write(`[angular-playground]: \x1b[31mFailed to load config file ${configFile}\x1b[0m\n`); + process.exit(1); } build(config.sourceRoot); -startWatch(config, () => build(config.sourceRoot)); -if (config.angularCli) { +if (runWatch) { + startWatch(config, () => build(config.sourceRoot)); +} +if (runAngularCliServe && config.angularCli) { runAngularCli(config.angularCli); }