Skip to content

Commit

Permalink
update build script to use parseArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Feb 28, 2023
1 parent 1d487c2 commit 4878cd8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
39 changes: 21 additions & 18 deletions esbuild.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env node

// @ts-check
import path from 'path'
import fs from 'fs'
import * as esbuild from 'esbuild'
import fs from 'node:fs'
import path from 'node:path'
import { promisify, parseArgs } from 'node:util'
import rimraf from 'rimraf'
import * as esbuild from 'esbuild'

const VALID_COMMANDS = ['build', 'watch', 'clean']
const VALID_MODES = ['dev', 'prod']
Expand Down Expand Up @@ -56,35 +56,38 @@ await runCommand(extractArgs())

/* ------------------------------------------------------- */

// TODO: Use parseArgs
/**
* @returns {{command: 'build'|'watch'|'clean', mode: 'dev'|'mode'}}
*/
function extractArgs () {
const [command, modeArg] = process.argv.slice(2)
const { values, positionals } = parseArgs({
options: {
mode: {
type: 'string',
short: 'm'
}
},
allowPositionals: true
})

const command = positionals[0]
const mode = values.mode || 'dev'

if (!VALID_COMMANDS.includes(command)) {
const message =
(c ? `Invalid command '${command}' specified.` : `No command specified`) +
(command
? `Invalid command '${command}' specified.`
: `No command specified`) +
`\nPlease use one of the following: ${VALID_COMMANDS.join(', ')}`

console.error(message)

process.exit(1)
}

/**
* @type {'prod' | 'dev'}
*/
let mode = 'dev'

if (!modeArg) {
console.log('No mode argument provided. Using dev')
} else if (VALID_MODES.includes(modeArg)) {
mode = modeArg
} else {
if (!VALID_MODES.includes(mode)) {
const message =
`Invalid mode '${modeArg}' specified.` +
`Invalid mode '${mode}' specified.` +
`\nPlease use of of the following: ${VALID_MODES.join(', ')}`

console.error(message)
Expand All @@ -100,7 +103,7 @@ function extractArgs () {

/**
* @param {Object} opts
* @param {'build' | 'watch'|'clean'} opts.command
* @param {'build'|'watch'|'clean'} opts.command
* @param {'dev' | 'prod'} opts.mode
*/
async function runCommand (opts) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"pack": "electron-builder --config ./builder.config.js --dir",
"dist": "electron-builder --config ./builder.config.js --publish=onTag",
"build": "npm-run-all -s build:translations build:js",
"build:js": "node esbuild.mjs build prod",
"build:js": "node esbuild.mjs build --mode prod",
"build:translations": "node bin/build-translations.js",
"lint": "eslint .",
"test": "npm run lint && npm run test-integration",
"test-unit": "jest",
"test-integration": "tape test",
"device": "DEBUG=* node bin/mock.js",
"watch": "node esbuild.mjs watch dev",
"watch": "node esbuild.mjs watch --mode dev",
"start": "electron . --disable-http-cache",
"server": "node index.js --headless",
"dev": "electron . --inspect --disable-http-cache",
Expand Down

0 comments on commit 4878cd8

Please sign in to comment.