/
index.js
157 lines (128 loc) · 4.86 KB
/
index.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
// @ts-check
"use strict";
const { Command } = require("@lerna/command");
const { writeJsonFile, readJsonFile, workspaceRoot, joinPathFragments } = require("@nrwl/devkit");
const inquirer = require("inquirer");
const log = require("npmlog");
module.exports = factory;
function factory(argv) {
return new AddCachingCommand(argv);
}
class AddCachingCommand extends Command {
constructor(argv) {
super(argv, { skipValidations: true });
}
initialize() {
if (this.options.useNx === false) {
this.logger.error(
"add-caching",
"The `add-caching` command is only available when using the Nx task runner"
);
// eslint-disable-next-line no-process-exit
process.exit(1);
}
const packages = this.packageGraph?.rawPackageList || [];
const uniqueScriptNames = new Set();
for (const pkg of packages) {
for (const scriptName of Object.keys(pkg.scripts || {})) {
uniqueScriptNames.add(scriptName);
}
}
this.uniqueScriptNames = Array.from(uniqueScriptNames);
}
async execute() {
this.logger.info(
"add-caching",
"Please answer the following questions about the scripts found in your workspace in order to generate task runner configuration"
);
process.stdout.write("\n");
log.pause();
const { targetDefaults } = await inquirer.prompt([
{
type: "checkbox",
name: "targetDefaults",
message: "Which of the following scripts need to be run in deterministic/topoglogical order?\n",
choices: this.uniqueScriptNames,
},
]);
const { cacheableOperations } = await inquirer.prompt([
{
type: "checkbox",
name: "cacheableOperations",
message:
"Which of the following scripts are cacheable? (Produce the same output given the same input)\n",
choices: this.uniqueScriptNames,
},
]);
const scriptOutputs = {};
for (const scriptName of cacheableOperations) {
// eslint-disable-next-line no-await-in-loop
scriptOutputs[scriptName] = await inquirer.prompt([
{
type: "input",
name: scriptName,
message: `Where within a particular project directory does the "${scriptName}" script write its outputs?\n`,
default: "dist",
validate: (input) => {
if (!input) {
return "Please provide a project relative path";
}
return true;
},
},
]);
}
log.resume();
process.stdout.write("\n");
this.convertAnswersToNxConfig({ cacheableOperations, targetDefaults, scriptOutputs });
this.logger.success("add-caching", "Successfully updated task runner configuration in `nx.json`");
this.logger.info(
"add-caching",
"Learn more about task runner configuration here: https://lerna.js.org/docs/concepts/task-pipeline-configuration"
);
}
convertAnswersToNxConfig(answers) {
const nxJsonPath = joinPathFragments(workspaceRoot, "nx.json");
let nxJson = {};
try {
nxJson = readJsonFile(nxJsonPath);
// eslint-disable-next-line no-empty
} catch {}
nxJson.tasksRunnerOptions = nxJson.tasksRunnerOptions || {};
nxJson.tasksRunnerOptions.default = nxJson.tasksRunnerOptions.default || {};
nxJson.tasksRunnerOptions.default.runner =
nxJson.tasksRunnerOptions.default.runner || "nx/tasks-runners/default";
nxJson.tasksRunnerOptions.default.options = nxJson.tasksRunnerOptions.default.options || {};
if (nxJson.tasksRunnerOptions.default.options.cacheableOperations) {
this.logger.warn(
"add-caching",
"The `tasksRunnerOptions.default.cacheableOperations` property already exists in `nx.json` and will be overwritten by your answers"
);
}
nxJson.tasksRunnerOptions.default.options.cacheableOperations = answers.cacheableOperations;
if (nxJson.targetDefaults) {
this.logger.warn(
"add-caching",
"The `targetDefaults` property already exists in `nx.json` and will be overwritten by your answers"
);
}
nxJson.targetDefaults = nxJson.targetDefaults || {};
for (const scriptName of answers.targetDefaults) {
nxJson.targetDefaults[scriptName] = nxJson.targetDefaults[scriptName] || {};
nxJson.targetDefaults[scriptName] = { dependsOn: [`^${scriptName}`] };
}
for (const [scriptName, scriptAnswerData] of Object.entries(answers.scriptOutputs)) {
nxJson.targetDefaults[scriptName] = nxJson.targetDefaults[scriptName] || {};
nxJson.targetDefaults[scriptName].outputs = [`{projectRoot}/${scriptAnswerData[scriptName]}`];
}
writeJsonFile(nxJsonPath, nxJson);
}
// eslint-disable-next-line class-methods-use-this
normalizePathInput(pathInput) {
if (pathInput.startsWith("/")) {
return pathInput.substring(1);
}
return pathInput;
}
}
module.exports.AddCachingCommand = AddCachingCommand;