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,32 +20,33 @@ 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 {
@ -60,9 +61,9 @@ const checkEntries = (got, want) => {
} }
} }
return changed; return changed;
}; };
const updateDeps = async (parsedPackageJson, key, wantDeps) => { const updateDeps = async (parsedPackageJson, key, wantDeps) => {
const {[key]: deps = {}} = parsedPackageJson; const {[key]: deps = {}} = parsedPackageJson;
let changed = false; let changed = false;
for (const [pkg, verInfo] of Object.entries(wantDeps)) { for (const [pkg, verInfo] of Object.entries(wantDeps)) {
@ -83,9 +84,9 @@ const updateDeps = async (parsedPackageJson, key, wantDeps) => {
parsedPackageJson[key] = deps; parsedPackageJson[key] = deps;
await writePackageJson(parsedPackageJson); await writePackageJson(parsedPackageJson);
} }
}; };
const prepareRepo = () => { const prepareRepo = () => {
const modified = execSync('git diff-files --name-status'); const modified = execSync('git diff-files --name-status');
if (modified !== '') throw new Error(`working directory has modifications:\n${modified}`); if (modified !== '') throw new Error(`working directory has modifications:\n${modified}`);
const untracked = execSync('git ls-files -o --exclude-standard'); const untracked = execSync('git ls-files -o --exclude-standard');
@ -108,9 +109,9 @@ const prepareRepo = () => {
execSync('git pull --ff-only', {stdio: 'inherit'}); execSync('git pull --ff-only', {stdio: 'inherit'});
if (execSync('git rev-list @{u}...') !== '') throw new Error('repo contains unpushed commits'); if (execSync('git rev-list @{u}...') !== '') throw new Error('repo contains unpushed commits');
} }
}; };
const checkFile = async (srcFn, dstFn) => { const checkFile = async (srcFn, dstFn) => {
const outFn = path.join(pluginPath, dstFn); const outFn = path.join(pluginPath, dstFn);
const wantContents = await fsp.readFile(srcFn, {encoding: 'utf8'}); const wantContents = await fsp.readFile(srcFn, {encoding: 'utf8'});
let gotContents = null; let gotContents = null;
@ -127,13 +128,12 @@ const checkFile = async (srcFn, dstFn) => {
await fsp.writeFile(outFn, wantContents); await fsp.writeFile(outFn, wantContents);
} }
} }
}; };
if (autoPush) { if (autoPush) {
console.warn('Auto push is enabled, I hope you know what you are doing...'); 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.