forked from LiveDuo/destack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react.bin.js
executable file
·64 lines (52 loc) · 2.5 KB
/
react.bin.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
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
const { spawn } = require('child_process')
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const fs = require('fs')
const path = require('path')
const isImported = __dirname.split(path.sep).includes('node_modules')
const copyDirectory = async (src, dest) => {
await fs.promises.mkdir(dest, { recursive: true })
const entries = await fs.promises.readdir(src, { withFileTypes: true })
for (let entry of entries) {
const srcPath = path.join(src, entry.name)
const destPath = path.join(dest, entry.name)
entry.isDirectory() ? await copyDirectory(srcPath, destPath) : await fs.promises.copyFile(srcPath, destPath)
}
}
const { argv } = yargs(hideBin(process.argv))
;(async () => {
if (argv.dev || argv.d) {
// CLI: ./node_modules/.bin/concurrently "./node_modules/.bin/ts-node-dev -O \"{\\\"module\\\":\\\"commonjs\\\"}\" ./lib/server/react.ts"
const startServerCommandUnix = isImported
? '"node ./node_modules/destack/build/custom/react.js"'
: 'ts-node-dev -O "{\\"module\\":\\"commonjs\\"}" ../../lib/server/react.ts'
// CLI: .\node_modules\.bin\concurrently ".\node_modules\.bin\ts-node-dev -O ""{""""module"""": """"commonjs""""}"" ./lib/server/react.ts"
const startServerCommandWindows = isImported
? '"node ./node_modules/destack/build/custom/react.js"'
: '"ts-node-dev -O ""{""""module"""": """"commonjs""""}"" ../../lib/server/react.ts"'
const startServerCommand = process.platform === 'win32' ? startServerCommandWindows : startServerCommandUnix
const spawnArgs = [startServerCommand, `\"${argv.d}\"`]
const spawnOptions = process.platform === 'win32' ? { shell: true } : {}
const proc = spawn('concurrently', spawnArgs, spawnOptions)
proc.stdout.on('data', (data) => {
process.stdout.write(data.toString())
})
proc.stderr.on('data', (data) => {
process.stdout.write(data.toString())
})
} else if (argv.build || argv.b) {
const projectPath = isImported ? '..' : path.join('dev', 'react-project')
const pathFrom = path.join(__dirname, '..', '..', projectPath, 'data')
const pathTo = path.join(__dirname, '..', '..', projectPath, 'public', 'data')
await copyDirectory(pathFrom, pathTo)
const spawnArgs = ['&&', argv.b]
const proc = spawn('echo', spawnArgs, { shell: true })
proc.stdout.on('data', (data) => {
process.stdout.write(data.toString())
})
proc.stderr.on('data', (data) => {
process.stdout.write(data.toString())
})
}
})()