-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·123 lines (113 loc) · 4.33 KB
/
index.js
File metadata and controls
executable file
·123 lines (113 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#! /usr/bin/env node
const { program } = require('commander')
const dotenv = require('dotenv')
const { existsSync, readdirSync, unlinkSync } = require('fs')
const path = require('path')
const webpack = require(`webpack`)
const { version } = require('../package.json')
const customConfig = path.resolve(process.cwd(), 'webpack.config.js')
const nullstackConfig = path.resolve(process.cwd(), 'node_modules', 'nullstack', 'webpack.config.js')
const config = existsSync(customConfig) ? require(customConfig) : require(nullstackConfig)
function getConfig(options) {
return config.map((env) => env(null, options))
}
function getCompiler(options) {
return webpack(getConfig(options))
}
function loadEnv(name) {
let envPath = '.env'
if (name) {
envPath += `.${name}`
}
dotenv.config({ path: envPath })
}
function clearDir() {
if (existsSync(path.join(process.cwd(), '.development'))) {
const tempFiles = readdirSync(path.join(process.cwd(), '.development'))
for (const file of tempFiles) {
if (file !== '.cache') {
unlinkSync(path.join(process.cwd(), '.development', file))
}
}
}
}
async function start({ port, name, disk, skipCache, trace }) {
process.env.__NULLSTACK_TRACE = (!!trace).toString()
const progress = require('../builders/logger')('server', 'development')
loadEnv(name)
const environment = 'development'
process.env.NULLSTACK_ENVIRONMENT_MODE = 'spa'
process.env.NULLSTACK_ENVIRONMENT_DISK = (!!disk).toString()
process.env.__NULLSTACK_CLI_ENVIRONMENT = environment
if (name) {
process.env.NULLSTACK_ENVIRONMENT_NAME = name
}
if (port) {
process.env.NULLSTACK_SERVER_PORT = port
}
if (!process.env.NULLSTACK_PROJECT_DOMAIN) process.env.NULLSTACK_PROJECT_DOMAIN = 'localhost'
if (!process.env.NULLSTACK_WORKER_PROTOCOL) process.env.NULLSTACK_WORKER_PROTOCOL = 'http'
const settings = config[0](null, { environment, disk, skipCache, name, trace })
const compiler = webpack(settings)
clearDir()
compiler.watch({ aggregateTimeout: 200, hot: true, ignored: /node_modules/ }, (error, stats) => {
progress.stop()
if (error) {
console.error(error.stack || error)
if (error.details) {
console.error(error.details)
}
} else if (stats.hasErrors()) {
console.info(stats.toString({ colors: true, warnings: false, logging: false, assets: false, modules: false }))
}
})
}
function build({ mode = 'ssr', output, name, skipCache }) {
const environment = 'production'
const progress = require('../builders/logger')('application', environment)
const compiler = getCompiler({ environment, skipCache, name })
if (name) {
process.env.NULLSTACK_ENVIRONMENT_NAME = name
}
compiler.run((error, stats) => {
if (error) {
console.error(error.stack || error)
if (error.details) {
console.error(error.details)
}
} else if (stats.hasErrors()) {
console.info(stats.toString({ colors: true }))
process.exit(1)
}
if (stats.hasErrors()) process.exit(1)
progress.stop()
require(`../builders/${mode}`)({ output, environment })
})
}
program
.command('start')
.alias('s')
.description('Start application in development environment')
.option('-p, --port <port>', 'Port number to run the server')
.option('-n, --name <name>', 'Name of the environment. Affects which .env file that will be loaded')
.option('-d, --disk', 'Write files to disk')
.option('-sc, --skip-cache', 'Skip loding and building cache in .development folder')
.option('-t, --trace', 'Trace file compilation')
.helpOption('-h, --help', 'Learn more about this command')
.action(start)
program
.command('build')
.alias('b')
.description('Build application for production environment')
.addOption(new program.Option('-m, --mode <mode>', 'Build production bundles').choices(['ssr', 'spa', 'ssg']))
.option('-o, --output <output>', 'Path to build output folder')
.option('-n, --name <name>', 'Name of the environment. Affects which .env file that will be loaded')
.option('-sc, --skip-cache', 'Skip loding and building cache in .production folder')
.helpOption('-h, --help', 'Learn more about this command')
.action(build)
program
.name('nullstack')
.addHelpCommand(false)
.helpOption('-h, --help', 'Learn more about a specific command')
.version(version, '-v, --version', 'Nullstack version being used')
.parse(process.argv)