2025-07-02 12:54:50 -07:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test CLI Package Locally
|
|
|
|
|
*
|
|
|
|
|
* This script allows testing the CLI package locally before publishing to npm.
|
|
|
|
|
* It builds both packages, creates local tarballs, and installs the CLI package
|
|
|
|
|
* globally for testing.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-07-02 16:16:19 -07:00
|
|
|
import { execSync } from 'child_process'
|
|
|
|
|
import fs from 'fs'
|
|
|
|
|
import path from 'path'
|
|
|
|
|
import { fileURLToPath } from 'url'
|
2025-07-02 12:54:50 -07:00
|
|
|
|
|
|
|
|
// Get the directory of the current module
|
2025-07-02 16:16:19 -07:00
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
|
|
|
const __dirname = path.dirname(__filename)
|
|
|
|
|
const rootDir = path.join(__dirname, '..')
|
|
|
|
|
const cliPackageDir = path.join(rootDir, 'cli-package')
|
2025-07-02 12:54:50 -07:00
|
|
|
|
|
|
|
|
// Ensure the CLI package directory exists
|
|
|
|
|
if (!fs.existsSync(cliPackageDir)) {
|
2025-07-02 16:16:19 -07:00
|
|
|
console.error(`Error: CLI package directory not found at ${cliPackageDir}`)
|
|
|
|
|
process.exit(1)
|
2025-07-02 12:54:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Step 1: Ensure versions are in sync
|
2025-07-02 16:16:19 -07:00
|
|
|
console.log('Ensuring versions are in sync...')
|
|
|
|
|
execSync('node scripts/generate-version.js', {
|
|
|
|
|
stdio: 'inherit',
|
|
|
|
|
cwd: rootDir
|
|
|
|
|
})
|
2025-07-02 12:54:50 -07:00
|
|
|
|
|
|
|
|
// Step 2: Build the main package
|
2025-07-02 16:16:19 -07:00
|
|
|
console.log('Building main package...')
|
|
|
|
|
execSync('npm run build', { stdio: 'inherit', cwd: rootDir })
|
2025-07-02 12:54:50 -07:00
|
|
|
|
|
|
|
|
// Step 3: Create a local tarball of the main package
|
2025-07-02 16:16:19 -07:00
|
|
|
console.log('Creating local tarball of main package...')
|
|
|
|
|
execSync('npm pack', { stdio: 'inherit', cwd: rootDir })
|
|
|
|
|
|
|
|
|
|
// Read the main package.json to get the name and version
|
|
|
|
|
const mainPackageJsonPath = path.join(rootDir, 'package.json')
|
|
|
|
|
const mainPackageJson = JSON.parse(
|
|
|
|
|
fs.readFileSync(mainPackageJsonPath, 'utf8')
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// The tarball name follows a standard format: <package-name>-<version>.tgz
|
|
|
|
|
const mainPackageName = mainPackageJson.name
|
|
|
|
|
.replace('@', '')
|
|
|
|
|
.replace('/', '-')
|
|
|
|
|
const mainPackageVersion = mainPackageJson.version
|
|
|
|
|
const mainTarballName = `${mainPackageName}-${mainPackageVersion}.tgz`
|
|
|
|
|
const mainPackageTarball = path.join(rootDir, mainTarballName)
|
|
|
|
|
|
|
|
|
|
// Verify the tarball exists
|
|
|
|
|
if (!fs.existsSync(mainPackageTarball)) {
|
|
|
|
|
console.error(
|
|
|
|
|
`Error: Main package tarball not found at ${mainPackageTarball}`
|
|
|
|
|
)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`Main package tarball created: ${mainPackageTarball}`)
|
2025-07-02 12:54:50 -07:00
|
|
|
|
|
|
|
|
// Step 4: Build the CLI package
|
2025-07-02 16:16:19 -07:00
|
|
|
console.log('Building CLI package...')
|
|
|
|
|
execSync('npm run build', { stdio: 'inherit', cwd: cliPackageDir })
|
2025-07-02 12:54:50 -07:00
|
|
|
|
|
|
|
|
// Step 5: Verify the CLI was built successfully
|
2025-07-02 16:16:19 -07:00
|
|
|
const cliPath = path.join(cliPackageDir, 'dist', 'cli.js')
|
2025-07-02 12:54:50 -07:00
|
|
|
if (!fs.existsSync(cliPath)) {
|
2025-07-02 16:16:19 -07:00
|
|
|
console.error(`Error: CLI build failed. File not found at ${cliPath}`)
|
|
|
|
|
process.exit(1)
|
2025-07-02 12:54:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 6: Temporarily update the CLI package.json to use the local main package
|
2025-07-02 16:16:19 -07:00
|
|
|
console.log('Updating CLI package.json to use local main package...')
|
|
|
|
|
const cliPackageJsonPath = path.join(cliPackageDir, 'package.json')
|
|
|
|
|
const cliPackageJson = JSON.parse(fs.readFileSync(cliPackageJsonPath, 'utf8'))
|
|
|
|
|
|
2025-07-02 12:54:50 -07:00
|
|
|
// Save the original dependency for restoration later
|
2025-07-02 16:16:19 -07:00
|
|
|
const originalDependency = cliPackageJson.dependencies['@soulcraft/brainy']
|
|
|
|
|
|
2025-07-02 12:54:50 -07:00
|
|
|
// Update to use the local tarball
|
2025-07-02 16:16:19 -07:00
|
|
|
cliPackageJson.dependencies['@soulcraft/brainy'] =
|
|
|
|
|
`file:${mainPackageTarball}`
|
|
|
|
|
|
2025-07-02 12:54:50 -07:00
|
|
|
// Write the updated package.json
|
2025-07-02 16:16:19 -07:00
|
|
|
fs.writeFileSync(cliPackageJsonPath, JSON.stringify(cliPackageJson, null, 2))
|
2025-07-02 12:54:50 -07:00
|
|
|
|
|
|
|
|
// Step 7: Create a local tarball of the CLI package
|
2025-07-02 16:16:19 -07:00
|
|
|
console.log('Creating local tarball of CLI package...')
|
|
|
|
|
execSync('npm pack', { stdio: 'inherit', cwd: cliPackageDir })
|
|
|
|
|
|
|
|
|
|
// The tarball name follows a standard format: <package-name>-<version>.tgz
|
|
|
|
|
const cliPackageName = cliPackageJson.name.replace('@', '').replace('/', '-')
|
|
|
|
|
const cliPackageVersion = cliPackageJson.version
|
|
|
|
|
const cliTarballName = `${cliPackageName}-${cliPackageVersion}.tgz`
|
|
|
|
|
const cliPackageTarball = path.join(cliPackageDir, cliTarballName)
|
|
|
|
|
|
|
|
|
|
// Verify the tarball exists
|
|
|
|
|
if (!fs.existsSync(cliPackageTarball)) {
|
|
|
|
|
console.error(
|
|
|
|
|
`Error: CLI package tarball not found at ${cliPackageTarball}`
|
|
|
|
|
)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`CLI package tarball created: ${cliPackageTarball}`)
|
|
|
|
|
|
|
|
|
|
// Step 8: Install the CLI package globally for testing
|
|
|
|
|
console.log('Installing CLI package globally for testing...')
|
|
|
|
|
execSync(`npm install -g "${cliPackageTarball}"`, { stdio: 'inherit' })
|
|
|
|
|
|
|
|
|
|
// Step 9: Restore the original dependency in CLI package.json
|
|
|
|
|
console.log('Restoring original dependency in CLI package.json...')
|
|
|
|
|
cliPackageJson.dependencies['@soulcraft/brainy'] = originalDependency
|
|
|
|
|
fs.writeFileSync(cliPackageJsonPath, JSON.stringify(cliPackageJson, null, 2))
|
|
|
|
|
|
|
|
|
|
console.log('\nCLI package installed globally for testing!')
|
|
|
|
|
console.log('You can now run the CLI using the "brainy" command.')
|
|
|
|
|
console.log('\nTo uninstall after testing:')
|
|
|
|
|
console.log('npm uninstall -g @soulcraft/brainy-cli')
|
2025-07-02 12:54:50 -07:00
|
|
|
} catch (error) {
|
2025-07-02 16:16:19 -07:00
|
|
|
console.error('Error:', error.message)
|
|
|
|
|
process.exit(1)
|
2025-07-02 12:54:50 -07:00
|
|
|
}
|