2025-06-24 11:41:30 -07:00
#!/usr/bin/env node
/ * *
* Generate Version Script
*
* This script generates a version . js file that exports the version from package . json .
* This allows the CLI to access the version without having to read package . json directly ,
* which can be problematic when the package is installed globally .
*
2025-07-02 11:32:06 -07:00
* It also updates the version in the README . md file and CLI package . json to ensure they
* stay in sync with the main package . json .
2025-06-24 11:41:30 -07:00
* /
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
// Get the directory of the current module
const _ _filename = fileURLToPath ( import . meta . url )
const _ _dirname = path . dirname ( _ _filename )
// Path to the root directory
const rootDir = path . join ( _ _dirname , '..' )
// Path to package.json
const packageJsonPath = path . join ( rootDir , 'package.json' )
// Path to the output directory
const outputDir = path . join ( rootDir , 'src' , 'utils' )
// Path to the output file
const outputFile = path . join ( outputDir , 'version.ts' )
// Path to README.md
const readmePath = path . join ( rootDir , 'README.md' )
2025-07-02 11:32:06 -07:00
// Path to CLI package.json
const cliPackageJsonPath = path . join ( rootDir , 'cli-package' , 'package.json' )
2025-06-24 11:41:30 -07:00
// Read package.json
const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) )
const version = packageJson . version
// Create the output directory if it doesn't exist
if ( ! fs . existsSync ( outputDir ) ) {
fs . mkdirSync ( outputDir , { recursive : true } )
}
// Generate the version.ts file
const content = ` /**
* This file is auto - generated during the build process .
* Do not modify this file directly .
* /
export const VERSION = '${version}' ;
`
// Write the file
fs . writeFileSync ( outputFile , content )
console . log ( ` Generated version.ts with version ${ version } ` )
// Update README.md with the current version, Node.js version, and TypeScript version
try {
let readmeContent = fs . readFileSync ( readmePath , 'utf8' )
// Get Node.js version requirement from package.json
const nodeVersion = packageJson . engines . node . replace ( '>=' , '' )
// Get TypeScript version from package.json devDependencies
const typescriptVersion = packageJson . devDependencies . typescript . replace ( '^' , '' )
// Update npm badge - using a more flexible approach with explicit version
const npmBadgeRegex = /\[\!\[npm\].*?\]\(https:\/\/www\.npmjs\.com\/package\/@soulcraft\/brainy\)/g
if ( npmBadgeRegex . test ( readmeContent ) ) {
readmeContent = readmeContent . replace (
npmBadgeRegex ,
` [](https://www.npmjs.com/package/@soulcraft/brainy) `
)
} else {
console . log ( 'Warning: Could not find npm badge in README.md' )
}
// Update Node.js badge - using a more flexible approach
const nodeBadgeRegex = /\[\!\[Node\.js\].*?\]\(https:\/\/nodejs\.org\/\)/g
if ( nodeBadgeRegex . test ( readmeContent ) ) {
readmeContent = readmeContent . replace (
nodeBadgeRegex ,
` [](https://nodejs.org/) `
)
} else {
console . log ( 'Warning: Could not find Node.js badge in README.md' )
}
// Update TypeScript badge - using a more flexible approach
const tsBadgeRegex = /\[\!\[TypeScript\].*?\]\(https:\/\/www\.typescriptlang\.org\/\)/g
if ( tsBadgeRegex . test ( readmeContent ) ) {
readmeContent = readmeContent . replace (
tsBadgeRegex ,
` [](https://www.typescriptlang.org/) `
)
} else {
console . log ( 'Warning: Could not find TypeScript badge in README.md' )
}
// Write the updated README back to disk
fs . writeFileSync ( readmePath , readmeContent )
console . log ( ` Updated README.md with npm version ${ version } , Node.js version ${ nodeVersion } , and TypeScript version ${ typescriptVersion } ` )
} catch ( error ) {
console . error ( 'Error updating README.md:' , error )
}
2025-07-02 11:32:06 -07:00
// Update CLI package.json with the same version
try {
// Read CLI package.json
const cliPackageJson = JSON . parse ( fs . readFileSync ( cliPackageJsonPath , 'utf8' ) )
// Update version
cliPackageJson . version = version
// Update dependency on main package to be exact
if ( cliPackageJson . dependencies && cliPackageJson . dependencies [ '@soulcraft/brainy' ] ) {
cliPackageJson . dependencies [ '@soulcraft/brainy' ] = version
}
// Write the updated CLI package.json back to disk
fs . writeFileSync ( cliPackageJsonPath , JSON . stringify ( cliPackageJson , null , 2 ) )
console . log ( ` Updated CLI package.json with version ${ version } and exact dependency on main package ` )
} catch ( error ) {
console . error ( 'Error updating CLI package.json:' , error )
}