|
| 1 | +// @ts-check |
| 2 | +import * as url from 'node:url'; |
| 3 | +import * as fs from 'node:fs/promises'; |
| 4 | +import * as path from 'node:path'; |
| 5 | +import * as process from 'node:process'; |
| 6 | +import * as child_process from 'node:child_process'; |
| 7 | +import * as util from 'node:util'; |
| 8 | +const exec = util.promisify(child_process.exec); |
| 9 | + |
| 10 | +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); |
| 11 | +const pkgFilePath = path.join(__dirname, '..', '..', 'package.json'); |
| 12 | + |
| 13 | +process.env.TZ = 'Etc/UTC'; |
| 14 | + |
| 15 | +/** |
| 16 | + * FORMAT : M.M.P-dev+YYYYMMDD.sha.########## |
| 17 | + * EXAMPLE: 5.6.0-dev+20230601.sha.0853c6957c |
| 18 | + */ |
| 19 | +class NightlyVersion { |
| 20 | + /** @param {string} version */ |
| 21 | + constructor(version) { |
| 22 | + /** @type {string} */ |
| 23 | + this.version = version; |
| 24 | + const [, meta] = this.version.split('+'); |
| 25 | + const [dateString, commit] = meta.split('.sha.'); |
| 26 | + /** @type {string} */ |
| 27 | + this.commit = commit; |
| 28 | + /** @type {string} */ |
| 29 | + this.dateString = dateString; |
| 30 | + } |
| 31 | + static async currentNightlyVersion() { |
| 32 | + const { stdout } = await exec('npm show --json mongodb', { encoding: 'utf8' }); |
| 33 | + /** @type {{'dist-tags': {nightly?: string} }} */ |
| 34 | + const showInfo = JSON.parse(stdout); |
| 35 | + const version = showInfo?.['dist-tags']?.nightly ?? '0.0.0-dev+YYYYMMDD.sha.##########'; |
| 36 | + return new NightlyVersion(version); |
| 37 | + } |
| 38 | + static async currentCommit() { |
| 39 | + const { stdout } = await exec('git rev-parse --short HEAD', { encoding: 'utf8' }); |
| 40 | + return stdout.trim(); |
| 41 | + } |
| 42 | + static async generateNightlyVersion() { |
| 43 | + console.log('Generating new nightly version'); |
| 44 | + const currentCommit = await NightlyVersion.currentCommit(); |
| 45 | + const today = new Date(); |
| 46 | + const year = `${today.getFullYear()}`; |
| 47 | + const month = `${today.getMonth()}`.padStart(2, '0'); |
| 48 | + const day = `${today.getUTCDate()}`.padStart(2, '0'); |
| 49 | + const yyyymmdd = `${year}${month}${day}`; |
| 50 | + |
| 51 | + const pkg = JSON.parse(await fs.readFile(pkgFilePath, { encoding: 'utf8' })); |
| 52 | + |
| 53 | + console.log('package.json version is:', pkg.version); |
| 54 | + pkg.version = `${pkg.version}-dev+${yyyymmdd}.sha.${currentCommit}`; |
| 55 | + console.log('package.json version updated to:', pkg.version); |
| 56 | + |
| 57 | + await fs.writeFile(pkgFilePath, JSON.stringify(pkg, undefined, 2), { encoding: 'utf8' }); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const currentPublishedNightly = await NightlyVersion.currentNightlyVersion(); |
| 62 | +console.log('current published nightly:', currentPublishedNightly?.version); |
| 63 | +const currentCommit = await NightlyVersion.currentCommit(); |
| 64 | +console.log('current commit sha:', currentCommit); |
| 65 | + |
| 66 | +if (currentPublishedNightly.commit === currentCommit) { |
| 67 | + console.log('Published nightly is up to date'); |
| 68 | + process.exit(1); |
| 69 | +} |
| 70 | +await NightlyVersion.generateNightlyVersion(); |
| 71 | +process.exit(0); |
0 commit comments