Skip to content

Commit

Permalink
Fix: non-zero exit codes, env arguments (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtimebuddy authored Mar 7, 2024
1 parent db56a05 commit 8c527c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
23 changes: 11 additions & 12 deletions lib/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,22 @@ const lint = async (additionalArgs = []) =>
await promises.copyFile(path.join(__dirname, 'configs/eslint.json'), eslintrcJson);
}

const cleanup = async () =>
{
if (!isDefined)
{
await promises.unlink(eslintrcJson);
}
};

await spawn('eslint', [
'--ext', '.ts',
'--ext', '.js',
'--ext', '.mjs',
'--no-error-on-unmatched-pattern',
...extensionConfig.lint,
...additionalArgs,
], {
onClose: async () =>
{
if (!isDefined)
{
await promises.unlink(eslintrcJson);
}
}
});
], cleanup);
};

/** Open the exmaples folder */
Expand Down Expand Up @@ -181,11 +181,10 @@ const docs = async () =>
const templateConfig = path.join(__dirname, 'configs/webdoc.json');
const webdocConfig = path.join(process.cwd(), '.webdoc.json');
const contents = await promises.readFile(templateConfig, 'utf8');
const cleanup = async () => await promises.unlink(webdocConfig);

await promises.writeFile(webdocConfig, template(contents, extensionConfig), 'utf8');
await spawn('webdoc', ['-c', webdocConfig], {
onClose: async () => await promises.unlink(webdocConfig),
});
await spawn('webdoc', ['-c', webdocConfig], cleanup);
};

/** Clean up the package contents */
Expand Down
27 changes: 21 additions & 6 deletions lib/utils/spawn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import { prefix } from './prefix.mjs';

const projectPath = path.join(process.cwd());

/** Utility to do spawn but as a Promise */
export const spawn = (command, args, { onClose, ...options } = {}) =>
new Promise((resolve, reject) =>
/**
* Utility to do spawn but as a Promise
* @param {string} command - Command to run
* @param {string[]} args - Arguments for the command
* @param {Function} [onClose] - Function to run when the process closes or errors
* this is useful for cleaning up temporary files even if the process errors.
*/
export const spawn = (command, args, onClose) =>
new Promise((resolve) =>
{
if (!extensionConfig.silent)
{
Expand All @@ -20,18 +26,27 @@ export const spawn = (command, args, { onClose, ...options } = {}) =>
stdio: 'inherit',
// See https://nodejs.org/api/child_process.html#spawning-bat-and-cmd-files-on-windows
shell: process.platform.startsWith('win'),
...options,
env: process.env,
});

child.on('close', async (code) =>
child.once('close', async (code) =>
{
await onClose?.();

if (code === 0)
{
resolve();
}
else
{
process.exit(code);
}
});
child.once('error', async () =>
{
await onClose?.();

process.exit(1);
});
child.on('error', reject);
});

0 comments on commit 8c527c7

Please sign in to comment.