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
|
import fs from "fs-extra";
import chalk from "chalk";
import path from "path";
import {
Project,
Workspace,
DependencyList,
PackageManagerDetails,
Options,
PackageJsonDependencies,
} from "./types";
import { Logger } from "./logger";
import { getPackageJson } from "./utils";
function updateDependencyList({
dependencyList,
project,
to,
}: {
dependencyList: DependencyList;
project: Project;
to: PackageManagerDetails;
}): { dependencyList: DependencyList; updated: Array<string> } {
const updated: Array<string> = [];
project.workspaceData.workspaces.forEach((workspace) => {
const { name } = workspace;
if (dependencyList[name]) {
const workspaceVersion = dependencyList[name];
const version = workspaceVersion.startsWith("workspace:")
? workspaceVersion.slice("workspace:".length)
: workspaceVersion;
dependencyList[name] =
to.name === "pnpm" ? `workspace:${version}` : version;
updated.push(name);
}
});
return { dependencyList, updated };
}
export default function updateDependencies({
project,
workspace,
to,
logger,
options,
}: {
workspace: Workspace;
project: Project;
to: PackageManagerDetails;
logger: Logger;
options?: Options;
}): void {
// this step isn't required if moving between yarn / npm
if (
["yarn", "npm"].includes(to.name) &&
["yarn", "npm"].includes(project.packageManager)
) {
return;
}
// update all dependencies
const workspacePackageJson = getPackageJson({
workspaceRoot: workspace.paths.root,
});
// collect stats as we go for consolidated output at the end
const stats: Record<keyof PackageJsonDependencies, Array<string>> = {
dependencies: [],
devDependencies: [],
peerDependencies: [],
optionalDependencies: [],
};
const allDependencyKeys: Array<keyof PackageJsonDependencies> = [
"dependencies",
"devDependencies",
"peerDependencies",
"optionalDependencies",
];
allDependencyKeys.forEach((depKey) => {
const depList = workspacePackageJson[depKey];
if (depList) {
const { updated, dependencyList } = updateDependencyList({
dependencyList: depList,
project,
to,
});
workspacePackageJson[depKey] = dependencyList;
stats[depKey] = updated;
}
});
const toLog = (key: keyof PackageJsonDependencies) => {
const total = stats[key].length;
if (total > 0) {
return `${chalk.green(total.toString())} ${key}`;
}
return undefined;
};
const allChanges = allDependencyKeys.map(toLog).filter(Boolean);
const workspaceLocation = `./${path.relative(
project.paths.root,
workspace.paths.packageJson
)}`;
if (allChanges.length >= 1) {
let logLine = "updating";
allChanges.forEach((stat, idx) => {
if (allChanges.length === 1) {
logLine += ` ${stat} in ${workspaceLocation}`;
} else {
if (idx === allChanges.length - 1) {
logLine += `and ${stat} in ${workspaceLocation}`;
} else {
logLine += ` ${stat}, `;
}
}
});
logger.workspaceStep(logLine);
} else {
logger.workspaceStep(
`no workspace dependencies found in ${workspaceLocation}`
);
}
if (!options?.dry) {
fs.writeJSONSync(workspace.paths.packageJson, workspacePackageJson, {
spaces: 2,
});
}
}
|