/
index.js
53 lines (51 loc) · 1.71 KB
/
index.js
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
const { SolitConfig, ServerConfig, dev } = require('./config')
const { spawn } = require('child_process')
const fs = require('fs')
const MFS = require('memory-fs')
const path = require('path')
const appRootDir = require('app-root-dir').get()
const webpack = require('webpack')
class Solit {
// customConfig is only used when someone wants to use solit as a module (rarely)
constructor (customConfig) {
this.config = customConfig || this.getModifiedSolitConfig()
}
getModifiedSolitConfig () {
let solitConfigDir = path.resolve(appRootDir, 'solit.config.js')
let exists = fs.existsSync(solitConfigDir)
return exists ? require(solitConfigDir) : {}
}
executionPath () {
let solitConfig = new SolitConfig(this.config)
return path.resolve(appRootDir, solitConfig.buildDir, 'server.js')
}
getServerCompiler (config) {
let solitConfig = new SolitConfig(config)
let serverConfig = ServerConfig(solitConfig)
let serverCompiler = webpack(serverConfig)
return serverCompiler
}
dev () {
this.config.dev = true
let serverCompiler = this.getServerCompiler(this.config)
serverCompiler.outputFileSystem = new MFS()
serverCompiler.watch({ quiet: true, stats: 'none' }, stats => {})
}
build () {
this.config.dev = false
let serverCompiler = this.getServerCompiler(this.config)
serverCompiler.run((error, stats) => {
if (error) console.error(error)
console.log('Build complete!')
})
}
start () {
this.config.dev = false
if (fs.existsSync(this.executionPath())) {
spawn('node', [this.executionPath()], { shell: true, stdio: 'inherit' })
} else {
throw Error('The file doesn\'t exist. Run solit build.')
}
}
}
module.exports = Solit