2023-03-31 17:22:37 +02:00
import { readFile , writeFile } from 'fs/promises'
import { inc , valid } from 'semver'
import { simpleGit } from 'simple-git'
import { fileURLToPath } from 'url'
import oramaPackage from '../packages/orama/package.json' assert { type : 'json' }
2023-06-15 11:48:59 +02:00
const dryRun = process . env . DRY _RUN === 'true'
const skipGit = process . env . SKIP _GIT === 'true'
2023-03-31 17:22:37 +02:00
const skip = ( process . env . SKIP ? ? '' ) . split ( /\s*,\s*/ ) . map ( s => s . trim ( ) )
const packages = [
'orama' ,
'plugin-astro' ,
'plugin-data-persistence' ,
'plugin-docusaurus' ,
2024-01-23 16:44:17 +01:00
'plugin-docusaurus-v3' ,
2023-03-31 17:22:37 +02:00
'plugin-match-highlight' ,
2023-06-15 11:48:59 +02:00
'plugin-nextra' ,
2023-03-31 17:22:37 +02:00
'plugin-parsedoc' ,
2023-06-15 11:48:59 +02:00
'stemmers' ,
'stopwords' ,
2023-03-31 17:22:37 +02:00
] . filter ( p => ! skip . includes ( p ) )
2023-06-15 11:48:59 +02:00
async function updatePackage ( path , version ) {
const packageJson = JSON . parse ( await readFile ( path ) )
console . log (
` \x 1b[32mUpdating package \x 1b[1m ${ packageJson . name } \x 1b[22m from \x 1b[1m ${ packageJson . version } \x 1b[22m to version \x 1b[1m ${ version } \x 1b[22m ... \x 1b[0m ` ,
)
packageJson . version = version
if ( ! dryRun ) {
await writeFile ( path , JSON . stringify ( packageJson , null , 2 ) , 'utf-8' )
}
}
2023-03-31 17:22:37 +02:00
async function main ( ) {
if ( ! process . argv [ 2 ] ) {
console . error ( '\x1b[33mUsage: node version.mjs [VERSION | CHANGE [PRERELEASE]]\x1b[0m' )
process . exit ( 1 )
}
// Check if the new version is absolute, otherwise we take it from @orama/orama and increase
let version = process . argv [ 2 ]
if ( ! valid ( version ) ) {
version = inc ( oramaPackage . version , version , process . argv [ 3 ] )
}
if ( ! valid ( version ) ) {
console . error (
` \x 1b[31mCannot increase version \x 1b[1m ${ oramaPackage . version } \x 1b[22m using operator \x 1b[1m ${ process . argv [ 2 ] } \x 1b[22m. \x 1b[0m ` ,
)
process . exit ( 1 )
}
2023-06-15 11:48:59 +02:00
// Update the main package.json
await updatePackage ( fileURLToPath ( new URL ( ` ../package.json ` , import . meta . url ) ) , version )
2023-03-31 17:22:37 +02:00
// Perform the update
for ( const pkg of packages ) {
2023-06-15 11:48:59 +02:00
await updatePackage ( fileURLToPath ( new URL ( ` ../packages/ ${ pkg } /package.json ` , import . meta . url ) ) , version )
2023-03-31 17:22:37 +02:00
}
// Make the commit, including the tag
2023-06-15 11:48:59 +02:00
if ( ! dryRun && ! skipGit ) {
2023-03-31 17:22:37 +02:00
const git = simpleGit ( { baseDir : fileURLToPath ( new URL ( '..' , import . meta . url ) ) } )
console . log ( ` \x 1b[32mCommitting changes and creating a tag (pushing is left to you) ... \x 1b[0m ` )
await git . commit (
'chore: Version bump' ,
packages . map ( p => ` packages/ ${ p } /package.json ` ) ,
{ '--no-verify' : true } ,
)
await git . addTag ( ` v ${ version } ` )
}
}
await main ( )