From 00a2524d424a00106de45a11fef15e9accdcbd0c Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Fri, 29 Nov 2019 22:31:35 -0800 Subject: [PATCH] feat(*): allow cli args of form `--arg=a` in addition to `--arg a` Fixes #1 --- src/main.js | 11 ++++++++++- test/spec.js | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main.js b/src/main.js index f8ea77c..dd2145f 100644 --- a/src/main.js +++ b/src/main.js @@ -29,12 +29,21 @@ function readVarArgs(params, i) { return [args, i - 1]; } -function parseArgs(params) { +function parseArgs(cmdParams) { let inputFile; let outputFile; let assets = []; let rootDirs = []; + const params = cmdParams.reduce((a, p) => { + if (p.startsWith("--") && p.match(/^--[a-z]+=/)) { + a.push( ... p.split('=', 2)); + } else { + a.push(p); + } + return a; + }, []); + for (let i = 0; i < params.length; i++) { switch (params[i]) { case "--assets": diff --git a/test/spec.js b/test/spec.js index 419fb88..7e5c56e 100644 --- a/test/spec.js +++ b/test/spec.js @@ -1,7 +1,5 @@ const {main, parseArgs} = require('../src/main'); -console.log("parseArgs", parseArgs); - describe('HTML inserter', () => { const inFile = 'data/some/index.html'; const outFile = 'out/some/index.html'; @@ -176,4 +174,17 @@ describe('parseArgs', () => { expect(rootDirs).toEqual(["/c/", "/d/", "./"]); }); + it('should allow `--param=a b` in addition to `--param a b', () => { + const {outputFile, inputFile, assets, rootDirs} = parseArgs([ + "--out=./out", + "--html=./in", + "--assets=./a", "./b", + "--roots=/c/", "/d/" + ]); + + expect(outputFile).toBe("./out"); + expect(inputFile).toBe("./in"); + expect(assets).toEqual(["./a", "./b"]); + expect(rootDirs).toEqual(["/c/", "/d/", "./"]); + }); }); \ No newline at end of file