UNPKG

3.36 kBPlain TextView Raw
1#!/usr/bin/env node
2
3import fs from 'fs-extra'
4import sade from 'sade'
5
6import pkg from '../package.json'
7
8import * as logger from './log'
9import { createConfig, getConfigFile } from './config'
10import { watch } from './watch'
11import { build } from './build'
12import { serve } from './serve'
13import { Env } from './constants'
14
15const prog = sade('presta')
16const CONFIG_DEFAULT = 'presta.config.js'
17
18function registerRuntime(options = {}) {
19 require('module-alias').addAliases({
20 '@': process.cwd(),
21 'presta:internal': __dirname, // wherever this is running from
22 })
23
24 require('esbuild-register/dist/node').register(options)
25}
26
27prog
28 .version(pkg.version)
29 // do not provide default config here
30 .option('--config, -c', `Path to a config file. (default ${CONFIG_DEFAULT})`)
31 .option('--output, -o', `Specify output directory for built files. (default ./build)`)
32 .option('--assets, -a', `Specify static asset directory. (default ./public)`)
33 .option('--debug, -d', `Enable debug mode (prints more logs)`)
34 .example(`dev index.jsx -o dist`)
35 .example(`dev 'pages/*.tsx' -o static`)
36 .example(`'pages/*.tsx'`)
37 .example(`-c site.json`)
38 .example(`serve -p 8080`)
39
40prog
41 .command('build', 'Build project to output directory.', { default: true })
42 .example(``)
43 .example(`files/**/*.js`)
44 .example(`-c ${CONFIG_DEFAULT}`)
45 .action(async (opts) => {
46 registerRuntime()
47
48 console.clear()
49
50 const config = await createConfig({
51 env: Env.PRODUCTION,
52 config: getConfigFile(opts.config, true),
53 cli: {
54 ...opts,
55 files: opts._,
56 },
57 })
58
59 fs.emptyDirSync(config.output)
60
61 logger.raw(`${logger.colors.blue('presta build')}`)
62 logger.newline()
63
64 await build(config)
65 })
66
67prog
68 .command('dev', 'Start Presta dev server and watch files', { alias: 'watch' })
69 .option('--port, -p', `Port to run the local server. (default 4000)`)
70 .option('--no-serve, -n', `Do not run local dev server. (default false)`)
71 .describe('Watch project and build to output directory.')
72 .example(`dev`)
73 .example(`dev ./files/**/*.js`)
74 .example(`dev ./files/**/*.js -o ./out`)
75 .example(`dev -c ${CONFIG_DEFAULT}`)
76 .action(async (opts) => {
77 registerRuntime()
78
79 console.clear()
80
81 const config = await createConfig({
82 env: Env.DEVELOPMENT,
83 config: getConfigFile(opts.config),
84 cli: {
85 ...opts,
86 files: opts._,
87 },
88 })
89
90 if (!opts.n) {
91 const server = await serve(config)
92
93 logger.raw(`${logger.colors.blue('presta dev')} - http://localhost:${server.port}`)
94 logger.newline()
95 } else {
96 logger.info({
97 label: 'dev',
98 })
99 logger.newline()
100 }
101
102 watch(config)
103 })
104
105prog
106 .command('serve')
107 .option('--port, -p', `Port to run the local server. (default 4000)`)
108 .describe('Serve built files, lambdas, and static assets.')
109 .example(`serve`)
110 .example(`serve -o ./out -p 8080`)
111 .example(`serve -c ${CONFIG_DEFAULT}`)
112 .action(async (opts) => {
113 registerRuntime()
114
115 console.clear()
116
117 const config = await createConfig({
118 env: Env.PRODUCTION,
119 config: getConfigFile(opts.config),
120 cli: opts,
121 })
122 const server = await serve(config)
123
124 logger.raw(`${logger.colors.blue('presta serve')} - http://localhost:${server.port}`)
125 logger.newline()
126 })
127
128prog.parse(process.argv)