1+ #!/usr/bin/env node
2+
3+ const fs = require ( 'fs-extra' ) ;
4+ const path = require ( 'path' ) ;
5+ const https = require ( 'https' ) ;
6+ const { exec } = require ( 'child_process' ) ;
7+
8+ const packageJson = require ( '../package.json' ) ;
9+
10+ const scripts = `"start": "node start.js",
11+ "dev": "nodemon start.js",
12+ "test": "mocha --timeout 10000"` ;
13+
14+ const jestConfig = `"license": "ISC",
15+ "jest": {
16+ "moduleFileExtensions": [
17+ "js",
18+ "jsx"
19+ ],
20+ "moduleDirectories": [
21+ "node_modules"
22+ ],
23+ "setupFiles": [
24+ "<rootDir>/src/tests/setup.js"
25+ ],
26+ "moduleNameMapper": {
27+ "\\\\.(css|styl|less|sass|scss)$": "identity-obj-proxy"
28+ },
29+ "transform": {
30+ "^.+\\\\.js$": "babel-jest",
31+ "^.+\\\\.jsx$": "babel-jest",
32+ "\\\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/src/tests/__mock__/fileTransformer.js"
33+ }
34+ }` ;
35+
36+ /**
37+ * we pass the object key dependency || devdependency to this function
38+ * @param {object } deps object key that we want to extract
39+ * @returns {string } a string of 'dependencies@version'
40+ * that we can attach to an `npm i {value}` to install
41+ * every dep the exact version speficied in package.json
42+ */
43+ const getDeps = deps =>
44+ Object . entries ( deps )
45+ . map ( dep => `${ dep [ 0 ] } @${ dep [ 1 ] } ` )
46+ . toString ( )
47+ . replace ( / , / g, ' ' )
48+ . replace ( / ^ / g, '' )
49+ // exclude the plugin only used in this file, nor relevant to the boilerplate
50+ . replace ( / f s - e x t r a [ ^ \s ] + / g, '' ) ;
51+
52+ console . log ( 'Initializing project..' ) ;
53+
54+ // create folder and initialize npm
55+ exec (
56+ `mkdir ${ process . argv [ 2 ] } && cd ${ process . argv [ 2 ] } && npm init -f` ,
57+ ( initErr , initStdout , initStderr ) => {
58+ if ( initErr ) {
59+ console . error ( `Everything was fine, then it wasn't:
60+ ${ initErr } ` ) ;
61+ return ;
62+ }
63+ const packageJSON = `${ process . argv [ 2 ] } /package.json` ;
64+ // replace the default scripts, with the webpack scripts in package.json
65+ fs . readFile ( packageJSON , ( err , file ) => {
66+ if ( err ) throw err ;
67+ const data = file
68+ . toString ( )
69+ . replace ( '"test": "echo \\"Error: no test specified\\" && exit 1"' , scripts )
70+ . replace ( '"license": "ISC"' , jestConfig ) ;
71+ fs . writeFile ( packageJSON , data , err2 => err2 || true ) ;
72+ } ) ;
73+
74+ const filesToCopy = [ 'README.md' , '.babelrc' ] ;
75+
76+ for ( let i = 0 ; i < filesToCopy . length ; i += 1 ) {
77+ fs
78+ . createReadStream ( path . join ( __dirname , `../${ filesToCopy [ i ] } ` ) )
79+ . pipe ( fs . createWriteStream ( `${ process . argv [ 2 ] } /${ filesToCopy [ i ] } ` ) ) ;
80+ }
81+
82+ // npm will remove the .gitignore file when the package is installed, therefore it cannot be copied
83+ // locally and needs to be downloaded. See https://github.com/Kornil/simple-react-app/issues/12
84+ https . get (
85+ 'https://raw.githubusercontent.com/Aakashdeveloper/create-node-app/master/.gitignore' ,
86+ ( res ) => {
87+ res . setEncoding ( 'utf8' ) ;
88+ let body = '' ;
89+ res . on ( 'data' , ( data ) => {
90+ body += data ;
91+ } ) ;
92+ res . on ( 'end' , ( ) => {
93+ fs . writeFile ( `${ process . argv [ 2 ] } /.gitignore` , body , { encoding : 'utf-8' } , ( err ) => {
94+ if ( err ) throw err ;
95+ } ) ;
96+ } ) ;
97+ } ,
98+ ) ;
99+
100+ console . log ( 'npm init -- done\n' ) ;
101+
102+ // installing dependencies
103+ console . log ( 'Installing deps -- it might take a few minutes..' ) ;
104+ const devDeps = getDeps ( packageJson . devDependencies ) ;
105+ const deps = getDeps ( packageJson . dependencies ) ;
106+ exec (
107+ `cd ${ process . argv [ 2 ] } && npm i -D ${ devDeps } && npm i -S ${ deps } ` ,
108+ ( npmErr , npmStdout , npmStderr ) => {
109+ if ( npmErr ) {
110+ console . error ( `it's always npm, ain't it?
111+ ${ npmErr } ` ) ;
112+ return ;
113+ }
114+ console . log ( npmStdout ) ;
115+ console . log ( 'Dependencies installed' ) ;
116+
117+ console . log ( 'Copying additional files..' ) ;
118+ // copy additional source files
119+ fs
120+ . copy ( path . join ( __dirname , '../src' ) , `${ process . argv [ 2 ] } /src` )
121+ . then ( ( ) =>
122+ console . log ( `All done!\nYour project is now started into ${
123+ process . argv [ 2 ]
124+ } folder, refer to the README for the project structure.\nHappy Coding!`) )
125+ . catch ( err => console . error ( err ) ) ;
126+ } ,
127+ ) ;
128+ } ,
129+ ) ;
0 commit comments