UNPKG

reactium

Version:

A CLI for creating Reactium / Actinium projects.

97 lines (79 loc) 2.52 kB
import chalk from 'chalk'; import fs from 'fs-extra'; import semver from 'semver'; import path from 'node:path'; import op from 'object-path'; import inquirer from 'inquirer'; import { fileURLToPath } from 'node:url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const pkg = fs.readJsonSync(path.resolve(__dirname, 'package.json')); const utils = { normalize: (...args) => path.normalize(path.join(process.cwd(), ...args)), prefix: chalk.magenta(' > '), suffix: chalk.magenta(': '), validate: (val, field) => { switch (field) { case 'version': return !semver.valid(semver.coerce(val)) ? `invalid version: ${chalk.magenta(val)}` : true; default: return !val ? `${field} is required` : true; } }, }; const env = op.get(process.env, 'NODE_ENV'); const versionUpdater = async () => { const { normalize, prefix, suffix, validate } = utils; const pkgFilePath = normalize('package.json'); const currentVersion = pkg.version; console.log(''); console.log(` Publishing ${chalk.magenta(pkg.name)}...`); console.log(''); const { target } = await inquirer.prompt([ { type: 'list', message: `Current version is ${currentVersion}. Select target version:`, name: 'target', choices: [ 'major', 'minor', 'patch', 'premajor', 'preminor', 'prepatch', 'prerelease', ], default: 'patch', }, ]); const defaultVer = semver.inc(pkg.version, target); // Input version number const { version } = await inquirer.prompt([ { type: 'input', prefix, suffix, name: 'version', default: defaultVer, message: chalk.cyan('Version'), validate: (val) => validate(val, 'version'), filter: (val) => semver.valid(val) || defaultVer, }, ]); // Write package.json if (env !== 'test') { await Promise.all([ fs.writeFile( pkgFilePath, JSON.stringify({ ...pkg, version }, null, 2), ), ]); } console.log(''); if (env === 'test') { console.log(JSON.stringify({ version }, null, 2)); console.log(''); } }; versionUpdater();