Skip to content

Commit

Permalink
feat: refactor into single class
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Aug 3, 2022
1 parent 2a372f8 commit 5e9f53b
Show file tree
Hide file tree
Showing 4 changed files with 845 additions and 1 deletion.
81 changes: 81 additions & 0 deletions packages/presta/lib/bin.beta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node

import sade from 'sade'
import { register as esbuild } from 'esbuild-register/dist/node'

import pkg from '../package.json'
import {
PrestaEnv,
defaultJSConfigFilepath,
findAndParseConfigFile,
mergeConfig,
PrestaCliArgs,
Presta,
} from './index.beta'

esbuild()

const program = sade('presta')

program
.version(pkg.version)
// do not provide default config here
.option('--config, -c', `Path to a config file. (default ${defaultJSConfigFilepath})`)
.option('--output, -o', `Specify output directory for built files. (default ./build)`)
.option('--assets, -a', `Specify static asset directory. (default ./public)`)
.option('--debug, -d', `Enable debug mode (prints more logs)`)
.example(`dev index.jsx -o dist`)
.example(`dev 'pages/*.tsx' -o static`)
.example(`'pages/*.tsx'`)
.example(`-c site.json`)
.example(`serve -p 8080`)

program
.command('build', 'Build project to output directory.', { default: true })
.example(``)
.example(`files/**/*.js`)
.example(`-c ${defaultJSConfigFilepath}`)
.action(async (cliArgs: PrestaCliArgs) => {
process.env.PRESTA_ENV = PrestaEnv.Production
process.env.PRESTA_DEBUG = cliArgs.debug ? 'debug' : ''
console.clear()
const configFile = findAndParseConfigFile(cliArgs.config)
const config = await mergeConfig(cliArgs, configFile)
await new Presta(config, { cliArgs }).build()
})

program
.command('dev', 'Start Presta dev server and watch files', { alias: 'watch' })
.option('--port, -p', `Port to run the local server. (default 4000)`)
.option('--no-serve, -n', `Do not run local dev server. (default false)`)
.describe('Watch project and build to output directory.')
.example(`dev`)
.example(`dev ./files/**/*.js`)
.example(`dev ./files/**/*.js -o ./out`)
.example(`dev -c ${defaultJSConfigFilepath}`)
.action(async (cliArgs: PrestaCliArgs) => {
process.env.PRESTA_ENV = PrestaEnv.Development
process.env.PRESTA_DEBUG = cliArgs.debug ? 'debug' : ''
console.clear()
const configFile = findAndParseConfigFile(cliArgs.config)
const config = await mergeConfig(cliArgs, configFile)
new Presta(config, { cliArgs }).watch()
})

program
.command('serve')
.option('--port, -p', `Port to run the local server. (default 4000)`)
.describe('Serve built files, lambdas, and static assets.')
.example(`serve`)
.example(`serve -o ./out -p 8080`)
.example(`serve -c ${defaultJSConfigFilepath}`)
.action(async (cliArgs: PrestaCliArgs) => {
process.env.PRESTA_ENV = PrestaEnv.Development
process.env.PRESTA_DEBUG = cliArgs.debug ? 'debug' : ''
console.clear()
const configFile = findAndParseConfigFile(cliArgs.config)
const config = await mergeConfig(cliArgs, configFile)
new Presta(config, { cliArgs }).serve()
})

program.parse(process.argv)

0 comments on commit 5e9f53b

Please sign in to comment.