Skip to content

Commit a3a926a

Browse files
author
Shane Osbourne
committed
poc
1 parent 8592daa commit a3a926a

26 files changed

+2696
-7203
lines changed

packages/browser-sync/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/dist/*
22
/dist_backup/*
3-
/dist2/*
3+
/dist2/*s
44
meta1.json
55
meta2.json

packages/browser-sync/lib/bin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,14 @@ function handleNoCommand(argv, input, yargs) {
166166
*/
167167
function handleCli(opts) {
168168
opts.cb = opts.cb || utils.defaultCallback;
169-
const m = require(`./cli/command.${opts.cli.input[0]}`);
170-
if (m.default) {
169+
const mapping = {
170+
init: () => require("./cli/command.init.js"),
171+
recipe: () => require("./cli/command.recipe.js"),
172+
reload: () => require("./cli/command.reload.js"),
173+
start: () => require("./cli/command.start.js")
174+
};
175+
const m = mapping[opts.cli.input[0]]?.();
176+
if (m?.default) {
171177
return m.default(opts);
172178
}
173179
return m(opts);

packages/browser-sync/lib/browser-sync.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ BrowserSync.prototype.callback = function(name) {
7272
/**
7373
* @param {import("immutable").Map<string, any>} options
7474
* @param {Function} cb
75-
* @returns {BrowserSync}
75+
* @returns {BrowserSync|undefined}
7676
*/
7777
BrowserSync.prototype.init = function(options, cb) {
7878
/**
@@ -268,14 +268,14 @@ function tasksComplete(bs) {
268268
/**
269269
* Finally, call the user-provided callback given as last arg
270270
*/
271-
bs.cb(null, bs);
271+
bs.cb?.(null, bs);
272272
};
273273
}
274274

275275
/**
276276
* @param module
277277
* @param opts
278-
* @param cb
278+
* @param [cb]
279279
*/
280280
BrowserSync.prototype.registerPlugin = function(module, opts, cb) {
281281
var bs = this;

packages/browser-sync/lib/cli/cli-options.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,10 @@ export function explodeFilesArg(string): string {
9393

9494
/**
9595
* @param value
96-
* @returns {{globs: Array, objs: Array}}
9796
*/
9897
export function makeFilesArg(value) {
99-
let globs = [];
100-
let objs = [];
98+
let globs: string[] = [];
99+
let objs: any[] = [];
101100

102101
if (_.isString(value)) {
103102
globs = globs.concat(explodeFilesArg(value));

packages/browser-sync/lib/cli/command.recipe.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ var chalk = require("chalk");
1010
* or one given with the --output flag
1111
*
1212
* @param opts
13-
* @returns {Function}
1413
*/
1514
module.exports = function(opts) {
1615
var path = require("path");

packages/browser-sync/lib/cli/transforms/addDefaultIgnorePatterns.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function addDefaultIgnorePatterns(incoming: BsTempOptions): TransformResu
2121
.filter(Boolean)
2222
.toSet();
2323

24+
// @ts-expect-error
2425
const merged = userIgnored.merge(defaultIgnorePatterns);
2526

2627
return watchOptions.merge({

packages/browser-sync/lib/cli/transforms/addToFilesOption.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function addToFilesOption(incoming: BsTempOptions): TransformResult {
66
return [incoming, []];
77
}
88

9-
let serverPaths = [];
9+
let serverPaths: string[] = [];
1010

1111
const fromServeStatic = incoming.get("serveStatic", List([])).toArray();
1212
const ssPaths = fromServeStatic.reduce((acc, ss) => {
@@ -22,6 +22,7 @@ export function addToFilesOption(incoming: BsTempOptions): TransformResult {
2222
ssPaths.forEach(p => serverPaths.push(p));
2323

2424
const server = incoming.get("server");
25+
2526
if (server) {
2627
if (server === true) {
2728
serverPaths.push(".");
@@ -37,6 +38,8 @@ export function addToFilesOption(incoming: BsTempOptions): TransformResult {
3738
const baseDirs = List([])
3839
.concat(baseDirProp)
3940
.filter(Boolean);
41+
42+
// @ts-expect-error
4043
baseDirs.forEach(s => serverPaths.push(s));
4144
}
4245
}
Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { fromJS } from "immutable";
1+
import { fromJS, List } from "immutable";
22
import { BsTempOptions, makeFilesArg, TransformResult } from "../cli-options";
3-
import { FilesNamespaces } from "../../types";
3+
import { FilesNamespace, FilesNamespaces, runnerOption } from "../../types";
4+
import { z } from "zod";
45

56
export function handleFilesOption(incoming: BsTempOptions): TransformResult {
6-
const value = incoming.get("files");
77
const namespaces: FilesNamespaces = {
88
core: {
99
globs: [],
1010
objs: []
1111
}
1212
};
1313

14-
const processed = makeFilesArg(value);
14+
const processed = makeFilesArg(incoming.get("files"));
1515

1616
if (processed.globs.length) {
1717
namespaces.core.globs = processed.globs;
@@ -21,5 +21,36 @@ export function handleFilesOption(incoming: BsTempOptions): TransformResult {
2121
namespaces.core.objs = processed.objs;
2222
}
2323

24-
return [incoming.set("files", fromJS(namespaces)), []];
24+
const runners = convertRunnerOption(incoming);
25+
26+
return [incoming.set("files", fromJS({ ...namespaces, ...runners })), []];
27+
}
28+
29+
export function convertRunnerOption(incoming: BsTempOptions): FilesNamespaces | null {
30+
const runners = incoming.has("runners");
31+
if (!runners) return null;
32+
33+
const parser = z.array(runnerOption);
34+
const parsed = parser.safeParse(incoming.get("runners").toJS());
35+
if (!parsed.success) {
36+
// todo: what to do in this case?
37+
return null;
38+
}
39+
const runnerData = parsed.data;
40+
const output: FilesNamespaces = {};
41+
42+
runnerData.forEach((runner, index) => {
43+
const next: FilesNamespace = {
44+
index,
45+
globs: [],
46+
objs: []
47+
};
48+
runner.files.forEach(fileOption => {
49+
if (typeof fileOption === "string") {
50+
next.globs.push(fileOption);
51+
}
52+
});
53+
output["__unstable_runner_" + index] = next;
54+
});
55+
return output;
2556
}

packages/browser-sync/lib/default-config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* @module BrowserSync.options
44
*/
55
module.exports = {
6+
/**
7+
*/
8+
runners: [],
69
/**
710
* Browsersync includes a user-interface that is accessed via a separate port.
811
* The UI allows to controls all devices, push sync updates and much more.
@@ -474,7 +477,7 @@ module.exports = {
474477

475478
/**
476479
* @property host
477-
* @type String
480+
* @type String|null
478481
* @default null
479482
*/
480483
host: null,

packages/browser-sync/lib/file-event-handler.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var utils = require("./utils");
55
* Apply the operators that apply to the 'file:changed' event
66
* @param {import("rxjs").Observable} subject
77
* @param options
8-
* @return {import("rxjs").Observable<{type: string, files: Array<any>}>}
98
*/
109
function fileChanges(subject, options) {
1110
const operators = [
@@ -26,28 +25,14 @@ function fileChanges(subject, options) {
2625
*/
2726
const initial = getAggregatedDebouncedStream(subject, options, scheduler);
2827

29-
return applyOperators(operators, initial, options, scheduler).map(function(items) {
30-
const paths = items.map(x => x.path);
31-
32-
if (utils.willCauseReload(paths, options.get("injectFileTypes").toJS())) {
33-
return {
34-
type: "reload",
35-
files: items
36-
};
37-
}
38-
return {
39-
type: "inject",
40-
files: items
41-
};
42-
});
28+
return applyOperators(operators, initial, options, scheduler);
4329
}
4430
module.exports.fileChanges = fileChanges;
4531

4632
/**
4733
* Apply the operators that apply to the 'browser:reload' event
4834
* @param {import("rxjs").Observable} subject
4935
* @param options
50-
* @returns {import("rxjs").Observable}
5136
*/
5237
function applyReloadOperators(subject, options) {
5338
var operators = [

0 commit comments

Comments
 (0)