Browse Source

chore: CTA defaults in CI mode (#1671)

* better defaults in CI mode

* chalk should be a dep since it needs to be installed by user

* always install webkit2gtk

* avoid installing `cli.js` from npm in `--dev` mode

* use correct path for `api` linking in tests

* update `tauri.conf.json` after init

* remove `beforeBuild/DevCommand` from vanilla recipe

* explicitly install `vite` deps

* change file

Co-authored-by: amrbashir <48618675+amrbashir@users.noreply.github.com>
Jacob Bolda 4 years ago
parent
commit
397b7af395

+ 5 - 0
.changes/cta-explicitly-install-vite.md

@@ -0,0 +1,5 @@
+---
+"create-tauri-app": patch
+---
+
+Explicitly install deps after a vite recipe.

+ 2 - 4
.github/workflows/test-cta.yml

@@ -44,8 +44,7 @@ jobs:
           node-version: ${{ matrix.node }}
           npm-version: ${{ matrix.manager }}
           yarn-version: 1.22.5
-      - name: install webkit2gtk (ubuntu only)
-        if: matrix.platform == 'ubuntu-latest'
+      - name: install webkit2gtk
         run: |
           sudo apt-get update
           sudo apt-get install -y webkit2gtk-4.0
@@ -81,8 +80,7 @@ jobs:
         with:
           node-version: ${{ matrix.node }}
           yarn-version: 1.22.5
-      - name: install webkit2gtk (ubuntu only)
-        if: matrix.platform == 'ubuntu-latest'
+      - name: install webkit2gtk
         run: |
           sudo apt-get update
           sudo apt-get install -y webkit2gtk-4.0

+ 1 - 1
tooling/create-tauri-app/package.json

@@ -33,6 +33,7 @@
     "test": "jest --runInBand"
   },
   "dependencies": {
+    "chalk": "4.1.1",
     "execa": "^5.0.0",
     "inquirer": "^8.0.0",
     "minimist": "^1.2.5",
@@ -49,7 +50,6 @@
     "@types/semver": "7.3.5",
     "@typescript-eslint/eslint-plugin": "4.22.0",
     "@typescript-eslint/parser": "4.22.0",
-    "chalk": "4.1.1",
     "eslint": "7.25.0",
     "eslint-config-prettier": "8.3.0",
     "eslint-config-standard-with-typescript": "20.0.0",

+ 2 - 4
tooling/create-tauri-app/src/helpers/add-tauri-script.ts

@@ -9,12 +9,10 @@ export function addTauriScript(appDirectory: string): void {
   const pkgPath = join(appDirectory, 'package.json')
   const pkgString = readFileSync(pkgPath, 'utf8')
   const pkg = JSON.parse(pkgString) as {
-    scripts: {
-      tauri: string
-    }
+    scripts: {}
   }
 
-  const outputPkg = {
+  const outputPkg: { scripts: { tauri: string } } = {
     ...pkg,
     scripts: {
       ...pkg.scripts,

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

@@ -0,0 +1,29 @@
+// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-License-Identifier: MIT
+
+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
+    }
+  }
+
+  writeFileSync(tauriConfPath, JSON.stringify(outputPkg, undefined, 2))
+}

+ 21 - 5
tooling/create-tauri-app/src/index.ts

@@ -14,6 +14,7 @@ import { install, checkPackageManager } from './dependency-manager'
 import { shell } from './shell'
 import { addTauriScript } from './helpers/add-tauri-script'
 import { Recipe } from './types/recipe'
+import { updateTauriConf } from './helpers/update-tauri-conf'
 
 interface Argv {
   h: boolean
@@ -183,7 +184,16 @@ const runInit = async (argv: Argv): Promise<void> => {
     recipe = recipeByDescriptiveName(recipeName)
   }
 
-  if (!recipe) throw new Error('Could not find the recipe specified.')
+  if (!recipe) {
+    if (argv.ci) {
+      recipe = recipeByShortName('vanillajs')
+    }
+    // throw if recipe is not set
+    // if it fails to set in CI, throw as well
+    if (!recipe) {
+      throw new Error('Could not find the recipe specified.')
+    }
+  }
 
   const packageManager =
     argv.m === 'yarn' || argv.m === 'npm'
@@ -279,10 +289,10 @@ const runInit = async (argv: Argv): Promise<void> => {
   if (recipe.shortName !== 'vuecli') {
     logStep('Installing any additional needed dependencies')
     if (argv.dev) {
-      await shell('yarn', ['link', '@tauri-apps/cli'], {
+      await shell(packageManager, ['link', '@tauri-apps/cli'], {
         cwd: appDirectory
       })
-      await shell('yarn', ['link', '@tauri-apps/api'], {
+      await shell(packageManager, ['link', '@tauri-apps/api'], {
         cwd: appDirectory
       })
     }
@@ -292,13 +302,16 @@ const runInit = async (argv: Argv): Promise<void> => {
       dependencies: recipe.extraNpmDependencies,
       devDependencies: argv.dev
         ? [...recipe.extraNpmDevDependencies]
-        : ['@tauri-apps/cli'].concat(recipe.extraNpmDevDependencies),
+        : [argv.dev ? '@tauri-apps/cli' : ''].concat(
+            recipe.extraNpmDevDependencies
+          ),
       packageManager
     })
 
-    logStep(`Running: ${reset(yellow('tauri init'))}`)
+    logStep('Adding `tauri` script to package.json')
     addTauriScript(appDirectory)
 
+    logStep(`Running: ${reset(yellow('tauri init'))}`)
     const binary = !argv.b ? packageManager : resolve(appDirectory, argv.b)
     const runTauriArgs =
       packageManager === 'npm' && !argv.b
@@ -307,6 +320,9 @@ const runInit = async (argv: Argv): Promise<void> => {
     await shell(binary, [...runTauriArgs, ...initArgs, '--ci'], {
       cwd: appDirectory
     })
+
+    logStep('Updating `tauri.conf.json`')
+    updateTauriConf(appDirectory, cfg)
   }
 
   if (recipe.postInit) {

+ 3 - 5
tooling/create-tauri-app/src/recipes/vanilla.ts

@@ -10,14 +10,12 @@ import { Recipe } from '../types/recipe'
 export const vanillajs: Recipe = {
   descriptiveName: 'Vanilla.js',
   shortName: 'vanillajs',
-  configUpdate: ({ cfg, packageManager }) => ({
+  configUpdate: ({ cfg }) => ({
     ...cfg,
     distDir: `../dist`,
     devPath: `../dist`,
-    beforeDevCommand: `${packageManager === 'yarn' ? 'yarn' : 'npm run'} start`,
-    beforeBuildCommand: `${
-      packageManager === 'yarn' ? 'yarn' : 'npm run'
-    } build`
+    beforeDevCommand: '',
+    beforeBuildCommand: ''
   }),
   extraNpmDevDependencies: [],
   extraNpmDependencies: [],

+ 2 - 0
tooling/create-tauri-app/src/recipes/vite.ts

@@ -73,6 +73,7 @@ const vite: Recipe = {
           cwd
         }
       )
+      await shell('yarn', ['install'], { cwd })
     } else {
       await shell(
         'npx',
@@ -81,6 +82,7 @@ const vite: Recipe = {
           cwd
         }
       )
+      await shell('npm', ['install'], { cwd })
     }
 
     await afterViteCA(cwd, cfg.appName, template)

+ 1 - 1
tooling/create-tauri-app/test/index.spec.ts

@@ -53,7 +53,7 @@ beforeAll(async () => {
 
   const linkAPI = await execa('yarn', ['link'], {
     stdio: logOut,
-    cwd: api,
+    cwd: path.join(api, 'dist'),
     timeout: timeoutLong
   })
 }, timeoutLittleLonger)