Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-zero exit codes, env arguments #17

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});

Loading