Skip to content

Commit

Permalink
ci: add unpublish workflow (#1487)
Browse files Browse the repository at this point in the history
* Revert "feat: add `Predicate.getTransferTxId` helper (#1467)"

This reverts commit 70233c1.

* feat: implement script and workflow to unpublish old version

* chore: changeset
  • Loading branch information
danielbate authored Dec 8, 2023
1 parent 9352685 commit 03010f8
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .changeset/chatty-cougars-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
28 changes: 28 additions & 0 deletions .github/workflows/release-unpublish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: "Unpublish old versions"

on:
workflow_dispatch:
inputs:
delete_packages:
type: boolean
description: Delete packages? otherwise dry-run mode will be used
default: false

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
clean-npm-versions:
name: Unpublish versions next, preview, master...
runs-on: buildjet-4vcpu-ubuntu-2204
steps:
- uses: actions/checkout@v3
- uses: FuelLabs/github-actions/setups/node@master
- uses: FuelLabs/github-actions/setups/npm@master
with:
npm-token: ${{ secrets.NPM_TOKEN }}
- run: |
node ./scripts/release-unpublish.js
env:
DELETE_PACKAGES: ${{ github.event.inputs.delete_packages}}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"packageManager": "[email protected]",
"scripts": {
"dan": "tsx ./scripts/release-unpublish.ts",
"dev": "nodemon --config nodemon.config.json -x 'pnpm build:packages'",
"build": "turbo run build",
"build:packages": "turbo run build --filter=!docs",
Expand Down Expand Up @@ -49,6 +50,7 @@
"@changesets/get-github-info": "^0.5.2",
"@fuel-ts/forc": "workspace:*",
"@fuel-ts/fuel-core": "workspace:*",
"@fuel-ts/versions": "workspace:^",
"@internal/tsup": "workspace:*",
"@jest/types": "^29.5.0",
"@types/jest": "^29.5.0",
Expand All @@ -57,6 +59,7 @@
"@types/web": "^0.0.65",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
"compare-versions": "^6.1.0",
"conventional-changelog-angular": "^5.0.13",
"dotenv": "^9.0.2",
"eslint": "^8.52.0",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions scripts/release-unpublish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { versions } from '@fuel-ts/versions';
import { exec } from 'child_process';
import { compare } from 'compare-versions';
import { readFileSync, readdirSync } from 'fs';
import { join } from 'path';

const DELETE_TAGS = /next|pr/;
const { FUELS: CURRENT_VERSION } = versions;
const DELETE_PACKAGES = process.env.DELETE_PACKAGES === 'true';
const dryRun = DELETE_PACKAGES ? '' : '--dry-run';

const { log, error } = console;

const getPublicPackages = () => {
const packagesDir = join(__dirname, '../packages');
const packages = readdirSync(packagesDir, { withFileTypes: true });
const packagesNames = packages.map((p) => {
try {
const packageContent = readFileSync(join(packagesDir, p.name, 'package.json'), 'utf8');
const packageJson = JSON.parse(packageContent.toString());
return packageJson.private ? null : packageJson.name;
} catch (err) {
return null;
}
});
return packagesNames.filter((p) => !!p);
};

const main = async () => {
const packages = getPublicPackages();
await packages.map(async (packageName) => {
log(`📦 Fetching ${packageName} versions`);
const { versions: packageVersions } = await fetch(
`https://registry.npmjs.org/${packageName}`
).then((resp) => resp.json());

const versionsToDelete = Object.keys(packageVersions).filter(
(packageVersion) =>
packageVersion.search(DELETE_TAGS) > -1 && !compare(packageVersion, CURRENT_VERSION, '>=')
);
log('The following versions will be deleted:');
log(versionsToDelete.map((v) => ` - ${v}`).join('\n'));
versionsToDelete.map(async (versionToDelete) => {
const { stderr } = await exec(`npm unpublish ${packageName}@${versionToDelete} ${dryRun}`);
if (stderr) {
log(`❌ Error ${packageName}@${versionToDelete} not deleted!\n`);
} else {
log(`✅ Package ${packageName}@${versionToDelete} deleted!\n`);
}
});
});
};

main().catch((err) => {
error(err);
process.exit(1);
});

0 comments on commit 03010f8

Please sign in to comment.