Skip to content

Commit b0382f8

Browse files
committed
bin: convert to TS
1 parent b456e57 commit b0382f8

16 files changed

Lines changed: 79 additions & 88 deletions

lib/bin.js renamed to lib/bin.ts

Lines changed: 61 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
#!/usr/bin/env node
2-
var startOpts = require("../cli-options/opts.start.json");
3-
var reloadOpts = require("../cli-options/opts.reload.json");
4-
var recipeOpts = require("../cli-options/opts.recipe.json");
5-
var pkg = require("../package.json");
6-
var utils = require("./utils");
7-
var resolve = require("path").resolve;
8-
var existsSync = require("fs").existsSync;
9-
var logger = require("./logger").logger;
10-
var compile = require("eazy-logger").compile;
11-
12-
var BsErrorLevels = {
13-
Fatal: "Fatal"
14-
};
15-
16-
var BsErrorTypes = {
17-
PathNotFound: "PathNotFound"
18-
};
2+
const startOpts = require("../cli-options/opts.start.json");
3+
const reloadOpts = require("../cli-options/opts.reload.json");
4+
const recipeOpts = require("../cli-options/opts.recipe.json");
5+
const pkg = require("../package.json");
6+
import * as utils from "./utils";
7+
import {resolve} from "path";
8+
import {existsSync} from "fs";
9+
import {logger} from "./logger";
10+
import {compile} from "eazy-logger";
11+
12+
export enum BsErrorLevels {
13+
Fatal = "Fatal"
14+
}
15+
16+
export enum BsErrorTypes {
17+
PathNotFound = "PathNotFound"
18+
}
1919

