Skip to content

Commit

Permalink
chore: auto delete unwanted versions (microsoft#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored May 31, 2022
1 parent 3fa8523 commit 89e1576
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ jobs:
- name: Install dependencies
run: npm ci
- name: Delete optionally previous version
run: node src/remove_version.js ${{ github.event.inputs.version }}
run: node src/versions.js --delete ${{ github.event.inputs.version }}
- name: Delete stale versions
run: node src/versions.js --delete-stale-minor-versions ${{ github.event.inputs.version }}
- name: Roll
run: npm run roll
env:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SRC_DIR=~/code/playwright npm run roll
### Remove old release

```sh
node src/remove_version.js 1.16
node src/versions.js --delete 1.16
```

### Run dev server
Expand Down
18 changes: 0 additions & 18 deletions src/remove_version.js

This file was deleted.

59 changes: 59 additions & 0 deletions src/versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// @ts-check
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');

const rootDir = path.join(__dirname, '..');

/**
* @param {string} version
*/
function removeVersion(version) {
for (const lang of ['nodejs', 'python', 'java', 'dotnet']) {
rimraf.sync(path.join(rootDir, lang, 'versioned_sidebars', `version-${version}-sidebars.json`));
rimraf.sync(path.join(rootDir, lang, 'versioned_docs', `version-${version}`));
const versions = JSON.parse(fs.readFileSync(path.join(rootDir, lang, 'versions.json'), 'utf-8'));
const newVersions = versions.filter(v => v !== version);
fs.writeFileSync(path.join(rootDir, lang, 'versions.json'), JSON.stringify(newVersions, null, 2) + '\n');
}
}

/**
* @param {string} currentVersion
*/
function removeStaleMinorVersions(currentVersion) {
const kKeepRecentMinorVersions = 3;
const [currentMajorVersion, currentMinorVersion] = currentVersion.split('.').map(v => parseInt(v, 10));
/** @type {string[]} */
const existingVersions = require(path.join(rootDir, 'nodejs', 'versions.json'));
for (const version of existingVersions) {
const [majorVersion, minorVersion] = version.split('.').map(v => parseInt(v, 10));
if (currentMajorVersion === majorVersion && minorVersion <= (currentMinorVersion - kKeepRecentMinorVersions))
removeVersion(version);
}
}

if (!process.argv[2]) {
console.error([
'Usage: node versions.js --delete <version>',
'Usage: node versions.js --delete-stale-minor-versions <version>',
].join('\n'));
process.exit(1);
}

switch (process.argv[2]) {
case '--delete':
if (!process.argv[3])
throw new Error('Missing version');
removeVersion(process.argv[3]);
break;

case '--delete-stale-minor-versions':
if (!process.argv[3])
throw new Error('Missing version');
removeStaleMinorVersions(process.argv[3]);
break;

default:
throw new Error('Unknown command: ' + process.argv[2]);
}

0 comments on commit 89e1576

Please sign in to comment.