-
Notifications
You must be signed in to change notification settings - Fork 582
/
utils.js
362 lines (343 loc) · 11.3 KB
/
utils.js
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
const tsMorph = require("ts-morph");
const path = require("path");
const fs = require("fs");
const prettier = require("prettier");
const loadJson = require("load-json-file");
const writeJson = require("write-json-file");
const semverCoerce = require("semver/functions/coerce");
const execa = require("execa");
/**
*
* @param pkg {string}
* @param version {string}
*
* @return {boolean}
*/
const validateVersion = (pkg, version) => {
if (version === "latest") {
return true;
}
const coerced = semverCoerce(version);
if (coerced) {
return true;
}
return false;
};
/**
* @deprecated
*/
const insertImport = (source, name, pkg) => {
throw new Error("Should not be used anymore.");
const statements = source.getStatements();
let importAlreadyExists = false;
const lastImportStatement = statements.reduce((indexAt, statement, index) => {
if (statement.getKind() !== tsMorph.SyntaxKind.ImportDeclaration) {
return indexAt;
}
const compilerNode = statement.compilerNode || {};
const moduleSpecifier = compilerNode.moduleSpecifier || {};
const importedPackageName = moduleSpecifier.text;
if (importedPackageName === pkg) {
importAlreadyExists = true;
}
return index > indexAt ? index : indexAt;
}, -1);
if (importAlreadyExists) {
throw new Error(`Package "${pkg}" is already imported.`);
} else if (lastImportStatement === -1) {
throw new Error(
`Could not find last import statement so we can insert new import "${name}"`
);
}
console.log(`Inserting import to position ${lastImportStatement + 1}`);
source.insertStatements(lastImportStatement + 1, `import ${name} from "${pkg}";`);
};
/**
*
* @param context {ContextInterface}
* @param targetPath {string}
* @param packages {object}
*/
const addPackagesToDependencies = (context, targetPath, packages) => {
addPackagesToDeps("dependencies", context, targetPath, packages);
};
/**
* @param context {ContextInterface}
* @param targetPath {string}
* @param packages {object}
*/
const addPackagesToDevDependencies = (context, targetPath, packages) => {
addPackagesToDeps("devDependencies", context, targetPath, packages);
};
/**
* @param context {ContextInterface}
* @param targetPath {string}
* @param packages {object}
*/
const addPackagesToPeerDependencies = (context, targetPath, packages) => {
addPackagesToDeps("peerDependencies", context, targetPath, packages);
};
/**
*
* @param type {string}
* @param context {ContextInterface}
* @param targetPath {string}
* @param packages {object}
*/
const allowedPackageDependencyTypes = ["dependencies", "devDependencies", "peerDependencies"];
const addPackagesToDeps = (type, context, targetPath, packages) => {
if (!allowedPackageDependencyTypes.includes(type)) {
context.error(`Package dependency type "${type}" is not valid.`);
return;
}
const file = `${targetPath}/package.json`;
if (!fs.existsSync(file)) {
context.error(`There is no package.json file at path "${file}".`);
return;
}
const json = loadJson.sync(file);
if (!json) {
context.error(`There is no package.json file "${file}"`);
return;
}
const dependencies = json[type] || {};
for (const pkg in packages) {
if (!packages.hasOwnProperty(pkg)) {
continue;
}
const version = packages[pkg];
if (version === null) {
// Remove package from deps
delete dependencies[pkg];
continue;
}
if (!validateVersion(pkg, version)) {
context.error(`Package "${pkg}" version is not a valid semver version: "${version}".`);
continue;
}
dependencies[pkg] = version;
}
json[type] = dependencies;
writeJson.sync(file, json);
};
const createMorphProject = files => {
const project = new tsMorph.Project();
for (const file of files) {
project.addSourceFileAtPath(file);
}
return project;
};
const prettierFormat = async (files, context) => {
try {
context.info("Running prettier...");
for (const file of files) {
const filePath = path.join(process.cwd(), file);
const options = await prettier.resolveConfig(filePath);
const fileContentRaw = fs.readFileSync(filePath).toString("utf8");
const fileContentFormatted = prettier.format(fileContentRaw, {
...options,
filepath: filePath
});
fs.writeFileSync(filePath, fileContentFormatted);
}
context.info("Finished formatting files.");
} catch (ex) {
console.log(context.error.hl("Prettier failed."));
context.error(ex.message);
if (ex.stdout) {
console.log(ex.stdout);
}
}
};
/**
* Run to install new packages in the project.
*/
const yarnInstall = async ({ context }) => {
const { info, error } = context;
try {
info("Installing new packages...");
await execa("yarn", { cwd: process.cwd() });
info("Finished installing new packages.");
} catch (ex) {
error("Installation of new packages failed.");
console.log(error(ex.message));
if (ex.stdout) {
console.log(ex.stdout);
}
}
};
/**
*
* @param plugins {tsMorph.Node}
* @param afterElement {String|undefined}
* @returns {null|number}
*/
const findElementIndex = (plugins, afterElement) => {
if (!afterElement) {
return null;
}
const index = plugins
.getInitializer()
.getElements()
.findIndex(node => {
return Node.isCallExpression(node) && node.getExpression().getText() === afterElement;
});
if (index >= 0) {
return index;
}
return null;
};
/**
*
* @param context {CliContext}
* @param source {tsMorph.SourceFile}
* @param imports {{elementName: String, importPath: String, afterElement: String | undefined, isImportCallable: boolean, addToPlugins: boolean}[]}
* @param file {String}
*/
const addImportsToSource = ({ context, source, imports, file }) => {
const { info, warning, error } = context;
let pluginsElement;
const getPluginsElement = () => {
if (pluginsElement) {
return pluginsElement;
}
/**
* We need the plugins property in the createHandler argument.
* @type {tsMorph.Node}
*/
pluginsElement = source.getFirstDescendant(
node => tsMorph.Node.isPropertyAssignment(node) && node.getName() === "plugins"
);
if (!pluginsElement) {
error(
`Cannot find "plugins" property of the "createHandler" argument object in ${info.hl(
file
)}.`
);
}
return pluginsElement;
};
for (const value of imports) {
const {
elementName,
importPath,
afterElement,
afterImport,
isImportCallable = true,
addToPlugins = true
} = value;
const importDefinition = source.getImportDeclaration(importPath);
/**
* If there is required import already, do not proceed.
* We do not check if imported plugins are actually sent into the handler because we dont know what user might have done.
*/
if (importDefinition) {
info(`Import ${info.hl(importPath)} already exists in ${info.hl(file)}.`);
continue;
}
let addedImport = false;
/**
* If we need to import after already defined import.
*/
if (afterImport) {
const afterImportDeclaration = source.getImportDeclaration(afterImport);
if (afterImportDeclaration) {
source.insertImportDeclaration(afterImportDeclaration.getChildIndex() + 1, {
defaultImport: elementName,
moduleSpecifier: importPath
});
addedImport = true;
} else {
warning(
`Import ${warning.hl(afterImport)} does not exist in ${warning.hl(
file
)}. Adding new import after the last one.`
);
}
}
/**
* If no import was added, add it at the end.
*/
if (addedImport === false) {
/**
* We add the import and after that we add the imported name to the plugins array in the createHandler.
*/
source.addImportDeclaration({
defaultImport: elementName,
moduleSpecifier: importPath
});
}
if (addToPlugins === false) {
continue;
}
const importedElementName = `${elementName}${isImportCallable === false ? "" : "()"}`;
const plugins = getPluginsElement();
if (!plugins) {
error(`Could not add ${error.hl(importedElementName)} to the plugins.`);
continue;
}
/**
* If after is specified, add the imported value after the one given in the args..
*/
if (afterElement) {
const afterIndex = findElementIndex(plugins, afterElement);
if (afterIndex === null) {
warning(
`Could not find ${warning.hl(
after
)} of the createHandler.plugins array in ${warning.hl(file)}.`
);
} else {
plugins.getInitializer().insertElement(afterIndex + 1, importedElementName);
continue;
}
}
/**
* Add imported plugin at the end of the array as no other options are viable.
*/
plugins.getInitializer().addElement(importedElementName);
}
};
/**
* @param packageJsonPath {String}
* @param pathsToAdd {String[]}
*/
const addWorkspaceToRootPackageJson = async (packageJsonPath, pathsToAdd) => {
const rootPackageJson = await loadJson(packageJsonPath);
pathsToAdd.forEach(pathToAdd => {
// Ensure forward slashes are used.
pathToAdd = pathToAdd.replace(/\\/g, "/");
// Add it to workspaces packages if not already
if (!rootPackageJson.workspaces.packages.includes(pathToAdd)) {
rootPackageJson.workspaces.packages.push(pathToAdd);
}
});
await writeJson(packageJsonPath, rootPackageJson);
};
/**
* @param packageJsonPath {String}
* @param pathsToRemove {String[]}
*/
const removeWorkspaceToRootPackageJson = async (packageJsonPath, pathsToRemove) => {
const rootPackageJson = await loadJson(packageJsonPath);
pathsToRemove.forEach(pathToRemove => {
// Remove it from workspaces packages if present
const index = rootPackageJson.workspaces.packages.indexOf(pathToRemove);
if (index !== -1) {
rootPackageJson.workspaces.packages.splice(index, 1);
}
});
await writeJson(packageJsonPath, rootPackageJson);
};
module.exports = {
insertImport,
addPackagesToDependencies,
addPackagesToDevDependencies,
addPackagesToPeerDependencies,
createMorphProject,
prettierFormat,
yarnInstall,
addImportsToSource,
addWorkspaceToRootPackageJson,
removeWorkspaceToRootPackageJson
};