|
| 1 | +/** |
| 2 | + * This script was inspired by https://dev.to/antongolub/you-don-t-need-semantic-release-sometimes-3k6k |
| 3 | + */ |
| 4 | + |
| 5 | +import { $ } from 'zx' |
| 6 | +import semver from 'semver' |
| 7 | +import packageJson from 'package-json' |
| 8 | +import path from 'path' |
| 9 | +import fs from 'fs/promises' |
| 10 | +import syncFs from 'fs' |
| 11 | +import { fileURLToPath } from 'url' |
| 12 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 13 | + |
| 14 | +/** |
| 15 | + * Comes from https://github.com/semantic-release/semantic-release/blob/9589a96239826abe9b07e8deffcc7d8aeb9c2e40/index.js#L45 |
| 16 | + */ |
| 17 | +const COMMIT_NAME = 'release-bot' |
| 18 | +const COMMIT_EMAIL = '[email protected]' |
| 19 | +$.env = { |
| 20 | + GIT_AUTHOR_NAME: COMMIT_NAME, |
| 21 | + GIT_AUTHOR_EMAIL: COMMIT_EMAIL, |
| 22 | + GIT_COMMITTER_NAME: COMMIT_NAME, |
| 23 | + GIT_COMMITTER_EMAIL: COMMIT_EMAIL, |
| 24 | + ...process.env, |
| 25 | + GIT_ASKPASS: 'echo', |
| 26 | + GIT_TERMINAL_PROMPT: '0' |
| 27 | +} |
| 28 | + |
| 29 | +function escapeRegExp (string: string) { |
| 30 | + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
| 31 | +} |
| 32 | + |
| 33 | +const vscodeVersion = process.argv[process.argv.length - 1]! |
| 34 | +const minorVscodeVersion = `${semver.major(vscodeVersion)}.${semver.minor(vscodeVersion)}` |
| 35 | +const tagPattern = new RegExp(`^v?(${escapeRegExp(minorVscodeVersion)}\\.\\d+)$`) |
| 36 | + |
| 37 | +async function getLastTag () { |
| 38 | + const tags = (await $`git tag -l --sort=-v:refname`).toString().split('\n').map(tag => tag.trim()) |
| 39 | + |
| 40 | + const matchingTags = tags.filter(tag => tagPattern.test(tag)).sort(semver.compare) |
| 41 | + const lastTag = matchingTags[matchingTags.length - 1]! |
| 42 | + |
| 43 | + return lastTag |
| 44 | +} |
| 45 | + |
| 46 | +async function getNextVersion (lastTag: string) { |
| 47 | + // Find available next version |
| 48 | + const allVersions = new Set(Object.keys((await packageJson('@codingame/monaco-vscode-api', { |
| 49 | + allVersions: true |
| 50 | + })).versions)) |
| 51 | + let nextVersion: string = lastTag |
| 52 | + do { |
| 53 | + nextVersion = semver.inc(nextVersion, 'prerelease', true, 'next')! |
| 54 | + } while (allVersions.has(nextVersion)) |
| 55 | + |
| 56 | + return nextVersion |
| 57 | +} |
| 58 | + |
| 59 | +async function publishNpm (version: string) { |
| 60 | + const distDir = path.resolve(__dirname, 'dist') |
| 61 | + for (const dirName of await fs.readdir(distDir)) { |
| 62 | + const libDir = path.resolve(distDir, dirName) |
| 63 | + const packageJsonFile = path.resolve(libDir, 'package.json') |
| 64 | + if (syncFs.existsSync(packageJsonFile)) { |
| 65 | + const packageJson = JSON.parse((await fs.readFile(packageJsonFile)).toString()) |
| 66 | + packageJson.version = version |
| 67 | + if (packageJson.dependencies?.vscode != null) { |
| 68 | + packageJson.dependencies.vscode = `npm:@codingame/monaco-vscode-api@${version}` |
| 69 | + } |
| 70 | + await fs.writeFile(packageJsonFile, JSON.stringify(packageJson, null, 2)) |
| 71 | + |
| 72 | + $.cwd = libDir |
| 73 | + await $`npm publish --tag next` |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +async function run () { |
| 79 | + const lastTag = await getLastTag() |
| 80 | + const nextVersion = await getNextVersion(lastTag) |
| 81 | + await publishNpm(nextVersion) |
| 82 | +} |
| 83 | + |
| 84 | +run().catch(error => { |
| 85 | + console.error(error) |
| 86 | + process.exit(1) |
| 87 | +}) |
0 commit comments