4
4
const { execFileSync } = require ( 'child_process' )
5
5
const fs = require ( 'fs' )
6
6
const path = require ( 'path' )
7
+ process . chdir ( path . join ( __dirname , '../' ) )
7
8
8
- console . log ( 'Cleaning up old build ' )
9
- rmdirsSync ( relativePathname ( 'build' ) )
9
+ console . log ( 'Cleaning up any previous builds ' )
10
+ require ( 'rimraf' ) . sync ( 'build' )
10
11
11
12
console . log ( 'Building' )
12
- fs . mkdirSync ( relativePathname ( 'build/app' ) , { recursive : true } )
13
- fs . mkdirSync ( relativePathname ( 'build/boot' ) )
14
- babelCompileSync ( relativePathname ( 'app' ) , relativePathname ( 'build/app' ) )
15
- babelCompileSync ( relativePathname ( 'boot' ) , relativePathname ( 'build/boot' ) )
16
-
17
- console . log ( 'Setting up bins' )
18
- fs . mkdirSync ( 'build/bin' )
19
- fs . copyFileSync ( 'bin/cli' , 'build/bin/cli' )
20
-
21
- console . log ( 'Finalizing boot files' )
22
- stripBabelSync ( relativePathname ( 'build/boot/Cli.js' ) )
23
- stripBabelSync ( relativePathname ( 'build/boot/Http.js' ) )
24
-
25
- console . log ( 'Finished' )
26
-
27
- function stripBabelSync ( file ) {
28
- let content = fs . readFileSync ( file ) . toString ( )
29
- content = content . replace ( / r e q u i r e \( ' @ b a b e l \/ r e g i s t e r ' \) / g, '' )
30
- return fs . writeFileSync ( file , content )
31
- }
32
-
33
- function babelCompileSync ( input , output ) {
34
- return execFileSync ( 'node' , [
13
+ for ( const [ input , output ] of Object . entries ( {
14
+ app : 'build/app' ,
15
+ boot : 'build/boot' ,
16
+ } ) ) {
17
+ fs . mkdirSync ( output , { recursive : true } )
18
+ execFileSync ( 'node' , [
35
19
require . resolve ( '@babel/cli/lib/babel' ) ,
36
20
'--copy-files' ,
37
21
'--source-maps' , 'inline' ,
@@ -40,20 +24,15 @@ function babelCompileSync(input, output) {
40
24
] )
41
25
}
42
26
43
- function rmdirsSync ( pathname ) {
44
- if ( ! fs . existsSync ( pathname ) ) {
45
- return
46
- } else if ( ! fs . lstatSync ( pathname ) . isDirectory ( ) ) {
47
- return fs . unlinkSync ( pathname )
48
- }
49
-
50
- for ( const child of fs . readdirSync ( pathname ) ) {
51
- rmdirsSync ( path . join ( pathname , child ) )
52
- }
27
+ console . log ( 'Setting up bins' )
28
+ fs . mkdirSync ( 'build/bin' )
29
+ fs . copyFileSync ( 'bin/cli' , 'build/bin/cli' )
53
30
54
- fs . rmdirSync ( pathname )
31
+ console . log ( 'Finalizing boot files' )
32
+ for ( const file of [ 'build/boot/Cli.js' , 'build/boot/Http.js' ] ) {
33
+ let content = fs . readFileSync ( file ) . toString ( )
34
+ content = content . replace ( / r e q u i r e \( ' @ b a b e l \/ r e g i s t e r ' \) / g, '' )
35
+ fs . writeFileSync ( file , content )
55
36
}
56
37
57
- function relativePathname ( pathname ) {
58
- return path . join ( __dirname , '../' , pathname )
59
- }
38
+ console . log ( 'Finished' )
0 commit comments