Skip to content

Commit

Permalink
refactor(tools/dev-server): use sade for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Feb 18, 2021
1 parent 15373b2 commit acc2bbf
Showing 1 changed file with 97 additions and 125 deletions.
222 changes: 97 additions & 125 deletions tools/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,149 +6,121 @@ const karma = require("karma");
const serve = require("serve-handler");
const colors = require("colors");
const boxen = require("boxen");
const commandLineArgs = require("command-line-args");
const sade = require("sade");
const serveConfig = require("../serve.json");
const { Builder } = require("./builder");

const KARMA_PORT = 9876;
const SERVE_PORT = 5000;

/** @type {import("command-line-args").OptionDefinition[]} */
const optionList = [
{
name: "profile",
alias: "p",
description: "Name of profile to build.",
defaultValue: "w3c",
multiple: false,
type: String,
},
{
name: "interactive",
alias: "i",
description: "Run in interactive mode",
defaultValue: false,
type: Boolean,
},
{
name: "browsers",
description: "Browsers for Karma unit tests",
defaultOption: undefined,
multiple: true,
type: String,
},
{
name: "grep",
description: "Run specific tests using karma --grep",
defaultOption: undefined,
type: String,
},
];

let args;
try {
args = commandLineArgs(optionList);
} catch (err) {
console.error(colors.error(err.message));
process.exit(127);
}
sade("./tools/dev-server.js", true)
.option("-p, --profile", "Name of profile to build.", "w3c")
.option("-i, --interactive", "Run in interactive mode.", false)
.option("--browsers", "Browsers for Karma unit tests")
.option("--grep", "Run specific tests using karma --grep")
.action(opts => run(opts))
.parse(process.argv);

function run(args) {
let isActive = false;

const karmaConfig = karma.config.parseConfig(
path.join(__dirname, "../karma.conf.js"),
{
browsers: args.browsers,
autoWatch: false,
port: KARMA_PORT,
logLevel: karma.constants.LOG_WARN,
client: {
args: ["--grep", args.grep || ""],
},
mochaReporter: { ignoreSkipped: true },
}
);
const karmaServer = new karma.Server(karmaConfig);
const devServer = createServer((req, res) => serve(req, res, serveConfig));
devServer.on("error", onError);

let isActive = false;

const karmaConfig = karma.config.parseConfig(
path.join(__dirname, "../karma.conf.js"),
{
browsers: args.browsers,
autoWatch: false,
port: KARMA_PORT,
logLevel: karma.constants.LOG_WARN,
client: {
args: ["--grep", args.grep || ""],
},
mochaReporter: { ignoreSkipped: true },
if (args.interactive) {
registerStdinHandler();
} else {
const paths = ["./src", "./tests/spec"];
const watcher = chokidar.watch(paths, { ignoreInitial: true });
watcher.on("all", onFileChange);
watcher.on("error", onError);
}
);
const karmaServer = new karma.Server(karmaConfig);
karmaServer.start();

const devServer = createServer((req, res) => serve(req, res, serveConfig));
devServer.listen(SERVE_PORT);
devServer.on("error", onError);

if (args.interactive) {
registerStdinHandler();
} else {
const paths = ["./src", "./tests/spec"];
const watcher = chokidar.watch(paths, { ignoreInitial: true });
watcher.on("all", onFileChange);
watcher.on("error", onError);
}

process.on("exit", () => {
execSync("git checkout -- builds", { stdio: "inherit" });
});

printWelcomeMessage(args);
karmaServer.on("browsers_ready", buildAndTest);
process.on("exit", () => {
execSync("git checkout -- builds", { stdio: "inherit" });
});

function registerStdinHandler() {
// https://stackoverflow.com/a/12506613
const stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding("utf8");
printWelcomeMessage(args);

karmaServer.start();
devServer.listen(SERVE_PORT);
karmaServer.on("browsers_ready", () =>
buildAndTest({ profile: args.profile })
);

function registerStdinHandler() {
// https://stackoverflow.com/a/12506613
const stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding("utf8");

stdin.on("data", async key => {
if (isActive) {
// do nothing if already active
return process.stdout.write(key);
}

stdin.on("data", async key => {
if (isActive) {
// do nothing if already active
return process.stdout.write(key);
}
switch (key) {
case "\u0003": // ctrl-c (end of text)
case "q": {
return karma.stopper.stop(karmaConfig, code => {
setTimeout(() => process.exit(code), 0);
});
}
case "t":
return await buildAndTest();
case "T":
return await buildAndTest({ preventBuild: true });
case "h":
return printWelcomeMessage(args);
default:
process.stdout.write(key);
}
});
}

switch (key) {
case "\u0003": // ctrl-c (end of text)
case "q": {
return karma.stopper.stop(karmaConfig, code => {
setTimeout(() => process.exit(code), 0);
});
async function buildAndTest(options = {}) {
const { preventBuild = false } = options;
if (isActive) return;
try {
isActive = true;
if (!preventBuild) {
await Builder.build({ name: args.profile, debug: true });
}
case "t":
return await buildAndTest();
case "T":
return await buildAndTest({ preventBuild: true });
case "h":
return printWelcomeMessage(args);
default:
process.stdout.write(key);
karma.runner.run(karmaConfig, () => {});
} catch (err) {
console.error(colors.error(err.stack));
} finally {
isActive = false;
}
});
}

async function onFileChange(_event, file) {
const preventBuild = file.startsWith("tests");
await buildAndTest({ preventBuild });
}
}

async function buildAndTest(options = {}) {
const { preventBuild = false } = options;
if (isActive) return;
try {
isActive = true;
if (!preventBuild) {
await Builder.build({ name: args.profile, debug: true });
}
karma.runner.run(karmaConfig, () => {});
} catch (err) {
function onError(err) {
console.error(colors.error(err.stack));
} finally {
isActive = false;
karma.stopper.stop(karmaConfig, () => {
process.exit(1);
});
}
}

function onError(err) {
console.error(colors.error(err.stack));
karma.stopper.stop(karmaConfig, () => {
process.exit(1);
});
async function onFileChange(_event, file) {
const preventBuild = file.startsWith("tests");
await buildAndTest({ preventBuild });
}
}

function printWelcomeMessage(args) {
Expand Down

0 comments on commit acc2bbf

Please sign in to comment.