create-electron-base 0.2.0 → 0.3.0

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/README.md CHANGED
@@ -22,6 +22,35 @@ pnpm i
22
22
  pnpm dev
23
23
  ```
24
24
 
25
+ ## 非交互生成
26
+
27
+ ```bash
28
+ npx create-electron-base my-app --yes
29
+ ```
30
+
31
+ 全部用默认值,不提问:项目名取目录名,应用 ID 是 `com.example.<项目名>`,能力用默认组合,前端配套不装。
32
+
33
+ 需要指定时加参数:
34
+
35
+ ```bash
36
+ npx create-electron-base my-app --yes \
37
+ --app-id com.foo.bar \
38
+ --product-name "我的应用" \
39
+ --features tray,notify \
40
+ --frontend router,pinia
41
+ ```
42
+
43
+ | 参数 | 说明 |
44
+ | --- | --- |
45
+ | `--yes` | 不提问。不加就是交互式 |
46
+ | `--name` | 项目名,默认取目标目录名 |
47
+ | `--app-id` | 应用 ID,默认 `com.example.<项目名>` |
48
+ | `--product-name` | 产品名称,默认同项目名 |
49
+ | `--features` | 能力,逗号分隔。默认 `updater,notify,dialog,clipboard` |
50
+ | `--frontend` | 前端配套,逗号分隔。默认不装 |
51
+
52
+ 项目名只能用小写字母、数字和连字符。
53
+
25
54
  ## 生成后的结构
26
55
 
27
56
  ```
package/bin/index.mjs CHANGED
@@ -1,16 +1,101 @@
1
1
  #!/usr/bin/env node
2
- import { resolve } from 'node:path'
2
+ import { basename, resolve } from 'node:path'
3
3
  import { existsSync } from 'node:fs'
4
4
  import { ask } from '../src/prompts.mjs'
5
5
  import { generate } from '../src/generate.mjs'
6
+ import { capabilityOptions, defaultCapabilities, frontendOptions } from '../src/features.mjs'
6
7
 
7
- const targetArg = process.argv[2]
8
+ const NAME_RE = /^[a-z0-9-]+$/
9
+
10
+ /** 把 --key value / --key=value / -y 解析成对象,位置参数放 _ */
11
+ function parseArgs(argv) {
12
+ const out = { _: [] }
13
+
14
+ for (let i = 0; i < argv.length; i += 1) {
15
+ const arg = argv[i]
16
+
17
+ if (arg.startsWith('--')) {
18
+ const eq = arg.indexOf('=')
19
+ if (eq > -1) {
20
+ out[arg.slice(2, eq)] = arg.slice(eq + 1)
21
+ continue
22
+ }
23
+
24
+ const key = arg.slice(2)
25
+ const next = argv[i + 1]
26
+ if (next !== undefined && !next.startsWith('-')) {
27
+ out[key] = next
28
+ i += 1
29
+ } else {
30
+ out[key] = true
31
+ }
32
+ } else if (arg.startsWith('-')) {
33
+ out[arg.slice(1)] = true
34
+ } else {
35
+ out._.push(arg)
36
+ }
37
+ }
38
+
39
+ return out
40
+ }
41
+
42
+ function text(value) {
43
+ return typeof value === 'string' ? value : undefined
44
+ }
45
+
46
+ function pickList(raw, options, flag) {
47
+ const values = String(raw)
48
+ .split(',')
49
+ .map((item) => item.trim())
50
+ .filter(Boolean)
51
+ const allowed = new Set(options.map((option) => option.value))
52
+
53
+ for (const value of values) {
54
+ if (!allowed.has(value)) {
55
+ console.error(`${flag} 里有未知选项:${value}`)
56
+ console.error(`可选:${[...allowed].join(', ')}`)
57
+ process.exit(1)
58
+ }
59
+ }
60
+
61
+ return values
62
+ }
63
+
64
+ const args = parseArgs(process.argv.slice(2))
65
+ const targetArg = args._[0]
66
+ const nonInteractive = args.yes === true || args.y === true
8
67
 
9
68
  async function main() {
10
- const answers = await ask({ defaultName: targetArg ?? 'my-app' })
11
- if (!answers) {
12
- console.log('已取消')
13
- process.exit(0)
69
+ let answers
70
+
71
+ if (nonInteractive) {
72
+ const name = text(args.name) ?? (targetArg ? basename(resolve(targetArg)) : 'my-app')
73
+ if (!NAME_RE.test(name)) {
74
+ console.error(`项目名只能用小写字母、数字和连字符,收到:${name}`)
75
+ process.exit(1)
76
+ }
77
+
78
+ answers = {
79
+ name,
80
+ appId: text(args['app-id']) ?? `com.example.${name}`,
81
+ productName: text(args['product-name']) ?? name,
82
+ capabilities:
83
+ text(args.features) !== undefined
84
+ ? pickList(args.features, capabilityOptions, '--features')
85
+ : capabilityOptions
86
+ .filter((_, index) => defaultCapabilities.includes(index))
87
+ .map((option) => option.value),
88
+ frontend:
89
+ text(args.frontend) !== undefined
90
+ ? pickList(args.frontend, frontendOptions, '--frontend')
91
+ : [],
92
+ }
93
+ } else {
94
+ answers = await ask({ defaultName: targetArg ?? 'my-app' })
95
+ if (!answers) {
96
+ console.log('已取消')
97
+ process.exit(0)
98
+ }
14
99
  }
15
100
 
16
101
  // 传了命令行参数就用参数,否则用用户输入的项目名
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-electron-base",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "生成 Electron 桌面项目",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -20,6 +20,7 @@
20
20
  "node": ">=18"
21
21
  },
22
22
  "scripts": {
23
+ "demo": "node scripts/demo.mjs",
23
24
  "release": "node scripts/release.mjs"
24
25
  },
25
26
  "dependencies": {
package/src/generate.mjs CHANGED
@@ -93,6 +93,7 @@ export async function generate({ target, answers }) {
93
93
  features,
94
94
  update: { provider: 'generic', url: '' },
95
95
  license: { publicKey: '' },
96
+ log: { level: 'info', maxDays: 7, console: true },
96
97
  window: {
97
98
  width: 1200,
98
99
  height: 800,
@@ -1,6 +1,6 @@
1
1
  import { app, BrowserWindow, ipcMain } from 'electron'
2
2
  import { join } from 'node:path'
3
- import { bootstrap, createWindow } from '@thirty-coder/electron-base-kit'
3
+ import { bootstrap, createWindow, logger } from '@thirty-coder/electron-base-kit'
4
4
  import type { AppConfig } from '@thirty-coder/electron-base-kit'
5
5
  import config from '../app.config.json'
6
6
  import { prefs } from './modules/preferences'
@@ -21,6 +21,9 @@ function openMainWindow() {
21
21
  app.whenReady().then(() => {
22
22
  bootstrap(config as AppConfig)
23
23
 
24
+ // 日志写到 <userData>/logs/<日期>.log,按模块分来源用 logger.child('window')
25
+ logger.info('应用启动', { version: app.getVersion() })
26
+
24
27
  // 用 settings 存偏好,字段定义在 modules/preferences.ts
25
28
  prefs.launchCount += 1
26
29