1
1
#!/usr/bin/env node
2
2
/* eslint no-console: 0 */
3
3
4
- // THIS SCRIPT SHOULD ONLY USE NATIVE NODE.JS APIs, NO PACKAGES FROM NPM ALLOWED
5
4
const copyFolder = require ( './copy-folder' ) ;
6
5
const fs = require ( 'fs' ) ;
7
6
const os = require ( 'os' ) ;
8
7
const path = require ( 'path' ) ;
9
8
const { spawn } = require ( 'child_process' ) ;
9
+ const chalk = require ( 'chalk' ) ;
10
+ const commander = require ( 'commander' ) ;
11
+ const templatePkg = require ( path . join ( __dirname , '..' , '/template/package.json' ) ) ;
10
12
11
13
let TARGET_DIR ;
12
14
13
- console . log ( '-------------------------------------------------------' ) ;
14
- console . log ( 'Welcome to Create Evergreen App ♻️' ) ;
15
- console . log ( '-------------------------------------------------------' ) ;
15
+ console . log ( `${ chalk . rgb ( 175 , 207 , 71 ) ( '-------------------------------------------------------' ) } ` ) ;
16
+ console . log ( `${ chalk . rgb ( 175 , 207 , 71 ) ( 'Welcome to Create Evergreen App ♻️' ) } ` ) ;
17
+ console . log ( `${ chalk . rgb ( 175 , 207 , 71 ) ( '-------------------------------------------------------' ) } ` ) ;
18
+
19
+ const program = new commander . Command ( templatePkg . name )
20
+ . version ( templatePkg . version )
21
+ . arguments ( '<application-directory>' )
22
+ . usage ( `${ chalk . green ( '<application-directory>' ) } [options]` )
23
+ . action ( name => {
24
+ TARGET_DIR = name ;
25
+ } )
26
+ . option ( '--yarn' , 'Use yarn package manager instead of npm default' )
27
+ . parse ( process . argv ) ;
28
+
29
+ if ( program . yarn ) {
30
+ console . log ( 'Yarn Enabled' ) ;
31
+ }
16
32
17
33
// Check target application directory/name is included in args
18
34
// warn if directory is present, else create new target directory
19
- const checkTargetDir = async appDir => {
20
- if ( ! appDir ) {
35
+ const checkTargetDir = async ( ) => {
36
+ if ( typeof TARGET_DIR === 'undefined' ) {
21
37
console . error (
22
- ' Missing Project Directory! Please specifiy the application name e.g. create-evergreen-app my-app'
38
+ ` Missing Project Directory! Please specifiy the application name e.g. ${ chalk . green ( ' create-evergreen-app my-app') } `
23
39
) ;
40
+ console . log ( ) ;
41
+ console . log ( `Run ${ chalk . green ( 'create-evergreen-app --help' ) } for available options` ) ;
24
42
process . exit ( 1 ) ; // eslint-disable-line no-process-exit
25
43
}
26
44
27
- const targetExists = await fs . existsSync ( appDir ) ;
45
+ const targetExists = await fs . existsSync ( TARGET_DIR ) ;
28
46
29
47
if ( targetExists ) {
30
48
console . error (
31
- `${ appDir } already exists, existing project detected? Delete ${ appDir } to try again or run from a different directory.`
49
+ `${ TARGET_DIR } already exists, existing project detected? Delete ${ TARGET_DIR } to try again or run from a different directory.`
32
50
) ;
33
51
process . exit ( 1 ) ; // eslint-disable-line no-process-exit
34
52
}
35
53
36
- await fs . mkdirSync ( appDir ) ;
37
-
38
- return appDir ;
54
+ return await fs . mkdirSync ( TARGET_DIR ) ;
39
55
} ;
40
56
41
57
// Create new package.json
42
58
const npmInit = async ( ) => {
43
- const templatePkg = require ( path . join ( __dirname , '..' , 'package.json' ) ) ;
44
59
const appPkg = {
45
60
name : TARGET_DIR ,
46
61
version : '0.1.0' ,
@@ -59,19 +74,19 @@ const npmInit = async () => {
59
74
60
75
// Copy root and src files to target directory
61
76
const srcInit = async ( ) => {
62
- const copyBlacklist = [ 'tasks/ ' ] ;
63
- const packageFiles = require ( path . join ( __dirname , '..' , 'package.json' ) ) . files ;
64
- const files = packageFiles . filter ( ( file ) => {
65
- if ( copyBlacklist . indexOf ( file ) < 0 ) {
66
- return file ;
67
- }
68
- } ) ;
77
+ // const copyBlacklist = [''];
78
+ const packageFiles = require ( path . join ( __dirname , '..' , '/template/ package.json' ) ) . files ;
79
+ // const files = packageFiles.filter((file) => {
80
+ // if (copyBlacklist.indexOf(file) < 0) {
81
+ // return file;
82
+ // }
83
+ // });
69
84
70
85
await createGitIgnore ( ) ;
71
86
72
87
return await Promise . all (
73
- files . map ( async file => {
74
- const resolvedPath = path . join ( __dirname , '..' , file ) ;
88
+ packageFiles . map ( async file => {
89
+ const resolvedPath = path . join ( __dirname , '..' , '/template/' , file ) ;
75
90
76
91
if ( fs . lstatSync ( resolvedPath ) . isDirectory ( ) ) {
77
92
return await copyFolder ( resolvedPath , TARGET_DIR ) ;
@@ -111,7 +126,8 @@ const createGitIgnore = () => {
111
126
// Install npm dependencies
112
127
const install = ( ) => {
113
128
return new Promise ( ( resolve , reject ) => {
114
- const command = os . platform ( ) === 'win32' ? 'npm.cmd' : 'npm' ;
129
+ const pkgMng = program . yarn ? 'yarn' : 'npm' ; // default to npm
130
+ const command = os . platform ( ) === 'win32' ? `${ pkgMng } .cmd` : pkgMng ;
115
131
const args = [ 'install' , '--save' , '--save-exact' , '--loglevel' , 'error' ] ;
116
132
const process = spawn ( command , args , { stdio : 'inherit' } ) ;
117
133
@@ -130,7 +146,7 @@ const install = () => {
130
146
const run = async ( ) => {
131
147
try {
132
148
console . log ( 'Preparing project directory...' ) ;
133
- TARGET_DIR = await checkTargetDir ( process . argv [ 2 ] ) ;
149
+ await checkTargetDir ( ) ;
134
150
135
151
console . log ( 'Initializing npm dependencies...' ) ;
136
152
npmInit ( ) ;
0 commit comments