Skip to content

Commit

Permalink
chore(npm): Adding script to unpublish package versions (#2964)
Browse files Browse the repository at this point in the history
  • Loading branch information
planctus authored Jul 25, 2023
1 parent 966331f commit 8623368
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions scripts/unpublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { execSync } = require('child_process');
const minimist = require('minimist');
const axios = require('axios');

const excludedPackages = [
'@ec-europa/ecl-dialogs',
'@hpcc-js/eclwatch',
'@eui/ecl-styles',
'snomed-ecl-builder',
'@eui/ecl',
'@eui/ecl-core',
];

async function fetchPackageNames(scope) {
try {
const response = await axios.get(
`https://registry.npmjs.org/-/v1/search?text=${scope}/&size=250`,
);
return response.data.objects
.filter((pkg) => !excludedPackages.includes(pkg.package.name))
.map((pkg) => pkg.package.name);
} catch (error) {
console.error(
'Error occurred while fetching package names:',
error.message,
);
return [];
}
}

async function unpublishPackages(scope, version, dryRun = true) {
console.log(
`Running npm unpublish ${dryRun ? 'with dry-run option...' : '...'} `,
);
const packageNames = await fetchPackageNames(scope);
// eslint-disable-next-line no-restricted-syntax
for (const packageName of packageNames) {
const npmUnpublishCommand = `npm unpublish ${packageName}@${version}${
dryRun ? ' --dry-run' : ''
}`;
if (dryRun) {
console.log(`[Dry Run] Would have run: ${npmUnpublishCommand}`);
} else {
console.log(`Unpublishing ${packageName} @${version}`);
execSync(npmUnpublishCommand);
}
}
}

function printUsage() {
console.log(
'Usage: node unpublish.js --scope <scope> --version <version> [--dry-run]',
);
}

const args = minimist(process.argv.slice(2), {
string: ['scope', 'version'],
boolean: ['dry-run'],
alias: { s: 'scope', v: 'version', d: 'dry-run' },
});

if (!args.scope || !args.version) {
printUsage();
process.exit(1);
}

unpublishPackages(args.scope, args.version, args['dry-run']);

1 comment on commit 8623368

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.