Skip to content

Commit

Permalink
! Rewrite to use opts.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Feb 20, 2024
1 parent 13b10bc commit 215fe8f
Show file tree
Hide file tree
Showing 19 changed files with 337 additions and 180 deletions.
4 changes: 0 additions & 4 deletions .github/litejs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
{
"build": false,
"install": false,
"lint": false,
"test": "node -r ./test.js --expose-gc test/index.js"
}
114 changes: 114 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env node
//-
//- Usage
//- lj [init|bench|build|help|test]
//-
//- build options
//- --banner, -b Add comment banner to output
//- --input, -i Input file
//- --output, -o Output file
//- --readme, -r Replase readme tags in file
//-
//- Examples
//- lj b -r README.md -i ui/dev.html -o ui/index.html
//- lj r
//-

var fs = require("fs")
, path = require("path")
, opts = require("./opts.js").opts(mergeOpts({
build_b: {
banner: "",
cat: true,
fetch: true,
out: "",
readme: "",
worker: ""
},
init_i: {},
bench: {},
release_r: {
build: true,
commit: true,
global: "",
install: true,
lint: true,
rewrite: false,
tag:true,
test: true,
update: true,
upstream: true
},
test_t: {
brief: false,
coverage: false,
lcov: true,
sources: "./*.js",
status: true,
tz: "",
up: false,
watch: false
},
color: true,
help: false,
version: true
}, [ "package.json", "litejs", ".github/litejs.json", null ]))
, libFile = opts._cmd && "./lib/" + opts._cmd + ".js"
, package = require("./package.json")
, userPackage = {}
, hasOwn = userPackage.hasOwnProperty

try {
userPackage = require(path.resolve("package.json"))
} catch(e) {}


if (opts.tz) process.env.TZ = opts.tz

if (opts._unknown[0] && opts._cmd !== "test") {
console.error("Unknown option: " + opts._unknown)
usage()
process.exit(1)
} else if (libFile && !opts.help) {
if (opts.version) console.error("# %s %s@%s with %s@%s", opts._cmd, userPackage.name, userPackage.version, package.name, package.version)
require(libFile)(opts)
} else {
usage()
}

function usage() {
if (opts.version) console.log("%s v%s", package.name, package.version)
var helpFile = libFile ? path.resolve(module.filename, "." + libFile) : module.filename
console.log(fs.readFileSync(helpFile, "utf8").match(/^\/\/-.*/gm).join("\n").replace(/^.../gm, ""))
}

function mergeOpts(opts, searchList) {
var file = searchList.shift()
, key = searchList.shift()
if (file) try {
var conf = require(path.resolve(file))
if (key) conf = conf[key]
if (conf) {
return assignOpts(opts, conf)
}
} catch(e) {}
if (searchList[0]) mergeOpts(opts, searchList)
return opts
}

function assignOpts(to, from) {
var key, val, tmp
for (key in to) if (hasOwn.call(to, key)) {
tmp = key.split("_")
val = hasOwn.call(from, tmp[0]) ? from[tmp[0]] : hasOwn.call(from, tmp[1]) ? from[tmp[1]] : null
if (val !== null) to[key] = isObj(val) ? assignOpts(to[key], val) : val
}
return to
}

function isObj(obj) {
return !!obj && obj.constructor === Object
}



168 changes: 22 additions & 146 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
#!/usr/bin/env node
//-
//- Usage
//- lj [init|bench|build|help|test]
//-
//- build options
//- --banner, -b Add comment banner to output
//- --input, -i Input file
//- --output, -o Output file
//- --readme, -r Replase readme tags in file
//-
//- Examples
//- lj b -r README.md -i ui/dev.html -o ui/index.html
//- lj r
//-

exports.command = command
exports.cp = cp
exports.debounce = debounce
exports.deepAssign = deepAssign
exports.hold = hold
exports.isObj = isObj
exports.ls = ls
exports.mkdirp = mkdirp
exports.readFile = readFile
exports.rmrf = rmrf
exports.wait = wait
exports.watch = watch
exports.writeFile = writeFile
exports.writePackage = writePackage

exports.cols = +process.env.COLUMNS || process.stdout.columns || 80
exports.rows = +process.env.ROWS || process.stdout.rows || 24
Expand All @@ -36,137 +24,8 @@ if (parseInt(process.version.slice(1), 10) < 15) require("./lib/shim.js")
var child = require("child_process")
, fs = require("fs")
, path = require("path")
, now = new Date()
, cli = Object.assign(exports, require("./package.json"), {
conf: {
date: now.toISOString().split("T")[0]
},
dom: require("@litejs/dom"),
writePackage: writePackage
})
, defaults = {
"bench": "lj bench ./test/bench/*.js",
"build": "lj build --out=ui/index.html ui/dev.html",
"commit": true,
"launch": "node",
"lcov": true,
"sources": "./*.js",
"status": 1,
"tag": true,
"test": "lj test ./test/index.js",
"threads": 0,
"update": true
}
, shortcut = {
b: "build",
h: "help",
r: "release",
t: "test"
}
, commands = {
build: 1,
init: 1,
bench: 1,
release: 1,
test: 1
}
, hasOwn = commands.hasOwnProperty
, intArgs = /^(samples|sample-time|warmup)$/
, nodeArgs = /^(allow-natives-syntax)$/




