@next2d/builder 0.0.2 → 0.0.4

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/index.js DELETED
@@ -1,592 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- "use strict";
4
-
5
- const version = process.versions.node;
6
- if (15 > version.split(".")[0]) {
7
- console.error(
8
- "You are running Node Version:" + version + ".\n" +
9
- "this command requires Node 15 or higher. \n" +
10
- "Please update your version of Node."
11
- );
12
- process.exit(1);
13
- }
14
-
15
- const fs = require("fs");
16
- const cp = require("child_process");
17
- const webpackConfig = require(`${process.cwd()}/webpack.config.js`);
18
- const electronBuilder = require("electron-builder");
19
- const chalk = require("chalk");
20
-
21
- let platform = "";
22
- let environment = "";
23
- let hasHelp = false;
24
- let debug = false;
25
-
26
- for (let idx = 0; idx < process.argv.length; ++idx) {
27
-
28
- switch (process.argv[idx]) {
29
-
30
- case "--debug":
31
- debug = true;
32
- break;
33
-
34
- case "--help":
35
- case "--h":
36
- hasHelp = true;
37
- break;
38
-
39
- case "--platform":
40
- platform = process.argv[++idx].toLowerCase();
41
- break;
42
-
43
- case "--env":
44
- environment = process.argv[++idx];
45
- break;
46
-
47
- default:
48
- break;
49
-
50
- }
51
-
52
- if (hasHelp) {
53
- break;
54
- }
55
- }
56
-
57
- if (!platform || !environment) {
58
- hasHelp = true;
59
- }
60
-
61
- if (hasHelp) {
62
- console.log();
63
- console.log("Export with `--platform` and `--env` arguments.");
64
- console.log();
65
- console.log("For build example:");
66
- console.log("npx @next2d/builder --platform web --env prd");
67
- console.log();
68
- console.log("For debug example:");
69
- console.log("npx @next2d/builder --debug --platform web --env prd");
70
- console.log();
71
- process.exit(1);
72
- }
73
-
74
- if (!fs.existsSync(`${webpackConfig.output.path}`)) {
75
- fs.mkdirSync(`${webpackConfig.output.path}`, { "recursive": true });
76
- }
77
-
78
- /**
79
- * @description 一時的に読み込みようのHTMLを生成
80
- * Generate HTML for temporary loading
81
- *
82
- * @param {string} file_path
83
- * @param {string} product_name
84
- * @return {boolean}
85
- * @method
86
- * @public
87
- */
88
- const buildHTML = (file_path, product_name) =>
89
- {
90
- if (!fs.existsSync(file_path)) {
91
-
92
- fs.writeFileSync(
93
- file_path,
94
- `<!DOCTYPE html>
95
- <html>
96
- <head>
97
- <meta charset="utf-8"/>
98
- <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
99
- <title>${product_name}</title>
100
- <script src="./app.js"></script>
101
- </head>
102
- <body style="margin: 0; padding: 0;">
103
- </body>
104
- </html>`);
105
-
106
- return true;
107
- }
108
-
109
- return false;
110
- };
111
-
112
- /**
113
- * @description Web用アプリの書き出し関数
114
- * Export function for web applications
115
- *
116
- * @return {Promise}
117
- * @method
118
- * @public
119
- */
120
- const buildWeb = () =>
121
- {
122
- return new Promise((resolve) =>
123
- {
124
- const command = `${process.cwd()}/node_modules/.bin/webpack`;
125
-
126
- const mode = debug ? "development" : "production";
127
- const stream = cp.spawn(command, [
128
- "--mode",
129
- mode,
130
- "--env",
131
- environment
132
- ]);
133
-
134
- let errorCheck = false;
135
- stream.stderr.on("data", (error) =>
136
- {
137
- errorCheck = true;
138
- console.error(chalk.red(error.toString()));
139
- });
140
-
141
- stream.stdout.on("data", (data) =>
142
- {
143
- console.log(data.toString());
144
- });
145
-
146
- stream.on("exit", () =>
147
- {
148
- if (errorCheck) {
149
- process.exit(1);
150
- }
151
-
152
- console.log(chalk.green("Build web app."));
153
- console.log();
154
- resolve();
155
- });
156
- });
157
- };
158
-
159
- /**
160
- * @description Electronの書き出しに必要なconfigを生成
161
- * Generate config for Electron export
162
- *
163
- * @return {object}
164
- * @method
165
- * @public
166
- */
167
- const generateElectronConfig = (platform) =>
168
- {
169
- let config = {};
170
-
171
- const configPath = `${process.cwd()}/electron.build.js`;
172
- if (!fs.existsSync(configPath)) {
173
- console.error(chalk.red("The file `electron.build.js` could not be found."));
174
- process.exit(1);
175
- }
176
-
177
- config = Object.assign(config, require(configPath));
178
- if (!config.appId) {
179
- console.log();
180
- console.log(chalk.red("`appId` is not set."));
181
- console.log("Please set `appId` in `electron.build.js`.");
182
- console.log();
183
- process.exit(1);
184
- }
185
-
186
- switch (platform) {
187
-
188
- case "windows":
189
- if (!("win" in config)) {
190
- config.win = {
191
- "target": "nsis"
192
- };
193
- }
194
-
195
- if ("mac" in config) {
196
- delete config.mac;
197
- }
198
-
199
- break;
200
-
201
- case "macos":
202
- if (!("mac" in config)) {
203
- config.mac = {
204
- "target": "dmg"
205
- };
206
- }
207
-
208
- if ("win" in config) {
209
- delete config.win;
210
- }
211
-
212
- break;
213
-
214
- default:
215
- break;
216
-
217
- }
218
-
219
- if (!("directories" in config)) {
220
- config.directories = {
221
- "output": "dist"
222
- };
223
- }
224
-
225
- if (config.directories.output.slice(-1) !== "/") {
226
- config.directories.output += "/";
227
- }
228
-
229
- config.directories.output += `${platform}/${environment}`;
230
-
231
- return config;
232
- };
233
-
234
- /**
235
- * @description Electronで読み込むindex.htmlのファイルパスをセット
236
- * Set the file path of index.html to be loaded by Electron
237
- *
238
- * @return {void}
239
- * @method
240
- * @public
241
- */
242
- const setElectronIndexPath = () =>
243
- {
244
- const htmlFilePath = `${webpackConfig.output.path}/index.html`;
245
- fs.writeFileSync(
246
- `${process.cwd()}/electron.index.json`,
247
- JSON.stringify({
248
- "path": htmlFilePath.replace(process.cwd(), ".").trim()
249
- }, null, 2)
250
- );
251
- };
252
-
253
- /**
254
- * @description Electronで読み込むindex.htmlのファイルパスを初期化
255
- * Initialize the file path of index.html to be loaded by Electron
256
- *
257
- * @return {void}
258
- * @method
259
- * @public
260
- */
261
- const unlinkElectronIndexPath = () =>
262
- {
263
- const jsonPath = `${process.cwd()}/electron.index.json`;
264
- if (fs.existsSync(jsonPath)) {
265
- fs.unlinkSync(jsonPath);
266
- }
267
- };
268
-
269
- /**
270
- * @description Windows用アプリの書き出し関数
271
- * Export function for Windows apps
272
- *
273
- * @return {void}
274
- * @method
275
- * @public
276
- */
277
- const buildWindow = () =>
278
- {
279
- /**
280
- * web版の書き出し
281
- * Exporting the web version
282
- */
283
- buildWeb()
284
- .then(() =>
285
- {
286
- /**
287
- * Electronの書き出しに必要な設定を生成
288
- * Generate settings necessary for exporting Electron
289
- */
290
- const config = generateElectronConfig("windows");
291
-
292
- /**
293
- * 読み込みようのindex.htmlファイルがなければ生成
294
- * If there is no index.html file to load, generate one.
295
- */
296
- const htmlFile = `${webpackConfig.output.path}/index.html`;
297
- const generatedHTML = buildHTML(htmlFile, config.productName);
298
-
299
- /**
300
- * Electronで読み込むindex.htmlのパスをセット
301
- * Set the path to index.html to load in Electron
302
- */
303
- setElectronIndexPath();
304
-
305
- if (debug) {
306
-
307
- const stream = cp.spawn(
308
- `${process.cwd()}/node_modules/.bin/electron`,
309
- [
310
- `${process.cwd()}/electron.js`,
311
- "--env",
312
- environment
313
- ]
314
- );
315
-
316
- stream.on("close", () =>
317
- {
318
- if (generatedHTML) {
319
- fs.unlinkSync(htmlFile);
320
- }
321
-
322
- unlinkElectronIndexPath();
323
- });
324
-
325
- stream.on("disconnect", () =>
326
- {
327
- if (generatedHTML) {
328
- fs.unlinkSync(htmlFile);
329
- }
330
-
331
- unlinkElectronIndexPath();
332
- });
333
-
334
- } else {
335
-
336
- electronBuilder
337
- .build({
338
- "projectDir": process.cwd(),
339
- "targets": electronBuilder.Platform.WINDOWS.createTarget(),
340
- "config": config
341
- })
342
- .then(() =>
343
- {
344
- if (generatedHTML) {
345
- fs.unlinkSync(htmlFile);
346
- }
347
-
348
- unlinkElectronIndexPath();
349
- });
350
-
351
- }
352
- });
353
- };
354
-
355
- /**
356
- * @description macOS用アプリの書き出し関数
357
- * Export function for macOS apps
358
- *
359
- * @return {void}
360
- * @method
361
- * @public
362
- */
363
- const buildMac = () =>
364
- {
365
- /**
366
- * web版の書き出し
367
- * Exporting the web version
368
- */
369
- buildWeb()
370
- .then(() =>
371
- {
372
- /**
373
- * Electronの書き出しに必要な設定を生成
374
- * Generate settings necessary for exporting Electron
375
- */
376
- const config = generateElectronConfig("macos");
377
-
378
- /**
379
- * 読み込みようのindex.htmlファイルがなければ生成
380
- * If there is no index.html file to load, generate one.
381
- */
382
- const htmlFile = `${webpackConfig.output.path}/index.html`;
383
- const generatedHTML = buildHTML(htmlFile, config.productName);
384
-
385
- /**
386
- * Electronで読み込むindex.htmlのパスをセット
387
- * Set the path to index.html to load in Electron
388
- */
389
- setElectronIndexPath();
390
-
391
- if (debug) {
392
-
393
- const stream = cp.spawn(
394
- `${process.cwd()}/node_modules/.bin/electron`,
395
- [
396
- `${process.cwd()}/electron.js`,
397
- "--env",
398
- environment
399
- ]
400
- );
401
-
402
- stream.on("close", () =>
403
- {
404
- if (generatedHTML) {
405
- fs.unlinkSync(htmlFile);
406
- }
407
-
408
- unlinkElectronIndexPath();
409
- });
410
-
411
- stream.on("disconnect", () =>
412
- {
413
- if (generatedHTML) {
414
- fs.unlinkSync(htmlFile);
415
- }
416
-
417
- unlinkElectronIndexPath();
418
- });
419
-
420
- } else {
421
-
422
- electronBuilder
423
- .build({
424
- "projectDir": process.cwd(),
425
- "targets": electronBuilder.Platform.MAC.createTarget(),
426
- "config": config
427
- })
428
- .then(() =>
429
- {
430
- if (generatedHTML) {
431
- fs.unlinkSync(htmlFile);
432
- }
433
-
434
- unlinkElectronIndexPath();
435
- });
436
-
437
- }
438
- });
439
- };
440
-
441
- /**
442
- * @description iOSのプロジェクトを生成
443
- * Generate iOS project
444
- *
445
- * @return {Promise<void>}
446
- * @method
447
- * @public
448
- */
449
- const generateNativeProject = () =>
450
- {
451
- if (fs.existsSync(`${process.cwd()}/${platform}`)) {
452
- return Promise.resolve();
453
- }
454
-
455
- return new Promise((resolve) =>
456
- {
457
- const stream = cp.spawn("npx", ["cap", "add", platform]);
458
-
459
- let errorCheck = false;
460
- stream.stderr.on("data", (error) =>
461
- {
462
- errorCheck = true;
463
- console.error(chalk.red(error.toString()));
464
- });
465
-
466
- stream.stdout.on("data", (data) =>
467
- {
468
- console.log(data.toString());
469
- });
470
-
471
- stream.on("exit", () =>
472
- {
473
- if (errorCheck) {
474
- process.exit(1);
475
- }
476
-
477
- console.log(chalk.green(`Successfully generated ${platform} project.`));
478
- console.log();
479
-
480
- resolve();
481
- });
482
- });
483
- };
484
-
485
- /**
486
- * @description iOS用アプリの書き出し関数
487
- * Export function for iOS apps
488
- *
489
- * @return {void}
490
- * @method
491
- * @public
492
- */
493
- const runNative = () =>
494
- {
495
- generateNativeProject()
496
- .then(() =>
497
- {
498
- return buildWeb();
499
- })
500
- .then(() =>
501
- {
502
- /**
503
- * Capacitorの書き出しに必要な設定を生成
504
- * Generate settings necessary for exporting Capacitor
505
- */
506
- const config = JSON.parse(
507
- fs.readFileSync(`${process.cwd()}/capacitor.config.json`, { "encoding": "utf8" })
508
- );
509
-
510
- /**
511
- * 読み込みようのindex.htmlファイルがなければ生成
512
- * If there is no index.html file to load, generate one.
513
- */
514
- const htmlFile = `${webpackConfig.output.path}/index.html`;
515
- const generatedHTML = buildHTML(htmlFile, config.appName);
516
-
517
- config.webDir = htmlFile
518
- .replace(process.cwd(), ".")
519
- .replace("index.html", "")
520
- .trim();
521
-
522
- fs.writeFileSync(
523
- `${process.cwd()}/capacitor.config.json`,
524
- JSON.stringify(config, null, 2)
525
- );
526
-
527
- return Promise.resolve(generatedHTML);
528
-
529
- })
530
- .then((generated_html) =>
531
- {
532
- process.on("exit", () =>
533
- {
534
- if (generated_html) {
535
- fs.unlinkSync(`${webpackConfig.output.path}/index.html`);
536
- }
537
- });
538
-
539
- const { run } = require("@capacitor/cli");
540
- run();
541
- });
542
- };
543
-
544
- switch (platform) {
545
-
546
- case "windows":
547
- buildWindow();
548
- break;
549
-
550
- case "macos":
551
- buildMac();
552
- break;
553
-
554
- case "ios":
555
- if (debug) {
556
- runNative();
557
- }
558
- break;
559
-
560
- case "android":
561
- if (debug) {
562
- runNative();
563
- }
564
- break;
565
-
566
- case "web":
567
- buildWeb()
568
- .then(() =>
569
- {
570
- console.log("build done.");
571
- })
572
- .catch((error) => {
573
- console.error(error);
574
- process.exit(1);
575
- });
576
- break;
577
-
578
- default:
579
- console.log();
580
- console.log("`--platform` can be specified for macOS, Windows, iOS, Android, and Web");
581
- console.log("It is not case sensitive.");
582
- console.log();
583
- console.log("For example:");
584
- console.log("npx @next2d/builder --platform web --env prd");
585
- console.log();
586
- console.log("For Debug example:");
587
- console.log("npx @next2d/builder --debug --platform web --env prd");
588
- console.log();
589
- process.exit(1);
590
- break;
591
-
592
- }