@next2d/builder 0.0.8 → 0.0.9

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.
Files changed (2) hide show
  1. package/dist/index.js +78 -138
  2. package/package.json +2 -3
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import pc from "picocolors";
4
4
  import fs from "fs";
5
5
  import cp from "child_process";
6
6
  import { loadConfigFromFile } from "vite";
7
- import { Platform, build as electronBuilder } from "electron-builder";
7
+ import { api } from "@electron-forge/core";
8
8
  const recommendeVersion = 18;
9
9
  const version = process.versions.node;
10
10
  if (recommendeVersion > parseInt(version.split(".")[0])) {
@@ -112,16 +112,6 @@ process.env.NEXT2D_TARGET_PLATFORM = platform;
112
112
  * @constant
113
113
  */
114
114
  const CAPACITOR_CONFIG_NAME = "capacitor.config.json";
115
- /**
116
- * @type {string}
117
- * @constant
118
- */
119
- const ELECTRON_BUILD_CONFIG_NAME = "electron.build.json";
120
- /**
121
- * @type {string}
122
- * @constant
123
- */
124
- const ELECTRON_INDEX_CONFIG_NAME = "electron.index.json";
125
115
  /**
126
116
  * @return {Promise}
127
117
  * @method
@@ -179,184 +169,134 @@ const buildWeb = () => {
179
169
  });
180
170
  };
181
171
  /**
182
- * @description Electronの書き出しに必要なconfigを生成
183
- * Generate config for Electron export
172
+ * @description electronがインストールされてなければインストールを実行
173
+ * If electron is not installed, run install.
184
174
  *
185
- * @return {object}
175
+ * @return {Promise}
186
176
  * @method
187
177
  * @public
188
178
  */
189
- const generateElectronConfig = () => {
190
- let config = {};
191
- const configPath = `${process.cwd()}/${ELECTRON_BUILD_CONFIG_NAME}`;
192
- if (!fs.existsSync(configPath)) {
193
- console.error(pc.red(`The file \`${ELECTRON_BUILD_CONFIG_NAME}\` could not be found.`));
194
- process.exit(1);
195
- }
196
- config = Object.assign(config, JSON.parse(fs.readFileSync(configPath, { "encoding": "utf8" })));
197
- if (!config.appId) {
198
- console.log();
199
- console.log(pc.red("`appId` is not set."));
200
- console.log(`Please set \`appId\` in \`${ELECTRON_BUILD_CONFIG_NAME}\`.`);
201
- console.log();
202
- process.exit(1);
203
- }
204
- switch (platform) {
205
- case "windows":
206
- case "steam:windows":
207
- if (!("win" in config)) {
208
- config.win = {
209
- "target": "portable"
210
- };
211
- }
212
- if ("mac" in config) {
213
- delete config.mac;
214
- }
215
- if ("linux" in config) {
216
- delete config.linux;
217
- }
218
- break;
219
- case "macos":
220
- case "steam:macos":
221
- if (!("mac" in config)) {
222
- config.mac = {
223
- "target": "dmg"
224
- };
225
- }
226
- if ("win" in config) {
227
- delete config.win;
228
- }
229
- if ("linux" in config) {
230
- delete config.linux;
231
- }
232
- break;
233
- case "linux":
234
- case "steam:linux":
235
- if (!("linux" in config)) {
236
- config.linux = {
237
- "target": "deb"
238
- };
239
- }
240
- if ("win" in config) {
241
- delete config.win;
242
- }
243
- if ("mac" in config) {
244
- delete config.mac;
179
+ const installElectron = () => {
180
+ return new Promise((resolve, reject) => {
181
+ if (fs.existsSync(`${process.cwd()}/electron/node_modules`)) {
182
+ return resolve();
183
+ }
184
+ const stream = cp.spawn("npm", [
185
+ "--prefix",
186
+ `${process.cwd()}/electron`,
187
+ "install",
188
+ `${process.cwd()}/electron`
189
+ ], { "stdio": "inherit" });
190
+ stream.on("close", (code) => {
191
+ if (code !== 0) {
192
+ reject("`Electron` installation failed.");
245
193
  }
246
- break;
247
- default:
248
- break;
249
- }
250
- if (!("directories" in config)) {
251
- config.directories = {
252
- "output": "dist"
253
- };
254
- }
255
- if (!("files" in config)) {
256
- config.files = [];
257
- }
258
- config.files.push(`${$outDir}/${platformDir}/${environment}/`);
259
- config.directories.output = `${$outDir}/${platformDir}/build`;
260
- return config;
194
+ console.log(pc.green("`Electron` successfully installed."));
195
+ resolve();
196
+ });
197
+ });
261
198
  };
262
199
  /**
263
- * @description Electronで読み込むindex.htmlのファイルパスをセット
264
- * Set the file path of index.html to be loaded by Electron
200
+ * @description electronに含むリソースを初期化
201
+ * Initialize resources included in electron
265
202
  *
266
- * @return {void}
203
+ * @return {Promise}
267
204
  * @method
268
205
  * @public
269
206
  */
270
- const setElectronIndexPath = () => {
271
- const htmlFilePath = `${$buildDir}/index.html`;
272
- fs.writeFileSync(`${process.cwd()}/${ELECTRON_INDEX_CONFIG_NAME}`, JSON.stringify({
273
- "path": htmlFilePath.replace(process.cwd(), ".").trim()
274
- }, null, 2));
207
+ const removeResources = () => {
208
+ return new Promise((resolve, reject) => {
209
+ const stream = cp.spawn("rm", [
210
+ "-rf",
211
+ `${process.cwd()}/electron/resources/`
212
+ ], { "stdio": "inherit" });
213
+ stream.on("close", (code) => {
214
+ if (code !== 0) {
215
+ reject("Failed to remove built resources.");
216
+ }
217
+ console.log(pc.green("Successfully remove built resources."));
218
+ resolve();
219
+ });
220
+ });
275
221
  };
276
222
  /**
277
- * @description Electronで読み込むindex.htmlのファイルパスを初期化
278
- * Initialize the file path of index.html to be loaded by Electron
223
+ * @description ビルドしたリソースをコピー
224
+ * Copy built resources
279
225
  *
280
- * @return {void}
226
+ * @return {Promise}
281
227
  * @method
282
228
  * @public
283
229
  */
284
- const unlinkElectronIndexPath = () => {
285
- const jsonPath = `${process.cwd()}/${ELECTRON_INDEX_CONFIG_NAME}`;
286
- if (fs.existsSync(jsonPath)) {
287
- fs.unlinkSync(jsonPath);
288
- }
230
+ const copyResources = () => {
231
+ return new Promise((resolve, reject) => {
232
+ const stream = cp.spawn("cp", [
233
+ "-r",
234
+ `${$buildDir}/`,
235
+ `${process.cwd()}/electron/resources`
236
+ ], { "stdio": "inherit" });
237
+ stream.on("close", (code) => {
238
+ if (code !== 0) {
239
+ reject("Failed to copy built resources.");
240
+ }
241
+ console.log(pc.green("Successfully copy built resources."));
242
+ resolve();
243
+ });
244
+ });
289
245
  };
290
246
  /**
291
247
  * @description Windows用アプリの書き出し関数
292
248
  * Export function for Windows apps
293
249
  *
294
- * @return {void}
250
+ * @return {Promise}
295
251
  * @method
296
252
  * @public
297
253
  */
298
- const buildSteam = () => {
299
- /**
300
- * Electronで読み込むindex.htmlのパスをセット
301
- * Set the path to index.html to load in Electron
302
- */
303
- setElectronIndexPath();
254
+ const buildSteam = async () => {
255
+ // reset
256
+ await removeResources();
257
+ // copy HTML, JavaScript
258
+ await copyResources();
304
259
  if (preview) {
305
- const packageJson = JSON.parse(fs.readFileSync(`${process.cwd()}/package.json`, { "encoding": "utf8" }));
306
- packageJson.type = "commonjs";
307
- fs.writeFileSync(`${process.cwd()}/package.json`, JSON.stringify(packageJson, null, 2));
308
- cp.spawn(`${process.cwd()}/node_modules/.bin/electron`, [
309
- `${process.cwd()}/electron.js`
260
+ cp.spawn("npx", [
261
+ "electron",
262
+ `${process.cwd()}/electron/index.js`
310
263
  ], { "stdio": "inherit" });
311
- setTimeout(() => {
312
- if (packageJson.type !== "module") {
313
- packageJson.type = "module";
314
- // overwride
315
- fs.writeFileSync(`${process.cwd()}/package.json`, JSON.stringify(packageJson, null, 2));
316
- }
317
- unlinkElectronIndexPath();
318
- }, 1000);
319
264
  }
320
265
  else {
321
- /**
322
- * Electronの書き出しに必要な設定を生成
323
- * Generate settings necessary for exporting Electron
324
- */
325
- const config = generateElectronConfig();
266
+ await installElectron();
326
267
  console.log(pc.green("Start the `Electron` build process."));
327
268
  console.log();
328
- const buildObject = {
329
- "projectDir": process.cwd(),
330
- "config": config
269
+ const packageOptions = {
270
+ "dir": `${process.cwd()}/electron`,
271
+ "outDir": `${$outDir}/${platformDir}/build`,
272
+ "platform": "",
273
+ "arch": "all"
331
274
  };
332
275
  switch (platform) {
333
276
  case "windows":
334
277
  case "steam:windows":
335
- buildObject.targets = Platform.WINDOWS.createTarget();
278
+ packageOptions.platform = "win32";
336
279
  break;
337
280
  case "macos":
338
281
  case "steam:macos":
339
- buildObject.targets = Platform.MAC.createTarget();
282
+ packageOptions.platform = "mas";
340
283
  break;
341
284
  case "linux":
342
285
  case "steam:linux":
343
- buildObject.targets = Platform.LINUX.createTarget();
286
+ packageOptions.platform = "linux";
344
287
  break;
345
288
  default:
346
- unlinkElectronIndexPath();
347
289
  console.log(pc.red("There is an error in the export platform settings."));
348
290
  console.log();
349
291
  process.exit(1);
350
- break;
351
292
  }
352
- electronBuilder(buildObject)
293
+ api
294
+ .package(packageOptions)
353
295
  .then(() => {
354
- unlinkElectronIndexPath();
355
296
  console.log(pc.green(`Finished building \`Electron\` for ${platform}.`));
356
297
  console.log();
357
298
  })
358
299
  .catch((error) => {
359
- unlinkElectronIndexPath();
360
300
  console.log(pc.red("Export of Electron failed."));
361
301
  console.log(pc.red(error));
362
302
  });
@@ -366,7 +306,7 @@ const buildSteam = () => {
366
306
  * @description iOSのプロジェクトを生成
367
307
  * Generate iOS project
368
308
  *
369
- * @return {Promise<void>}
309
+ * @return {Promise}
370
310
  * @method
371
311
  * @public
372
312
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/builder",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Multi-platform builder for Next2d Framework, supporting export to various platforms such as macOS, Windows, iOS, Android and Web(HTML)",
5
5
  "author": "Toshiyuki Ienaga <ienaga@next2d.app>",
6
6
  "license": "MIT",
@@ -34,9 +34,8 @@
34
34
  "@next2d/builder": "dist/index.js"
35
35
  },
36
36
  "dependencies": {
37
+ "@electron-forge/core": "^6.4.2",
37
38
  "@types/node": "^20.9.0",
38
- "electron": "^27.0.4",
39
- "electron-builder": "^24.6.4",
40
39
  "picocolors": "^1.0.0",
41
40
  "vite": "^4.5.0"
42
41
  },