electronup 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 安静
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,346 @@
1
+ # @quiteer/electronup
2
+
3
+
4
+ > 融合构建 electron 应用需要的构建工具,保留原有配置习惯的命令行工具
5
+
6
+
7
+
8
+ ## 插件前置
9
+
10
+ ```json
11
+ {
12
+ "peerDependencies": {
13
+ "@types/node": ">= 16",
14
+ "electron": ">= 20",
15
+ "vue": ">= 3"
16
+ }
17
+ }
18
+ ```
19
+
20
+
21
+ ## 安装
22
+
23
+ ```bash
24
+ npm i @quiteer/electronup -D
25
+ ```
26
+
27
+ ```bash
28
+ yarn add @quiteer/electronup -D
29
+ ```
30
+
31
+ ```bash
32
+ pnpm add @quiteer/electronup -D
33
+ ```
34
+
35
+
36
+ ## 使用
37
+
38
+ - 查看命令行指令
39
+
40
+ - `electornup -h`
41
+
42
+ - `electornup build -h`
43
+
44
+
45
+ - 查看命令行版本
46
+
47
+ - `electornup -v`
48
+
49
+
50
+ - 开发环境
51
+
52
+ - `electornup`
53
+
54
+ - `electornup dev`
55
+
56
+ - 指定配置文件 `electornup [config file]`
57
+
58
+ - 指定配置文件 `electornup dev [config file]`
59
+
60
+
61
+ - 构建打包
62
+
63
+ - `electornup build`
64
+
65
+ - 指定配置文件 `electornup build [config file]`
66
+
67
+ - 开启选项式构建 `electornup build -o` 或 `electornup build --option`
68
+
69
+ - 输出选项默认当前平台架构
70
+
71
+ - 指定平台 `--win` ,`--mac` ,`--linux`
72
+
73
+ - 指定架构 `--ia32` ,`--x64` ,`--arm64`
74
+
75
+ - 可不指定平台架构版本, 默认为本机当前平台架构版本
76
+
77
+ ## 内置的依赖
78
+
79
+ > 部分依赖已内置 无需重复安装 (开发此脚手架的目的也是给项目 package 瘦瘦身)
80
+
81
+ ```json
82
+ {
83
+ "dependencies": {
84
+ "@quiteer/resolve-config": "workspace:^0.0.4",
85
+ "cac": "^6.7.14",
86
+ "dotenv": "^16.0.3",
87
+ "electron-builder": "^23.6.0",
88
+ "fs-extra": "^10.1.0",
89
+ "inquirer": "8.2.5",
90
+ "portfinder": "^1.0.32",
91
+ "rimraf": "^3.0.2",
92
+ "tsup": "^6.7.0",
93
+ "typescript": "^5.0.4",
94
+ "vite": "^4.3.1",
95
+ "yaml": "^2.2.1"
96
+ }
97
+ }
98
+ ```
99
+
100
+ ### cli配置项
101
+
102
+ ```ts
103
+ import type { Configuration } from 'electron-builder'
104
+ import type { AliasOptions, PluginOption, ResolveOptions, UserConfig } from 'vite'
105
+ import type { Options } from 'tsup'
106
+
107
+ interface ViteConfig {
108
+ resolve?: ResolveOptions & {
109
+ alias?: AliasOptions
110
+ }
111
+ plugins?: PluginOption[]
112
+ viteOptions?: Omit<UserConfig, 'plugins' | 'resolve' | 'publicDir'>
113
+ }
114
+
115
+ interface TsupConfig {
116
+ entry?: string[] | Record<string, string>
117
+ target?: string | string[]
118
+ minify?: boolean
119
+ external?: (string | RegExp)[]
120
+ noExternal?: (string | RegExp)[]
121
+ }
122
+
123
+ interface BuilderConfig extends Configuration { }
124
+
125
+ interface ElectronupConfig {
126
+ viteConfig?: ViteConfig
127
+ tsupConfig?: TsupConfig
128
+ preloadTsup?: Options | Options[]
129
+ builderConfig: BuilderConfig
130
+
131
+ /**
132
+ * 渲染进程入口目录
133
+ * @default 'render'
134
+ */
135
+ renderDir?: string
136
+
137
+ /**
138
+ * 主进程入口目录
139
+ * @default 'main'
140
+ */
141
+ mainDir?: string
142
+
143
+ /**
144
+ * 静态资源目录
145
+ * @default 'public'
146
+ */
147
+ publicDir?: string
148
+
149
+ /**
150
+ * 动态库目录
151
+ * @default 'lib'
152
+ */
153
+ libDir?: string
154
+
155
+ /**
156
+ * 资源构建输出目录
157
+ * @default 'dist/resource'
158
+ */
159
+ resourceDir?: string
160
+
161
+ /**
162
+ * electron-builder 输出目录
163
+ * @default 'dist/out'
164
+ */
165
+ outDir?: string
166
+ }
167
+
168
+ interface ConfigEnv {
169
+ command: 'build' | 'serve'
170
+ root: string
171
+ }
172
+
173
+ type ElectronupConfigFn = (env: ConfigEnv) => ElectronupConfig
174
+ type UserElectronupConfig = ElectronupConfig | ElectronupConfigFn
175
+
176
+ declare const defineConfig: (config: UserElectronupConfig) => UserElectronupConfig
177
+
178
+ export { BuilderConfig, ConfigEnv, ElectronupConfig, TsupConfig, ViteConfig, defineConfig }
179
+ ```
180
+
181
+ #### 获取类型提示
182
+
183
+ 引入导出的 api 即可获取类型提示
184
+
185
+ ## 环境变量配置
186
+
187
+ 根目录下的 `.env` 文件在任何环境下都会注入到业务代码中
188
+
189
+ ### command
190
+
191
+ 目前 `command` 命令仅支持 `dev` 及 `build`
192
+
193
+ `dev` 指令下向 `process.env` 中注入 `NODE_ENV` 字段 为 `develpoment`
194
+
195
+ `build` 指令下向 `process.env` 中注入 `NODE_ENV` 字段 为 `production`
196
+
197
+ 双进程下区分环境略有不同 , 主进程代码中直接访问 `process.env` 上的 `NODE_ENV` 即可,渲染进程中访问 `vite` 提供的 `import.meta.env` 获取
198
+
199
+ ```typescript
200
+ // main.ts
201
+ // dev
202
+ console.log('NODE_ENV', process.env.NODE_ENV) // NODE_ENV development
203
+
204
+ // build
205
+ console.log('NODE_ENV', process.env.NODE_ENV) // NODE_ENV production
206
+ ```
207
+
208
+ ```typescript
209
+ // render.ts
210
+ // dev
211
+ console.log('isDev', import.meta.env.DEV) // isDev true
212
+
213
+ // build
214
+ console.log('isDev', import.meta.env.DEV) // isDev fasle
215
+ ```
216
+
217
+
218
+
219
+ ### mode
220
+
221
+ 通过添加命令行指令 `-m xxx | -mode xxx` 指定加载环境变量文件以满足不同环境下的不同环境变量。
222
+
223
+ `mode` 指令传入的 `mode` 字符串需与根目录下的 `env` 文件一致。 规则如下
224
+
225
+ ```shell
226
+ electronup -m test
227
+ ```
228
+
229
+ 命令行运行后根据 `mode` 参数加载根目录下的 `.env.test` 环境变量文件
230
+
231
+ 即 `electronup -m test`
232
+
233
+ `.env`
234
+
235
+ `.env.test`
236
+
237
+ 不传 mode 参数,默认根据环境区分加载环境变量。
238
+
239
+ `electronup dev`
240
+
241
+ `.env`
242
+
243
+ `.env.devlepoment`
244
+
245
+ `electronup build`
246
+
247
+ `.env`
248
+
249
+ `.env.production`
250
+
251
+ 当然也可通过传入 mode 参数使 dev 环境加载 production 的环境变量。
252
+
253
+ `electronup dev -m production`
254
+
255
+
256
+
257
+ #### 文件命名
258
+
259
+ `.env`
260
+
261
+ `.env.development`
262
+
263
+ `.env.production`
264
+
265
+ `.env.test`
266
+
267
+ `.env.staging`
268
+
269
+
270
+
271
+ ### 预置配置
272
+
273
+ 可自定义配置覆盖默认配置。
274
+
275
+ #### vite
276
+
277
+ - `base` : `./`
278
+
279
+ - `mode` : `development` | `production`
280
+
281
+ - `root` : `render`
282
+
283
+ - `publicDir` : `public`
284
+
285
+ - `server` : `{ host: '0.0.0.0' }`
286
+
287
+ - `build` :
288
+
289
+ ```ts
290
+ {
291
+ outDir: 'dist',
292
+ target: 'esnext',
293
+ minify: true,
294
+ reportCompressedSize: false,
295
+ emptyOutDir: true
296
+ }
297
+ ```
298
+
299
+ 所有选项皆可通过 `viteOptions` 覆盖
300
+
301
+ #### tsup配置选项
302
+
303
+ - `external` : `['electorn']`
304
+ - `target` : `node14`
305
+ - `noExternal` : `[]`
306
+
307
+ ##### 路径别名
308
+
309
+ 直接在项目中配置 `tsconfig.json` 的 `paths` 选项即可在主进程代码中使用路径别名。
310
+
311
+ #### electron-builder
312
+
313
+ 可全部覆盖,默认配置如下:
314
+
315
+ ```typescript
316
+ {
317
+ asar: true,
318
+ appId: 'org.quiteer.electronup',
319
+ productName: packages.name,
320
+ protocols: {
321
+ name: packages.name,
322
+ schemes: ['deeplink']
323
+ },
324
+ nsis: {
325
+ oneClick: false,
326
+ language: '2052',
327
+ perMachine: true,
328
+ allowElevation: true,
329
+ allowToChangeInstallationDirectory: true,
330
+ runAfterFinish: true,
331
+ createDesktopShortcut: true,
332
+ createStartMenuShortcut: true,
333
+ artifactName: `${packages.name} \${arch} Setup ${packages.version}.\${ext}`
334
+ },
335
+ files: [`${allConfig.resourceDir || DefaultDirs.resourceDir}/**/*`],
336
+ extraFiles: [allConfig.libDir || DefaultDirs.libDir],
337
+ directories: {
338
+ output: allConfig.outDir || config.directories?.output || DefaultDirs.outDir
339
+ },
340
+ // ...传入同名参数即可完成覆盖
341
+ }
342
+ ```
343
+
344
+ ## 示例
345
+
346
+ > https://github.com/TaiAiAc/electron-quiet-monorepo/tree/main/experiment/electronup-test
@@ -0,0 +1,512 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __commonJS = (cb, mod) => function __require() {
11
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __publicField = (obj, key, value) => {
30
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
31
+ return value;
32
+ };
33
+
34
+ // node_modules/.pnpm/electron@22.3.14/node_modules/electron/index.js
35
+ var require_electron = __commonJS({
36
+ "node_modules/.pnpm/electron@22.3.14/node_modules/electron/index.js"(exports, module2) {
37
+ var fs = require("fs");
38
+ var path2 = require("path");
39
+ var pathFile = path2.join(__dirname, "path.txt");
40
+ function getElectronPath() {
41
+ let executablePath;
42
+ if (fs.existsSync(pathFile)) {
43
+ executablePath = fs.readFileSync(pathFile, "utf-8");
44
+ }
45
+ if (process.env.ELECTRON_OVERRIDE_DIST_PATH) {
46
+ return path2.join(process.env.ELECTRON_OVERRIDE_DIST_PATH, executablePath || "electron");
47
+ }
48
+ if (executablePath) {
49
+ return path2.join(__dirname, "dist", executablePath);
50
+ } else {
51
+ throw new Error("Electron failed to install correctly, please delete node_modules/electron and try installing again");
52
+ }
53
+ }
54
+ module2.exports = getElectronPath();
55
+ }
56
+ });
57
+
58
+ // src/cli.ts
59
+ var import_path6 = require("path");
60
+ var import_cac = require("cac");
61
+ var import_rimraf = require("rimraf");
62
+
63
+ // package.json
64
+ var version = "0.0.1";
65
+
66
+ // src/transform/getConfig.ts
67
+ var import_path = require("path");
68
+ var import_fs_extra = require("fs-extra");
69
+ var import_parser_config = require("@quiteer/parser-config");
70
+
71
+ // src/utils/store.ts
72
+ var _Store = class {
73
+ command;
74
+ config;
75
+ mode;
76
+ minify;
77
+ port;
78
+ option;
79
+ win;
80
+ mac;
81
+ linux;
82
+ ia32;
83
+ x64;
84
+ arm64;
85
+ armv7l;
86
+ universal;
87
+ dir;
88
+ asar;
89
+ targets;
90
+ static getInstance() {
91
+ if (this.instance)
92
+ return this.instance;
93
+ return this.instance = new _Store();
94
+ }
95
+ get root() {
96
+ return process.cwd();
97
+ }
98
+ get isWin() {
99
+ return process.platform === "win32";
100
+ }
101
+ get isMac() {
102
+ return process.platform === "darwin";
103
+ }
104
+ get isLinux() {
105
+ return process.platform === "linux";
106
+ }
107
+ get currentArch() {
108
+ return process.arch;
109
+ }
110
+ };
111
+ var Store = _Store;
112
+ __publicField(Store, "instance");
113
+ var store = Store.getInstance();
114
+
115
+ // src/transform/getConfig.ts
116
+ var NOT_FOUND = "\u627E\u4E0D\u5230 electronup.config.ts | electronup.config.js | electronup.config.json , \u8BF7\u5728\u6839\u76EE\u5F55\u4E0B\u6DFB\u52A0\u914D\u7F6E\u6587\u4EF6 , \u6216\u663E\u5F0F\u7684\u6307\u5B9A\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84\uFF08\u76F8\u5BF9\u4E8E\u6839\u76EE\u5F55\uFF09";
117
+ var PARSING_FAILED = "\u627E\u5230\u4E86\u914D\u7F6E\u6587\u4EF6,\u4F46\u89E3\u6790\u914D\u7F6E\u6587\u4EF6\u5931\u8D25\uFF01";
118
+ var configPath = async (filePath) => {
119
+ const { root } = store;
120
+ if (filePath)
121
+ return (0, import_path.join)(root, filePath);
122
+ const configList = ["ts", "mjs", "cjs", "js"].map((suffix) => `${(0, import_path.join)(root, "electronup.config")}.${suffix}`);
123
+ const index = (await Promise.all(configList.map((path2) => (0, import_fs_extra.pathExists)(path2)))).findIndex((flag) => flag);
124
+ if (index > -1)
125
+ return configList[index];
126
+ throw new Error(NOT_FOUND);
127
+ };
128
+ var getConfig = async (filePath) => {
129
+ const path2 = await configPath(filePath);
130
+ try {
131
+ const option = await (0, import_parser_config.parserConfig)(path2, "electronup.config");
132
+ return option;
133
+ } catch (error) {
134
+ console.error("error :>> ", error);
135
+ throw new Error(PARSING_FAILED);
136
+ }
137
+ };
138
+
139
+ // src/configs/builder.ts
140
+ var import_path2 = require("path");
141
+ var import_fs_extra2 = require("fs-extra");
142
+ async function getBuilderConfig(config, allConfig) {
143
+ const packages = await (0, import_fs_extra2.readJSON)((0, import_path2.resolve)(store.root, "package.json"));
144
+ const outConfig = {
145
+ targets: store.targets,
146
+ x64: store.x64,
147
+ ia32: store.ia32,
148
+ armv7l: store.armv7l,
149
+ arm64: store.arm64,
150
+ universal: store.universal,
151
+ dir: store.dir
152
+ };
153
+ const defaultConfig = {
154
+ config: {
155
+ asar: store.asar,
156
+ appId: "org.quiteer.electronup",
157
+ productName: packages.name,
158
+ protocols: {
159
+ name: packages.name,
160
+ schemes: ["deeplink"]
161
+ },
162
+ nsis: {
163
+ oneClick: false,
164
+ language: "2052",
165
+ perMachine: true,
166
+ allowElevation: true,
167
+ allowToChangeInstallationDirectory: true,
168
+ runAfterFinish: true,
169
+ createDesktopShortcut: true,
170
+ createStartMenuShortcut: true,
171
+ artifactName: `${packages.name} \${arch} Setup ${packages.version}.\${ext}`
172
+ },
173
+ files: [`${allConfig.resourceDir || "dist/resource" /* resourceDir */}/**/*`],
174
+ extraFiles: [allConfig.libDir || "lib" /* libDir */],
175
+ directories: {
176
+ output: allConfig.outDir || config.directories?.output || "dist/out" /* outDir */
177
+ },
178
+ ...config
179
+ }
180
+ };
181
+ return { ...defaultConfig, ...outConfig };
182
+ }
183
+
184
+ // src/configs/tsup.ts
185
+ var import_path3 = require("path");
186
+ var import_child_process = require("child_process");
187
+ var import_dotenv = require("dotenv");
188
+ var import_electron = __toESM(require_electron());
189
+ var defaultEnvPath = (0, import_path3.resolve)(store.root, ".env");
190
+ var { parsed: defaultEnv } = (0, import_dotenv.config)({ path: defaultEnvPath });
191
+ var getModeDev = () => {
192
+ const path2 = `${defaultEnvPath}.${store.mode}`;
193
+ const { parsed, error } = (0, import_dotenv.config)({ path: path2 });
194
+ if (error)
195
+ throw new Error(`\u672A\u80FD\u52A0\u8F7D .env.${store.mode} \u4E0B\u7684\u73AF\u5883\u53D8\u91CF,\u68C0\u67E5\u6587\u4EF6\u662F\u5426\u5B58\u5728\uFF01`);
196
+ return {
197
+ ...defaultEnv,
198
+ ...parsed
199
+ };
200
+ };
201
+ var injectEnv = () => {
202
+ const { command, port } = store;
203
+ const env = getModeDev();
204
+ if (command === "serve") {
205
+ return {
206
+ ...env,
207
+ NODE_ENV: "development",
208
+ RENDER_PORT: String(port)
209
+ };
210
+ }
211
+ return {
212
+ ...env,
213
+ NODE_ENV: "production"
214
+ };
215
+ };
216
+ function getTsupConfig(config, allConfig) {
217
+ const { command, root, minify } = store;
218
+ const isServe = command === "serve";
219
+ const userConfig = {
220
+ minify: minify === false ? false : isServe,
221
+ ...config
222
+ };
223
+ const defaultConfig = {
224
+ external: ["electron", ...config.external ?? []],
225
+ noExternal: config.noExternal,
226
+ entry: { electron: (0, import_path3.resolve)(root, allConfig.mainDir || "main" /* mainDir */, "index.ts") },
227
+ outDir: allConfig.resourceDir || "dist/resource" /* resourceDir */,
228
+ watch: isServe,
229
+ dts: false,
230
+ clean: false,
231
+ env: injectEnv(),
232
+ async onSuccess() {
233
+ if (isServe)
234
+ return startElectron((0, import_path3.resolve)(root, allConfig.resourceDir || "dist/resource" /* resourceDir */, "electron.js"));
235
+ }
236
+ };
237
+ config.target && (defaultConfig.target = config.target);
238
+ return { ...userConfig, ...defaultConfig };
239
+ }
240
+ var electronProcess;
241
+ var manualRestart = false;
242
+ function startElectron(mainPath) {
243
+ if (electronProcess) {
244
+ manualRestart = true;
245
+ electronProcess.pid && process.kill(electronProcess.pid);
246
+ electronProcess = null;
247
+ setTimeout(() => {
248
+ manualRestart = false;
249
+ }, 5e3);
250
+ }
251
+ electronProcess = (0, import_child_process.spawn)(import_electron.default, [mainPath, "--inspect=9528"]);
252
+ electronProcess.stdout.on("data", removeJunk);
253
+ electronProcess.stderr.on("data", removeJunk);
254
+ electronProcess.on("close", () => {
255
+ manualRestart || process.exit();
256
+ });
257
+ }
258
+ function removeJunk(chunk) {
259
+ if (/\d+-\d+-\d+ \d+:\d+:\d+\.\d+ Electron(?: Helper)?\[\d+:\d+] /.test(chunk))
260
+ return false;
261
+ if (/\[\d+:\d+\/|\d+\.\d+:ERROR:CONSOLE\(\d+\)\]/.test(chunk))
262
+ return false;
263
+ if (/ALSA lib [a-z]+\.c:\d+:\([a-z_]+\)/.test(chunk))
264
+ return false;
265
+ const data = chunk.toString().split(/\r?\n/);
266
+ let log = "";
267
+ data.forEach((line) => {
268
+ log += ` ${line}
269
+ `;
270
+ });
271
+ console.info(log);
272
+ }
273
+
274
+ // src/configs/vite.ts
275
+ var import_path4 = require("path");
276
+ function getViteConfig(config, allConfig) {
277
+ const { root, minify } = store;
278
+ const defaultConfig = {
279
+ base: "./",
280
+ root: allConfig.renderDir || "render" /* renderDir */,
281
+ server: { host: "0.0.0.0" },
282
+ build: {
283
+ outDir: (0, import_path4.resolve)(root, allConfig.resourceDir || "dist/resource" /* resourceDir */),
284
+ target: "esnext",
285
+ minify: minify && "esbuild",
286
+ reportCompressedSize: false,
287
+ emptyOutDir: true,
288
+ ...config?.build
289
+ },
290
+ ...config.viteOptions
291
+ };
292
+ config?.resolve && (defaultConfig.resolve = config.resolve);
293
+ config?.plugins && (defaultConfig.plugins = config.plugins);
294
+ defaultConfig.publicDir = (0, import_path4.resolve)(root, allConfig.publicDir || "public" /* publicDir */);
295
+ return defaultConfig;
296
+ }
297
+
298
+ // src/configs/electronup.ts
299
+ async function getElectronupConfig(config) {
300
+ const { viteConfig, tsupConfig, preloadTsup, builderConfig, ...dirConfig } = config;
301
+ const vite = getViteConfig(viteConfig || {}, config);
302
+ const tsup = getTsupConfig(tsupConfig || {}, config);
303
+ const initConfig = { vite, tsup };
304
+ if (store.command === "build") {
305
+ const builder2 = await getBuilderConfig(builderConfig, config);
306
+ initConfig.builder = builder2;
307
+ }
308
+ preloadTsup && (initConfig.preload = preloadTsup);
309
+ return { ...dirConfig, ...initConfig };
310
+ }
311
+
312
+ // src/transform/getExportConfig.ts
313
+ var exportElectronupConfig = (config) => {
314
+ const typeStr = typeof config;
315
+ if (typeStr === "function") {
316
+ const option = config({ command: store.command, root: store.root });
317
+ return option;
318
+ }
319
+ if (typeStr === "object")
320
+ return config;
321
+ throw new Error("electronup \u914D\u7F6E\u9519\u8BEF,\u89E3\u6790\u5931\u8D25\uFF01");
322
+ };
323
+ var electronupConfig = (config) => getElectronupConfig(exportElectronupConfig(config));
324
+
325
+ // src/runner/watch.ts
326
+ var import_portfinder = require("portfinder");
327
+ var import_vite2 = require("vite");
328
+ var import_tsup2 = require("tsup");
329
+ async function watch(options) {
330
+ const p = await (0, import_portfinder.getPortPromise)({
331
+ port: Number(store.port)
332
+ });
333
+ store.port = p;
334
+ const initConfig = await electronupConfig(options);
335
+ const viteDevServer = await (0, import_vite2.createServer)({ configFile: false, ...initConfig.vite });
336
+ viteDevServer.listen(p).then(viteDevServer.printUrls);
337
+ (0, import_tsup2.build)(initConfig.tsup);
338
+ }
339
+
340
+ // src/runner/build.ts
341
+ var import_path5 = __toESM(require("path"));
342
+ var import_electron_builder = require("electron-builder");
343
+ var import_vite3 = require("vite");
344
+ var import_tsup3 = require("tsup");
345
+ var import_inquirer = __toESM(require("inquirer"));
346
+ var import_yaml = require("yaml");
347
+ var import_fs_extra3 = require("fs-extra");
348
+ async function build2(options) {
349
+ if (store.option) {
350
+ const { isMinify } = await import_inquirer.default.prompt([{ type: "confirm", name: "isMinify", message: "\u662F\u5426\u538B\u7F29\u4EE3\u7801?" }]).catch((err) => {
351
+ console.error("err: ", err);
352
+ process.exit(1);
353
+ });
354
+ store.minify = isMinify;
355
+ const { isPackage } = await import_inquirer.default.prompt([{ type: "confirm", name: "isPackage", message: "\u662F\u5426\u751F\u6210\u5B89\u88C5\u5305?" }]).catch((err) => {
356
+ console.error("err: ", err);
357
+ process.exit(1);
358
+ });
359
+ store.dir = !isPackage;
360
+ if (store.isWin) {
361
+ if (store.currentArch === "ia32")
362
+ store.ia32 = true;
363
+ if (store.currentArch === "x64") {
364
+ const { pattern } = await import_inquirer.default.prompt([
365
+ {
366
+ type: "checkbox",
367
+ name: "pattern",
368
+ message: "\u8BF7\u9009\u62E9\u6784\u5EFA\u6A21\u5F0F , \u8DF3\u8FC7\u9009\u62E9\u4E3A\u5F53\u524D\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0 ~",
369
+ pageSize: 10,
370
+ choices: [
371
+ { name: "win-x64", value: "x64" },
372
+ { name: "win-ia32", value: "ia32" }
373
+ ]
374
+ }
375
+ ]).catch((err) => {
376
+ console.error("err: ", err);
377
+ process.exit(1);
378
+ });
379
+ if (pattern.length) {
380
+ pattern.forEach((arch) => {
381
+ store[arch] = true;
382
+ });
383
+ }
384
+ }
385
+ store.dir = !isPackage;
386
+ }
387
+ if (store.isMac) {
388
+ const { outPlatform } = await import_inquirer.default.prompt([
389
+ {
390
+ type: "list",
391
+ name: "outPlatform",
392
+ message: "\u8BF7\u9009\u62E9\u6784\u5EFA\u5E73\u53F0 , \u8DF3\u8FC7\u9009\u62E9\u4E3A\u5F53\u524D\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0 ~",
393
+ pageSize: 10,
394
+ choices: [
395
+ { name: "win", value: "win" },
396
+ { name: "mac", value: "mac" },
397
+ { name: "linux", value: "linux" }
398
+ ]
399
+ }
400
+ ]).catch((err) => {
401
+ console.error("err: ", err);
402
+ process.exit(1);
403
+ });
404
+ if (outPlatform === "mac") {
405
+ const { pattern } = await import_inquirer.default.prompt([
406
+ {
407
+ type: "checkbox",
408
+ name: "pattern",
409
+ message: "\u8BF7\u9009\u62E9\u6784\u5EFA\u6A21\u5F0F , \u8DF3\u8FC7\u9009\u62E9\u4E3A\u5F53\u524D\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0 ~",
410
+ pageSize: 10,
411
+ choices: [
412
+ { name: "mac-x64", value: import_electron_builder.Arch.x64 },
413
+ { name: "mac-arm64", value: import_electron_builder.Arch.arm64 }
414
+ ]
415
+ }
416
+ ]).catch((err) => {
417
+ console.error("err: ", err);
418
+ process.exit(1);
419
+ });
420
+ const archList = [];
421
+ pattern.length ? archList.push(...pattern) : archList.push(import_electron_builder.Arch[store.currentArch]);
422
+ store.targets = import_electron_builder.Platform.MAC.createTarget(!isPackage ? "dir" : null, ...archList);
423
+ }
424
+ if (outPlatform === "win") {
425
+ const { pattern } = await import_inquirer.default.prompt([
426
+ {
427
+ type: "checkbox",
428
+ name: "pattern",
429
+ message: "\u8BF7\u9009\u62E9\u6784\u5EFA\u6A21\u5F0F , \u8DF3\u8FC7\u9009\u62E9\u4E3A\u5F53\u524D\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0 ~",
430
+ pageSize: 10,
431
+ choices: [
432
+ { name: "win-x64", value: import_electron_builder.Arch.x64 },
433
+ { name: "win-ia32", value: import_electron_builder.Arch.ia32 }
434
+ ]
435
+ }
436
+ ]).catch((err) => {
437
+ console.error("err: ", err);
438
+ process.exit(1);
439
+ });
440
+ const archList = [];
441
+ pattern.length ? archList.push(...pattern) : archList.push(import_electron_builder.Arch.x64);
442
+ store.targets = import_electron_builder.Platform.WINDOWS.createTarget(!isPackage ? "dir" : null, ...archList);
443
+ }
444
+ if (outPlatform === "linux")
445
+ store.targets = import_electron_builder.Platform.LINUX.createTarget(!isPackage ? "dir" : null, import_electron_builder.Arch.armv7l);
446
+ }
447
+ if (store.isLinux) {
448
+ }
449
+ }
450
+ const initConfig = await electronupConfig(options);
451
+ await (0, import_vite3.build)(initConfig.vite);
452
+ await (0, import_tsup3.build)(initConfig.tsup);
453
+ await (0, import_electron_builder.build)(initConfig.builder);
454
+ await (0, import_fs_extra3.writeFile)(
455
+ import_path5.default.resolve(store.root, options.outDir || "dist/out" /* outDir */, "electronup-effective-config.yaml"),
456
+ (0, import_yaml.stringify)(JSON.parse(JSON.stringify(initConfig)))
457
+ );
458
+ }
459
+
460
+ // src/cli.ts
461
+ var cli = (0, import_cac.cac)("electronup");
462
+ cli.option("-m , --mode <mode>", "[development | production | test | staging | ...] \u73AF\u5883\u6A21\u5F0F ");
463
+ cli.option("--no-minify", "\u4F7F\u4E3B\u8FDB\u7A0B\u548C\u6E32\u67D3\u8FDB\u7A0B\u4EE3\u7801\u538B\u7F29 ");
464
+ cli.command("[config-file]", "start dev server").alias("dev").option("-p , --port <port>", "[number] \u6E32\u67D3\u8FDB\u7A0B\u7684\u7AEF\u53E3\u53F7 \uFF0C\u5982\u679C\u5360\u7528\u4F1A\u5207\u6362\u975E\u5360\u7528\u7684\u7AEF\u53E3 ").action(async (configFile, options) => {
465
+ const { mode, port, minify } = options;
466
+ const option = await getConfig(configFile);
467
+ (0, import_rimraf.sync)((0, import_path6.resolve)(store.root, option.resourceDir || "dist/resource" /* resourceDir */));
468
+ (0, import_rimraf.sync)((0, import_path6.resolve)(store.root, option.outDir || "dist/out" /* outDir */));
469
+ store.command = "serve";
470
+ store.mode = mode || "development";
471
+ store.port = port || 8090;
472
+ store.minify = !!minify;
473
+ watch(option);
474
+ });
475
+ cli.command("build [root]", "\u6784\u5EFA").option("-o , --option", "\u81EA\u5B9A\u4E49 , \u81EA\u5B9A\u4E49\u6784\u5EFA\u9009\u9879 ").option("--dir", "\u53EA\u751F\u6210\u76EE\u5F55").option("--no-asar", "asar false").option("--win", " \u6784\u5EFA win \u4E0B\u8F93\u51FA\u5305").option("--mac", " \u6784\u5EFA mac \u4E0B\u8F93\u51FA\u5305").option("--linux", " \u6784\u5EFA linux \u8F93\u51FA\u5305").option("--ia32", " \u6784\u5EFA ia32 \u5E73\u53F0\u5305").option("--x64", " \u6784\u5EFA x64 \u5E73\u53F0\u5305").option("--arm64", " \u6784\u5EFA arm64 \u5E73\u53F0\u5305").option("--armv7l", " \u6784\u5EFA armv7l \u5E73\u53F0\u5305").option("--universal", " \u6784\u5EFA universal \u5E73\u53F0\u5305").action(async (configFile, options) => {
476
+ const {
477
+ mode,
478
+ minify,
479
+ option,
480
+ win,
481
+ mac,
482
+ linux,
483
+ ia32,
484
+ x64,
485
+ arm64,
486
+ armv7l,
487
+ universal,
488
+ dir,
489
+ asar
490
+ } = options;
491
+ const configOption = await getConfig(configFile);
492
+ (0, import_rimraf.sync)((0, import_path6.resolve)(store.root, configOption.resourceDir || "dist/resource" /* resourceDir */));
493
+ (0, import_rimraf.sync)((0, import_path6.resolve)(store.root, configOption.outDir || "dist/out" /* outDir */));
494
+ store.command = "build";
495
+ store.mode = mode || "production";
496
+ store.minify = minify;
497
+ store.option = !!option;
498
+ store.dir = !!dir;
499
+ store.asar = asar;
500
+ store.win = !!win;
501
+ store.mac = !!mac;
502
+ store.linux = !!linux;
503
+ store.ia32 = !!ia32;
504
+ store.x64 = !!x64;
505
+ store.arm64 = !!arm64;
506
+ store.armv7l = !!armv7l;
507
+ store.universal = !!universal;
508
+ build2(configOption);
509
+ });
510
+ cli.help();
511
+ cli.version(version);
512
+ cli.parse();
@@ -0,0 +1,75 @@
1
+ import { Configuration } from 'electron-builder';
2
+ import { ResolveOptions, AliasOptions, PluginOption, UserConfig } from 'vite';
3
+ import { Options } from 'tsup';
4
+
5
+ interface ViteConfig {
6
+ resolve?: ResolveOptions & {
7
+ alias?: AliasOptions;
8
+ }
9
+ plugins?: PluginOption[]
10
+ build?:UserConfig['build']
11
+ viteOptions?: Omit<UserConfig, 'plugins' | 'resolve' | 'publicDir'| 'build'>
12
+ }
13
+
14
+ interface TsupConfig {
15
+ target?: Options['target'];
16
+ external?: (string | RegExp)[];
17
+ noExternal?: (string | RegExp)[];
18
+ }
19
+
20
+ interface BuilderConfig extends Configuration { }
21
+
22
+ interface ElectronupConfig {
23
+ viteConfig?: ViteConfig
24
+ tsupConfig?: TsupConfig
25
+ preloadTsup?: Options | Options[]
26
+ builderConfig: BuilderConfig
27
+
28
+ /**
29
+ * 渲染进程入口目录
30
+ * @default 'render'
31
+ */
32
+ renderDir?: string
33
+
34
+ /**
35
+ * 主进程入口目录
36
+ * @default 'main'
37
+ */
38
+ mainDir?: string
39
+
40
+ /**
41
+ * 静态资源目录
42
+ * @default 'public'
43
+ */
44
+ publicDir?: string
45
+
46
+ /**
47
+ * 动态库目录
48
+ * @default 'lib'
49
+ */
50
+ libDir?: string
51
+
52
+ /**
53
+ * 资源构建输出目录
54
+ * @default 'dist'
55
+ */
56
+ resourceDir?: string
57
+
58
+ /**
59
+ * electron-builder 输出目录
60
+ * @default 'out'
61
+ */
62
+ outDir?: string
63
+ }
64
+
65
+ interface ConfigEnv {
66
+ command: 'build' | 'serve'
67
+ root: string
68
+ }
69
+
70
+ type ElectronupConfigFn = (env: ConfigEnv) => ElectronupConfig
71
+ type UserElectronupConfig = ElectronupConfig | ElectronupConfigFn
72
+
73
+ declare const defineConfig: (config: UserElectronupConfig) => UserElectronupConfig;
74
+
75
+ export { BuilderConfig, ConfigEnv, ElectronupConfig, TsupConfig, ViteConfig, defineConfig };
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ defineConfig: () => defineConfig
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+ var defineConfig = (config) => config;
27
+ // Annotate the CommonJS export names for ESM import in node:
28
+ 0 && (module.exports = {
29
+ defineConfig
30
+ });
@@ -0,0 +1,5 @@
1
+ // src/index.ts
2
+ var defineConfig = (config) => config;
3
+ export {
4
+ defineConfig
5
+ };
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "electronup",
3
+ "version": "0.0.1",
4
+ "description": "融合构建 electron 应用需要的构建工具,保留原有配置习惯的命令行工具 ",
5
+ "author": "quiteer",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/QuiteerJs/electronup#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/QuiteerJs/electronup.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/QuiteerJs/electronup/issues"
14
+ },
15
+ "keywords": [
16
+ "vite",
17
+ "tsup",
18
+ "electron-builder",
19
+ "electron"
20
+ ],
21
+ "publishConfig": {
22
+ "access": "public",
23
+ "registry": "https://registry.npmjs.org/"
24
+ },
25
+ "main": "dist/client/index.js",
26
+ "module": "dist/client/index.mjs",
27
+ "types": "dist/client/index.d.ts",
28
+ "bin": {
29
+ "electronup": "dist/bin/electronup.js"
30
+ },
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "scripts": {
35
+ "dev": "tsup --watch",
36
+ "build": "tsup",
37
+ "update-version": "bumpp package.json"
38
+ },
39
+ "peerDependencies": {
40
+ "@types/node": ">= 16",
41
+ "node": ">= 16",
42
+ "vue": ">= 3"
43
+ },
44
+ "dependencies": {
45
+ "@quiteer/parser-config": "^1.0.3",
46
+ "cac": "^6.7.14",
47
+ "dotenv": "^16.0.3",
48
+ "electron-builder": "^23.6.0",
49
+ "fs-extra": "^10.1.0",
50
+ "inquirer": "8.2.5",
51
+ "portfinder": "^1.0.32",
52
+ "rimraf": "^3.0.2",
53
+ "tsup": "^6.7.0",
54
+ "typescript": "^5.0.4",
55
+ "vite": "^4.3.1",
56
+ "yaml": "^2.2.1"
57
+ },
58
+ "devDependencies": {
59
+ "@quiteer/eslint-config": "^0.0.3",
60
+ "@quiteer/ts-config": "^0.0.6",
61
+ "@types/fs-extra": "^9.0.13",
62
+ "@types/inquirer": "^9.0.3",
63
+ "@types/node": "^20.3.2",
64
+ "@types/rimraf": "^3.0.2",
65
+ "bumpp": "^9.1.1",
66
+ "electron": "^22.3.6"
67
+ }
68
+ }