checkPlugin: Move everything inside async IIFE

This commit is contained in:
Richard Hansen 2022-02-20 17:35:56 -05:00
parent 2e0e872ae3
commit 5a66abae8b

View file

@ -20,120 +20,120 @@ const fsp = fs.promises;
const childProcess = require('child_process'); const childProcess = require('child_process');
const path = require('path'); const path = require('path');
// get plugin name & path from user input (async () => {
const pluginName = process.argv[2]; // get plugin name & path from user input
const pluginName = process.argv[2];
if (!pluginName) throw new Error('no plugin name specified'); if (!pluginName) throw new Error('no plugin name specified');
const pluginPath = `node_modules/${pluginName}`; const pluginPath = `node_modules/${pluginName}`;
console.log(`Checking the plugin: ${pluginName}`); console.log(`Checking the plugin: ${pluginName}`);
const optArgs = process.argv.slice(3); const optArgs = process.argv.slice(3);
const autoPush = optArgs.includes('autopush'); const autoPush = optArgs.includes('autopush');
const autoCommit = autoPush || optArgs.includes('autocommit'); const autoCommit = autoPush || optArgs.includes('autocommit');
const autoFix = autoCommit || optArgs.includes('autofix'); const autoFix = autoCommit || optArgs.includes('autofix');
const execSync = (cmd, opts = {}) => (childProcess.execSync(cmd, { const execSync = (cmd, opts = {}) => (childProcess.execSync(cmd, {
cwd: `${pluginPath}/`, cwd: `${pluginPath}/`,
...opts, ...opts,
}) || '').toString().replace(/\n+$/, ''); }) || '').toString().replace(/\n+$/, '');
const writePackageJson = async (obj) => { const writePackageJson = async (obj) => {
let s = JSON.stringify(obj, null, 2); let s = JSON.stringify(obj, null, 2);
if (s.length && s.slice(s.length - 1) !== '\n') s += '\n'; if (s.length && s.slice(s.length - 1) !== '\n') s += '\n';
return await fsp.writeFile(`${pluginPath}/package.json`, s); return await fsp.writeFile(`${pluginPath}/package.json`, s);
}; };
const checkEntries = (got, want) => { const checkEntries = (got, want) => {
let changed = false; let changed = false;
for (const [key, val] of Object.entries(want)) { for (const [key, val] of Object.entries(want)) {
try { try {
assert.deepEqual(got[key], val); assert.deepEqual(got[key], val);
} catch (err) { } catch (err) {
console.warn(`${key} possibly outdated.`); console.warn(`${key} possibly outdated.`);
console.warn(err.message); console.warn(err.message);
if (autoFix) {
got[key] = val;
changed = true;
}
}
}
return changed;
};
const updateDeps = async (parsedPackageJson, key, wantDeps) => {
const {[key]: deps = {}} = parsedPackageJson;
let changed = false;
for (const [pkg, verInfo] of Object.entries(wantDeps)) {
const {ver, overwrite = true} = typeof verInfo === 'string' ? {ver: verInfo} : verInfo;
if (deps[pkg] === ver) continue;
if (deps[pkg] == null) {
console.warn(`Missing dependency in ${key}: '${pkg}': '${ver}'`);
} else {
if (!overwrite) continue;
console.warn(`Dependency mismatch in ${key}: '${pkg}': '${ver}' (current: ${deps[pkg]})`);
}
if (autoFix) { if (autoFix) {
got[key] = val; deps[pkg] = ver;
changed = true; changed = true;
} }
} }
} if (changed) {
return changed; parsedPackageJson[key] = deps;
}; await writePackageJson(parsedPackageJson);
const updateDeps = async (parsedPackageJson, key, wantDeps) => {
const {[key]: deps = {}} = parsedPackageJson;
let changed = false;
for (const [pkg, verInfo] of Object.entries(wantDeps)) {
const {ver, overwrite = true} = typeof verInfo === 'string' ? {ver: verInfo} : verInfo;
if (deps[pkg] === ver) continue;
if (deps[pkg] == null) {
console.warn(`Missing dependency in ${key}: '${pkg}': '${ver}'`);
} else {
if (!overwrite) continue;
console.warn(`Dependency mismatch in ${key}: '${pkg}': '${ver}' (current: ${deps[pkg]})`);
} }
if (autoFix) { };
deps[pkg] = ver;
changed = true; const prepareRepo = () => {
} const modified = execSync('git diff-files --name-status');
} if (modified !== '') throw new Error(`working directory has modifications:\n${modified}`);
if (changed) { const untracked = execSync('git ls-files -o --exclude-standard');
parsedPackageJson[key] = deps; if (untracked !== '') throw new Error(`working directory has untracked files:\n${untracked}`);
await writePackageJson(parsedPackageJson); const indexStatus = execSync('git diff-index --cached --name-status HEAD');
} if (indexStatus !== '') throw new Error(`uncommitted staged changes to files:\n${indexStatus}`);
}; let br;
if (autoCommit) {
br = execSync('git symbolic-ref HEAD');
if (!br.startsWith('refs/heads/')) throw new Error('detached HEAD');
br = br.replace(/^refs\/heads\//, '');
execSync('git rev-parse --verify -q HEAD^0 || ' +
`{ echo "Error: no commits on ${br}" >&2; exit 1; }`);
execSync('git config --get user.name');
execSync('git config --get user.email');
}
if (autoPush) {
if (!['master', 'main'].includes(br)) throw new Error('master/main not checked out');
execSync('git rev-parse --verify @{u}');
execSync('git pull --ff-only', {stdio: 'inherit'});
if (execSync('git rev-list @{u}...') !== '') throw new Error('repo contains unpushed commits');
}
};
const checkFile = async (srcFn, dstFn) => {
const outFn = path.join(pluginPath, dstFn);
const wantContents = await fsp.readFile(srcFn, {encoding: 'utf8'});
let gotContents = null;
try {
gotContents = await fsp.readFile(outFn, {encoding: 'utf8'});
} catch (err) { /* treat as if the file doesn't exist */ }
try {
assert.equal(gotContents, wantContents);
} catch (err) {
console.warn(`File ${dstFn} is out of date`);
console.warn(err.message);
if (autoFix) {
await fsp.mkdir(path.dirname(outFn), {recursive: true});
await fsp.writeFile(outFn, wantContents);
}
}
};
const prepareRepo = () => {
const modified = execSync('git diff-files --name-status');
if (modified !== '') throw new Error(`working directory has modifications:\n${modified}`);
const untracked = execSync('git ls-files -o --exclude-standard');
if (untracked !== '') throw new Error(`working directory has untracked files:\n${untracked}`);
const indexStatus = execSync('git diff-index --cached --name-status HEAD');
if (indexStatus !== '') throw new Error(`uncommitted staged changes to files:\n${indexStatus}`);
let br;
if (autoCommit) {
br = execSync('git symbolic-ref HEAD');
if (!br.startsWith('refs/heads/')) throw new Error('detached HEAD');
br = br.replace(/^refs\/heads\//, '');
execSync('git rev-parse --verify -q HEAD^0 || ' +
`{ echo "Error: no commits on ${br}" >&2; exit 1; }`);
execSync('git config --get user.name');
execSync('git config --get user.email');
}
if (autoPush) { if (autoPush) {
if (!['master', 'main'].includes(br)) throw new Error('master/main not checked out'); console.warn('Auto push is enabled, I hope you know what you are doing...');
execSync('git rev-parse --verify @{u}');
execSync('git pull --ff-only', {stdio: 'inherit'});
if (execSync('git rev-list @{u}...') !== '') throw new Error('repo contains unpushed commits');
} }
};
const checkFile = async (srcFn, dstFn) => {
const outFn = path.join(pluginPath, dstFn);
const wantContents = await fsp.readFile(srcFn, {encoding: 'utf8'});
let gotContents = null;
try {
gotContents = await fsp.readFile(outFn, {encoding: 'utf8'});
} catch (err) { /* treat as if the file doesn't exist */ }
try {
assert.equal(gotContents, wantContents);
} catch (err) {
console.warn(`File ${dstFn} is out of date`);
console.warn(err.message);
if (autoFix) {
await fsp.mkdir(path.dirname(outFn), {recursive: true});
await fsp.writeFile(outFn, wantContents);
}
}
};
if (autoPush) {
console.warn('Auto push is enabled, I hope you know what you are doing...');
}
(async () => {
const files = await fsp.readdir(pluginPath); const files = await fsp.readdir(pluginPath);
// some files we need to know the actual file name. Not compulsory but might help in the future. // some files we need to know the actual file name. Not compulsory but might help in the future.