Skip to content

Commit f5e77ff

Browse files
refactor(cta): use commander instead of minimst (#2551)
* refactor(cta): use `commander` instead of `minimst` * fix default * pin deps * update lock file * rearrange options * style changes * colorful help message * add missing `dev` option * style * use local api for tests * concise checks for vite and solid * update lock file * fix api formatting * improvements to updating json files * hopefully the last commit in this PR * fix eslint Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 2d39f12 commit f5e77ff

17 files changed

Lines changed: 472 additions & 513 deletions

.changes/cta-ci-compatible.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-tauri-app": patch
3+
---
4+
5+
`create-tauri-app` should now be fully compatiable with CI environments.

tooling/create-tauri-app/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
},
3434
"dependencies": {
3535
"chalk": "4.1.2",
36-
"execa": "^6.0.0",
37-
"inquirer": "^8.0.0",
38-
"minimist": "^1.2.5",
36+
"commander": "8.1.0",
37+
"execa": "5.0.0",
38+
"inquirer": "8.0.0",
3939
"scaffe": "1.2.0"
4040
},
4141
"devDependencies": {
@@ -45,7 +45,6 @@
4545
"@rollup/plugin-typescript": "8.3.0",
4646
"@types/cross-spawn": "6.0.2",
4747
"@types/inquirer": "8.1.3",
48-
"@types/minimist": "1.2.2",
4948
"@types/semver": "7.3.9",
5049
"@typescript-eslint/eslint-plugin": "5.8.1",
5150
"@typescript-eslint/parser": "5.8.1",

tooling/create-tauri-app/src/dependency-manager.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,18 @@ async function installNpmPackage(
5353
const packages = packageNames.filter((p) => p !== '')
5454
if (packages.length !== 0) {
5555
console.log(`- Installing ${packages.join(', ')}...`)
56-
if (packageManager === 'npm') {
57-
await shell('npm', ['install', packageNames.join(' ')], {
58-
cwd: appDir
59-
})
60-
} else {
61-
await shell(packageManager, ['add', packageNames.join(' ')], {
62-
cwd: appDir
63-
})
56+
switch (packageManager) {
57+
case 'pnpm':
58+
case 'yarn':
59+
await shell(packageManager, ['add', packageNames.join(' ')], {
60+
cwd: appDir
61+
})
62+
break
63+
case 'npm':
64+
await shell('npm', ['install', packageNames.join(' ')], {
65+
cwd: appDir
66+
})
67+
break
6468
}
6569
}
6670
}
@@ -73,22 +77,27 @@ async function installNpmDevPackage(
7377
const packages = packageNames.filter((p) => p !== '')
7478
if (packages.length !== 0) {
7579
console.log(`- Installing ${packages.join(', ')}...`)
76-
if (packageManager === 'npm') {
77-
await shell(
78-
'npm',
79-
['install', '--save-dev', '--ignore-scripts', packageNames.join(' ')],
80-
{
81-
cwd: appDir
82-
}
83-
)
84-
} else {
85-
await shell(
86-
packageManager,
87-
['add', '-D', '--ignore-scripts', packageNames.join(' ')],
88-
{
89-
cwd: appDir
90-
}
91-
)
80+
81+
switch (packageManager) {
82+
case 'pnpm':
83+
case 'yarn':
84+
await shell(
85+
packageManager,
86+
['add', '-D', '--ignore-scripts', packageNames.join(' ')],
87+
{
88+
cwd: appDir
89+
}
90+
)
91+
break
92+
case 'npm':
93+
await shell(
94+
'npm',
95+
['install', '--save-dev', '--ignore-scripts', packageNames.join(' ')],
96+
{
97+
cwd: appDir
98+
}
99+
)
100+
break
92101
}
93102
}
94103
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function pkgManagerFromUserAgent(userAgent: string | undefined):
2+
| {
3+
name: string
4+
version: string
5+
}
6+
| undefined {
7+
if (!userAgent) return undefined
8+
const pkgSpec = userAgent.split(' ')[0]
9+
const pkgSpecArr = pkgSpec.split('/')
10+
return {
11+
name: pkgSpecArr[0],
12+
version: pkgSpecArr[1]
13+
}
14+
}

tooling/create-tauri-app/src/helpers/update-package-json.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,17 @@
55
import { readFileSync, writeFileSync } from 'fs'
66
import { join } from 'path'
77

8-
interface Package {
8+
interface PackageJSON {
99
name?: string
1010
scripts?: Record<string, string>
1111
}
1212

1313
export function updatePackageJson(
14-
appDirectory: string,
15-
appName: string,
16-
recipeShortName: string
14+
f: (pkg: PackageJSON) => PackageJSON,
15+
cwd: string = process.cwd()
1716
): void {
18-
const pkgPath = join(appDirectory, 'package.json')
19-
const pkgString = readFileSync(pkgPath, 'utf8')
20-
const pkg = JSON.parse(pkgString) as Package
21-
const outputPkg = {
22-
...pkg,
23-
name: appName,
24-
scripts: {
25-
...pkg.scripts,
26-
start: `${recipeShortName === 'cra' ? 'cross-env BROWSER=none ' : ''}${
27-
pkg.scripts?.start as string
28-
}`,
29-
tauri: 'tauri'
30-
}
31-
}
32-
writeFileSync(pkgPath, JSON.stringify(outputPkg, undefined, 2))
17+
const pkgPath = join(cwd, 'package.json')
18+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as PackageJSON
19+
const output = f(pkg)
20+
writeFileSync(pkgPath, JSON.stringify(output, undefined, 2))
3321
}

tooling/create-tauri-app/src/helpers/update-tauri-conf.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,25 @@
44

55
import { readFileSync, writeFileSync } from 'fs'
66
import { join } from 'path'
7-
import { TauriBuildConfig } from '../types/config'
87

9-
export function updateTauriConf(
10-
appDirectory: string,
11-
cfg: TauriBuildConfig
12-
): void {
13-
const tauriConfPath = join(appDirectory, 'src-tauri', 'tauri.conf.json')
14-
const tauriConfString = readFileSync(tauriConfPath, 'utf8')
15-
const tauriConf = JSON.parse(tauriConfString) as {
16-
build: TauriBuildConfig
17-
}
18-
19-
const outputPkg: { build: TauriBuildConfig } = {
20-
...tauriConf,
21-
build: {
22-
...tauriConf.build,
23-
beforeBuildCommand: cfg.beforeBuildCommand,
24-
beforeDevCommand: cfg.beforeDevCommand
25-
}
8+
interface TauriConfJSON {
9+
build?: {
10+
beforeDevCommand?: string
11+
beforeBuildCommand?: string
12+
distDir?: string
13+
devPath?: string
14+
withGlobalTauri?: boolean
2615
}
16+
}
2717

28-
writeFileSync(tauriConfPath, JSON.stringify(outputPkg, undefined, 2))
18+
export function updateTauriConf(
19+
f: (tauriConf: TauriConfJSON) => TauriConfJSON,
20+
cwd: string = process.cwd()
21+
): void {
22+
const tauriConfPath = join(cwd, 'src-tauri', 'tauri.conf.json')
23+
const tauriConf = JSON.parse(
24+
readFileSync(tauriConfPath, 'utf8')
25+
) as TauriConfJSON
26+
const output = f(tauriConf)
27+
writeFileSync(tauriConfPath, JSON.stringify(output, undefined, 2))
2928
}

0 commit comments

Comments
 (0)