Переглянути джерело

Add svelte recipe to create-tauri-app (#2276) (#2279)

Kris Scott 4 роки тому
батько
коміт
151c3157be

+ 5 - 0
.changes/cta-svelte-recipe.md

@@ -0,0 +1,5 @@
+---
+"create-tauri-app": patch
+---
+
+Add Svelte recipe using the official template.

+ 2 - 1
tooling/create-tauri-app/src/index.ts

@@ -12,6 +12,7 @@ import { vuecli } from './recipes/vue-cli'
 import { vanillajs } from './recipes/vanilla'
 import { vite } from './recipes/vite'
 import { ngcli } from './recipes/ng-cli'
+import { svelte } from './recipes/svelte'
 import { install, checkPackageManager } from './dependency-manager'
 import { shell } from './shell'
 import { addTauriScript } from './helpers/add-tauri-script'
@@ -114,7 +115,7 @@ interface Responses {
   recipeName: string
 }
 
-const allRecipes: Recipe[] = [vanillajs, cra, vite, vuecli, ngcli]
+const allRecipes: Recipe[] = [vanillajs, cra, vite, vuecli, ngcli, svelte]
 
 const recipeByShortName = (name: string): Recipe | undefined =>
   allRecipes.find((r) => r.shortName === name)

+ 69 - 0
tooling/create-tauri-app/src/recipes/svelte.ts

@@ -0,0 +1,69 @@
+// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-License-Identifier: MIT
+
+import { join } from 'path'
+import { shell } from '../shell'
+import { Recipe } from '../types/recipe'
+
+const svelte: Recipe = {
+  descriptiveName: {
+    name: 'Svelte (https://github.com/sveltejs/template)',
+    value: 'svelte'
+  },
+  shortName: 'svelte',
+  extraNpmDevDependencies: [],
+  extraNpmDependencies: [],
+  extraQuestions: () => {
+    return [
+      {
+        type: 'confirm',
+        name: 'typescript',
+        message: 'Enable Typescript?',
+        default: true,
+        loop: false
+      }
+    ]
+  },
+  configUpdate: ({ cfg, packageManager }) => ({
+    ...cfg,
+    distDir: `../public`,
+    devPath: 'http://localhost:5000',
+    beforeDevCommand: `${packageManager === 'yarn' ? 'yarn' : 'npm run'} dev`,
+    beforeBuildCommand: `${
+      packageManager === 'yarn' ? 'yarn' : 'npm run'
+    } build`
+  }),
+  preInit: async ({ cwd, cfg, answers }) => {
+    let typescript = false
+    if (answers) {
+      typescript = !!answers.typescript
+    }
+
+    await shell('npx', ['degit', 'sveltejs/template', `${cfg.appName}`], {
+      cwd
+    })
+
+    // Add Typescript
+    if (typescript) {
+      await shell('node', ['scripts/setupTypeScript.js'], {
+        cwd: join(cwd, cfg.appName)
+      })
+    }
+  },
+  postInit: async ({ cfg, packageManager }) => {
+    console.log(`
+      Your installation completed.
+      To start, run the dev script:
+
+      $ cd ${cfg.appName}
+      $ ${packageManager === 'yarn' ? 'yarn' : 'npm run'} tauri ${
+      packageManager === 'npm' ? '-- ' : ''
+    }dev
+    `)
+
+    return await Promise.resolve()
+  }
+}
+
+export { svelte }