diff --git a/.github/workflows/delete-package-versions.yml b/.github/workflows/delete-package-versions.yml new file mode 100644 index 000000000..b3eb9f1e9 --- /dev/null +++ b/.github/workflows/delete-package-versions.yml @@ -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 \ No newline at end of file diff --git a/deleteVersions.js b/deleteVersions.js new file mode 100644 index 000000000..e1f08044d --- /dev/null +++ b/deleteVersions.js @@ -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(); \ No newline at end of file