@next2d/builder 0.0.7 → 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 +80 -139
  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
@@ -135,10 +125,11 @@ const loadConfig = () => {
135
125
  // overwride
136
126
  fs.writeFileSync(`${process.cwd()}/package.json`, JSON.stringify(packageJson, null, 2));
137
127
  }
128
+ const ext = fs.existsSync(`${process.cwd()}/vite.config.ts`) ? "ts" : "js";
138
129
  const config = await loadConfigFromFile({
139
130
  "command": "build",
140
131
  "mode": "build"
141
- }, `${process.cwd()}/vite.config.ts`);
132
+ }, `${process.cwd()}/vite.config.${ext}`);
142
133
  // update config
143
134
  $configObject = config;
144
135
  $outDir = $configObject.config?.build?.outDir || "dist";
@@ -178,184 +169,134 @@ const buildWeb = () => {
178
169
  });
179
170
  };
180
171
  /**
181
- * @description Electronの書き出しに必要なconfigを生成
182
- * Generate config for Electron export
172
+ * @description electronがインストールされてなければインストールを実行
173
+ * If electron is not installed, run install.
183
174
  *
184
- * @return {object}
175
+ * @return {Promise}
185
176
  * @method
186
177
  * @public
187
178
  */
188
- const generateElectronConfig = () => {
189
- let config = {};
190
- const configPath = `${process.cwd()}/${ELECTRON_BUILD_CONFIG_NAME}`;
191
- if (!fs.existsSync(configPath)) {
192
- console.error(pc.red(`The file \`${ELECTRON_BUILD_CONFIG_NAME}\` could not be found.`));
193
- process.exit(1);
194
- }
195
- config = Object.assign(config, JSON.parse(fs.readFileSync(configPath, { "encoding": "utf8" })));
196
- if (!config.appId) {
197
- console.log();
198
- console.log(pc.red("`appId` is not set."));
199
- console.log(`Please set \`appId\` in \`${ELECTRON_BUILD_CONFIG_NAME}\`.`);
200
- console.log();
201
- process.exit(1);
202
- }
203
- switch (platform) {
204
- case "windows":
205
- case "steam:windows":
206
- if (!("win" in config)) {
207
- config.win = {
208
- "target": "portable"
209
- };
210
- }
211
- if ("mac" in config) {
212
- delete config.mac;
213
- }
214
- if ("linux" in config) {
215
- delete config.linux;
216
- }
217
- break;
218
- case "macos":
219
- case "steam:macos":
220
- if (!("mac" in config)) {
221
- config.mac = {
222
- "target": "dmg"
223
- };
224
- }
225
- if ("win" in config) {
226
- delete config.win;
227
- }
228
- if ("linux" in config) {
229
- delete config.linux;
230
- }
231
- break;
232
- case "linux":
233
- case "steam:linux":
234
- if (!("linux" in config)) {
235
- config.linux = {
236
- "target": "deb"
237
- };
238
- }
239
- if ("win" in config) {
240
- delete config.win;
241
- }
242
- if ("mac" in config) {
243
- 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.");
244
193
  }
245
- break;
246
- default:
247
- break;
248
- }
249
- if (!("directories" in config)) {
250
- config.directories = {
251
- "output": "dist"
252
- };
253
- }
254
- if (!("files" in config)) {
255
- config.files = [];
256
- }
257
- config.files.push(`${$outDir}/${platformDir}/${environment}/`);
258
- config.directories.output = `${$outDir}/${platformDir}/build`;
259
- return config;
194
+ console.log(pc.green("`Electron` successfully installed."));
195
+ resolve();
196
+ });
197
+ });
260
198
  };
261
199
  /**
262
- * @description Electronで読み込むindex.htmlのファイルパスをセット
263
- * Set the file path of index.html to be loaded by Electron
200
+ * @description electronに含むリソースを初期化
201
+ * Initialize resources included in electron
264
202
  *
265
- * @return {void}
203
+ * @return {Promise}
266
204
  * @method
267
205
  * @public
268
206
  */
269
- const setElectronIndexPath = () => {
270
- const htmlFilePath = `${$buildDir}/index.html`;
271
- fs.writeFileSync(`${process.cwd()}/${ELECTRON_INDEX_CONFIG_NAME}`, JSON.stringify({
272
- "path": htmlFilePath.replace(process.cwd(), ".").trim()
273
- }, 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
+ });
274
221
  };