try {
var userPackage = require(path.resolve("package.json"))
Object.assign(cli.conf, userPackage)
} /* c8 ignore next */ catch(e) {}

readConf([
"package.json", "litejs",
".github/litejs.json", null
])

function readConf(opts) {
var file = opts.shift()
, key = opts.shift()
if (file) try {
var conf = require(path.resolve(file))
if (key) conf = conf[key]
if (conf) return Object.assign(defaults, conf)
} /* c8 ignore next */ catch(e) {}
if (opts[0]) readConf(opts)
}
, hasOwn = {}.hasOwnProperty

function getopts(argv) {
var opts = Object.assign({}, defaults, {args: argv, opts: [], nodeArgs: []})
for (var arg, i = argv.length; i; ) {
arg = argv[--i].split(/^--(no-)?|=/)
if (arg[0] === "") {
opts[nodeArgs.test(arg[2]) ? "nodeArgs" : "opts"].push(argv[i])
opts[arg[2]] = intArgs.test(opts[arg[2]]) ? 0|(arg[4] || !arg[1]) : arg[4] || !arg[1]
argv.splice(i, 1)
}
}
opts.cmd = argv.shift()
return opts
}

if (!module.parent) {
execute(getopts(process.argv.slice(2)))
}

function run(opt, cmd, addOpts) {
if (cmd) try {
;(Array.isArray(cmd) ? cmd : [cmd]).forEach(function(cmd) {
cmd += addOpts ? " " + addOpts : ""
child.execSync(replaceVersion(cmd), { stdio: "inherit" })
})
} catch (e) {
console.error("\n%s\nIgnore with --no-%s option.", e.message, opt)
process.exit(1)
}
}
function replaceVersion(cmd) {
var re = /{v(\d)}/g
, ver = (cli.conf.version || "0.0.0").split(".")
return cmd.replace(re, function(all, num) {
return ver[num]
})
}

function execute(opts) {
var sub
, cmd = shortcut[opts.cmd] || opts.cmd
, helpFile = module.filename

if (opts.version) console.log("%s v%s", cli.name, cli.version)

if (!opts.version || cmd) switch (cmd) {
case "bench":
case "build":
case "test":
if (opts.args.length < 1) {
return run(cmd, opts[cmd], opts.opts.join(" "))
}
/* falls through */
case "init":
case "release":
require("./cli/" + cmd)(opts)
break;
case "lint":
run(cmd, opts[cmd])
break;
case "help":
sub = shortcut[opts.args[0]] || opts.args[0]
if (hasOwn.call(commands, sub)) {
helpFile = path.join(path.dirname(module.filename), "cli", sub + ".js")
}
/* falls through */
default:
console.log(readFile(helpFile).match(/^\/\/-.*/gm).join("\n").replace(/^.../gm, ""))
}
}

function command(name) {
try {
Expand Down Expand Up @@ -198,11 +57,29 @@ function debounce(fn, time) {
}
}

function deepAssign(to) {
if (to !== Object.prototype) for (var key, from, a = arguments, i = 1, len = a.length; i < len; ) {
if ((from = a[i++])) for (key in from) if (hasOwn.call(from, key)) {
if (from[key] === null) delete to[key]
else to[key] = (
isObj(from[key]) ?
deepAssign(isObj(to[key]) ? to[key] : {}, from[key]) :
from[key]
)
}
}
return to
}

function flat(arr) {
var out = []
return out.concat.apply(out, arr)
}

function isObj(obj) {
return !!obj && obj.constructor === Object
}

function ls() {
var key, dirRe, outRe, tmp, tmp2
, arr = flat(arguments)
Expand Down Expand Up @@ -284,7 +161,6 @@ function writePackage(obj) {
writeFile("package.json", JSON.stringify(obj, null, " ") + "\n")
}


function wait(fn) {
var pending = 1
function resume() {
Expand Down
9 changes: 6 additions & 3 deletions cli/bench.js → lib/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ var cli = require("..")
, bench = require("../bench.js")

global.requireGit = requireGit
if (!global.gc) global.gc = require("vm").runInNewContext("gc")
if (!global.gc) {
require("v8").setFlagsFromString("--expose_gc")
global.gc = require("vm").runInNewContext("gc")
}

module.exports = function(opts) {
var files = cli.ls(opts.args)
if (!files[0]) return console.error("No files found: " + opts.args)
var files = cli.ls(opts._)
if (!files[0]) return console.error("No files found: " + opts._)
run(files, opts)
}

Expand Down
Loading

0 comments on commit 215fe8f

Please sign in to comment.