Skip to content

Commit 73093d0

Browse files
authored
Merge pull request #10 from SoCreate/feature/add-cli-commands
add support for -no-watch and -no-serve flags in cli
2 parents 85ad590 + 46b1624 commit 73093d0

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

example-app-angular-cli/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
"scripts": {
77
"playground:fresh": "npm i ../ && npm run playground",
88
"playground": "angular-playground angular-playground.json",
9-
"cli": "ts-node --project ../src/cli ../src/cli/cli.ts angular-playground.json",
10-
"start": "ng serve --progress=false",
9+
"cli": "ts-node --project ../src/cli ../src/cli/cli.ts",
10+
"cli:build": "ts-node --project ../src/cli ../src/cli/cli.ts -no-watch -no-serve",
11+
"start": "ng serve -no-progress",
1112
"lint": "tslint \"src/**/*.ts\"",
1213
"test": "ng test"
1314
},

src/cli/cli.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
#! /usr/bin/env node
22
const path = require('path');
3-
import { build } from './build';
4-
import { startWatch } from './start-watch';
5-
import { runAngularCli } from './run-angular-cli';
3+
import {build} from './build';
4+
import {startWatch} from './start-watch';
5+
import {runAngularCli} from './run-angular-cli';
66

7-
let configFile = path.resolve(process.argv[2]);
7+
const supportedFlags = ['-no-watch', '-no-serve'];
8+
let args = process.argv.slice(2);
9+
let flags = [];
10+
args = args.reduce((accr, value) => {
11+
supportedFlags.indexOf(value) > -1 ? flags.push(value) : accr.push(value);
12+
return accr;
13+
}, []);
14+
15+
const runWatch = flags.indexOf('-no-watch') === -1;
16+
const runAngularCliServe = flags.indexOf('-no-serve') === -1;
17+
18+
const configFilePath = args[0] || 'angular-playground.json';
19+
20+
let configFile = path.resolve(configFilePath);
821
let config;
9-
if (configFile) {
22+
try {
1023
config = require(configFile.replace(/.json$/, ''));
11-
} else {
12-
config = {
13-
sourceRoot: './'
14-
}
24+
} catch(e) {
25+
process.stdout.write(`[angular-playground]: \x1b[31mFailed to load config file ${configFile}\x1b[0m\n`);
26+
process.exit(1);
1527
}
1628

1729
build(config.sourceRoot);
18-
startWatch(config, () => build(config.sourceRoot));
19-
if (config.angularCli) {
30+
if (runWatch) {
31+
startWatch(config, () => build(config.sourceRoot));
32+
}
33+
if (runAngularCliServe && config.angularCli) {
2034
runAngularCli(config.angularCli);
2135
}

0 commit comments

Comments
 (0)