create-electron-base 0.1.4 → 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 +29 -0
- package/bin/index.mjs +91 -6
- package/package.json +2 -1
- package/src/generate.mjs +9 -1
- package/templates/app/electron/main.ts +19 -20
- package/templates/app/electron/modules/preferences.ts +33 -0
- package/templates/app/renderer/env.d.ts +1 -0
- package/templates/app/tsconfig.json +2 -3
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
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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.
|
|
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,7 +93,15 @@ export async function generate({ target, answers }) {
|
|
|
93
93
|
features,
|
|
94
94
|
update: { provider: 'generic', url: '' },
|
|
95
95
|
license: { publicKey: '' },
|
|
96
|
-
|
|
96
|
+
log: { level: 'info', maxDays: 7, console: true },
|
|
97
|
+
window: {
|
|
98
|
+
width: 1200,
|
|
99
|
+
height: 800,
|
|
100
|
+
minWidth: 900,
|
|
101
|
+
minHeight: 600,
|
|
102
|
+
remember: true,
|
|
103
|
+
ensureVisible: true,
|
|
104
|
+
},
|
|
97
105
|
}
|
|
98
106
|
|
|
99
107
|
await writeFile(
|
|
@@ -1,39 +1,38 @@
|
|
|
1
1
|
import { app, BrowserWindow, ipcMain } from 'electron'
|
|
2
2
|
import { join } from 'node:path'
|
|
3
|
-
import { bootstrap } 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
|
+
import { prefs } from './modules/preferences'
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
webPreferences: {
|
|
14
|
-
preload: join(__dirname, '../preload/preload.js'),
|
|
15
|
-
contextIsolation: true,
|
|
16
|
-
nodeIntegration: false,
|
|
17
|
-
sandbox: true,
|
|
18
|
-
},
|
|
8
|
+
let mainWindow: BrowserWindow | null = null
|
|
9
|
+
|
|
10
|
+
function openMainWindow() {
|
|
11
|
+
mainWindow = createWindow({
|
|
12
|
+
preload: join(__dirname, '../preload/preload.js'),
|
|
13
|
+
renderer: join(__dirname, '../renderer/index.html'),
|
|
19
14
|
})
|
|
20
15
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
win.loadFile(join(__dirname, '../renderer/index.html'))
|
|
25
|
-
}
|
|
16
|
+
mainWindow.on('closed', () => {
|
|
17
|
+
mainWindow = null
|
|
18
|
+
})
|
|
26
19
|
}
|
|
27
20
|
|
|
28
21
|
app.whenReady().then(() => {
|
|
29
22
|
bootstrap(config as AppConfig)
|
|
30
23
|
|
|
24
|
+
// 日志写到 <userData>/logs/<日期>.log,按模块分来源用 logger.child('window')
|
|
25
|
+
logger.info('应用启动', { version: app.getVersion() })
|
|
26
|
+
|
|
27
|
+
// 用 settings 存偏好,字段定义在 modules/preferences.ts
|
|
28
|
+
prefs.launchCount += 1
|
|
29
|
+
|
|
31
30
|
ipcMain.handle('app:version', () => app.getVersion())
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
openMainWindow()
|
|
34
33
|
|
|
35
34
|
app.on('activate', () => {
|
|
36
|
-
if (BrowserWindow.getAllWindows().length === 0)
|
|
35
|
+
if (BrowserWindow.getAllWindows().length === 0) openMainWindow()
|
|
37
36
|
})
|
|
38
37
|
})
|
|
39
38
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { settings } from '@thirty-coder/electron-base-kit'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 应用偏好,落在 userData/settings.json。
|
|
5
|
+
*
|
|
6
|
+
* 键名带 `prefs.` 前缀,默认值写在 get 的第二个参数上。
|
|
7
|
+
* 业务按需增删字段,主进程里直接 `prefs.xxx = 值`。
|
|
8
|
+
*/
|
|
9
|
+
export const prefs = {
|
|
10
|
+
/** 启动次数,示例用,不需要可以删 */
|
|
11
|
+
get launchCount(): number {
|
|
12
|
+
return settings.get('prefs.launchCount', 0)
|
|
13
|
+
},
|
|
14
|
+
set launchCount(value: number) {
|
|
15
|
+
settings.set('prefs.launchCount', value)
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
/** 上次打开的目录,接文件对话框时用 */
|
|
19
|
+
get lastDir(): string {
|
|
20
|
+
return settings.get('prefs.lastDir', '')
|
|
21
|
+
},
|
|
22
|
+
set lastDir(value: string) {
|
|
23
|
+
settings.set('prefs.lastDir', value)
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
/** 侧边栏是否收起 */
|
|
27
|
+
get sidebarCollapsed(): boolean {
|
|
28
|
+
return settings.get('prefs.sidebarCollapsed', false)
|
|
29
|
+
},
|
|
30
|
+
set sidebarCollapsed(value: boolean) {
|
|
31
|
+
settings.set('prefs.sidebarCollapsed', value)
|
|
32
|
+
},
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
@@ -11,10 +11,9 @@
|
|
|
11
11
|
"resolveJsonModule": true,
|
|
12
12
|
"isolatedModules": true,
|
|
13
13
|
"noEmit": true,
|
|
14
|
-
"baseUrl": ".",
|
|
15
14
|
"paths": {
|
|
16
|
-
"@/*": ["renderer/*"],
|
|
17
|
-
"@shared/*": ["shared/*"]
|
|
15
|
+
"@/*": ["./renderer/*"],
|
|
16
|
+
"@shared/*": ["./shared/*"]
|
|
18
17
|
},
|
|
19
18
|
"types": ["node"]
|
|
20
19
|
},
|