Skip to content

Commit

Permalink
feat(*): change CLI to use specific flags for input/output html, asse…
Browse files Browse the repository at this point in the history
…ts, root dirs
  • Loading branch information
jbedard committed Nov 23, 2019
1 parent 5033768 commit 3372b0c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
59 changes: 45 additions & 14 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,52 @@ function findElementByName(d, name) {
return undefined;
}

function main(params, read = fs.readFileSync, write = fs.writeFileSync, timestamp = Date.now) {
const outputFile = params.shift();
const inputFile = params.shift();
const rootDirs = [];
while (params.length && params[0] !== '--assets') {
let r = params.shift();
if (!r.endsWith('/')) {
r += '/';
function readVarArgs(params, i) {
const args = [];
while (i < params.length && !params[i].startsWith("--")) {
args.push(params[i++]);
}
return [args, i - 1];
}

function parseArgs(params) {
let inputFile;
let outputFile;
let assets = [];
let rootDirs = [];

for (let i = 0; i < params.length; i++) {
switch (params[i]) {
case "--assets":
[assets, i] = readVarArgs(params, i+1);
break;

case "--roots":
[rootDirs, i] = readVarArgs(params, i+1);
break;

case "--out":
outputFile = params[++i];
break;

case "--html":
inputFile = params[++i];
break;

default:
throw Error(`Unknown arg: ${params[i]}`);
}
rootDirs.push(r);
}
// Always trim the longest prefix

// Make dir '/'s consistent. Always trim the longest prefix
rootDirs = rootDirs.map(r => r.endsWith('/') ? r : r + '/');
rootDirs.sort((a, b) => b.length - a.length);
params.shift(); // --assets

return {inputFile, outputFile, assets, rootDirs};
}

function main(params, read = fs.readFileSync, write = fs.writeFileSync, timestamp = Date.now) {
const {inputFile, outputFile, assets, rootDirs} = parseArgs(params);

const document = parse5.parse(read(inputFile, {encoding: 'utf-8'}), {treeAdapter});

Expand Down Expand Up @@ -62,7 +94,7 @@ function main(params, read = fs.readFileSync, write = fs.writeFileSync, timestam
return execPath;
}

const jsFiles = params.filter(s => /\.m?js$/i.test(s));
const jsFiles = assets.filter(s => /\.m?js$/i.test(s));
for (const s of jsFiles) {
// Differential loading: for filenames like
// foo.mjs
Expand Down Expand Up @@ -118,6 +150,5 @@ module.exports = {main};
if (require.main === module) {
// We always require the arguments are encoded into a flagfile
// so that we don't exhaust the command-line limit.
const params = fs.readFileSync(process.argv[2], {encoding: 'utf-8'}).split('\n').filter(l => !!l);
process.exitCode = main(params);
process.exitCode = main(process.argv.slice(2));
}
20 changes: 10 additions & 10 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,57 @@ describe('HTML inserter', () => {
}

it('should do be a no-op', () => {
expect(inserter.main([outFile, inFile], read, write)).toBe(0);
expect(inserter.main(["--out", outFile, "--html", inFile,], read, write)).toBe(0);
expect(output).toBe('<html><head></head><body></body></html>');
});

it('should inject script tag', () => {
expect(inserter.main([outFile, inFile, '--assets', 'path/to/my.js'], read, write, () => 123)).toBe(0);
expect(inserter.main(["--out", outFile, "--html", inFile, '--assets', 'path/to/my.js'], read, write, () => 123)).toBe(0);
expect(output).toBe(
'<html><head></head><body><script src="/path/to/my.js?v=123"></script></body></html>');
});

it('should allow the "module js" extension', () => {
expect(inserter.main([outFile, inFile, '--assets', 'path/to/my.mjs'], read, write, () => 123))
expect(inserter.main(["--out", outFile, "--html", inFile, '--assets', 'path/to/my.mjs'], read, write, () => 123))
.toBe(0);
expect(output).toBe(
'<html><head></head><body><script type="module" src="/path/to/my.mjs?v=123"></script></body></html>');
});

it('should allow the ".es2015.js" extension', () => {
expect(inserter.main(
[outFile, inFile, '--assets', 'path/to/my.es2015.js'], read, write, () => 123))
["--out", outFile, "--html", inFile, '--assets', 'path/to/my.es2015.js'], read, write, () => 123))
.toBe(0);
expect(output).toBe(
'<html><head></head><body><script type="module" src="/path/to/my.es2015.js?v=123"></script></body></html>');
});

it('should strip longest prefix', () => {
expect(inserter.main([outFile, inFile,
'path', 'path/to',
expect(inserter.main(["--out", outFile, "--html", inFile,
"--roots", 'path', 'path/to',
'--assets', 'path/to/my.js'], read, write, () => 123)).toBe(0);
expect(output).toBe(
'<html><head></head><body><script src="/my.js?v=123"></script></body></html>');
});

it('should strip external workspaces', () => {
expect(inserter.main([outFile, inFile,
'npm/node_modules/zone.js/dist',
expect(inserter.main(["--out", outFile, "--html", inFile,
"--roots", 'npm/node_modules/zone.js/dist',
'--assets', 'external/npm/node_modules/zone.js/dist/zone.min.js'], read, write, () => 123)).toBe(0);
expect(output).toBe(
'<html><head></head><body><script src="/zone.min.js?v=123"></script></body></html>');

});

it('should inject link tag', () => {
expect(inserter.main([outFile, inFile, '--assets', 'path/to/my.css'], read, write, () => 123)).toBe(0);
expect(inserter.main(["--out", outFile, "--html", inFile, '--assets', 'path/to/my.css'], read, write, () => 123)).toBe(0);
expect(output).toBe(
'<html><head><link rel="stylesheet" href="/path/to/my.css?v=123"></head><body></body></html>');
});

it('should create a pair of script tags for differential loading', () => {
expect(inserter.main(
[outFile, inFile, '--assets', 'path/to/my.js', 'path/to/my.es2015.js'], read, write,
["--out", outFile, "--html", inFile, '--assets', 'path/to/my.js', 'path/to/my.es2015.js'], read, write,
() => 123))
.toBe(0);
expect(output).toBe(
Expand Down

0 comments on commit 3372b0c

Please sign in to comment.