diff --git a/packages/runmate/src/cli.ts b/packages/runmate/src/cli.ts index 320989ed..d0008175 100644 --- a/packages/runmate/src/cli.ts +++ b/packages/runmate/src/cli.ts @@ -5,9 +5,9 @@ import { Command } from 'commander'; import { align } from './commands/align'; import { executeInPackages } from './commands/executeInPackages'; +import { getSetJSON } from './commands/getSetJSON'; import { list } from './commands/list'; import { peers } from './commands/peers'; -import { updateJsonValue } from './commands/update-json-value'; import { version } from './commands/version'; import { packageRunner, PackageRunnerUtils } from './packageRunner'; import { packageJSONDependencyKeys } from './packageVersion'; @@ -30,7 +30,7 @@ program const { chunkSize, cwd: src } = options || {}; try { - const runner = await packageRunner(src); + const runner = await packageRunner({ cwd: src, includeRoot: true }); await runner.run( { command: 'rm -rf node_modules' }, { @@ -91,6 +91,7 @@ program const runner = await packageRunner({ cwd: src, failFast: false, + // includeRoot: true // link? is it used? }); if (clean) { @@ -150,7 +151,7 @@ program list(program); version(program); align(program); -updateJsonValue(program); +getSetJSON(program); peers(program); program.version('> Runmate ' + require('../package.json').version); diff --git a/packages/runmate/src/commands/align.ts b/packages/runmate/src/commands/align.ts index d977011a..844ea688 100644 --- a/packages/runmate/src/commands/align.ts +++ b/packages/runmate/src/commands/align.ts @@ -18,6 +18,7 @@ export function align(program: Command) { const runner = await packageRunner({ failFast: false, cwd, + includeRoot: true, }); const commons: { [K: string]: string[] } = {}; diff --git a/packages/runmate/src/commands/executeInPackages.ts b/packages/runmate/src/commands/executeInPackages.ts index c95e1cca..f1ce566d 100644 --- a/packages/runmate/src/commands/executeInPackages.ts +++ b/packages/runmate/src/commands/executeInPackages.ts @@ -33,11 +33,7 @@ function _executeInPackages( ) { return program .option('-d, --cwd ', 'Source directory or glob pattern') - .option( - '--include-root ', - 'Include root project in execution', - 'true' - ) + .option('--include-root', 'Include root project in execution', true) .option( '-c, --chunk-size ', 'Chunk size of parallel executions', @@ -60,7 +56,7 @@ function _executeInPackages( .action(async function run( commands: string[], options?: { - includeRoot?: unknown; + includeRoot?: boolean; cwd?: string; chunkSize: string; failFast: boolean; @@ -81,7 +77,7 @@ function _executeInPackages( packages, packageNameCommand, from, - includeRoot, + includeRoot = true, } = options || {}; if (commands?.[0]?.startsWith('./')) { @@ -95,17 +91,7 @@ function _executeInPackages( const runner = await packageRunner({ cwd, failFast, - includeRoot: (() => { - if (typeof includeRoot === 'boolean') return includeRoot; - - if (typeof includeRoot !== 'string') { - throw new Error( - `includeRoot received invalid value: ${includeRoot} ` - ); - } - - return includeRoot === 'true'; - })(), + includeRoot: includeRoot, }); if (kind === 'runfile') { diff --git a/packages/runmate/src/commands/getSetJSON.ts b/packages/runmate/src/commands/getSetJSON.ts new file mode 100644 index 00000000..203d93af --- /dev/null +++ b/packages/runmate/src/commands/getSetJSON.ts @@ -0,0 +1,121 @@ +import { hey, jsonParse, pick, setByPath } from '@powership/utils'; +import { CWD, nodePath } from '@powership/utils/out/node'; +import { Command } from 'commander'; + +import { + getPackageRunnerUtils, + packageRunner, + PackageRunnerUtils, +} from '../packageRunner'; + +export function getSetJSON(program: Command) { + update('get', program.command('get [file]')); + + update( + 'set', + program + .command('set [file]') + .description('Set json value') + ); +} + +function update(op: 'get' | 'set', program: Command) { + program + .option('-d, --cwd ', 'Packages root folder.') + .option('--dry-run', 'Does not save the file.') + .action(async function run(...args): Promise { + const parameters = (() => { + if (op === 'get') { + const options = args[1] || {}; + const { cwd = CWD() } = options; + + const file = args[1]; + const filePath = file ? nodePath.resolve(cwd, file) : undefined; + + return { + propertyPath: args[0], + file, + filePath, + propertySetValue: undefined, + options, + cwd, + }; + } + + const options = args[3] || {}; + const { cwd = CWD() } = options; + + const file = args[2]; + const filePath = file ? nodePath.resolve(cwd, file) : undefined; + + return { + filePath, + propertyPath: args[0], + propertySetValue: args[1], + file, + options, + cwd, + }; + })(); + + const { + // + propertyPath, + propertySetValue, + options, + file, + filePath, + cwd, + } = parameters; + + let { dryRun } = options || {}; + + const running_in_all_packages = !file; + const append = running_in_all_packages ? '\n' : ''; + + const runIt = async (util: PackageRunnerUtils) => { + if (op === 'get') { + let picked = pick(util.json, propertyPath); + + picked = + typeof picked === 'string' + ? picked + : JSON.stringify(picked, null, 2); + + process.stdout.write(picked + append); + return; + // + } else if (op === 'set') { + const inputValue = jsonParse(propertySetValue)[1] || propertySetValue; + + setByPath(util.json, propertyPath, inputValue); + + if (dryRun) { + hey`${JSON.stringify(parameters, null, 2)}`; + hey`${JSON.stringify(util.json, null, 2)}`; + } else { + util.saveJSON(); + } + } else { + throw new Error(`unknown operation`); + } + }; + + try { + if (filePath) { + const utils = getPackageRunnerUtils(filePath); + return runIt(utils); + } + + const runner = await packageRunner({ + cwd, + failFast: false, + }); + + runner.utils.map(runIt); + } catch (e: any) { + hey.error(e); + process.exit(1); + } + }); +} diff --git a/packages/runmate/src/commands/install.ts b/packages/runmate/src/commands/install.ts deleted file mode 100644 index d67f29cd..00000000 --- a/packages/runmate/src/commands/install.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { hey } from '@powership/utils'; -import { Command } from 'commander'; - -import { packageRunner } from '../packageRunner'; -import { packageVersion } from '../packageVersion'; - -export function install(program: Command) { - program - .command('install [cwd]') - .option('--no-scripts', 'Ignores the prepublish scripts.') - .option('--dry-run', 'Ignores the publication.') - .action(async function run(cwd, options): Promise { - const { scripts, dryRun } = options || {}; - - try { - const runner = await packageRunner({ - cwd, - failFast: false, - }); - - if (scripts) { - await runner.run( - { command: 'npm install --dry-run' }, - { - failFast: true, - chunkSize: 1, - } - ); - } - - if (!dryRun) { - await runner.run({ command: 'npm install' }); - } - } catch (e: any) { - hey.error(e); - } - }); -} diff --git a/packages/runmate/src/commands/update-json-value.ts b/packages/runmate/src/commands/update-json-value.ts deleted file mode 100644 index 7985413c..00000000 --- a/packages/runmate/src/commands/update-json-value.ts +++ /dev/null @@ -1,69 +0,0 @@ -import fs from 'fs'; - -import { hey, jsonParse, setByPath } from '@powership/utils'; -import { nodePath } from '@powership/utils/out/node'; -import { Command } from 'commander'; - -import { writePackageJSON } from '../handleJSON'; -import { packageRunner } from '../packageRunner'; -import { packageVersion } from '../packageVersion'; - -export function updateJsonValue(program: Command) {} - -function update(program: Command, op: 'get' | 'set') { - program - .command('set') - .description('Set json value') - .argument('path', 'The object path - example "devDependencies.lodash"') - .argument('value', 'The value to set - example "^1.0.0"') - .option( - '-f, --file ', - 'Path of the file to change', - './package.json' - ) - .option('-d, --cwd ', 'Packages root folder.') - .option('--dry-run', 'Does not save the file.') - .action(async function run(objectPath, value, options): Promise { - const { file = 'package.json', cwd, dryRun } = options || {}; - - try { - const runner = await packageRunner({ - cwd, - failFast: false, - }); - - runner.utils.map(async (util) => { - const jsonPath = nodePath.resolve(util.cwd, file); - const runCommandResult = await util.run({ - command: `cat ${jsonPath}`, - }); - - const content = runCommandResult.data - .map((el) => el.toString('utf-8')) - .join(''); - - const [error, json] = jsonParse(content); - - if (error) { - throw error; - } - - if (op === 'get') { - fs.writeSync(process.stdout.fd, value); - return process.exit(0); - } - - const jsonValue = jsonParse(value)[1] || value; - // @ts-ignore - setByPath(json, objectPath, jsonValue); - - // @ts-ignore - writePackageJSON(jsonPath, json); - return; - }); - } catch (e: any) { - hey.error(e); - process.exit(1); - } - }); -} diff --git a/packages/runmate/src/packageRunner.ts b/packages/runmate/src/packageRunner.ts index f0a9b4a7..fcd10765 100644 --- a/packages/runmate/src/packageRunner.ts +++ b/packages/runmate/src/packageRunner.ts @@ -102,19 +102,11 @@ export async function packageRunner( const { cwd = process.cwd(), failFast: failFastRoot = true, - includeRoot, + includeRoot = true, } = options; const files = findWorkspacePackages({ cwd, includeRoot }); - if (files.length) { - hey.blue( - `Running command in:\n${(() => { - return files.map((el) => ` ‣ ${el.relative}`).join('\n'); - })()}` - ); - } - const utils = files.map((file) => getPackageRunnerUtils(file.path)); const packages = reduceObject(utils, (item) => ({ [item.name]: item })); diff --git a/packages/utils/src/stackTrace.ts b/packages/utils/src/stackTrace.ts index f54be50a..fbc6faf5 100644 --- a/packages/utils/src/stackTrace.ts +++ b/packages/utils/src/stackTrace.ts @@ -8,7 +8,8 @@ export function getStack(parent?: any) { export function captureStackTrace(error: any, parent?: any) { if (typeof Error.captureStackTrace === 'function') { - return Error.captureStackTrace(error, parent); + Error.captureStackTrace(error, parent); + return error; } const container = new Error(); @@ -21,4 +22,6 @@ export function captureStackTrace(error: any, parent?: any) { return stack; }, }); + + return error; }