Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Improve error handling in prepack script (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
esdmr authored May 1, 2022
1 parent bbbcf0b commit 23a29a2
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions scripts/prepack.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
#!/usr/bin/env node
import fs from 'node:fs/promises';
import process from 'node:process';
import { execaCommand } from 'execa';

async function getMetadata () {
const metadata = JSON.parse(await fs.readFile('package.json', 'utf8'));

/**
* @param {Record<string, any>} packageMeta
*/
function processPackageJson (packageMeta) {
// Cleanup package.json
delete metadata.scripts;
delete metadata.devDependencies;
delete metadata.engines.pnpm;
delete metadata.imports['#test/*'];
delete metadata.packageManager;
delete metadata.pnpm;

for (const subpath of Object.keys(metadata.exports ?? {})) {
delete metadata.exports[subpath].docs;
delete packageMeta.scripts;
delete packageMeta.devDependencies;
delete packageMeta.engines.pnpm;
delete packageMeta.imports['#test/*'];
delete packageMeta.packageManager;
delete packageMeta.pnpm;

const exports = packageMeta.exports ?? {};

for (const subpath of Object.keys(exports)) {
delete exports[subpath].docs;
}

return metadata;
return packageMeta;
}

const metadata = await getMetadata();

/** @type {import('execa').Options} */
const options = {
stdio: 'inherit',
};

console.log('pnpm install');
await execaCommand('pnpm install', options);
await execaCommand('pnpm install --prod=false', options);

console.log('pnpm run build');
await execaCommand('pnpm run build', options);
Expand All @@ -39,8 +41,25 @@ await execaCommand('pnpm run lint', options);
console.log('pnpm run test');
await execaCommand('pnpm run test', options);

console.log('mv package.json …');
await fs.rename('package.json', '.package.dev.json');
const packageJson = await fs.readFile('package.json', 'utf8');
const newPackageJson = JSON.stringify(processPackageJson(JSON.parse(packageJson)));

try {
console.log('mv package.json …');
await fs.rename('package.json', '.package.dev.json');

console.log('new package.json');
await fs.writeFile('package.json', JSON.stringify(metadata));
console.log('new package.json');
await fs.writeFile('package.json', newPackageJson);
} catch (error) {
console.error(error);

console.error('# Reverting changes to package.json');
await fs.writeFile('package.json', packageJson);

console.error('rm …');
await fs.rm('.package.dev.json', {
force: true,
});

process.exit(1);
}

0 comments on commit 23a29a2

Please sign in to comment.