Skip to content

Commit

Permalink
Switch to parseArgs and argsClopts
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Dec 28, 2023
1 parent d929420 commit 7c7d921
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 50 deletions.
98 changes: 52 additions & 46 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env node
/* eslint-disable dot-notation */

import minimist from 'minimist'
// @ts-ignore
import cliclopts from 'cliclopts'
import { readFile } from 'node:fs/promises'
import { resolve, join, relative } from 'node:path'
import { parseArgs } from 'node:util'
import { printHelpText } from 'argsclopts'
import readline from 'node:readline'
import desm from 'desm'
import process from 'process'
Expand All @@ -25,6 +25,7 @@ import { askYesNo } from './lib/helpers/cli-prompt.js'
/**
* @typedef {import('./lib/builder.js').TopBunOpts} TopBunOpts
* @typedef {import('./lib/builder.js').Results} Results
* @typedef {import('argsclopts').ArgscloptsParseArgsOptionsConfig} ArgscloptsParseArgsOptionsConfig
*/

const __dirname = desm(import.meta.url)
Expand All @@ -35,55 +36,52 @@ async function getPkg () {
return pkg
}

const clopts = cliclopts([
{
name: 'src',
abbr: 's',
help: 'path to source directory',
default: 'src'
/** @type {ArgscloptsParseArgsOptionsConfig} */
const options = {
src: {
type: 'string',
short: 's',
default: 'src',
help: 'path to source directory'
},
{
name: 'dest',
abbr: 'd',
help: 'path to build destination directory',
default: 'public'
dest: {
type: 'string',
short: 'd',
default: 'public',
help: 'path to build destination directory'
},
{
name: 'ignore',
abbr: 'i',
ignore: {
type: 'string',
short: 'i',
help: 'comma separated gitignore style ignore string'
},
{
name: 'eject',
abbr: 'e',
eject: {
type: 'boolean',
short: 'e',
help: 'eject the top bun default layout, style and client into the src flag directory'
},
{
name: 'watch',
abbr: 'w',
help: 'build, watch and serve the site build',
Boolean: true
watch: {
type: 'boolean',
short: 'w',
help: 'build, watch and serve the site build'
},
{
name: 'watch-only',
help: 'watch and build the src folder without serving',
Boolean: false
'watch-only': {
type: 'boolean',
help: 'watch and build the src folder without serving'
},
{
name: 'help',
abbr: 'h',
help: 'show help',
Boolean: true
help: {
type: 'boolean',
short: 'h',
help: 'show help'
},
{
name: 'version',
abbr: 'v',
boolean: true,
version: {
type: 'boolean',
short: 'v',
help: 'show version information'
}
])
}

const argv = minimist(process.argv.slice(2), clopts.options())
const { values: argv } = parseArgs({ options })

async function run () {
if (argv['version']) {
Expand All @@ -94,15 +92,23 @@ async function run () {

if (argv['help']) {
const pkg = await getPkg()
console.log('Usage: top-bun [options]\n')
console.log(' Example: top-bun --src website --dest public\n')
clopts.print()
console.log(`top-bun (v${pkg.version})`)
await printHelpText({
options,
name: pkg.name,
version: pkg.version,
exampleFn: ({ name }) => ' ' + `Example: ${name} --src website --dest public\n`
})

process.exit(0)
}
const cwd = process.cwd()
const src = resolve(join(cwd, argv['src']))
const dest = resolve(join(cwd, argv['dest']))
const srcFlag = String(argv['src'])
const destFlag = String(argv['dest'])
if (!srcFlag) throw new Error('The src flag is required')
if (!destFlag) throw new Error('The dest flag is required')

const src = resolve(join(cwd, srcFlag))
const dest = resolve(join(cwd, destFlag))

// Eject task
if (argv['eject']) {
Expand Down Expand Up @@ -181,7 +187,7 @@ top-bun eject actions:
/** @type {TopBunOpts} */
const opts = {}

if (argv['ignore']) opts.ignore = argv['ignore'].split(',')
if (argv['ignore']) opts.ignore = String(argv['ignore']).split(',')

const topBun = new TopBun(src, dest, opts)

Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"npm": ">=9"
},
"dependencies": {
"argsclopts": "^1.0.4",
"async-folder-walker": "^3.0.1",
"browser-sync": "^3.0.2",
"cheerio": "^1.0.0-rc.10",
"chokidar": "^3.5.2",
"clean-deep": "^3.4.0",
"cliclopts": "^1.1.1",
"cpx2": "^7.0.0",
"desm": "^1.1.0",
"esbuild": "^0.19.0",
Expand All @@ -47,13 +47,12 @@
"markdown-it-table-of-contents": "^0.6.0",
"markdown-it-task-lists": "^2.1.1",
"mine.css": "^9.0.1",
"minimist": "^1.2.5",
"p-map": "^7.0.0",
"package-json": "^8.1.1",
"pkg-dir": "^8.0.0",
"pretty": "^2.0.0",
"pretty-tree": "^1.0.0",
"read-pkg": "^9.0.1",
"pkg-dir": "^8.0.0",
"uhtml-isomorphic": "^2.0.0",
"write-package": "^7.0.0"
},
Expand All @@ -62,7 +61,6 @@
"@types/js-yaml": "^4.0.8",
"@types/markdown-it": "^13.0.5",
"@types/markdown-it-footnote": "^3.0.2",
"@types/minimist": "^1.2.4",
"@types/node": "^20.8.4",
"@voxpelli/tsconfig": "^9.0.0",
"auto-changelog": "^2.0.0",
Expand Down

0 comments on commit 7c7d921

Please sign in to comment.