@mobilabs/es6lib 2.1.0 → 2.1.2

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.
@@ -0,0 +1,284 @@
1
+ #!/usr/bin/env node
2
+ /* *****************************************************************************
3
+ *
4
+ * Creates the JS bundle.
5
+ *
6
+ * build:js.dev script creates the JS bundle from ./public/src/main.js by importing
7
+ * all the linked src files;
8
+ *
9
+ * Private Functions:
10
+ * . _help displays the help message,
11
+ * . _clean removes the previous build,
12
+ * . _docore creates the content of the library,
13
+ * . _doumdlib creates the UMD Module,
14
+ * . _domodule creates the ES6 module,
15
+ * . _delcore removes the temp file(s),
16
+ *
17
+ *
18
+ * Public Static Methods:
19
+ * . run executes the script,
20
+ *
21
+ *
22
+ * @namespace -
23
+ * @dependencies none
24
+ * @exports -
25
+ * @author -
26
+ * @since 0.0.0
27
+ * @version -
28
+ * ************************************************************************** */
29
+ /* eslint one-var: 0, semi-style: 0, no-underscore-dangle: 0 */
30
+
31
+ 'use strict';
32
+
33
+ // -- Vendor Modules
34
+ const fs = require('fs')
35
+ , nopt = require('nopt')
36
+ ;
37
+
38
+
39
+ // -- Local Modules
40
+ const pack = require('../package.json')
41
+ , config = require('./config')
42
+ ;
43
+
44
+
45
+ // -- Local Constants
46
+ const VERSION = '0.0.0-alpha.0'
47
+ , opts = {
48
+ help: [Boolean, false],
49
+ version: [String, null],
50
+ }
51
+ , shortOpts = {
52
+ h: ['--help'],
53
+ v: ['--version', VERSION],
54
+ }
55
+ , parsed = nopt(opts, shortOpts, process.argv, 2)
56
+ , destination = config.libdir
57
+ , { ES6GLOB } = config
58
+ , source = config.src
59
+ , { libname } = config
60
+ , { name } = config
61
+ , head = source[0]
62
+ , core = source.slice(1, -1)
63
+ , foot = source[source.length - 1]
64
+ , { version } = pack
65
+ ;
66
+
67
+
68
+ // -- Local Variables
69
+
70
+
71
+ // -- Private Functions --------------------------------------------------------
72
+
73
+ /**
74
+ * Dispays the help message.
75
+ *
76
+ * @function ()
77
+ * @private
78
+ * @param {} -,
79
+ * @returns {} -,
80
+ * @since 0.0.0
81
+ */
82
+ function _help() {
83
+ const message = ['',
84
+ 'Usage: command [options]',
85
+ '',
86
+ ' creates the js bundle from ./public/src/main.js',
87
+ '',
88
+ 'Options:',
89
+ '',
90
+ '-h, --help output usage information',
91
+ '-v, --version output the version number',
92
+ '',
93
+ ].join('\n');
94
+
95
+ process.stdout.write(`${message}\n`);
96
+ }
97
+
98
+ /**
99
+ * Removes the previous build.
100
+ *
101
+ * @function ()
102
+ * @private
103
+ * @param {} -,
104
+ * @returns {} -,
105
+ * @since 0.0.0
106
+ */
107
+ function _clean() {
108
+ const d1 = new Date();
109
+ process.stdout.write('Starting \'\x1b[36mclean\x1b[89m\x1b[0m\'...\n');
110
+
111
+ fs.rmSync(destination, { force: true, recursive: true });
112
+ fs.mkdirSync(destination, { recursive: true });
113
+
114
+ const d2 = new Date() - d1;
115
+ process.stdout.write(`Finished '\x1b[36mclean\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
116
+ }
117
+
118
+ /**
119
+ * Creates the content of the library.
120
+ *
121
+ * @function ()
122
+ * @private
123
+ * @param {} -,
124
+ * @returns {} -,
125
+ * @since 0.0.0
126
+ */
127
+ function _docore() {
128
+ const d1 = new Date();
129
+ process.stdout.write('Starting \'\x1b[36mdocore\x1b[89m\x1b[0m\'...\n');
130
+
131
+ let src = '';
132
+ for (let i = 0; i < core.length; i++) {
133
+ src += fs.readFileSync(core[i]);
134
+ if (i < core.length - 1) {
135
+ src += '\n';
136
+ }
137
+ }
138
+
139
+ src = src
140
+ .replace(/{{lib:name}}/g, libname)
141
+ .replace(/{{lib:version}}/g, version)
142
+ // remove the extra global and 'use strict':
143
+ .replace(/\/\* global[\w$_\s,]+\*\//g, '/* - */')
144
+ .replace(/\n'use strict';\n/g, '')
145
+ // indent the first line with 2 spaces:
146
+ .replace(/^/g, ' ')
147
+ // indent each other lines with 2 spaces:
148
+ .replace(/\n/g, '\n ')
149
+ ;
150
+
151
+ fs.writeFileSync(`${destination}/core.js`, src);
152
+ const d2 = new Date() - d1;
153
+ process.stdout.write(`Finished '\x1b[36mdocore\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
154
+ }
155
+
156
+ /**
157
+ * Creates the UMD Module.
158
+ *
159
+ * @function ()
160
+ * @private
161
+ * @param {} -,
162
+ * @returns {} -,
163
+ * @since 0.0.0
164
+ */
165
+ function _doumdlib() {
166
+ const d1 = new Date();
167
+ process.stdout.write('Starting \'\x1b[36mdoumdlib\x1b[89m\x1b[0m\'...\n');
168
+
169
+ let src = '';
170
+ src = fs.readFileSync(head);
171
+ src += '\n';
172
+ src += fs.readFileSync(`${destination}/core.js`);
173
+ src += '\n';
174
+ src += fs.readFileSync(foot);
175
+
176
+ src = src
177
+ .replace('{{lib:es6:define}}\n', '')
178
+ .replace('{{lib:es6:link}}', 'this')
179
+ .replace('{{lib:es6:export}}\n', '')
180
+ // fix the blanck lines we indented too:
181
+ .replace(/\s{2}\n/g, '\n')
182
+ ;
183
+
184
+ fs.writeFileSync(`${destination}/${name}.js`, src);
185
+ const d2 = new Date() - d1;
186
+ process.stdout.write(`Finished '\x1b[36mdoumdlib\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
187
+ }
188
+
189
+ /**
190
+ * Creates the ES6 module.
191
+ *
192
+ * @function ()
193
+ * @private
194
+ * @param {} -,
195
+ * @returns {} -,
196
+ * @since 0.0.0
197
+ */
198
+ function _domodule() {
199
+ const d1 = new Date();
200
+ process.stdout.write('Starting \'\x1b[36mdomodule\x1b[89m\x1b[0m\'...\n');
201
+
202
+ let exportM = '\n// -- Export\n';
203
+ exportM += `export default ${ES6GLOB}.${libname};`;
204
+
205
+ let src = '';
206
+ src = fs.readFileSync(head);
207
+ src += '\n';
208
+ src += fs.readFileSync(`${destination}/core.js`);
209
+ src += '\n';
210
+ src += fs.readFileSync(foot);
211
+
212
+ src = src
213
+ .replace('{{lib:es6:define}}', `const ${ES6GLOB} = {};`)
214
+ .replace('{{lib:es6:link}}', ES6GLOB)
215
+ .replace('{{lib:es6:export}}', exportM)
216
+ // fix the blanck lines we indented too:
217
+ .replace(/\s{2}\n/g, '\n')
218
+ ;
219
+
220
+ fs.writeFileSync(`${destination}/${name}.mjs`, src);
221
+ const d2 = new Date() - d1;
222
+ process.stdout.write(`Finished '\x1b[36mdomodule\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
223
+ }
224
+
225
+ /**
226
+ * Removes the temp file(s).
227
+ *
228
+ * @function ()
229
+ * @private
230
+ * @param {} -,
231
+ * @returns {} -,
232
+ * @since 0.0.0
233
+ */
234
+ function _delcore() {
235
+ const d1 = new Date();
236
+ process.stdout.write('Starting \'\x1b[36mdelcore\x1b[89m\x1b[0m\'...\n');
237
+ fs.unlinkSync(`${destination}/core.js`);
238
+
239
+ const d2 = new Date() - d1;
240
+ process.stdout.write(`Finished '\x1b[36mdelcore\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
241
+ }
242
+
243
+
244
+ // -- Main ---------------------------------------------------------------------
245
+
246
+ /**
247
+ * Executes the script.
248
+ *
249
+ * @function ()
250
+ * @public
251
+ * @param {} -,
252
+ * @returns {} -,
253
+ * @since 0.0.0
254
+ */
255
+ function run() {
256
+ if (parsed.help) {
257
+ _help();
258
+ return;
259
+ }
260
+
261
+ if (parsed.version) {
262
+ process.stdout.write(`version: ${parsed.version}\n`);
263
+ return;
264
+ }
265
+
266
+ const d1 = new Date();
267
+ process.stdout.write('Starting \'\x1b[36mbuild:js:dev\x1b[89m\x1b[0m\'...\n');
268
+
269
+ _clean();
270
+ _docore();
271
+ _doumdlib();
272
+ _domodule();
273
+ _delcore();
274
+
275
+ const d2 = new Date() - d1;
276
+ process.stdout.write(`Finished '\x1b[36mbuild:js:dev\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
277
+ }
278
+
279
+
280
+ // Start script.
281
+ run();
282
+
283
+
284
+ // -- oOo --
@@ -0,0 +1,294 @@
1
+ #!/usr/bin/env node
2
+ /* *****************************************************************************
3
+ *
4
+ * Creates the production JS files.
5
+ *
6
+ * build:js:prod.js script minifies JS files and copies them in the
7
+ * production folder.
8
+ *
9
+ * Private Functions:
10
+ * . _help displays the help message,
11
+ * . _clean removes the previous js production files,
12
+ * . _copydev builds the js production file,
13
+ * . _copydevm builds the ES6 module production file,
14
+ * . _makeminified builds and minifies the js production file,
15
+ * . _makeminifiedm builds and minifies the ES6 module production file,
16
+ *
17
+ *
18
+ * Public Static Methods:
19
+ * . run executes the script,
20
+ *
21
+ *
22
+ * @namespace -
23
+ * @dependencies none
24
+ * @exports -
25
+ * @author -
26
+ * @since 0.0.0
27
+ * @version -
28
+ * ************************************************************************** */
29
+ /* eslint one-var: 0, semi-style: 0, no-underscore-dangle: 0,
30
+ import/no-extraneous-dependencies: 0 */
31
+
32
+ 'use strict';
33
+
34
+ // -- Vendor Modules
35
+ const fs = require('fs')
36
+ , nopt = require('nopt')
37
+ , { minify } = require('terser')
38
+ ;
39
+
40
+
41
+ // -- Local Modules
42
+ const config = require('./config')
43
+ ;
44
+
45
+
46
+ // -- Local Constants
47
+ const VERSION = '0.0.0-alpha.0'
48
+ , opts = {
49
+ help: [Boolean, false],
50
+ version: [String, null],
51
+ }
52
+ , shortOpts = {
53
+ h: ['--help'],
54
+ v: ['--version', VERSION],
55
+ }
56
+ , parsed = nopt(opts, shortOpts, process.argv, 2)
57
+ , { dist } = config
58
+ , { libdir } = config
59
+ , { name } = config
60
+ , { license } = config
61
+ ;
62
+
63
+
64
+ // -- Local Variables
65
+
66
+
67
+ // -- Private Functions --------------------------------------------------------
68
+
69
+ /**
70
+ * Dispays the help message.
71
+ *
72
+ * @function ()
73
+ * @private
74
+ * @param {} -,
75
+ * @returns {} -,
76
+ * @since 0.0.0
77
+ */
78
+ function _help() {
79
+ const message = ['',
80
+ 'Usage: command [options]',
81
+ '',
82
+ ' creates the js production files',
83
+ '',
84
+ 'Options:',
85
+ '',
86
+ '-h, --help output usage information',
87
+ '-v, --version output the version number',
88
+ '',
89
+ ].join('\n');
90
+
91
+ process.stdout.write(`${message}\n`);
92
+ }
93
+
94
+ /**
95
+ * Removes the previous js production build.
96
+ *
97
+ * @function ()
98
+ * @private
99
+ * @param {} -,
100
+ * @returns {} -,
101
+ * @since 0.0.0
102
+ */
103
+ function _clean() {
104
+ const d1 = new Date();
105
+ process.stdout.write('Starting \'\x1b[36mclean\x1b[89m\x1b[0m\'...\n');
106
+
107
+ return new Promise((resolve) => {
108
+ fs.rm(`${dist}/lib`, { force: true, recursive: true }, (err1) => {
109
+ if (err1) throw new Error(err1);
110
+
111
+ fs.mkdir(`${dist}/lib`, { recursive: true }, (err2) => {
112
+ if (err2) throw new Error(err2);
113
+
114
+ const d2 = new Date() - d1;
115
+ process.stdout.write(`Finished '\x1b[36mclean\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
116
+ resolve();
117
+ });
118
+ });
119
+ });
120
+ }
121
+
122
+ /**
123
+ * Builds the js production file.
124
+ *
125
+ * @function (arg1)
126
+ * @private
127
+ * @param {Function} the function to call at the completion,
128
+ * @returns {} -,
129
+ * @since 0.0.0
130
+ */
131
+ function _copydev(done) {
132
+ const d1 = new Date();
133
+ process.stdout.write('Starting \'\x1b[36mcopydev\x1b[89m\x1b[0m\'...\n');
134
+
135
+ fs.readFile(`${libdir}/${name}.js`, 'utf8', (err1, data) => {
136
+ if (err1) throw new Error(err1);
137
+
138
+ let content = license;
139
+ content += data;
140
+ fs.writeFile(`${dist}/lib/${name}.js`, content, { encoding: 'utf8' }, (err2) => {
141
+ if (err2) throw new Error(err2);
142
+
143
+ const d2 = new Date() - d1;
144
+ process.stdout.write(`Finished '\x1b[36mcopydev\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
145
+ done();
146
+ });
147
+ });
148
+ }
149
+
150
+ /**
151
+ * Builds the ES6 module production file.
152
+ *
153
+ * @function (arg1)
154
+ * @private
155
+ * @param {Function} the function to call at the completion,
156
+ * @returns {} -,
157
+ * @since 0.0.0
158
+ */
159
+ function _copydevm(done) {
160
+ const d1 = new Date();
161
+ process.stdout.write('Starting \'\x1b[36mcopydevm\x1b[89m\x1b[0m\'...\n');
162
+
163
+ fs.readFile(`${libdir}/${name}.mjs`, 'utf8', (err1, data) => {
164
+ if (err1) throw new Error(err1);
165
+
166
+ let content = license;
167
+ content += data;
168
+ fs.writeFile(`${dist}/lib/${name}.mjs`, content, { encoding: 'utf8' }, (err2) => {
169
+ if (err2) throw new Error(err2);
170
+
171
+ const d2 = new Date() - d1;
172
+ process.stdout.write(`Finished '\x1b[36mcopydevm\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
173
+ done();
174
+ });
175
+ });
176
+ }
177
+
178
+ /**
179
+ * Builds and minifies the js production file.
180
+ *
181
+ * @function (arg1)
182
+ * @private
183
+ * @param {Function} the function to call at the completion,
184
+ * @returns {} -,
185
+ * @since 0.0.0
186
+ */
187
+ function _makeminified(done) {
188
+ const d1 = new Date();
189
+ process.stdout.write('Starting \'\x1b[36mmakeminified\x1b[89m\x1b[0m\'...\n');
190
+
191
+ fs.readFile(`${libdir}/${name}.js`, 'utf8', (err1, data) => {
192
+ if (err1) throw new Error(err1);
193
+
194
+ let content = license;
195
+ content += data.replace(/\/\*! \*\*\*/g, '/** ***');
196
+
197
+ minify(content, {})
198
+ .then((result) => {
199
+ fs.writeFile(`${dist}/lib/${name}.min.js`, result.code, { encoding: 'utf8' }, (err2) => {
200
+ if (err2) throw new Error(err2);
201
+
202
+ const d2 = new Date() - d1;
203
+ process.stdout.write(`Finished '\x1b[36mmakeminified\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
204
+ done();
205
+ });
206
+ });
207
+ });
208
+ }
209
+
210
+ /**
211
+ * Builds and minifies the ES6 module production file.
212
+ *
213
+ * @function (arg1)
214
+ * @private
215
+ * @param {Function} the function to call at the completion,
216
+ * @returns {} -,
217
+ * @since 0.0.0
218
+ */
219
+ function _makeminifiedm(done) {
220
+ const d1 = new Date();
221
+ process.stdout.write('Starting \'\x1b[36mmakeminified\x1b[89m\x1b[0m\'...\n');
222
+
223
+ fs.readFile(`${libdir}/${name}.mjs`, 'utf8', (err1, data) => {
224
+ if (err1) throw new Error(err1);
225
+
226
+ let content = license;
227
+ content += data.replace(/\/\*! \*\*\*/g, '/** ***');
228
+
229
+ minify(content, {})
230
+ .then((result) => {
231
+ fs.writeFile(`${dist}/lib/${name}.min.mjs`, result.code, { encoding: 'utf8' }, (err2) => {
232
+ if (err2) throw new Error(err2);
233
+
234
+ const d2 = new Date() - d1;
235
+ process.stdout.write(`Finished '\x1b[36mmakeminified\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
236
+ done();
237
+ });
238
+ });
239
+ });
240
+ }
241
+
242
+
243
+ // -- Main ---------------------------------------------------------------------
244
+
245
+ /**
246
+ * Executes the script.
247
+ *
248
+ * @function ()
249
+ * @public
250
+ * @param {} -,
251
+ * @returns {} -,
252
+ * @since 0.0.0
253
+ */
254
+ async function run() {
255
+ const PENDING = 4;
256
+
257
+ if (parsed.help) {
258
+ _help();
259
+ return;
260
+ }
261
+
262
+ if (parsed.version) {
263
+ process.stdout.write(`version: ${parsed.version}\n`);
264
+ return;
265
+ }
266
+
267
+ const d1 = new Date();
268
+ process.stdout.write('Starting \'\x1b[36mbuild:js:prod\x1b[89m\x1b[0m\'...\n');
269
+
270
+ let pending = PENDING;
271
+ /**
272
+ * Executes done until completion.
273
+ */
274
+ function done() {
275
+ pending -= 1;
276
+ if (!pending) {
277
+ const d2 = new Date() - d1;
278
+ process.stdout.write(`Finished '\x1b[36mbuild:js:prod\x1b[89m\x1b[0m' after \x1b[35m${d2} ms\x1b[89m\x1b[0m\n`);
279
+ }
280
+ }
281
+
282
+ await _clean();
283
+ _copydev(done);
284
+ _copydevm(done);
285
+ _makeminified(done);
286
+ _makeminifiedm(done);
287
+ }
288
+
289
+
290
+ // Start script.
291
+ run();
292
+
293
+
294
+ // -- oOo --