Browse Source

fix(CTA): #1569, manually set tauri script for compatability with older npm (#1572)

Amr Bashir 4 năm trước cách đây
mục cha
commit
f708ff824e

+ 5 - 0
.changes/fix-cta-set-script.md

@@ -0,0 +1,5 @@
+---
+"create-tauri-app": patch
+---
+
+Manually set `tauri` script instead of using `npm set-script` for compatabilty with older npm versions

+ 3 - 3
tooling/create-tauri-app/bin/create-tauri-app.js

@@ -14,6 +14,7 @@ const {
   install,
   checkPackageManager,
   shell,
+  addTauriScript,
 } = require("../dist/");
 
 /**
@@ -208,9 +209,8 @@ async function runInit(argv, config = {}) {
     });
 
     console.log("===== running tauri init =====");
-    await shell("npm", ["set-script", "tauri", "tauri"], {
-      cwd: appDirectory,
-    });
+    addTauriScript(appDirectory);
+
     const binary = !argv.b ? packageManager : resolve(appDirectory, argv.b);
     const runTauriArgs =
       packageManager === "npm" && !argv.b

+ 26 - 0
tooling/create-tauri-app/src/helpers/add-tauri-script.ts

@@ -0,0 +1,26 @@
+// 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";
+
+export async function addTauriScript(appDirectory: string) {
+  const pkgPath = join(appDirectory, "package.json");
+  const pkgString = readFileSync(pkgPath, "utf8");
+  const pkg = JSON.parse(pkgString) as {
+    scripts: {
+      tauri: string;
+    };
+  };
+
+  let outputPkg = {
+    ...pkg,
+    scripts: {
+      ...pkg.scripts,
+      tauri: "tauri",
+    },
+  };
+
+  writeFileSync(pkgPath, JSON.stringify(outputPkg, undefined, 2));
+}

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

@@ -10,6 +10,7 @@ import { vite } from "./recipes/vite";
 
 export { shell } from "./shell";
 export { install, checkPackageManager } from "./dependency-manager";
+export { addTauriScript } from "./helpers/add-tauri-script";
 import { PackageManager } from "./dependency-manager";
 
 export interface Recipe {
@@ -46,12 +47,17 @@ export interface Recipe {
 
 export const allRecipes: Recipe[] = [vanillajs, reactjs, reactts, vite, vuecli];
 
-export const recipeNames: Array<[string, string]> = allRecipes.map(r => [r.shortName, r.descriptiveName]);
+export const recipeNames: Array<[string, string]> = allRecipes.map((r) => [
+  r.shortName,
+  r.descriptiveName,
+]);
 
-export const recipeByShortName = (name: string) => allRecipes.find(r => r.shortName === name);
+export const recipeByShortName = (name: string) =>
+  allRecipes.find((r) => r.shortName === name);
 
-export const recipeByDescriptiveName = (name: string) => allRecipes.find(r => r.descriptiveName === name);
+export const recipeByDescriptiveName = (name: string) =>
+  allRecipes.find((r) => r.descriptiveName === name);
 
-export const recipeShortNames = allRecipes.map(r => r.shortName);
+export const recipeShortNames = allRecipes.map((r) => r.shortName);
 
-export const recipeDescriptiveNames = allRecipes.map(r => r.descriptiveName);
+export const recipeDescriptiveNames = allRecipes.map((r) => r.descriptiveName);