Skip to content

Commit

Permalink
runmate add getSetJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniopresto committed Mar 16, 2024
1 parent 07e153c commit de9267f
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 138 deletions.
7 changes: 4 additions & 3 deletions packages/runmate/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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' },
{
Expand Down Expand Up @@ -91,6 +91,7 @@ program
const runner = await packageRunner({
cwd: src,
failFast: false,
// includeRoot: true // link? is it used?
});

if (clean) {
Expand Down Expand Up @@ -150,7 +151,7 @@ program
list(program);
version(program);
align(program);
updateJsonValue(program);
getSetJSON(program);
peers(program);

program.version('> Runmate ' + require('../package.json').version);
Expand Down
1 change: 1 addition & 0 deletions packages/runmate/src/commands/align.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function align(program: Command) {
const runner = await packageRunner({
failFast: false,
cwd,
includeRoot: true,
});

const commons: { [K: string]: string[] } = {};
Expand Down
22 changes: 4 additions & 18 deletions packages/runmate/src/commands/executeInPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ function _executeInPackages(
) {
return program
.option('-d, --cwd <pattern>', 'Source directory or glob pattern')
.option(
'--include-root <boolean>',
'Include root project in execution',
'true'
)
.option('--include-root', 'Include root project in execution', true)
.option(
'-c, --chunk-size <number>',
'Chunk size of parallel executions',
Expand All @@ -60,7 +56,7 @@ function _executeInPackages(
.action(async function run(
commands: string[],
options?: {
includeRoot?: unknown;
includeRoot?: boolean;
cwd?: string;
chunkSize: string;
failFast: boolean;
Expand All @@ -81,7 +77,7 @@ function _executeInPackages(
packages,
packageNameCommand,
from,
includeRoot,
includeRoot = true,
} = options || {};

if (commands?.[0]?.startsWith('./')) {
Expand All @@ -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') {
Expand Down
121 changes: 121 additions & 0 deletions packages/runmate/src/commands/getSetJSON.ts
Original file line number Diff line number Diff line change
@@ -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 <propertyPath> [file]'));

update(
'set',
program
.command('set <propertyPath> <value> [file]')
.description('Set json value')
);
}

function update(op: 'get' | 'set', program: Command) {
program
.option('-d, --cwd <pattern>', 'Packages root folder.')
.option('--dry-run', 'Does not save the file.')
.action(async function run(...args): Promise<any> {
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);
}
});
}
38 changes: 0 additions & 38 deletions packages/runmate/src/commands/install.ts

This file was deleted.

69 changes: 0 additions & 69 deletions packages/runmate/src/commands/update-json-value.ts

This file was deleted.

10 changes: 1 addition & 9 deletions packages/runmate/src/packageRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));

Expand Down
5 changes: 4 additions & 1 deletion packages/utils/src/stackTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -21,4 +22,6 @@ export function captureStackTrace(error: any, parent?: any) {
return stack;
},
});

return error;
}

0 comments on commit de9267f

Please sign in to comment.