express-sweet-generator 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -2
- package/README.md +14 -42
- package/bin/express-sweet-cli.js +220 -217
- package/package.json +1 -1
- package/templates/cjs/config/authentication.js +15 -1
- package/templates/cjs/config/database.js +6 -12
- package/templates/cjs/config/view.js +15 -1
- package/templates/cjs/ecosystem.config.js.ejs +5 -1
- package/templates/cjs/public/assets/home.css +0 -0
- package/templates/cjs/public/assets/home.js +0 -0
- package/templates/cjs/public/assets/{styles/login.css → login.css} +0 -2
- package/templates/cjs/public/assets/{scripts/login.js → login.js} +0 -3
- package/templates/cjs/public/assets/{images/logo.svg → logo.svg} +0 -0
- package/templates/cjs/public/assets/user.css +0 -0
- package/templates/cjs/public/assets/{scripts/user.js → user.js} +0 -3
- package/templates/cjs/public/assets/users.css +0 -0
- package/templates/cjs/public/assets/{scripts/users.js → users.js} +11 -9
- package/templates/cjs/routes/api/users.js +7 -33
- package/templates/cjs/routes/home.js +0 -3
- package/templates/cjs/routes/login.js +0 -3
- package/templates/cjs/routes/users.js +3 -9
- package/templates/cjs/views/error.hbs +1 -2
- package/templates/cjs/views/home.hbs +2 -8
- package/templates/cjs/views/layout/default.hbs +13 -18
- package/templates/cjs/views/login.hbs +4 -15
- package/templates/cjs/views/user.hbs +9 -16
- package/templates/cjs/views/users.hbs +5 -17
- package/templates/esm/config/authentication.js +15 -0
- package/templates/esm/config/database.js +6 -12
- package/templates/esm/config/view.js +15 -1
- package/templates/esm/ecosystem.config.js.ejs +5 -1
- package/templates/esm/public/assets/home.css +0 -0
- package/templates/esm/public/assets/home.js +0 -0
- package/templates/esm/public/assets/{styles/login.css → login.css} +0 -2
- package/templates/esm/public/assets/{scripts/login.js → login.js} +0 -3
- package/templates/esm/public/assets/{images/logo.svg → logo.svg} +0 -0
- package/templates/esm/public/assets/user.css +0 -0
- package/templates/esm/public/assets/{scripts/user.js → user.js} +0 -3
- package/templates/esm/public/assets/users.css +0 -0
- package/templates/esm/public/assets/{scripts/users.js → users.js} +11 -9
- package/templates/esm/routes/api/users.js +7 -33
- package/templates/esm/routes/home.js +0 -3
- package/templates/esm/routes/login.js +0 -3
- package/templates/esm/routes/users.js +3 -9
- package/templates/esm/views/error.hbs +1 -2
- package/templates/esm/views/home.hbs +2 -8
- package/templates/esm/views/layout/default.hbs +13 -18
- package/templates/esm/views/login.hbs +4 -15
- package/templates/esm/views/user.hbs +9 -16
- package/templates/esm/views/users.hbs +5 -17
- package/templates/cjs/public/assets/images/logo-dark.svg +0 -12
- package/templates/cjs/public/assets/scripts/docs.js +0 -1
- package/templates/cjs/public/assets/scripts/home.js +0 -3
- package/templates/cjs/public/assets/styles/docs.css +0 -2200
- package/templates/cjs/public/assets/styles/home.css +0 -1
- package/templates/cjs/public/assets/styles/user.css +0 -1
- package/templates/cjs/public/assets/styles/users.css +0 -1
- package/templates/esm/public/assets/images/logo-dark.svg +0 -12
- package/templates/esm/public/assets/scripts/docs.js +0 -1
- package/templates/esm/public/assets/scripts/home.js +0 -3
- package/templates/esm/public/assets/styles/docs.css +0 -2200
- package/templates/esm/public/assets/styles/home.css +0 -1
- package/templates/esm/public/assets/styles/user.css +0 -1
- package/templates/esm/public/assets/styles/users.css +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,38 @@
|
|
|
1
1
|
# Changelog
|
|
2
|
-
|
|
3
2
|
All notable changes to this project will be documented in this file.
|
|
4
3
|
|
|
4
|
+
## [1.0.5] - 2022-07-27
|
|
5
|
+
### Fixed
|
|
6
|
+
- You can now set hook functions that are called before the view is rendered.
|
|
7
|
+
Hook functions can be used, for example, to set local variables that can be used in the view.
|
|
8
|
+
|
|
9
|
+
To use, add the beforeRender hook function to "config/view.js" as follows.
|
|
10
|
+
```js
|
|
11
|
+
/**
|
|
12
|
+
* Hook function just before the view is rendered.
|
|
13
|
+
* For example, you can set your own local variables that can be used within the view.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // The message set here can be referenced in the view as {{message}}.
|
|
17
|
+
* beforeRender: res => {
|
|
18
|
+
* res.loclas.message = 'Hello World';
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @type {(res: express.Response) => void}
|
|
22
|
+
*/
|
|
23
|
+
beforeRender: res => {
|
|
24
|
+
res.loclas.message = 'Hello World';
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## [1.0.4] - 2022-05-18
|
|
29
|
+
### Fixed
|
|
30
|
+
- Version 1.0.18 of express-sweet now supports redis as session store for authentication, so we have added the relevant options (session_store, redis_host) to the authentication configuration of the template.
|
|
31
|
+
|
|
32
|
+
## [1.0.3] - 2022-05-17
|
|
33
|
+
### Fixed
|
|
34
|
+
- Fix UX of template views.
|
|
35
|
+
|
|
5
36
|
## [1.0.2] - 2022-02-13
|
|
6
37
|
### Fixed
|
|
7
38
|
- Changed the type of 'config/authentication.js#allow_unauthenticated' from'string[]' to'(string|RegExp)[]}'.
|
|
@@ -24,4 +55,7 @@ All notable changes to this project will be documented in this file.
|
|
|
24
55
|
```
|
|
25
56
|
|
|
26
57
|
[1.0.1]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.0...v1.0.1
|
|
27
|
-
[1.0.2]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.1...v1.0.2
|
|
58
|
+
[1.0.2]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.1...v1.0.2
|
|
59
|
+
[1.0.3]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.2...v1.0.3
|
|
60
|
+
[1.0.4]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.3...v1.0.4
|
|
61
|
+
[1.0.5]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.4...v1.0.5
|
package/README.md
CHANGED
|
@@ -1,56 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# express-sweet-generator
|
|
3
2
|
[Express Sweet](https://www.npmjs.com/package/express-sweet) application generator.
|
|
4
3
|
|
|
5
|
-
## Docs
|
|
6
|
-
|
|
7
|
-
* <a href="https://takuya-motoshima.github.io/express-sweet/" target="_blank">Website and Documentation</a> - <a href="https://github.com/takuya-motoshima/express-sweet" target="_blank">[website repo]</a>
|
|
8
|
-
* <a href="https://github.com/takuya-motoshima/express-sweet-generator/blob/main/CHANGELOG.md" target="_blank">Changelog</a>
|
|
9
|
-
|
|
10
4
|
## Installation
|
|
11
|
-
|
|
12
5
|
```sh
|
|
13
6
|
npm install -g express-sweet-generator;
|
|
14
7
|
```
|
|
15
8
|
|
|
16
9
|
## Quick Start
|
|
17
|
-
|
|
18
10
|
The easiest way to get started with Express Sweet is to install express-sweet-generator and automatically create a template application.
|
|
19
|
-
From the beginning, the template includes the following screen and sample programs such as router, model class, and authentication.
|
|
20
|
-
Please refer to these and start developing your application.
|
|
21
|
-
See <a href="https://takuya-motoshima.github.io/express-sweet/#started-generator" target="_blank">"Express Sweet app generator"</a> for more information on how to use express-sweet-generator.
|
|
22
11
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
<td valign="top">
|
|
26
|
-
<div>Login</div>
|
|
27
|
-
<img src="https://raw.githubusercontent.com/takuya-motoshima/express-sweet-generator/main/screencaps/login.png" width="400">
|
|
28
|
-
</td>
|
|
29
|
-
<td valign="top">
|
|
30
|
-
<div>Home</div>
|
|
31
|
-
<img src="https://raw.githubusercontent.com/takuya-motoshima/express-sweet-generator/main/screencaps/home.png" width="400">
|
|
32
|
-
</td>
|
|
33
|
-
</tr>
|
|
34
|
-
<tr>
|
|
35
|
-
<td valign="top">
|
|
36
|
-
<div>User list</div>
|
|
37
|
-
<img src="https://raw.githubusercontent.com/takuya-motoshima/express-sweet-generator/main/screencaps/user-list.png" width="400">
|
|
38
|
-
</td>
|
|
39
|
-
<td valign="top">
|
|
40
|
-
<div>New user</div>
|
|
41
|
-
<img src="https://raw.githubusercontent.com/takuya-motoshima/express-sweet-generator/main/screencaps/new-user.png" width="400">
|
|
42
|
-
</td>
|
|
43
|
-
</tr>
|
|
44
|
-
<tr>
|
|
45
|
-
<td valign="top">
|
|
46
|
-
<div>Edit user</div>
|
|
47
|
-
<img src="https://raw.githubusercontent.com/takuya-motoshima/express-sweet-generator/main/screencaps/edit-user.png" width="400">
|
|
48
|
-
</td>
|
|
49
|
-
</tr>
|
|
50
|
-
</table>
|
|
12
|
+
From the beginning, the template includes views and sample programs for routers, model classes, authentication, etc., as shown below in the template.
|
|
13
|
+
Please use these as a reference to get started.
|
|
51
14
|
|
|
52
|
-
|
|
15
|
+
See <a href="https://takuya-motoshima.github.io/express-sweet/#quick-start" target="_blank">here</a> for instructions on using express-sweet-generator.
|
|
53
16
|
|
|
17
|
+
<img src="screencaps/login&users.jfif">
|
|
18
|
+
|
|
19
|
+
## Command Line Options
|
|
54
20
|
This generator can also be further configured with the following command line flags.
|
|
55
21
|
|
|
56
22
|
--version output the version number
|
|
@@ -59,6 +25,12 @@ This generator can also be further configured with the following command line fl
|
|
|
59
25
|
-f, --force force on non-empty directory
|
|
60
26
|
-h, --help output usage information
|
|
61
27
|
|
|
62
|
-
##
|
|
28
|
+
## Author
|
|
29
|
+
**Takuya Motoshima**
|
|
63
30
|
|
|
31
|
+
* [github/takuya-motoshima](https://github.com/takuya-motoshima)
|
|
32
|
+
* [twitter/TakuyaMotoshima](https://twitter.com/TakuyaMotoshima)
|
|
33
|
+
* [facebook/takuya.motoshima.7](https://www.facebook.com/takuya.motoshima.7)
|
|
34
|
+
|
|
35
|
+
## License
|
|
64
36
|
[MIT](LICENSE)
|
package/bin/express-sweet-cli.js
CHANGED
|
@@ -1,217 +1,220 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
1
|
* Install an around function; AOP.
|
|
14
|
-
|
|
15
2
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
3
|
const old = obj[method];
|
|
19
|
-
|
|
20
4
|
obj[method] = function() {
|
|
21
|
-
|
|
22
5
|
return fn.call(this, old, [...arguments]);
|
|
23
|
-
|
|
24
6
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
7
|
* Install a before function; AOP.
|
|
30
|
-
|
|
31
8
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
9
|
const old = obj[method];
|
|
35
|
-
|
|
36
10
|
obj[method] = function() {
|
|
37
|
-
|
|
38
11
|
fn.call(this);
|
|
39
|
-
|
|
40
12
|
old.apply(this, arguments);
|
|
41
|
-
|
|
42
13
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
14
|
* Prompt for confirmation on STDOUT/STDIN
|
|
48
|
-
|
|
49
15
|
*/
|
|
50
|
-
|
|
51
|
-
|
|
52
16
|
const rl = readline.createInterface({input: process.stdin, output: process.stdout})
|
|
53
|
-
|
|
54
17
|
rl.question(message, input => {
|
|
55
|
-
|
|
56
18
|
rl.close();
|
|
57
|
-
|
|
58
19
|
callback(/^y|yes|ok|true$/i.test(input))
|
|
59
|
-
|
|
60
20
|
})
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
21
|
* Create application at the given directory.
|
|
66
|
-
|
|
67
22
|
*
|
|
68
|
-
|
|
69
23
|
* @param {string} name
|
|
70
|
-
|
|
71
24
|
* @param {string} destDir
|
|
72
|
-
|
|
73
25
|
*/
|
|
74
|
-
|
|
75
|
-
|
|
76
26
|
console.log();
|
|
77
|
-
|
|
78
|
-
|
|
79
27
|
// Application listening port.
|
|
80
|
-
|
|
81
28
|
const port = program.port;
|
|
82
|
-
|
|
83
|
-
|
|
84
29
|
// Copy the entire template as a creation application.
|
|
85
|
-
|
|
86
30
|
const output = program.output === 'esm' ? 'esm' : 'cjs';
|
|
87
|
-
|
|
88
31
|
const templateDir = path.join(__dirname, '..', `templates/${output}`);
|
|
89
|
-
|
|
90
32
|
// const templateDir = path.join(__dirname, '..', 'templates/cjs');
|
|
91
|
-
|
|
92
33
|
fs.copySync(templateDir, destDir);
|
|
93
|
-
|
|
94
|
-
|
|
95
34
|
// Copy bin/www.
|
|
96
|
-
|
|
97
35
|
compile(path.join(destDir, 'bin/www'), {name});
|
|
98
|
-
|
|
99
|
-
|
|
100
36
|
// Copy ecosystem.config.js.
|
|
101
|
-
|
|
102
37
|
compile(path.join(destDir, 'ecosystem.config.js'), {name, port});
|
|
103
|
-
|
|
104
|
-
|
|
105
38
|
// Copy nginx.sample.conf.
|
|
106
|
-
|
|
107
39
|
compile(path.join(destDir, 'nginx.sample.conf'), {name, port});
|
|
108
|
-
|
|
109
|
-
|
|
110
40
|
// Copy package.json.
|
|
111
|
-
|
|
112
41
|
compile(path.join(destDir, 'package.json'), {name});
|
|
113
|
-
|
|
114
|
-
|
|
115
42
|
const prompt = launchedFromCmd() ? '>' : '$'
|
|
116
|
-
|
|
117
43
|
if (destDir !== '.') {
|
|
118
|
-
|
|
119
44
|
console.log();
|
|
120
|
-
|
|
121
45
|
console.log(' change directory:');
|
|
122
|
-
|
|
123
46
|
console.log(` ${prompt} cd ${destDir}`);
|
|
124
|
-
|
|
125
47
|
}
|
|
126
|
-
|
|
127
48
|
console.log();
|
|
128
|
-
|
|
129
49
|
console.log(' install dependencies:');
|
|
130
|
-
|
|
131
50
|
console.log(` ${prompt} npm install`);
|
|
132
|
-
|
|
133
51
|
console.log();
|
|
134
|
-
|
|
135
52
|
console.log(' run the app:');
|
|
136
|
-
|
|
137
53
|
if (launchedFromCmd())
|
|
138
|
-
|
|
139
54
|
console.log(` ${prompt} SET DEBUG=${name}:* & npm start`);
|
|
140
|
-
|
|
141
55
|
else
|
|
142
|
-
|
|
143
56
|
console.log(` ${prompt} DEBUG=${name}:* npm start`);
|
|
144
|
-
|
|
145
57
|
console.log();
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
58
|
* Create an app name from a directory path, fitting npm naming requirements.
|
|
151
|
-
|
|
152
59
|
*
|
|
153
|
-
|
|
154
60
|
* @param {String} destDir
|
|
155
|
-
|
|
156
61
|
*/
|
|
157
|
-
|
|
158
|
-
|
|
159
62
|
return path.basename(destDir)
|
|
160
|
-
|
|
161
63
|
.replace(/[^A-Za-z0-9.-]+/g, '-')
|
|
162
|
-
|
|
163
64
|
.replace(/^[-_.]+|-+$/g, '')
|
|
164
|
-
|
|
165
65
|
.toLowerCase();
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
66
|
* Check if the given directory `dir` is empty.
|
|
171
|
-
|
|
172
67
|
*
|
|
173
|
-
|
|
174
68
|
* @param {String} dir
|
|
175
|
-
|
|
176
69
|
* @param {Function} fn
|
|
177
|
-
|
|
178
70
|
*/
|
|
179
|
-
|
|
180
|
-
|
|
181
71
|
fs.readdir(dir, (err, files) => {
|
|
182
|
-
|
|
183
72
|
if (err && err.code !== 'ENOENT') throw err;
|
|
184
|
-
|
|
185
73
|
fn(!files || !files.length);
|
|
186
|
-
|
|
187
74
|
})
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
75
|
* Graceful exit for async STDIO
|
|
193
|
-
|
|
194
76
|
*/
|
|
195
|
-
|
|
196
|
-
|
|
197
77
|
// flush output for Node.js Windows pipe bug
|
|
198
|
-
|
|
199
78
|
// https://github.com/joyent/node/issues/6247 is just one bug example
|
|
200
|
-
|
|
201
79
|
// https://github.com/visionmedia/mocha/issues/333 has a good discussion
|
|
202
|
-
|
|
203
80
|
function done() {
|
|
204
|
-
|
|
205
81
|
if (!(draining--)) _exit(code)
|
|
206
|
-
|
|
207
82
|
}
|
|
208
|
-
|
|
209
83
|
let draining = 0
|
|
210
|
-
|
|
211
84
|
const streams = [process.stdout, process.stderr]
|
|
212
|
-
|
|
213
85
|
exit.exited = true
|
|
214
|
-
|
|
215
86
|
streams.forEach(function(stream) {
|
|
216
|
-
|
|
217
87
|
// submit empty write request and wait for completion
|
|
218
|
-
|
|
219
88
|
draining += 1;
|
|
220
|
-
|
|
221
89
|
stream.write('', done);
|
|
222
|
-
|
|
223
90
|
});
|
|
224
|
-
|
|
225
91
|
done();
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
92
|
* Determine if launched from cmd.exe
|
|
231
|
-
|
|
232
93
|
*/
|
|
233
|
-
|
|
234
|
-
|
|
235
94
|
return process.platform === 'win32' && process.env._ === undefined
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
95
|
* Compile template.
|
|
241
|
-
|
|
242
96
|
*/
|
|
243
|
-
|
|
244
|
-
|
|
245
97
|
const contents = fs.readFileSync(`${file}.ejs`, 'utf-8');
|
|
246
|
-
|
|
247
98
|
const compiled = ejs.render(contents, locals, {escape: util.inspect});
|
|
248
|
-
|
|
249
99
|
fs.writeFileSync(file, compiled, {mode: parseInt('0755', 8)})
|
|
250
|
-
|
|
251
100
|
console.log(` \x1b[36mcreate\x1b[0m : ${file}`);
|
|
252
|
-
|
|
253
101
|
fs.unlinkSync(`${file}.ejs`);
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
102
|
* Main program.
|
|
259
|
-
|
|
260
103
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
263
104
|
// Path.
|
|
264
|
-
|
|
265
105
|
const destDir = program.args.shift() || '.'
|
|
266
|
-
|
|
267
|
-
|
|
268
106
|
// App name.
|
|
269
|
-
|
|
270
107
|
const name = createAppName(path.resolve(destDir)) || 'hello-world'
|
|
271
|
-
|
|
272
|
-
|
|
273
108
|
// Generate application.
|
|
274
|
-
|
|
275
109
|
emptyDirectory(destDir, empty => {
|
|
276
|
-
|
|
277
110
|
if (empty || program.force)
|
|
278
|
-
|
|
279
111
|
createApplication(name, destDir);
|
|
280
|
-
|
|
281
112
|
else
|
|
282
|
-
|
|
283
113
|
confirm('destination is not empty, continue? [y/N] ', ok => {
|
|
284
|
-
|
|
285
114
|
if (ok) {
|
|
286
|
-
|
|
287
115
|
process.stdin.destroy()
|
|
288
|
-
|
|
289
116
|
createApplication(name, destDir)
|
|
290
|
-
|
|
291
117
|
} else {
|
|
292
|
-
|
|
293
118
|
console.error('aborting')
|
|
294
|
-
|
|
295
119
|
exit(1)
|
|
296
|
-
|
|
297
120
|
}
|
|
298
|
-
|
|
299
121
|
})
|
|
300
|
-
|
|
301
122
|
})
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
123
|
program.outputHelp();
|
|
313
|
-
|
|
314
124
|
fn.apply(this, args);
|
|
315
|
-
|
|
316
125
|
return {args: [], unknown: []};
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
126
|
// track if help was shown for unknown option
|
|
322
|
-
|
|
323
127
|
this._helpShown = true;
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
128
|
// allow unknown options if help was shown, to prevent trailing error
|
|
329
|
-
|
|
330
129
|
this._allowUnknownOption = this._helpShown;
|
|
331
|
-
|
|
332
130
|
// show help if not yet shown
|
|
333
|
-
|
|
334
131
|
if (!this._helpShown)
|
|
335
|
-
|
|
336
132
|
program.outputHelp();
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
133
|
.name('express-sweet')
|
|
342
|
-
|
|
343
134
|
.version(version, ' --version')
|
|
344
|
-
|
|
345
135
|
.usage('[options] [dir]')
|
|
346
|
-
|
|
347
136
|
.option('-o, --output <output>', 'add output <module> support (esm|cjs) (defaults to cjs)')
|
|
348
|
-
|
|
349
137
|
.option('-p, --port <port>', 'application listening port', value => parseInt(value, 10), 3000)
|
|
350
|
-
|
|
351
138
|
.option('-f, --force', 'force on non-empty directory')
|
|
352
|
-
|
|
353
139
|
.parse(process.argv);
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
140
|
main();
|
|
141
|
+
#!/usr/bin/env node
|
|
142
|
+
const ejs = require('ejs');
|
|
143
|
+
const fs = require('fs-extra');
|
|
144
|
+
const path = require('path');
|
|
145
|
+
const program = require('commander');
|
|
146
|
+
const readline = require('readline');
|
|
147
|
+
const util = require('util');
|
|
148
|
+
const version = require('../package').version;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Install an around function; AOP.
|
|
152
|
+
*/
|
|
153
|
+
function around(obj, method, fn) {
|
|
154
|
+
const old = obj[method];
|
|
155
|
+
obj[method] = function() {
|
|
156
|
+
return fn.call(this, old, [...arguments]);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Install a before function; AOP.
|
|
162
|
+
*/
|
|
163
|
+
function before(obj, method, fn) {
|
|
164
|
+
const old = obj[method];
|
|
165
|
+
obj[method] = function() {
|
|
166
|
+
fn.call(this);
|
|
167
|
+
old.apply(this, arguments);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Prompt for confirmation on STDOUT/STDIN
|
|
173
|
+
*/
|
|
174
|
+
function confirm(message, callback) {
|
|
175
|
+
const rl = readline.createInterface({input: process.stdin, output: process.stdout})
|
|
176
|
+
rl.question(message, input => {
|
|
177
|
+
rl.close();
|
|
178
|
+
callback(/^y|yes|ok|true$/i.test(input))
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Create application at the given directory.
|
|
184
|
+
*
|
|
185
|
+
* @param {string} name
|
|
186
|
+
* @param {string} destDir
|
|
187
|
+
*/
|
|
188
|
+
function createApplication(name, destDir) {
|
|
189
|
+
console.log();// Output blank line.
|
|
190
|
+
|
|
191
|
+
// Application listening port.
|
|
192
|
+
const port = program.port;
|
|
193
|
+
|
|
194
|
+
// Copy the entire template as a creation application.
|
|
195
|
+
const output = program.output === 'esm' ? 'esm' : 'cjs';
|
|
196
|
+
const templateDir = path.join(__dirname, '..', `templates/${output}`);
|
|
197
|
+
// const templateDir = path.join(__dirname, '..', 'templates/cjs');
|
|
198
|
+
fs.copySync(templateDir, destDir);
|
|
199
|
+
|
|
200
|
+
// Copy bin/www.
|
|
201
|
+
compile(path.join(destDir, 'bin/www'), {name});
|
|
202
|
+
|
|
203
|
+
// Copy ecosystem.config.js.
|
|
204
|
+
compile(path.join(destDir, 'ecosystem.config.js'), {name, port});
|
|
205
|
+
|
|
206
|
+
// Copy nginx.sample.conf.
|
|
207
|
+
compile(path.join(destDir, 'nginx.sample.conf'), {name, port});
|
|
208
|
+
|
|
209
|
+
// Copy package.json.
|
|
210
|
+
compile(path.join(destDir, 'package.json'), {name});
|
|
211
|
+
const prompt = launchedFromCmd() ? '>' : '$'
|
|
212
|
+
if (destDir !== '.') {
|
|
213
|
+
console.log();// Output blank line.
|
|
214
|
+
console.log(' change directory:');
|
|
215
|
+
console.log(` ${prompt} cd ${destDir}`);
|
|
216
|
+
}
|
|
217
|
+
console.log();// Output blank line.
|
|
218
|
+
console.log(' install dependencies:');
|
|
219
|
+
console.log(` ${prompt} npm install`);
|
|
220
|
+
console.log();// Output blank line.
|
|
221
|
+
console.log(' run the app:');
|
|
222
|
+
if (launchedFromCmd())
|
|
223
|
+
console.log(` ${prompt} SET DEBUG=${name}:* & npm start`);
|
|
224
|
+
else
|
|
225
|
+
console.log(` ${prompt} DEBUG=${name}:* npm start`);
|
|
226
|
+
console.log();// Output blank line.
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Create an app name from a directory path, fitting npm naming requirements.
|
|
231
|
+
*
|
|
232
|
+
* @param {String} destDir
|
|
233
|
+
*/
|
|
234
|
+
function createAppName(destDir) {
|
|
235
|
+
return path.basename(destDir)
|
|
236
|
+
.replace(/[^A-Za-z0-9.-]+/g, '-')
|
|
237
|
+
.replace(/^[-_.]+|-+$/g, '')
|
|
238
|
+
.toLowerCase();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Check if the given directory `dir` is empty.
|
|
243
|
+
*
|
|
244
|
+
* @param {String} dir
|
|
245
|
+
* @param {Function} fn
|
|
246
|
+
*/
|
|
247
|
+
function emptyDirectory(dir, fn) {
|
|
248
|
+
fs.readdir(dir, (err, files) => {
|
|
249
|
+
if (err && err.code !== 'ENOENT')
|
|
250
|
+
throw err;
|
|
251
|
+
fn(!files || !files.length);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Graceful exit for async STDIO
|
|
257
|
+
*/
|
|
258
|
+
function exit(code) {
|
|
259
|
+
// flush output for Node.js Windows pipe bug
|
|
260
|
+
// https://github.com/joyent/node/issues/6247 is just one bug example
|
|
261
|
+
// https://github.com/visionmedia/mocha/issues/333 has a good discussion
|
|
262
|
+
function done() {
|
|
263
|
+
if (!(draining--))
|
|
264
|
+
_exit(code)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
let draining = 0
|
|
268
|
+
const streams = [process.stdout, process.stderr]
|
|
269
|
+
exit.exited = true
|
|
270
|
+
streams.forEach(function(stream) {
|
|
271
|
+
// submit empty write request and wait for completion
|
|
272
|
+
draining += 1;
|
|
273
|
+
stream.write('', done);
|
|
274
|
+
});
|
|
275
|
+
done();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Determine if launched from cmd.exe
|
|
280
|
+
*/
|
|
281
|
+
function launchedFromCmd() {
|
|
282
|
+
return process.platform === 'win32' && process.env._ === undefined
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Compile template.
|
|
287
|
+
*/
|
|
288
|
+
function compile(file, locals) {
|
|
289
|
+
const contents = fs.readFileSync(`${file}.ejs`, 'utf-8');
|
|
290
|
+
const compiled = ejs.render(contents, locals, {escape: util.inspect});
|
|
291
|
+
fs.writeFileSync(file, compiled, {mode: parseInt('0755', 8)})
|
|
292
|
+
console.log(` \x1b[36mcreate\x1b[0m : ${file}`);
|
|
293
|
+
fs.unlinkSync(`${file}.ejs`);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Main program.
|
|
298
|
+
*/
|
|
299
|
+
function main() {
|
|
300
|
+
// Path.
|
|
301
|
+
const destDir = program.args.shift() || '.'
|
|
302
|
+
|
|
303
|
+
// App name.
|
|
304
|
+
const name = createAppName(path.resolve(destDir)) || 'hello-world'
|
|
305
|
+
|
|
306
|
+
// Generate application.
|
|
307
|
+
emptyDirectory(destDir, empty => {
|
|
308
|
+
if (empty || program.force)
|
|
309
|
+
createApplication(name, destDir);
|
|
310
|
+
else
|
|
311
|
+
confirm('destination is not empty, continue? [y/N] ', ok => {
|
|
312
|
+
if (ok) {
|
|
313
|
+
process.stdin.destroy()
|
|
314
|
+
createApplication(name, destDir)
|
|
315
|
+
} else {
|
|
316
|
+
console.error('aborting')
|
|
317
|
+
exit(1)
|
|
318
|
+
}
|
|
319
|
+
})
|
|
320
|
+
})
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Re-assign process.exit because of commander
|
|
324
|
+
|
|
325
|
+
// TODO: Switch to a different command framework
|
|
326
|
+
const _exit = process.exit;
|
|
327
|
+
process.exit = exit;
|
|
328
|
+
|
|
329
|
+
// CLI
|
|
330
|
+
around(program, 'optionMissingArgument', function(fn, args) {
|
|
331
|
+
program.outputHelp();
|
|
332
|
+
fn.apply(this, args);
|
|
333
|
+
return {args: [], unknown: []};
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
before(program, 'outputHelp', function() {
|
|
337
|
+
// track if help was shown for unknown option
|
|
338
|
+
this._helpShown = true;
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
before(program, 'unknownOption', function() {
|
|
342
|
+
// allow unknown options if help was shown, to prevent trailing error
|
|
343
|
+
this._allowUnknownOption = this._helpShown;
|
|
344
|
+
|
|
345
|
+
// show help if not yet shown
|
|
346
|
+
if (!this._helpShown)
|
|
347
|
+
program.outputHelp();
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
program
|
|
351
|
+
.name('express-sweet')
|
|
352
|
+
.version(version, ' --version')
|
|
353
|
+
.usage('[options] [dir]')
|
|
354
|
+
.option('-o, --output <output>', 'add output <module> support (esm|cjs) (defaults to cjs)')
|
|
355
|
+
.option('-p, --port <port>', 'application listening port', value => parseInt(value, 10), 3000)
|
|
356
|
+
.option('-f, --force', 'force on non-empty directory')
|
|
357
|
+
.parse(process.argv);
|
|
358
|
+
|
|
359
|
+
if (!exit.exited)
|
|
360
|
+
main();
|