checkPlugin: Move dependency update logic to a generic function

This will make it possible to reuse the logic for `peerDependencies`.
image-example
Richard Hansen 2021-01-09 18:51:31 -05:00 committed by John McLear
parent 747f323552
commit 42da976705
1 changed files with 26 additions and 18 deletions

View File

@ -37,6 +37,8 @@ const autoUpdate = optArgs.indexOf('autoupdate') !== -1;
// Should we automcommit and npm publish?!
const autoCommit = optArgs.indexOf('autocommit') !== -1;
let hasAutoFixed = false;
const execSync = (cmd, opts = {}) => (childProcess.execSync(cmd, {
cwd: `${pluginPath}/`,
...opts,
@ -48,6 +50,28 @@ const writePackageJson = (obj) => {
return fs.writeFileSync(`${pluginPath}/package.json`, s);
};
const updateDeps = (parsedPackageJson, key, wantDeps) => {
const {[key]: deps = {}} = parsedPackageJson;
let changed = false;
for (const [pkg, ver] of Object.entries(wantDeps)) {
if (deps[pkg] === ver) continue;
if (deps[pkg] == null) {
console.warn(`Missing dependency in ${key}: '${pkg}': '${ver}'`);
} else {
console.warn(`Dependency mismatch in ${key}: '${pkg}': '${ver}' (current: ${deps[pkg]})`);
}
if (autoFix) {
deps[pkg] = ver;
changed = true;
}
}
if (changed) {
hasAutoFixed = true;
parsedPackageJson[key] = deps;
writePackageJson(parsedPackageJson);
}
};
const prepareRepo = () => {
let branch = execSync('git symbolic-ref HEAD');
if (branch !== 'refs/heads/master' && branch !== 'refs/heads/main') {
@ -87,7 +111,6 @@ fs.readdir(pluginPath, (err, rootFiles) => {
// some files we need to know the actual file name. Not compulsory but might help in the future.
let readMeFileName;
let repository;
let hasAutoFixed = false;
for (let i = 0; i < rootFiles.length; i++) {
if (rootFiles[i].toLowerCase().indexOf('readme') !== -1) readMeFileName = rootFiles[i];
@ -213,8 +236,7 @@ fs.readdir(pluginPath, (err, rootFiles) => {
repository = parsedPackageJSON.repository.url;
}
// include lint config
const lintDeps = {
updateDeps(parsedPackageJSON, 'devDependencies', {
'eslint': '^7.17.0',
'eslint-config-etherpad': '^1.0.22',
'eslint-plugin-eslint-comments': '^3.2.0',
@ -223,21 +245,7 @@ fs.readdir(pluginPath, (err, rootFiles) => {
'eslint-plugin-prefer-arrow': '^1.2.2',
'eslint-plugin-promise': '^4.2.1',
'eslint-plugin-you-dont-need-lodash-underscore': '^6.10.0',
};
const {devDependencies = {}} = parsedPackageJSON;
let lintDepsNeedUpdating = false;
for (const [pkg, ver] of Object.entries(lintDeps)) {
if (devDependencies[pkg] !== ver) {
console.warn(`Missing/outdated ESLint dependency: '${pkg}': '${ver}' ` +
`(current: ${devDependencies[pkg]})`);
lintDepsNeedUpdating = true;
}
}
if (lintDepsNeedUpdating && autoFix) {
hasAutoFixed = true;
parsedPackageJSON.devDependencies = Object.assign(devDependencies, lintDeps);
writePackageJson(parsedPackageJSON);
}
});
// include peer deps config
if (packageJSON.toLowerCase().indexOf('peerdependencies') === -1 || !parsedPackageJSON.peerDependencies) {