Skip to content

Commit 756c49e

Browse files
committed
ts: command.start
1 parent f260b42 commit 756c49e

3 files changed

Lines changed: 36 additions & 38 deletions

File tree

lib/bin.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ function runFromCli() {
9292
* @returns {any}
9393
*/
9494
function handleNoCommand(argv, input, yargs) {
95-
9695
const processed = processStart(yargs);
9796
const paths = input.map(path => {
9897
const resolved = resolve(path);
@@ -112,7 +111,7 @@ function handleNoCommand(argv, input, yargs) {
112111
withErrors.forEach(item => {
113112
logger.unprefixed("error", printErrors(item.errors));
114113
});
115-
return process.exit(1);
114+
return process.exit(1);
116115
}
117116

118117
const serveStaticPaths = withoutErrors
@@ -134,7 +133,7 @@ function handleNoCommand(argv, input, yargs) {
134133
proxy,
135134
serveStatic: serveStaticPaths
136135
};
137-
return handleIncoming('start', yargs.reset());
136+
return handleIncoming("start", yargs.reset());
138137
}
139138

140139
/**
@@ -155,7 +154,11 @@ function handleNoCommand(argv, input, yargs) {
155154
*/
156155
function handleCli(opts) {
157156
opts.cb = opts.cb || utils.defaultCallback;
158-
return require(`./cli/command.${opts.cli.input[0]}`)(opts);
157+
const m = require(`./cli/command.${opts.cli.input[0]}`);
158+
if (m.default) {
159+
return m.default(opts);
160+
}
161+
return m(opts);
159162
}
160163

161164
export default handleCli;
@@ -164,10 +167,7 @@ function processStart(yargs) {
164167
return yargs
165168
.usage("Usage: $0 start [options]")
166169
.options(startOpts)
167-
.example(
168-
"$0 start -s app",
169-
"- Use the App directory to serve files"
170-
)
170+
.example("$0 start -s app", "- Use the App directory to serve files")
171171
.example("$0 start -p www.bbc.co.uk", "- Proxy an existing website")
172172
.help().argv;
173173
}

lib/cli/cli-options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";
2-
31
var path = require("path");
42
var url = require("url");
53
var _ = require("../lodash.custom");
@@ -11,6 +9,8 @@ var immDefs = Immutable.fromJS(defaultConfig);
119

1210
var opts = exports;
1311

12+
opts.explodeFilesArg = string => string.split(",").map(item => item.trim());
13+
1414
/**
1515
* @type {{wrapPattern: Function}}
1616
*/
Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
"use strict";
2-
3-
var path = require("path");
4-
var fs = require("fs");
5-
var _ = require("../lodash.custom");
6-
var utils = require("../utils");
7-
var opts = require("./cli-options").utils;
1+
import * as path from "path";
2+
import { existsSync } from "fs";
3+
import * as utils from "../utils";
4+
import { explodeFilesArg } from "./cli-options";
5+
const _ = require("../lodash.custom");
86

97
/**
108
* $ browser-sync start <options>
@@ -15,28 +13,26 @@ var opts = require("./cli-options").utils;
1513
* @param opts
1614
* @returns {Function}
1715
*/
18-
module.exports = function(opts) {
19-
var flags = preprocessFlags(opts.cli.flags);
20-
var maybepkg = path.resolve(process.cwd(), "package.json");
21-
var input = flags;
16+
export default function(opts) {
17+
const flags = preprocessFlags(opts.cli.flags);
18+
const maybepkg = path.resolve(process.cwd(), "package.json");
19+
let input = flags;
2220

2321
if (flags.config) {
24-
var maybeconf = path.resolve(process.cwd(), flags.config);
25-
if (fs.existsSync(maybeconf)) {
26-
var conf = require(maybeconf);
22+
const maybeconf = path.resolve(process.cwd(), flags.config);
23+
if (existsSync(maybeconf)) {
24+
const conf = require(maybeconf);
2725
input = _.merge({}, conf, flags);
2826
} else {
2927
utils.fail(
3028
true,
31-
new Error(
32-
"Configuration file '" + flags.config + "' not found"
33-
),
29+
new Error(`Configuration file '${flags.config}' not found`),
3430
opts.cb
3531
);
3632
}
3733
} else {
38-
if (fs.existsSync(maybepkg)) {
39-
var pkg = require(maybepkg);
34+
if (existsSync(maybepkg)) {
35+
const pkg = require(maybepkg);
4036
if (pkg["browser-sync"]) {
4137
console.log("> Configuration obtained from package.json");
4238
input = _.merge({}, pkg["browser-sync"], flags);
@@ -47,16 +43,17 @@ module.exports = function(opts) {
4743
return require("../")
4844
.create("cli")
4945
.init(input, opts.cb);
50-
};
46+
}
5147

5248
/**
5349
* @param flags
5450
* @returns {*}
5551
*/
5652
function preprocessFlags(flags) {
57-
return [stripUndefined, legacyFilesArgs].reduce(function(flags, fn) {
58-
return fn.call(null, flags);
59-
}, flags);
53+
return [stripUndefined, legacyFilesArgs].reduce(
54+
(flags, fn) => fn.call(null, flags),
55+
flags
56+
);
6057
}
6158

6259
/**
@@ -66,8 +63,8 @@ function preprocessFlags(flags) {
6663
* @returns {*}
6764
*/
6865
function stripUndefined(subject) {
69-
return Object.keys(subject).reduce(function(acc, key) {
70-
var value = subject[key];
66+
return Object.keys(subject).reduce((acc, key) => {
67+
const value = subject[key];
7168
if (typeof value === "undefined") {
7269
return acc;
7370
}
@@ -82,9 +79,10 @@ function stripUndefined(subject) {
8279
*/
8380
function legacyFilesArgs(flags) {
8481
if (flags.files && flags.files.length) {
85-
flags.files = flags.files.reduce(function(acc, item) {
86-
return acc.concat(opts.explodeFilesArg(item));
87-
}, []);
82+
flags.files = flags.files.reduce(
83+
(acc, item) => acc.concat(explodeFilesArg(item)),
84+
[]
85+
);
8886
}
8987
return flags;
9088
}

0 commit comments

Comments
 (0)