这是indexloc提供的服务,不要输入任何密码
Skip to content

Set packageManager in create-turbo before install #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions create-turbo/src/getPackageManagerVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { execSync } from "child_process";
import { PackageManager } from "./types";

export const getPackageManagerVersion = (ws: PackageManager): string => {
switch (ws) {
case "yarn":
return execSync("yarn --version").toString().trim();
case "pnpm":
return execSync("pnpm --version").toString().trim();
case "npm":
return execSync("npm --version").toString().trim();
default:
throw new Error(`${ws} is not supported`);
}
};
33 changes: 7 additions & 26 deletions create-turbo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as path from "path";
import execa from "execa";
import fs from 'fs';
import fs from "fs";
import fse from "fs-extra";
import inquirer from "inquirer";
import ora from "ora";
Expand All @@ -14,8 +14,9 @@ import cliPkgJson from "../package.json";
import { shouldUseYarn } from "./shouldUseYarn";
import { shouldUsePnpm, getNpxCommandOfPnpm } from "./shouldUsePnpm";
import { tryGitInit } from "./git";
import { PackageManager } from "./types";
import { getPackageManagerVersion } from "./getPackageManagerVersion";

type PackageManager = "yarn" | "pnpm" | "npm";
interface Answers {
packageManager: PackageManager;
}
Expand Down Expand Up @@ -154,15 +155,6 @@ async function run() {
await fse.copy(serverTemplate, projectDir, { overwrite: true });
}

// let serverLangTemplate = path.resolve(
// __dirname,
// "templates",
// `${answers.packageManager}_ts`
// );
// if (fse.existsSync(serverLangTemplate)) {
// await fse.copy(serverLangTemplate, projectDir, { overwrite: true });
// }

// rename dotfiles
await fse.move(
path.join(projectDir, "gitignore"),
Expand All @@ -181,6 +173,10 @@ async function run() {
}
});

appPkg.packageManager = `${answers.packageManager}@${getPackageManagerVersion(
answers.packageManager
)}`;

// write package.json
await fse.writeFile(
path.join(projectDir, "package.json"),
Expand Down Expand Up @@ -213,21 +209,6 @@ async function run() {
},
}).start();

const data = fs.readFileSync(`${projectDir}/package.json`, "utf8");
const pkg = JSON.parse(data.toString());
switch (answers.packageManager) {
case "yarn":
pkg.packageManager = "yarn@1.22.17";
break;
case "pnpm":
pkg.packageManager = "pnpm@6.26.1";
break;
case "npm":
pkg.packageManager = "npm@8.3.0";
break;
}
fs.writeFileSync(`${projectDir}/package.json`, JSON.stringify(pkg, null, 2));

await execa(`${answers.packageManager}`, [`install`], {
stdio: "ignore",
cwd: projectDir,
Expand Down
1 change: 1 addition & 0 deletions create-turbo/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type PackageManager = "yarn" | "pnpm" | "npm";