Skip to content

Commit

Permalink
add --only option to gro upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Jun 16, 2024
1 parent bb1d25a commit 99b0fb5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-jeans-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ryanatkn/gro': patch
---

add `--only` option to `gro upgrade`
36 changes: 26 additions & 10 deletions src/lib/task_logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,27 +172,43 @@ const to_args_schema_type = ({_def}: ZodTypeAny): Arg_Schema['type'] => {
case ZodFirstPartyTypeKind.ZodUnion:
return 'string | string[]'; // TODO support unions of arbitrary types, or more hardcoded ones as needed
default: {
if ('type' in _def) {
return to_args_schema_type(_def.type);
} else if ('innerType' in _def) {
return to_args_schema_type(_def.innerType);
const subschema = to_subschema(_def);
if (subschema) {
return to_args_schema_type(subschema);
} else {
throw Error('Unknown zod type ' + t);
}
}
}
};
const to_args_schema_description = ({_def}: ZodTypeAny): string => {
if (_def.description) return _def.description;
if ('innerType' in _def) {
return to_args_schema_description(_def.innerType);
if (_def.description) {
return _def.description;
}
const subschema = to_subschema(_def);
if (subschema) {
return to_args_schema_description(subschema);
}
return '';
};
const to_args_schema_default = ({_def}: ZodTypeAny): any => {
if (_def.defaultValue) return _def.defaultValue();
if ('innerType' in _def) {
return to_args_schema_default(_def.innerType);
if (_def.defaultValue) {
return _def.defaultValue();
}
const subschema = to_subschema(_def);
if (subschema) {
return to_args_schema_default(subschema);
}
return undefined;
};

const to_subschema = (_def: any): ZodTypeAny | undefined => {
if ('type' in _def) {
return _def.type;
} else if ('innerType' in _def) {
return _def.innerType;
} else if ('schema' in _def) {
return _def.schema;
}
return undefined;
};
35 changes: 31 additions & 4 deletions src/lib/upgrade.task.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import {spawn} from '@ryanatkn/belt/process.js';
import {z} from 'zod';

import type {Task} from './task.js';
import {Task_Error, type Task} from './task.js';
import {load_package_json, type Package_Json} from './package_json.js';
import {Git_Origin, git_pull} from './git.js';
import {spawn_cli} from './cli.js';

export const Args = z
.object({
_: z.array(z.string(), {description: 'names of deps to exclude from the upgrade'}).default([]),
only: z
.union([z.string(), z.array(z.string())], {
description: 'names of deps to include in the upgrade',
})
.default([])
.transform((v) => (Array.isArray(v) ? v : [v])),
origin: Git_Origin.describe('git origin to deploy to').default('origin'),
force: z.boolean({description: 'if true, print out the planned upgrades'}).default(false),
pull: z.boolean({description: 'dual of no-pull'}).default(true),
Expand All @@ -22,7 +28,11 @@ export const task: Task<Args> = {
summary: 'upgrade deps',
Args,
run: async ({args, log}): Promise<void> => {
const {_, origin, force, pull, dry} = args;
const {_, only, origin, force, pull, dry} = args;

if (_.length && only.length) {
throw new Task_Error('Cannot call `gro upgrade` with both rest args and --only.');
}

// TODO maybe a different task that pulls and does other things, like `gro ready`
if (pull) {
Expand All @@ -31,7 +41,17 @@ export const task: Task<Args> = {

const package_json = await load_package_json();

const deps = to_deps(package_json).filter((d) => !_.includes(d.key));
const all_deps = to_deps(package_json);

const deps = only.length
? all_deps.filter((d) => only.includes(d.key))
: all_deps.filter((d) => !_.includes(d.key));

if (only.length && only.length !== deps.length) {
throw new Task_Error(
`Some deps to upgrade were not found: ${only.filter((o) => !deps.find((d) => d.key === o)).join(', ')}`,
);
}

const upgrade_items = to_upgrade_items(deps);

Expand Down Expand Up @@ -70,5 +90,12 @@ const to_deps = (package_json: Package_Json): Dep[] => {
return prod_deps.concat(dev_deps);
};

// TODO hacky and limited
// TODO probably want to pass through exact deps as well, e.g. @foo/bar@1
const to_upgrade_items = (deps: Dep[]): string[] =>
deps.map((dep) => dep.key + (dep.value.includes('-next.') ? '@next' : '@latest'));
deps.map((dep) => {
if (dep.key.endsWith('@next') || dep.key.endsWith('@latest')) {
return dep.key;
}
return dep.key + (dep.value.includes('-next.') ? '@next' : '@latest');
});

0 comments on commit 99b0fb5

Please sign in to comment.