2020
/**
2121
* Handle cli input
2222
*/
2323
if (!module.parent) {
24-
var yargs = require("yargs")
24+
runFromCli();
25+
}
26+
27+
function runFromCli() {
28+
const yargs = require("yargs")
2529
.command("start", "Start the server")
2630
.command("init", "Create a configuration file")
2731
.command("reload", "Send a reload event over HTTP protocol")
2832
.command("recipe", "Generate the files for a recipe")
29-
.version(function() {
30-
return pkg.version;
31-
})
33+
.version(() => pkg.version)
3234
.epilogue(
3335
[
3436
"For help running a certain command, type <command> --help",
@@ -48,10 +50,10 @@ if (!module.parent) {
4850
].join("\n")
4951
);
5052

51-
var argv = yargs.argv;
52-
var input = argv._;
53-
var command = input[0];
54-
var valid = ["start", "init", "reload", "recipe"];
53+
const argv = yargs.argv;
54+
const input = argv._;
55+
const command = input[0];
56+
const valid = ["start", "init", "reload", "recipe"];
5557

5658
if (argv.help) {
5759
return yargs.showHelp();
@@ -73,51 +75,39 @@ if (!module.parent) {
7375
}
7476

7577
function handleNoCommand(argv, input) {
76-
var paths = input.map(function(path) {
77-
var resolved = resolve(path);
78-
var isUrl = /^https?:\/\//.test(path);
78+
const paths = input.map(path => {
79+
const resolved = resolve(path);
80+
const isUrl = /^https?:\/\//.test(path);
7981
return {
80-
isUrl: isUrl,
82+
isUrl,
8183
userInput: path,
82-
resolved: resolved,
84+
resolved,
8385
errors: isUrl ? [] : pathErrors(path, resolved)
8486
};
8587
});
8688

87-
var withErrors = paths.filter(function(item) {
88-
return item.errors.length;
89-
});
89+
const withErrors = paths.filter(item => item.errors.length);
9090

91-
var withoutErrors = paths.filter(function(item) {
92-
return item.errors.length === 0;
93-
});
91+
const withoutErrors = paths.filter(item => item.errors.length === 0);
9492

9593
if (withErrors.length) {
96-
withErrors.forEach(function(item) {
94+
withErrors.forEach(item => {
9795
logger.unprefixed("error", printErrors(item.errors));
9896
});
9997
process.exit(1);
10098
} else {
101-
var ssPaths = withoutErrors
102-
.filter(function(item) {
103-
return item.isUrl === false;
104-
})
105-
.map(function(item) {
106-
return item.resolved;
107-
});
99+
const ssPaths = withoutErrors
100+
.filter(item => item.isUrl === false)
101+
.map(item => item.resolved);
108102

109-
var urls = withoutErrors
110-
.filter(function(item) {
111-
return item.isUrl === true;
112-
})
113-
.map(function(item) {
114-
return item.userInput;
115-
});
103+
const urls = withoutErrors
104+
.filter(item => item.isUrl === true)
105+
.map(item => item.userInput);
116106

117107
if (urls.length) {
118-
var proxy = urls[0];
108+
const proxy = urls[0];
119109
var config = Object.assign({}, argv, {
120-
proxy: proxy,
110+
proxy,
121111
serveStatic: ssPaths
122112
});
123113
handleCli({ cli: { flags: config, input: ["start"] } });
@@ -136,17 +126,17 @@ function handleNoCommand(argv, input) {
136126
*/
137127
function handleCli(opts) {
138128
opts.cb = opts.cb || utils.defaultCallback;
139-
return require("./cli/command." + opts.cli.input[0])(opts);
129+
return require(`./cli/command.${opts.cli.input[0]}`)(opts);
140130
}
141131

142-
module.exports = handleCli;
132+
export default handleCli;
143133

144134
/**
145135
* @param {string} command
146136
* @param {object} yargs
147137
*/
148138
function handleIncoming(command, yargs) {
149-
var out;
139+
let out;
150140
if (command === "start") {
151141
out = yargs
152142
.usage("Usage: $0 start [options]")
@@ -196,12 +186,12 @@ function pathErrors(input, resolved) {
196186
level: BsErrorLevels.Fatal,
197187
errors: [
198188
{
199-
error: new Error("Path not found: " + input),
200-
meta: function() {
189+
error: new Error(`Path not found: ${input}`),
190+
meta() {
201191
return [
202-
"Your Input: {yellow:" + input + "}",
203-
"CWD: {yellow:" + process.cwd() + "}",
204-
"Resolved to: {yellow:" + resolved + "}"
192+
`Your Input: {yellow:${input}}`,
193+
`CWD: {yellow:${process.cwd()}}`,
194+
`Resolved to: {yellow:${resolved}}`
205195
];
206196
}
207197
}
@@ -214,19 +204,15 @@ function pathErrors(input, resolved) {
214204

215205
function printErrors(errors) {
216206
return errors
217-
.map(function(error) {
218-
return [
219-
"Error Type: {bold:" + error.type + "}",
220-
"Error Level: {bold:" + error.level + "}",
221-
error.errors.map(function(item) {
222-
return [
223-
"Error Message: " + item.error.message,
224-
item.meta ? item.meta().join("\n") : ""
225-
]
226-
.filter(Boolean)
227-
.join("\n");
228-
})
229-
].join("\n");
230-
})
207+
.map(error => [
208+
`Error Type: {bold:${error.type}}`,
209+
`Error Level: {bold:${error.level}}`,
210+
error.errors.map(item => [
211+
`Error Message: ${item.error.message}`,
212+
item.meta ? item.meta().join("\n") : ""
213+
]
214+
.filter(Boolean)
215+
.join("\n"))
216+
].join("\n"))
231217
.join("\n\n");
232218
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"prepublish": "tsc"
3636
},
3737
"dependencies": {
38+
"@types/node": "^8.5.2",
3839
"browser-sync-ui": "v1.0.1",
3940
"bs-recipes": "1.3.4",
4041
"chokidar": "1.7.0",

test/specs/commands/recipes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var pkg = require(path.resolve("package.json"));
77
var sinon = require("sinon");
88
var logger = require("../../../dist/logger").logger;
99
var assert = require("chai").assert;
10-
var cli = require(path.resolve(pkg.bin));
10+
var cli = require(path.resolve(pkg.bin)).default;
1111
var fs = require("fs");
1212
var rim = require("rimraf").sync;
1313

test/specs/commands/reload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var browserSync = require(path.resolve("./"));
66
var pkg = require(path.resolve("package.json"));
77
var sinon = require("sinon");
88
var assert = require("chai").assert;
9-
var cli = require(path.resolve(pkg.bin));
9+
var cli = require(path.resolve(pkg.bin)).default;
1010

1111
describe("E2E CLI `reload` with no files arg", function () {
1212

test/specs/e2e/cli/e2e.cli.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var pkg = require(path.resolve("package.json"));
66
var assert = require("chai").assert;
77
var sinon = require("sinon");
88
var fs = require("fs");
9-
var cli = require(path.resolve(pkg.bin));
9+
var cli = require(path.resolve(pkg.bin)).default;
1010
var utils = require("../../../../dist/utils");
1111

1212
describe("CLI: reading config file from disk", function () {

test/specs/e2e/cli/e2e.cli.error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var assert = require("chai").assert;
55
var browserSync = require(path.resolve("./"));
66

77
var pkg = require(path.resolve("package.json"));
8-
var cli = require(path.resolve(pkg.bin));
8+
var cli = require(path.resolve(pkg.bin)).default;
99

1010
describe.skip("E2E CLI - fail on invalid config", function () {
1111

test/specs/e2e/cli/e2e.cli.files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var assert = require("chai").assert;
55
var browserSync = require(path.resolve("./"));
66

77
var pkg = require(path.resolve("package.json"));
8-
var cli = require(path.resolve(pkg.bin));
8+
var cli = require(path.resolve(pkg.bin)).default;
99

1010
describe("E2E CLI `files` arg - multi globs", function () {
1111
it("Converts cli files arg to correct namespaced watchers", function (done) {

test/specs/e2e/cli/e2e.cli.init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var fs = require("fs");
55
var path = require("path");
66

77
var pkg = require(path.resolve("package.json"));
8-
var cli = require(path.resolve(pkg.bin));
8+
var cli = require(path.resolve(pkg.bin)).default;
99

1010
describe("E2E CLI init test", function () {
1111

test/specs/e2e/cli/e2e.cli.plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var assert = require("chai").assert;
55
var browserSync = require(path.resolve("./"));
66

77
var pkg = require(path.resolve("package.json"));
8-
var cli = require(path.resolve(pkg.bin));
8+
var cli = require(path.resolve(pkg.bin)).default;
99

1010
describe("E2E CLI `plugins` arg", function () {
1111
it("allows plugins to be registered by 'require' name only", function (done) {

test/specs/e2e/cli/e2e.cli.proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var browserSync = require(path.resolve("./"));
88
var serveStatic = require("serve-static");
99

1010
var pkg = require(path.resolve("package.json"));
11-
var cli = require(path.resolve(pkg.bin));
11+
var cli = require(path.resolve(pkg.bin)).default;
1212

1313
describe("E2E CLI proxy test", function () {
1414

0 commit comments

Comments
 (0)