Skip to content

Commit 433888b

Browse files
authored
Merge pull request #966 from OpenFn/project-versions
Projects: versions
2 parents 6946618 + 65b0e3b commit 433888b

14 files changed

Lines changed: 363 additions & 6 deletions

File tree

packages/cli/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# @openfn/cli
22

3+
## 1.17.0
4+
5+
### Minor Changes
6+
7+
- New command: openfn project version
8+
9+
### Patch Changes
10+
11+
- Updated dependencies
12+
- @openfn/project@0.6.0
13+
314
## 1.16.2
415

516
### Patch Changes

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openfn/cli",
3-
"version": "1.16.2",
3+
"version": "1.17.0",
44
"description": "CLI devtools for the OpenFn toolchain",
55
"engines": {
66
"node": ">=18",

packages/cli/src/cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import testCommand from './test/command';
1616
import projectsCommand from './projects/command';
1717
import checkoutCommand from './checkout/command';
1818
import mergeCommand from './merge/command';
19+
import workflowVersionCommand from './version/command';
1920

2021
const y = yargs(hideBin(process.argv));
2122

@@ -36,6 +37,7 @@ export const cmd = y
3637
.command(projectsCommand)
3738
.command(checkoutCommand)
3839
.command(mergeCommand)
40+
.command(workflowVersionCommand)
3941
.command({
4042
command: 'version',
4143
describe:

packages/cli/src/commands.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import docs from './docs/handler';
1010
import metadata from './metadata/handler';
1111
import pull from './pull/handler';
1212
import projects from './projects/handler';
13+
import workflowVersion from './version/handler';
1314
import checkout from './checkout/handler';
1415
import merge from './merge/handler';
1516
import { clean, install, pwd, list } from './repo/handler';
@@ -36,6 +37,7 @@ export type CommandList =
3637
| 'projects'
3738
| 'checkout'
3839
| 'merge'
40+
| 'project'
3941
| 'repo-clean'
4042
| 'repo-install'
4143
| 'repo-list'
@@ -56,6 +58,7 @@ const handlers = {
5658
projects,
5759
checkout,
5860
merge,
61+
project: workflowVersion,
5962
['collections-get']: collections.get,
6063
['collections-set']: collections.set,
6164
['collections-remove']: collections.remove,

packages/cli/src/options.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type Opts = {
2525
apolloUrl?: string;
2626
apiKey?: string;
2727
autoinstall?: boolean;
28+
json?: boolean;
2829
beta?: boolean;
2930
cacheSteps?: boolean;
3031
compile?: boolean;
@@ -176,6 +177,15 @@ export const apolloUrl: CLIOption = {
176177
},
177178
};
178179

180+
export const json: CLIOption = {
181+
name: 'json',
182+
yargs: {
183+
boolean: true,
184+
description: 'Output the result as a json object',
185+
default: false,
186+
},
187+
};
188+
179189
export const beta: CLIOption = {
180190
name: 'beta',
181191
yargs: {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import yargs from 'yargs';
2+
import { Opts } from '../options';
3+
import { ensure, build } from '../util/command-builders';
4+
import * as o from '../options';
5+
6+
export type VersionOptions = Required<
7+
Pick<
8+
Opts,
9+
| 'command'
10+
| 'workflow'
11+
| 'projectName'
12+
| 'projectPath'
13+
| 'workflowMappings'
14+
| 'json'
15+
>
16+
>;
17+
18+
const options = [
19+
o.workflow,
20+
o.projectName,
21+
o.projectPath,
22+
o.workflowMappings,
23+
o.json,
24+
];
25+
26+
const workflowVersionCommand: yargs.CommandModule = {
27+
command: 'project version [workflow]',
28+
describe: 'Returns the version has of a workflow',
29+
handler: ensure('project', options),
30+
builder: (yargs) => build(options, yargs),
31+
};
32+
33+
export default workflowVersionCommand;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Workspace } from '@openfn/project';
2+
import path from 'path';
3+
import type { Logger } from '../util/logger';
4+
import type { VersionOptions } from './command';
5+
6+
const workflowVersionHandler = async (
7+
options: VersionOptions,
8+
logger: Logger
9+
) => {
10+
const commandPath = path.resolve(options.projectPath ?? '.');
11+
const workspace = new Workspace(commandPath);
12+
if (!workspace.valid) {
13+
logger.error('Command was run in an invalid openfn workspace');
14+
return;
15+
}
16+
17+
const output = new Map<string, string>();
18+
19+
const activeProject = workspace.getActiveProject();
20+
if (options.workflow) {
21+
const workflow = activeProject?.getWorkflow(options.workflow);
22+
if (!workflow) {
23+
logger.error(`No workflow found with id/name ${options.workflow}`);
24+
return;
25+
}
26+
output.set(workflow.name || workflow.id, workflow.getVersionHash());
27+
} else {
28+
for (const wf of activeProject?.workflows || []) {
29+
output.set(wf.name || wf.id, wf.getVersionHash());
30+
}
31+
}
32+
if (!output.size) {
33+
logger.error('No workflow available');
34+
return;
35+
}
36+
37+
let final: string;
38+
if (options.json) {
39+
final = JSON.stringify(Object.fromEntries(output), undefined, 2);
40+
} else {
41+
final = Array.from(output.entries())
42+
.map(([key, value]) => key + '\n' + value)
43+
.join('\n\n');
44+
}
45+
logger.success(`Workflow(s) and their hashes\n\n${final}`);
46+
};
47+
48+
export default workflowVersionHandler;

packages/project/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @openfn/project
22

3+
## 0.6.0
4+
5+
### Minor Changes
6+
7+
- Add version hashes for workflows
8+
39
## 0.5.1
410

511
### Patch Changes

packages/project/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openfn/project",
3-
"version": "0.5.1",
3+
"version": "0.6.0",
44
"description": "Read, serialize, replicate and sync OpenFn projects",
55
"scripts": {
66
"test": "pnpm ava",

packages/project/src/Project.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class Project {
133133
static from(
134134
type: 'state' | 'path' | 'fs',
135135
data: any,
136-
options?: Partial<l.ProjectConfig>
136+
options: Partial<l.ProjectConfig> = {}
137137
): Project {
138138
if (type === 'state') {
139139
return fromAppState(data, options);
@@ -188,8 +188,11 @@ export class Project {
188188

189189
// get workflow by name or id
190190
// this is fuzzy, but is that wrong?
191-
getWorkflow(id: string) {
192-
return this.workflows.find((wf) => wf.id == id);
191+
getWorkflow(idOrName: string) {
192+
return (
193+
this.workflows.find((wf) => wf.id == idOrName) ||
194+
this.workflows.find((wf) => wf.name === idOrName)
195+
);
193196
}
194197

195198
// it's the name of the project.yaml file

0 commit comments

Comments
 (0)