Skip to content

Commit

Permalink
refactor(cta): use commander instead of minimst (#2551)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
amrbashir and lucasfernog authored Jan 1, 2022
1 parent 2d39f12 commit f5e77ff
Show file tree
Hide file tree
Showing 17 changed files with 472 additions and 513 deletions.
5 changes: 5 additions & 0 deletions .changes/cta-ci-compatible.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-tauri-app": patch
---

`create-tauri-app` should now be fully compatiable with CI environments.
7 changes: 3 additions & 4 deletions tooling/create-tauri-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
},
"dependencies": {
"chalk": "4.1.2",
"execa": "^6.0.0",
"inquirer": "^8.0.0",
"minimist": "^1.2.5",
"commander": "8.1.0",
"execa": "5.0.0",
"inquirer": "8.0.0",
"scaffe": "1.2.0"
},
"devDependencies": {
Expand All @@ -45,7 +45,6 @@
"@rollup/plugin-typescript": "8.3.0",
"@types/cross-spawn": "6.0.2",
"@types/inquirer": "8.1.3",
"@types/minimist": "1.2.2",
"@types/semver": "7.3.9",
"@typescript-eslint/eslint-plugin": "5.8.1",
"@typescript-eslint/parser": "5.8.1",
Expand Down
57 changes: 33 additions & 24 deletions tooling/create-tauri-app/src/dependency-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ async function installNpmPackage(
const packages = packageNames.filter((p) => p !== '')
if (packages.length !== 0) {
console.log(`- Installing ${packages.join(', ')}...`)
if (packageManager === 'npm') {
await shell('npm', ['install', packageNames.join(' ')], {
cwd: appDir
})
} else {
await shell(packageManager, ['add', packageNames.join(' ')], {
cwd: appDir
})
switch (packageManager) {
case 'pnpm':
case 'yarn':
await shell(packageManager, ['add', packageNames.join(' ')], {
cwd: appDir
})
break
case 'npm':
await shell('npm', ['install', packageNames.join(' ')], {
cwd: appDir
})
break
}
}
}
Expand All @@ -73,22 +77,27 @@ async function installNpmDevPackage(
const packages = packageNames.filter((p) => p !== '')
if (packages.length !== 0) {
console.log(`- Installing ${packages.join(', ')}...`)
if (packageManager === 'npm') {
await shell(
'npm',
['install', '--save-dev', '--ignore-scripts', packageNames.join(' ')],
{
cwd: appDir
}
)
} else {
await shell(
packageManager,
['add', '-D', '--ignore-scripts', packageNames.join(' ')],
{
cwd: appDir
}
)

switch (packageManager) {
case 'pnpm':
case 'yarn':
await shell(
packageManager,
['add', '-D', '--ignore-scripts', packageNames.join(' ')],
{
cwd: appDir
}
)
break
case 'npm':
await shell(
'npm',
['install', '--save-dev', '--ignore-scripts', packageNames.join(' ')],
{
cwd: appDir
}
)
break
}
}
}
14 changes: 14 additions & 0 deletions tooling/create-tauri-app/src/helpers/package-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function pkgManagerFromUserAgent(userAgent: string | undefined):
| {
name: string
version: string
}
| undefined {
if (!userAgent) return undefined
const pkgSpec = userAgent.split(' ')[0]
const pkgSpecArr = pkgSpec.split('/')
return {
name: pkgSpecArr[0],
version: pkgSpecArr[1]
}
}
26 changes: 7 additions & 19 deletions tooling/create-tauri-app/src/helpers/update-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,17 @@
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'

interface Package {
interface PackageJSON {
name?: string
scripts?: Record<string, string>
}

export function updatePackageJson(
appDirectory: string,
appName: string,
recipeShortName: string
f: (pkg: PackageJSON) => PackageJSON,
cwd: string = process.cwd()
): void {
const pkgPath = join(appDirectory, 'package.json')
const pkgString = readFileSync(pkgPath, 'utf8')
const pkg = JSON.parse(pkgString) as Package
const outputPkg = {
...pkg,
name: appName,
scripts: {
...pkg.scripts,
start: `${recipeShortName === 'cra' ? 'cross-env BROWSER=none ' : ''}${
pkg.scripts?.start as string
}`,
tauri: 'tauri'
}
}
writeFileSync(pkgPath, JSON.stringify(outputPkg, undefined, 2))
const pkgPath = join(cwd, 'package.json')
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as PackageJSON
const output = f(pkg)
writeFileSync(pkgPath, JSON.stringify(output, undefined, 2))
}
37 changes: 18 additions & 19 deletions tooling/create-tauri-app/src/helpers/update-tauri-conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@

import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import { TauriBuildConfig } from '../types/config'

export function updateTauriConf(
appDirectory: string,
cfg: TauriBuildConfig
): void {
const tauriConfPath = join(appDirectory, 'src-tauri', 'tauri.conf.json')
const tauriConfString = readFileSync(tauriConfPath, 'utf8')
const tauriConf = JSON.parse(tauriConfString) as {
build: TauriBuildConfig
}

const outputPkg: { build: TauriBuildConfig } = {
...tauriConf,
build: {
...tauriConf.build,
beforeBuildCommand: cfg.beforeBuildCommand,
beforeDevCommand: cfg.beforeDevCommand
}
interface TauriConfJSON {
build?: {
beforeDevCommand?: string
beforeBuildCommand?: string
distDir?: string
devPath?: string
withGlobalTauri?: boolean
}
}

writeFileSync(tauriConfPath, JSON.stringify(outputPkg, undefined, 2))
export function updateTauriConf(
f: (tauriConf: TauriConfJSON) => TauriConfJSON,
cwd: string = process.cwd()
): void {
const tauriConfPath = join(cwd, 'src-tauri', 'tauri.conf.json')
const tauriConf = JSON.parse(
readFileSync(tauriConfPath, 'utf8')
) as TauriConfJSON
const output = f(tauriConf)
writeFileSync(tauriConfPath, JSON.stringify(output, undefined, 2))
}
Loading

0 comments on commit f5e77ff

Please sign in to comment.