upgradePackageData.js
2.72 KB
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
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const prompts_1 = __importDefault(require("prompts"));
const semver_1 = require("semver");
const version_util_1 = require("../version-util");
/**
* @returns String safe for use in `new RegExp()`
*/
function escapeRegexp(s) {
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); // Thanks Stack Overflow!
}
/**
* Upgrade the dependency declarations in the package data.
*
* @param pkgData The package.json data, as utf8 text
* @param oldDependencies Old dependencies {package: range}
* @param newDependencies New dependencies {package: range}
* @param newVersions New versions {package: version}
* @param [options={}]
* @returns The updated package data, as utf8 text
* @description Side Effect: prompts
*/
async function upgradePackageData(pkgData, oldDependencies, newDependencies, newVersions, options = {}) {
// copy newDependencies for mutation via interactive mode
const selectedNewDependencies = { ...newDependencies };
let newPkgData = pkgData;
// eslint-disable-next-line fp/no-loops
for (const dependency in newDependencies) {
if (!options.minimal || !(0, semver_1.satisfies)(newVersions[dependency], oldDependencies[dependency])) {
if (options.interactive) {
const to = (0, version_util_1.colorizeDiff)(oldDependencies[dependency], newDependencies[dependency] || '');
const response = await (0, prompts_1.default)({
type: 'confirm',
name: 'value',
message: `Do you want to upgrade: ${dependency} ${oldDependencies[dependency]} → ${to}?`,
initial: true,
onState: state => {
if (state.aborted) {
process.nextTick(() => process.exit(1));
}
},
});
if (!response.value) {
// continue loop to next dependency and skip updating newPkgData
delete selectedNewDependencies[dependency]; // eslint-disable-line fp/no-delete
continue;
}
}
const expression = `"${dependency}"\\s*:\\s*"${escapeRegexp(`${oldDependencies[dependency]}"`)}`;
const regExp = new RegExp(expression, 'g');
newPkgData = newPkgData.replace(regExp, `"${dependency}": "${newDependencies[dependency]}"`);
}
}
return { newPkgData, selectedNewDependencies };
}
exports.default = upgradePackageData;
//# sourceMappingURL=upgradePackageData.js.map