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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
import fs from "fs-extra";
import path from "path";
import { ConvertError } from "../errors";
import updateDependencies from "../updateDependencies";
import {
DetectArgs,
ReadArgs,
CreateArgs,
RemoveArgs,
CleanArgs,
Project,
ConvertArgs,
ManagerHandler,
} from "../types";
import {
getMainStep,
getWorkspaceInfo,
getPackageJson,
expandWorkspaces,
getWorkspacePackageManager,
expandPaths,
} from "../utils";
/**
* Check if a given project is using npm workspaces
* Verify by checking for the existence of:
* 1. package-lock.json
* 2. packageManager field in package.json
*/
async function detect(args: DetectArgs): Promise<boolean> {
const lockFile = path.join(args.workspaceRoot, "package-lock.json");
const packageManager = getWorkspacePackageManager({
workspaceRoot: args.workspaceRoot,
});
return fs.existsSync(lockFile) || packageManager === "npm";
}
/**
Read workspace data from npm workspaces into generic format
*/
async function read(args: ReadArgs): Promise<Project> {
const isNpm = await detect(args);
if (!isNpm) {
throw new ConvertError("Not an npm project", {
type: "package_manager-unexpected",
});
}
const packageJson = getPackageJson(args);
const { name, description } = getWorkspaceInfo(args);
return {
name,
description,
packageManager: "npm",
paths: expandPaths({
root: args.workspaceRoot,
lockFile: "package-lock.json",
}),
workspaceData: {
globs: packageJson.workspaces || [],
workspaces: expandWorkspaces({
workspaceGlobs: packageJson.workspaces,
...args,
}),
},
};
}
/**
* Create npm workspaces from generic format
*
* Creating npm workspaces involves:
* 1. Adding the workspaces field in package.json
* 2. Setting the packageManager field in package.json
* 3. Updating all workspace package.json dependencies to ensure correct format
*/
async function create(args: CreateArgs): Promise<void> {
const { project, options, to, logger } = args;
const hasWorkspaces = project.workspaceData.globs.length > 0;
logger.mainStep(
getMainStep({ packageManager: "npm", action: "create", project })
);
const packageJson = getPackageJson({ workspaceRoot: project.paths.root });
logger.rootHeader();
// package manager
logger.rootStep(
`adding "packageManager" field to ${path.relative(
project.paths.root,
project.paths.packageJson
)}`
);
packageJson.packageManager = `${to.name}@${to.version}`;
if (hasWorkspaces) {
// workspaces field
logger.rootStep(
`adding "workspaces" field to ${path.relative(
project.paths.root,
project.paths.packageJson
)}`
);
packageJson.workspaces = project.workspaceData.globs;
// write package.json here instead of deferring to avoid negating the changes made by updateDependencies
if (!options?.dry) {
fs.writeJSONSync(project.paths.packageJson, packageJson, { spaces: 2 });
}
// root dependencies
updateDependencies({
workspace: { name: "root", paths: project.paths },
project,
to,
logger,
options,
});
// workspace dependencies
logger.workspaceHeader();
project.workspaceData.workspaces.forEach((workspace) =>
updateDependencies({ workspace, project, to, logger, options })
);
} else {
if (!options?.dry) {
fs.writeJSONSync(project.paths.packageJson, packageJson, { spaces: 2 });
}
}
}
/**
* Remove npm workspace data
* Removing npm workspaces involves:
* 1. Removing the workspaces field from package.json
* 2. Removing the node_modules directory
*/
async function remove(args: RemoveArgs): Promise<void> {
const { project, logger, options } = args;
const hasWorkspaces = project.workspaceData.globs.length > 0;
logger.mainStep(
getMainStep({ packageManager: "npm", action: "remove", project })
);
const packageJson = getPackageJson({ workspaceRoot: project.paths.root });
if (hasWorkspaces) {
logger.subStep(
`removing "workspaces" field in ${project.name} root "package.json"`
);
delete packageJson.workspaces;
}
logger.subStep(
`removing "packageManager" field in ${project.name} root "package.json"`
);
delete packageJson.packageManager;
if (!options?.dry) {
fs.writeJSONSync(project.paths.packageJson, packageJson, { spaces: 2 });
// collect all workspace node_modules directories
const allModulesDirs = [
project.paths.nodeModules,
...project.workspaceData.workspaces.map((w) => w.paths.nodeModules),
];
try {
logger.subStep(`removing "node_modules"`);
await Promise.all(
allModulesDirs.map((dir) =>
fs.rm(dir, { recursive: true, force: true })
)
);
} catch (err) {
throw new ConvertError("Failed to remove node_modules", {
type: "error_removing_node_modules",
});
}
}
}
/**
* Clean is called post install, and is used to clean up any files
* from this package manager that were needed for install,
* but not required after migration
*/
async function clean(args: CleanArgs): Promise<void> {
const { project, logger, options } = args;
logger.subStep(
`removing ${path.relative(project.paths.root, project.paths.lockfile)}`
);
if (!options?.dry) {
fs.rmSync(project.paths.lockfile, { force: true });
}
}
/**
* Attempts to convert an existing, non npm lockfile to an npm lockfile
*
* If this is not possible, the non npm lockfile is removed
*/
async function convertLock(args: ConvertArgs): Promise<void> {
const { project, options } = args;
if (project.packageManager !== "npm") {
// remove the lockfile
if (!options?.dry) {
fs.rmSync(project.paths.lockfile, { force: true });
}
}
}
const npm: ManagerHandler = {
detect,
read,
create,
remove,
clean,
convertLock,
};
export default npm;
|