1 | const path = require('path'); |
||
2 | const fs = require('fs'); |
||
3 | const jsonfile = require('jsonfile'); |
||
4 | |||
5 | const baseDir = require('./baseDir'); |
||
6 | const packages = require('../sample/dependencies'); |
||
7 | |||
8 | /** |
||
9 | * Auto generator for configuration files |
||
10 | * @class Generate |
||
11 | */ |
||
12 | class Generate { |
||
13 | |||
14 | /** |
||
15 | * @param {String} extend - extend the pworking dorectory |
||
16 | * Creates an instance of Generate. |
||
17 | * @memberOf Generate |
||
18 | */ |
||
19 | constructor(extend = '') { |
||
20 | this.mainpath = path.join(__dirname, '../'); |
||
21 | this.filepath = `${baseDir.getCurrentWorkingDir()}/${extend}`; |
||
22 | this.basename = baseDir.getCurrentDirectoryBase(); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * webpack - Generate a webpack config file from the sample |
||
27 | * @param {String} withExpress - configuration with express server |
||
28 | * @returns {Promise} if successful, resolves and pass in the |
||
29 | * configuration as argument. Else reject and pass the error |
||
30 | * @memberOf Generate |
||
31 | */ |
||
32 | webpack(withExpress = 'n') { |
||
33 | return new Promise((resolve, reject) => { |
||
34 | fs.readFile( |
||
35 | path.join(this.mainpath, '/sample/webpack.config.sample'), |
||
36 | (err, data) => { |
||
37 | let webpack = data.toString(); |
||
38 | if (withExpress === 'y') { |
||
39 | webpack = webpack.replace( |
||
40 | '\'webpack-dev-server/client?/\',', |
||
41 | '//\'webpack-dev-server/client?/\''); |
||
42 | webpack = webpack.replace( |
||
43 | 'webpack/hot/dev-server', |
||
44 | 'webpack-hot-middleware/client?reload=true'); |
||
45 | } |
||
46 | fs.writeFile( |
||
47 | path.join( |
||
48 | this.filepath, |
||
49 | '/webpack.config.js'), |
||
50 | webpack, |
||
51 | (err) => { |
||
52 | if (err) { |
||
53 | return reject(err); |
||
54 | } |
||
55 | return resolve(webpack); |
||
56 | }); |
||
57 | }); |
||
58 | }); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * babelrc - Generate a .babelrc file from the sample |
||
63 | * @returns {Promise} if successful, resolves and pass in the |
||
64 | * configuration as argument. Else reject and pass the error |
||
65 | * @memberOf Generate |
||
66 | */ |
||
67 | babelrc() { |
||
68 | return new Promise((resolve, reject) => { |
||
69 | fs.readFile( |
||
70 | path.join(this.mainpath, '/sample/babelrc.sample'), |
||
71 | (err, data) => { |
||
72 | fs.writeFile( |
||
73 | path.join( |
||
74 | this.filepath, |
||
75 | '/.babelrc'), |
||
76 | data.toString(), |
||
77 | (err) => { |
||
78 | if (err) { |
||
79 | return reject(err); |
||
80 | } |
||
81 | return resolve(data.toString()); |
||
82 | }); |
||
83 | }); |
||
84 | }); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * package - Generate a package.json file |
||
89 | * @param {Object} setupInfo - setup information provided by user |
||
90 | * @returns {Promise} if successful, resolves and pass in the |
||
91 | * configurations as argument. Else reject and pass the error |
||
92 | * @memberOf Generate |
||
93 | */ |
||
94 | package(setupInfo) { |
||
95 | return new Promise((resolve, reject) => { |
||
96 | let dependencies; |
||
97 | const sortedDependencies = {}; |
||
98 | if (setupInfo.express === 'y' || setupInfo.express === 'yes') { |
||
99 | dependencies = Object.assign( |
||
100 | {}, |
||
101 | packages.dependencies, |
||
102 | packages.express |
||
103 | ); |
||
104 | } else { |
||
105 | dependencies = packages.dependencies; |
||
106 | } |
||
107 | |||
108 | Object.keys(dependencies).sort().forEach((dependency) => { |
||
109 | sortedDependencies[dependency] = dependencies[dependency]; |
||
110 | }); |
||
111 | |||
112 | const packageEx = { |
||
113 | name: setupInfo.name, |
||
114 | version: '1.0.0', |
||
115 | description: (!setupInfo.description || setupInfo.description === '') ? |
||
116 | '' : setupInfo.description, |
||
117 | main: (!setupInfo.main || setupInfo.main === '') ? |
||
118 | 'index.js' : setupInfo.main, |
||
119 | scripts: { |
||
120 | test: '', |
||
121 | 'test:frontend': |
||
122 | 'NODE_ENV=test mocha -w test/mocha-helper.js test/**/*.spec.js --slow 5000 --compilers js:babel-register', |
||
123 | start: 'webpack', |
||
124 | 'start-dev': 'webpack-dev-server --config webpack.config.js --open --content-base dist/' |
||
125 | }, |
||
126 | keywords: [], |
||
127 | author: (!setupInfo.author || setupInfo.author === '') ? |
||
128 | '' : setupInfo.author, |
||
129 | license: (!setupInfo.license || setupInfo.license === '') ? |
||
130 | 'MIT' : setupInfo.license, |
||
131 | dependencies: sortedDependencies, |
||
132 | devDependencies: packages.devDependencies |
||
133 | }; |
||
134 | jsonfile.writeFile( |
||
135 | path.join( |
||
136 | this.filepath, |
||
137 | '/package.json'), |
||
138 | packageEx, { spaces: 2 }, (err) => { |
||
139 | if (err) { |
||
140 | return reject(err); |
||
141 | } |
||
142 | return resolve(packageEx); |
||
143 | }); |
||
144 | }); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * eslintrc - Generate a .eslintrc file from the sample |
||
149 | * @param {String} entry - name of express server file |
||
150 | * @returns {Promise} if successful, resolves and pass in the |
||
151 | * configuration as argument. Else reject and pass the error |
||
152 | * @memberOf Generate |
||
153 | */ |
||
154 | express(entry) { |
||
155 | return new Promise((resolve, reject) => { |
||
156 | const main = (!entry || entry === '') ? 'index.js' : entry; |
||
157 | fs.readFile( |
||
158 | path.join(this.mainpath, '/sample/express.sample'), |
||
159 | (err, data) => { |
||
160 | fs.writeFile( |
||
161 | path.join( |
||
162 | this.filepath, |
||
163 | `/${main}`), |
||
164 | data.toString(), |
||
165 | (err) => { |
||
166 | if (err) { |
||
167 | return reject(err); |
||
168 | } |
||
169 | resolve(data.toString()); |
||
0 ignored issues
–
show
Best Practice
introduced
by
![]() |
|||
170 | }); |
||
171 | }); |
||
172 | }); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * eslintrc - Generate a .eslintrc file from the sample |
||
177 | * @returns {Promise} if successful, resolves and pass in the |
||
178 | * configuration as argument. Else reject and pass the error |
||
179 | * @memberOf Generate |
||
180 | */ |
||
181 | eslintrc() { |
||
182 | return new Promise((resolve, reject) => { |
||
183 | fs.readFile( |
||
184 | path.join(this.mainpath, '/sample/eslintrc.sample'), |
||
185 | (err, data) => { |
||
186 | fs.writeFile( |
||
187 | path.join( |
||
188 | this.filepath, |
||
189 | '/.eslintrc'), |
||
190 | data.toString(), |
||
191 | (err) => { |
||
192 | if (err) { |
||
193 | return reject(err); |
||
194 | } |
||
195 | resolve(data.toString()); |
||
0 ignored issues
–
show
|
|||
196 | }); |
||
197 | }); |
||
198 | }); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * all - Generates package.json, webpack.config.js, .eslintrc, .babelrc |
||
203 | * @param {Object} setupInfo - setup information provided by user |
||
204 | * @returns {Promise} if successful, resolves the promise. |
||
205 | * Else reject and pass the error |
||
206 | * @memberOf Generate |
||
207 | */ |
||
208 | all(setupInfo) { |
||
209 | return new Promise((resolve, reject) => { |
||
210 | this.webpack(setupInfo.express[0]).then(() => { |
||
211 | this.eslintrc().then(() => { |
||
212 | this.package(setupInfo).then(() => { |
||
213 | this.babelrc().then(() => { |
||
214 | if (setupInfo.express[0] === 'y') { |
||
215 | this.express(setupInfo.main).then(() => { |
||
216 | resolve(); |
||
217 | }); |
||
218 | } else { |
||
219 | resolve(); |
||
220 | } |
||
221 | }); |
||
222 | }); |
||
223 | }); |
||
224 | }).catch((err) => { |
||
225 | reject(err); |
||
226 | }); |
||
227 | }); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | module.exports = Generate; |
||
232 |