275
222
  /**
276
- * @description Electronで読み込むindex.htmlのファイルパスを初期化
277
- * Initialize the file path of index.html to be loaded by Electron
223
+ * @description ビルドしたリソースをコピー
224
+ * Copy built resources
278
225
  *
279
- * @return {void}
226
+ * @return {Promise}
280
227
  * @method
281
228
  * @public
282
229
  */
283
- const unlinkElectronIndexPath = () => {
284
- const jsonPath = `${process.cwd()}/${ELECTRON_INDEX_CONFIG_NAME}`;
285
- if (fs.existsSync(jsonPath)) {
286
- fs.unlinkSync(jsonPath);
287
- }
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
+ });
288
245
  };
289
246
  /**
290
247
  * @description Windows用アプリの書き出し関数
291
248
  * Export function for Windows apps
292
249
  *
293
- * @return {void}
250
+ * @return {Promise}
294
251
  * @method
295
252
  * @public
296
253
  */
297
- const buildSteam = () => {
298
- /**
299
- * Electronで読み込むindex.htmlのパスをセット
300
- * Set the path to index.html to load in Electron
301
- */
302
- setElectronIndexPath();
254
+ const buildSteam = async () => {
255
+ // reset
256
+ await removeResources();
257
+ // copy HTML, JavaScript
258
+ await copyResources();
303
259
  if (preview) {
304
- const packageJson = JSON.parse(fs.readFileSync(`${process.cwd()}/package.json`, { "encoding": "utf8" }));
305
- packageJson.type = "commonjs";
306
- fs.writeFileSync(`${process.cwd()}/package.json`, JSON.stringify(packageJson, null, 2));
307
- cp.spawn(`${process.cwd()}/node_modules/.bin/electron`, [
308
- `${process.cwd()}/electron.js`
260
+ cp.spawn("npx", [
261
+ "electron",
262
+ `${process.cwd()}/electron/index.js`
309
263
  ], { "stdio": "inherit" });
310
- setTimeout(() => {
311
- if (packageJson.type !== "module") {
312
- packageJson.type = "module";
313
- // overwride
314
- fs.writeFileSync(`${process.cwd()}/package.json`, JSON.stringify(packageJson, null, 2));
315
- }
316
- unlinkElectronIndexPath();
317
- }, 1000);
318
264
  }
319
265
  else {
320
- /**
321
- * Electronの書き出しに必要な設定を生成
322
- * Generate settings necessary for exporting Electron
323
- */
324
- const config = generateElectronConfig();
266
+ await installElectron();
325
267
  console.log(pc.green("Start the `Electron` build process."));
326
268
  console.log();
327
- const buildObject = {
328
- "projectDir": process.cwd(),
329
- "config": config
269
+ const packageOptions = {
270
+ "dir": `${process.cwd()}/electron`,
271
+ "outDir": `${$outDir}/${platformDir}/build`,
272
+ "platform": "",
273
+ "arch": "all"
330
274
  };
331
275
  switch (platform) {
332
276
  case "windows":
333
277
  case "steam:windows":
334
- buildObject.targets = Platform.WINDOWS.createTarget();
278
+ packageOptions.platform = "win32";
335
279
  break;
336
280
  case "macos":
337
281
  case "steam:macos":
338
- buildObject.targets = Platform.MAC.createTarget();
282
+ packageOptions.platform = "mas";
339
283
  break;
340
284
  case "linux":
341
285
  case "steam:linux":
342
- buildObject.targets = Platform.LINUX.createTarget();
286
+ packageOptions.platform = "linux";
343
287
  break;
344
288
  default:
345
- unlinkElectronIndexPath();
346
289
  console.log(pc.red("There is an error in the export platform settings."));
347
290
  console.log();
348
291
  process.exit(1);
349
- break;
350
292
  }
351
- electronBuilder(buildObject)
293
+ api
294
+ .package(packageOptions)
352
295
  .then(() => {
353
- unlinkElectronIndexPath();
354
296
  console.log(pc.green(`Finished building \`Electron\` for ${platform}.`));
355
297
  console.log();
356
298
  })
357
299
  .catch((error) => {
358
- unlinkElectronIndexPath();
359
300
  console.log(pc.red("Export of Electron failed."));
360
301
  console.log(pc.red(error));
361
302
  });
@@ -365,7 +306,7 @@ const buildSteam = () => {
365
306
  * @description iOSのプロジェクトを生成
366
307
  * Generate iOS project
367
308
  *
368
- * @return {Promise<void>}
309
+ * @return {Promise}
369
310
  * @method
370
311
  * @public
371
312
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/builder",
3
- "version": "0.0.7",
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
  },