Skip to content

Commit

Permalink
Delete obsolete versions
Browse files Browse the repository at this point in the history
  • Loading branch information
nvuillam committed Jun 10, 2024
1 parent c48d311 commit 63d9a59
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/delete-package-versions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Delete NPM Versions

on:
workflow_dispatch:
push:

jobs:
delete-npm-versions:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 'lts/*'

- name: Install dependencies
run: npm install

- name: Run deleteVersions script
env:
PACKAGE_NAME: ${{ secrets.NPM_PACKAGE_NAME }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
node deleteVersions.js
31 changes: 31 additions & 0 deletions deleteVersions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { execSync } = require('child_process');

function getPackageVersions(packageName) {
const result = execSync(`npm view ${packageName} versions --json`);
return JSON.parse(result);
}

function deletePackageVersion(packageName, version) {
try {
execSync(`npm unpublish ${packageName}@${version}`);
console.log(`Successfully deleted ${packageName}@${version}`);
} catch (error) {
console.error(`Failed to delete ${packageName}@${version}: ${error.message}`);
}
}

function main() {
const packageName = process.env.PACKAGE_NAME;
if (!packageName) {
console.error('PACKAGE_NAME environment variable is not set');
process.exit(1);
}

const versions = getPackageVersions(packageName);
const versionsToDelete = versions.filter(version => /alpha|beta|canary/.test(version));

console.log(JSON.stringify(versionsToDelete,null,2));
// versionsToDelete.forEach(version => deletePackageVersion(packageName, version));
}

main();

0 comments on commit 63d9a59

Please sign in to comment.