Skip to content

Commit

Permalink
Switch to paparam
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed May 2, 2024
1 parent 6932c3a commit 5598bde
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
77 changes: 38 additions & 39 deletions bin/bare.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,48 @@
const Module = require('bare-module')
const os = require('bare-os')
const url = require('bare-url')

const argv = require('minimist')(Bare.argv.slice(1), {
stopEarly: true,
boolean: [
'version',
'help'
],
string: [
'eval',
'print'
],
alias: {
version: 'v',
help: 'h',
eval: 'e',
print: 'p'
}
})

const argc = argv._.length

Bare.argv.splice(1, argc, ...argv._)
const { command, flag, arg, rest } = require('paparam')

const parentURL = url.pathToFileURL(os.cwd())

if (parentURL.pathname[parentURL.pathname.length - 1] !== '/') {
parentURL.pathname += '/'
}

if (argv.v) {
console.log(Bare.version)
} else if (argv.h) {
console.log('usage: bare [-e, --eval <script>] [-p, --print <script>] [<filename>]')
} else if (argv.e) {
Module.load(parentURL, `(${argv.e})`)
} else if (argv.p) {
Module.load(parentURL, `console.log(${argv.p})`)
} else if (argc > 0) {
const resolved = new URL(Bare.argv[1], parentURL)

Bare.argv[1] = url.fileURLToPath(resolved)

Module.load(resolved)
} else {
require('bare-repl').start()
}
const bare = command(
'bare',
flag('--version|-v', ''),
flag('--eval|-e <script>', ''),
flag('--print|-p <script>', ''),
arg('<filename>', ''),
rest('[...args]'),
() => {
const { args, flags, rest } = bare

const argv = []

if (args.filename) {
args.filename = new URL(args.filename, parentURL)

argv.push(url.fileURLToPath(args.filename))
}

if (rest) argv.push(...rest)

Bare.argv.splice(1, Bare.argv.length - 1, ...argv)

if (flags.version) {
console.log(Bare.version)
} else if (flags.eval) {
Module.load(parentURL, `(${flags.eval})`)
} else if (flags.print) {
Module.load(parentURL, `console.log(${flags.print})`)
} else if (args.filename) {
Module.load(args.filename)
} else {
require('bare-repl').start()
}
}
)

bare.parse(Bare.argv.slice(1))
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"bare-timers": "^2.0.0",
"bare-tty": "^3.0.0",
"bare-url": "^1.0.0",
"minimist": "^1.2.8"
"paparam": "^1.1.2"
},
"devDependencies": {
"bare-assert": "^1.0.0",
Expand Down

0 comments on commit 5598bde

Please sign in to comment.