generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 161
/
index.ts
294 lines (273 loc) · 8.72 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { platform } from 'os'
import { readFileSync, existsSync, copyFileSync, writeFileSync } from 'fs'
import execa from 'execa'
import { parse as parseToml } from '@iarna/toml'
import { join } from 'path'
export function getPackageJson(root: string): any {
const packageJsonPath = join(root, 'package.json')
if (existsSync(packageJsonPath)) {
const packageJsonString = readFileSync(packageJsonPath).toString()
const packageJson = JSON.parse(packageJsonString)
return packageJson
}
return null
}
function hasDependency(dependencyName: string, root: string): boolean {
const packageJson = getPackageJson(root)
return (
packageJson &&
((packageJson.dependencies && packageJson.dependencies[dependencyName]) ||
(packageJson.devDependencies &&
packageJson.devDependencies[dependencyName]))
)
}
function usesYarn(root: string): boolean {
return existsSync(join(root, 'yarn.lock'))
}
export function execCommand(
command: string,
args: string[],
{ cwd }: { cwd: string | undefined }
): Promise<void> {
console.log(`running ${command}`, args)
return execa(command, args, {
cwd,
stdio: 'inherit',
env: { FORCE_COLOR: '0' }
}).then()
}
interface CargoManifestBin {
name: string
}
interface CargoManifest {
package: { version: string; name: string; 'default-run': string }
bin: CargoManifestBin[]
}
interface TauriConfig {
package?: {
productName?: string
version?: string
}
}
interface Application {
runner: Runner
name: string
version: string
}
export interface BuildOptions {
configPath: string | null
distPath: string | null
iconPath: string | null
npmScript: string | null
args: string[] | null
}
export interface Runner {
runnerCommand: string
runnerArgs: string[]
}
export async function buildProject(
preferGlobal: boolean,
root: string,
debug: boolean,
{ configPath, distPath, iconPath, npmScript, args }: BuildOptions
): Promise<string[]> {
return new Promise<Runner>((resolve, reject) => {
if (preferGlobal) {
resolve({ runnerCommand: 'tauri', runnerArgs: [] })
} else if (hasDependency('@tauri-apps/cli', root) || hasDependency('vue-cli-plugin-tauri', root)) {
if (npmScript) {
resolve(
usesYarn(root)
? { runnerCommand: 'yarn', runnerArgs: npmScript.split(' ') }
: { runnerCommand: 'npm', runnerArgs: ['run', ...npmScript.split(' ')] }
)
} else {
resolve(
usesYarn(root)
? { runnerCommand: 'yarn', runnerArgs: ['tauri'] }
: { runnerCommand: 'npx', runnerArgs: ['tauri'] }
)
}
} else {
execCommand('npm', ['install', '-g', '@tauri-apps/cli'], { cwd: undefined }).then(() => {
resolve({ runnerCommand: 'tauri', runnerArgs: [] })
}).catch(reject)
}
})
.then((runner: Runner) => {
const configPath = join(root, 'src-tauri/tauri.conf.json')
if (existsSync(configPath)) {
let name
let version
const config = JSON.parse(
readFileSync(configPath).toString()
) as TauriConfig
if (config.package) {
name = config.package.productName
version = config.package.version
}
if (!(name && version)) {
const manifestPath = join(root, 'src-tauri/Cargo.toml')
const cargoManifest = (parseToml(
readFileSync(manifestPath).toString()
) as any) as CargoManifest
name = name || cargoManifest.package.name
version = version || cargoManifest.package.version
}
if (!(name && version)) {
console.error('Could not determine package name and version')
process.exit(1)
}
return {
runner,
name,
version
}
} else {
const packageJson = getPackageJson(root)
const appName = packageJson
? (packageJson.displayName || packageJson.name).replace(/ /g, '-')
: 'app'
return execCommand(runner.runnerCommand, [...runner.runnerArgs, 'init', '--ci', '--app-name', appName], {
cwd: root
}).then(() => {
const config = JSON.parse(
readFileSync(configPath).toString()
) as TauriConfig
const version = packageJson ? packageJson.version : '0.1.0'
console.log(
`Replacing tauri.conf.json config - package.version=${version}`
)
const pkgConfig = {
...config.package,
version
}
if (packageJson?.productName) {
console.log(
`Replacing tauri.conf.json config - package.productName=${packageJson.productName}`
)
pkgConfig.productName = packageJson.productName
}
config.package = pkgConfig
writeFileSync(configPath, JSON.stringify(config, null, 2))
const app = {
runner,
name: appName,
version
}
if (iconPath) {
return execCommand(runner.runnerCommand, [...runner.runnerArgs, 'icon', join(root, iconPath)], {
cwd: root
}).then(() => app)
}
return app
})
}
})
.then((app: Application) => {
const tauriConfPath = join(root, 'src-tauri/tauri.conf.json')
if (configPath !== null) {
copyFileSync(configPath, tauriConfPath)
}
if (distPath) {
const tauriConf = JSON.parse(readFileSync(tauriConfPath).toString())
tauriConf.build.distDir = distPath
writeFileSync(tauriConfPath, JSON.stringify(tauriConf))
}
const tauriArgs = debug ? ['--debug', ...(args ?? [])] : (args ?? [])
let buildCommand
let buildArgs: string[] = []
if (hasDependency('vue-cli-plugin-tauri', root)) {
if (usesYarn(root)) {
buildCommand = 'yarn'
} else {
buildCommand = 'npx'
buildArgs = ['run', 'tauri:build']
}
} else {
buildCommand = app.runner.runnerCommand
buildArgs = [...app.runner.runnerArgs, 'build']
}
return execCommand(
buildCommand,
[...buildArgs, ...tauriArgs],
{ cwd: root }
)
.then(() => {
let appName = app.name
// on Linux, the app product name is converted to kebab-case
if (!['darwin', 'win32'].includes(platform())) {
appName = appName.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
.replace(/ /g, '-')
.toLowerCase()
}
const artifactsPath = join(
root,
`src-tauri/target/${debug ? 'debug' : 'release'}`
)
switch (platform()) {
case 'darwin':
return [
join(
artifactsPath,
`bundle/dmg/${appName}_${app.version}_${process.arch}.dmg`
),
join(
artifactsPath,
`bundle/macos/${appName}.app`
),
join(
artifactsPath,
`bundle/macos/${appName}.app.tar.gz`
),
join(
artifactsPath,
`bundle/macos/${appName}.app.tar.gz.sig`
)
]
case 'win32':
return [
join(
artifactsPath,
`bundle/msi/${appName}_${app.version}_${process.arch}.msi`
),
join(
artifactsPath,
`bundle/msi/${appName}_${app.version}_${process.arch}.msi.zip`
),
join(
artifactsPath,
`bundle/msi/${appName}_${app.version}_${process.arch}.msi.zip.sig`
)
]
default:
const arch =
process.arch === 'x64'
? 'amd64'
: process.arch === 'x32'
? 'i386'
: process.arch
return [
join(
artifactsPath,
`bundle/deb/${appName}_${app.version}_${arch}.deb`
),
join(
artifactsPath,
`bundle/appimage/${appName}_${app.version}_${arch}.AppImage`
),
join(
artifactsPath,
`bundle/appimage/${appName}_${app.version}_${arch}.AppImage.zip`
),
join(
artifactsPath,
`bundle/appimage/${appName}_${app.version}_${arch}.AppImage.zip.sig`
)
]
}
})
.then(paths => paths.filter(p => existsSync(p)))
})
}