1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
import os from "os";
import path from "path";
import fs from "fs-extra";
import { gte } from "semver";
import { exec } from "../utils";
import getPackageManager, {
PackageManager,
} from "../../../utils/getPackageManager";
import getPackageManagerVersion from "../../../utils/getPackageManagerVersion";
type InstallType = "dependencies" | "devDependencies";
function getGlobalBinaryPaths(): Record<PackageManager, string | undefined> {
return {
// we run these from a tmpdir to avoid corepack interference
yarn: exec(`yarn global bin`, { cwd: os.tmpdir() }),
npm: exec(`npm bin --global`, { cwd: os.tmpdir() }),
pnpm: exec(`pnpm bin --global`, { cwd: os.tmpdir() }),
};
}
function getGlobalUpgradeCommand(
packageManager: PackageManager,
to: string = "latest"
) {
switch (packageManager) {
case "yarn":
return `yarn global add turbo@${to}`;
case "npm":
return `npm install turbo@${to} --global`;
case "pnpm":
return `pnpm install turbo@${to} --global`;
}
}
function getLocalUpgradeCommand({
packageManager,
packageManagerVersion,
installType,
isUsingWorkspaces,
to = "latest",
}: {
packageManager: PackageManager;
packageManagerVersion: string;
installType: InstallType;
isUsingWorkspaces?: boolean;
to?: string;
}) {
const renderCommand = (
command: Array<string | boolean | undefined>
): string => command.filter(Boolean).join(" ");
switch (packageManager) {
// yarn command differs depending on the version
case "yarn":
// yarn 2.x and 3.x (berry)
if (gte(packageManagerVersion, "2.0.0")) {
return renderCommand([
"yarn",
"add",
`turbo@${to}`,
installType === "devDependencies" && "--dev",
]);
// yarn 1.x
} else {
return renderCommand([
"yarn",
"add",
`turbo@${to}`,
installType === "devDependencies" && "--dev",
isUsingWorkspaces && "-W",
]);
}
case "npm":
return renderCommand([
"npm",
"install",
`turbo@${to}`,
installType === "devDependencies" && "--save-dev",
]);
case "pnpm":
return renderCommand([
"pnpm",
"install",
`turbo@${to}`,
installType === "devDependencies" && "--save-dev",
isUsingWorkspaces && "-w",
]);
}
}
function getInstallType({ directory }: { directory: string }): {
installType?: InstallType;
isUsingWorkspaces?: boolean;
} {
// read package.json to make sure we have a reference to turbo
const packageJsonPath = path.join(directory, "package.json");
const pnpmWorkspaceConfig = path.join(directory, "pnpm-workspace.yaml");
const isPnpmWorkspaces = fs.existsSync(pnpmWorkspaceConfig);
if (!fs.existsSync(packageJsonPath)) {
console.error(`Unable to find package.json at ${packageJsonPath}`);
return { installType: undefined, isUsingWorkspaces: undefined };
}
const packageJson = fs.readJsonSync(packageJsonPath);
const isDevDependency =
packageJson.devDependencies && "turbo" in packageJson.devDependencies;
const isDependency =
packageJson.dependencies && "turbo" in packageJson.dependencies;
let isUsingWorkspaces = "workspaces" in packageJson || isPnpmWorkspaces;
if (isDependency || isDevDependency) {
return {
installType: isDependency ? "dependencies" : "devDependencies",
isUsingWorkspaces,
};
}
return {
installType: undefined,
isUsingWorkspaces,
};
}
/**
Finding the correct command to upgrade depends on two things:
1. The package manager
2. The install method (local or global)
We try global first to let turbo handle the inference, then we try local.
**/
export default function getTurboUpgradeCommand({
directory,
to,
}: {
directory: string;
to?: string;
}) {
const turboBinaryPathFromGlobal = exec(`turbo bin`, {
cwd: directory,
stdio: "pipe",
});
const packageManagerGlobalBinaryPaths = getGlobalBinaryPaths();
const globalPackageManager = Object.keys(
packageManagerGlobalBinaryPaths
).find((packageManager) => {
const packageManagerBinPath =
packageManagerGlobalBinaryPaths[packageManager as PackageManager];
if (packageManagerBinPath && turboBinaryPathFromGlobal) {
return turboBinaryPathFromGlobal.includes(packageManagerBinPath);
}
return false;
}) as PackageManager;
if (turboBinaryPathFromGlobal && globalPackageManager) {
// figure which package manager we need to upgrade
return getGlobalUpgradeCommand(globalPackageManager, to);
} else {
const packageManager = getPackageManager({ directory });
// we didn't find a global install, so we'll try to find a local one
const { installType, isUsingWorkspaces } = getInstallType({ directory });
if (packageManager && installType) {
const packageManagerVersion = getPackageManagerVersion(
packageManager,
directory
);
return getLocalUpgradeCommand({
packageManager,
packageManagerVersion,
installType,
isUsingWorkspaces,
to,
});
}
}
return undefined;
}
|