@openfn/cli 0.0.20 → 0.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,69 +1,101 @@
1
- # @openfn/cli (devtools)
1
+ # @openfn/cli
2
2
 
3
- This package contains a new devtools CLI.
3
+ This package contains a new devtools CLI for running openfn jobs.
4
4
 
5
- The CLI allows you to
5
+ The new CLI includes:
6
6
 
7
- - Run a job (expression), writing output to disk or stdout
8
- - Compile a job
9
- - Install modules for jobs
10
- - Use local language adaptors to run the job
7
+ * A new runtime for executing openfn jobs
8
+ * A new compiler for making openfn jobs runnable
9
+ * Improved, customisable logging output
10
+ * Auto installation of language adaptors
11
+ * Support for the adaptors monorepo
11
12
 
12
- The CLI reads a path as its main argument. That path should either point to a .js file or a folder.
13
+ ## Getting Started
13
14
 
14
- - If the path ends in .js, load as a job file and execute. State and output will be read/written relative to it.
15
- - If the path is a folder, the CLI will look for a job.js, state.json and write an output.json.
15
+ To install:
16
16
 
17
- From this input it will infer a working directory, from which state will be read and output will be written.
18
-
19
- All inputs and outputs can be configured with arguments.
17
+ ```
18
+ npm install -g @openfn/cli
19
+ ```
20
20
 
21
- Run `pnpm openfn -h` to print usage help (the best source of truth right now).
21
+ Make sure everything works by running the built-in test job:
22
22
 
23
- ## Usage from the global CLI
23
+ ```
24
+ openfn test
25
+ ```
24
26
 
25
- See Getting Started for more setup steps.
27
+ Check the version:
26
28
 
27
29
  ```
28
- $ npm install -g @openfn/cli`
29
- $ openfn --help
30
- $ openfn path/to/expression.js`
30
+ openfn -v
31
31
  ```
32
32
 
33
- ## Language Adaptors
33
+ Get help:
34
34
 
35
- The old engine used to compile knowlede of langauge adaptors into the job file. We no longer do this.
35
+ ```
36
+ openfn help
37
+ ```
36
38
 
37
- Generally the new runtime prefers to explictly import all dependencies - although the compiler will auto-insert imports from a given adaptor for you.
39
+ ## Basic Usage
38
40
 
39
- You need to pass the name of an adaptor for the runtime to use. It will auto-insert an import statement for you.
41
+ You're probably here to run jobs (expressions), which the CLI makes easy:
40
42
 
41
43
  ```
42
- $ openfn job.js -a commmon
44
+ openfn path/to/job.js -ia adaptor-name
43
45
  ```
44
46
 
45
- You can use a short-hand name, like above, but longform names also work:
46
- ```
47
- $ openfn job.js -a @openfn/language-commmon
48
- ```
47
+ You MUST specify which adaptor to use. Pass the `-i` flag to auto-install that adaptor (it's safe to do this redundantly).
49
48
 
50
- You can pass an explicit version number too:
49
+ If output.json and state.json are not passed, the CLI will look for them next to the job.js file. You can pass a path to state by adding `-s path/to/state.json`, and output by passing `-o path/to/output.json`. You can use `-S` and `-O` to pass state through stdin and return the output through stdout.
51
50
 
52
- ```
53
- $ openfn job.js -a commmon@1.7.3
54
- ```
51
+ The CLI can auto-install language adaptors to its own privately maintained repo. Run `openfn repo list` to see where the repo is, and what's in it. Set the `OPENFN_REPO_DIR` env var to specify the repo folder. When autoinstalling, the CLI will check to see if a matching version is found in the repo.
55
52
 
56
- The adaptor also needs to be installed in the CLI's module repo. You can do this manually:
53
+ You can specify adaptors with a shorthand (`http`) or use the full package name (`@openfn/language-http`). You can add a specific version like `http@2.0.0`. You can pass a path to a locally installed adaptor like `http=/repo/openfn/adaptors/my-http-build`. Set the OPENFN_ADAPTORS_REPO env var to load adaptors straight out of the monorepo (pass the `--no-adaptors-repo` flag to disable this for a single run).
57
54
 
58
- ```
59
- $ openfn install commmon
60
- ```
61
- If no version is provided, the latest will be installed. Again, long and short-form names can be used.
55
+ You can pass `--log info` to get more feedback about what's happening, or `--log debug` for more details than you could ever use.
62
56
 
63
- Alternatively, pass the -i flag when running a job (it's safe to do this redundantly):
64
- ```
65
- $ openfn job.js -i -a @openfn/language-commmon
66
- ```
57
+ ## Advanced Usage
58
+
59
+ The CLI has actually has a number of commands (the first argument after openfn)
60
+
61
+ * execute - run a job
62
+ * compile - compile a job to a .js file
63
+ * doc - show documentation for an adaptor function
64
+ * repo - manage the repo of installed modules
65
+ * docgen - generate JSON documentation for an adaptor based on its typescript
66
+
67
+ If no command is specified, execute will run.
68
+
69
+ To get more information about a command, including usage examples, run `openfn <command> help`, ie, `openfn compile help`.
70
+
71
+ ## Compilation
72
+
73
+ The CLI will attempt to compile your job code into normalized Javascript. It will do a number of things to make your code robust and portable:
74
+
75
+ * The language adaptor will be imported into the file
76
+ * The adaptor's execute function will be exported form the file
77
+ * All top level operations will be added to an array
78
+ * That array will be made the default export of the file
79
+
80
+ The result of this is a lightweight, modern JS source file. It can be executed in any runtime environment: just execute each function in the exported array.
81
+
82
+ The CLI uses openfn's own runtime to execute jobs in a safe environment.
83
+
84
+ All jobs which work against `@openfn/core` will work in the new CLI and runtime environment (note: although this is a work in progress and we are actively looking for help to test this!).
85
+
86
+ ## New Runtime notes
87
+
88
+ The new openfunction runtime basically does one thing: load a Javascript Module, find the default export, and execute the functions it holds.
89
+
90
+ So long as your job has an array of functions as its default export, it will run in the new runtime.
91
+
92
+ # Contributing
93
+
94
+ First of all, thanks for helping! You're contributing to a digital public good that will always be free and open source and aimed at serving innovative NGOs, governments, and social impact organizations the world over! You rock. heart
95
+
96
+ To get this started, you'll want to clone this repo.
97
+
98
+ You also need to install `pnpm`.
67
99
 
68
100
  ## Usage from this repo
69
101
 
@@ -78,7 +110,7 @@ See test/execute.test.ts for more usage examples
78
110
 
79
111
  ## Installing globally
80
112
 
81
- To install the CLI globally from this repo (ie, to do `openfn job.js` instead of `pnpm openfn job.js`), run:
113
+ To install the CLI globally from the build in repo:
82
114
 
83
115
  ```
84
116
  $ npm install -g .
@@ -94,24 +126,15 @@ You should set the OPENFN_REPO_DIR env var to something sensible.
94
126
 
95
127
  ```
96
128
  # In ~/.bashc or whatever
97
- export OPENFN_REPO_DIR=~/adaptors/@openfn
129
+ export OPENFN_REPO_DIR=~/repo/openfn/cli-repo
98
130
  ```
99
131
 
100
- At the time of writing, teh env var name is about to change. Soon you will be able to pass the repo dir into the command line, but the env var is a much easier way to work.
101
-
102
- Monorepo support is coming soon.
132
+ To run adaptors straight from the adaptors monorepo:
103
133
 
104
- ## Automatic Imports
134
+ export OPENFN_ADAPTORS_REPO=~/repo/openfn/adaptors
105
135
 
106
- The v2 runtime requires explicit imports to be in the job file, or else the job will fail.
107
-
108
- The v2 compiler can automatically insert import statements, but it needs to be told which adaptor to use.
109
-
110
- ```
111
- $ openfn job.js --adaptors @openfn/language-http
112
- $ openfn job.js --adaptors @openfn/language-http=path/to/adaptor
113
- ```
136
+ ## Contributing changes
114
137
 
115
- If a path is passed (relative to the working directory), that path will be used to load a local version of the adaptor (both at runtime and for import generation)
138
+ Open a PR at https://github.com/openfn/kit. Include a changeset and a description of your change.
116
139
 
117
- If no path is passed, the currently deployed npm package will be used.
140
+ See the root readme for more details about changests,
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@
4
4
  import path from "node:path";
5
5
  import * as url from "url";
6
6
  import { fork } from "node:child_process";
7
+ import process2 from "node:process";
7
8
  function spawn_default(basePath, opts2) {
8
9
  const execArgv = [
9
10
  "--no-warnings",
@@ -12,15 +13,18 @@ function spawn_default(basePath, opts2) {
12
13
  ];
13
14
  const dirname = path.dirname(url.fileURLToPath(import.meta.url));
14
15
  const child = fork(`${dirname}/process/runner.js`, [], { execArgv });
15
- child.on("message", ({ done, init }) => {
16
+ child.on("message", ({ done, init, exitCode }) => {
16
17
  if (init) {
17
18
  child.send({ init: true, basePath, opts: opts2 });
18
19
  }
19
20
  if (done) {
20
21
  child.kill();
21
- process.exit(0);
22
+ process2.exit(exitCode);
22
23
  }
23
24
  });
25
+ child.on("close", (code) => {
26
+ process2.exitCode = code;
27
+ });
24
28
  }
25
29
 
26
30
  // src/cli.ts
@@ -384,10 +384,17 @@ var executeHandler = async (options, logger) => {
384
384
  }
385
385
  const state = await load_state_default(options, logger);
386
386
  const code = await compile_default(options, logger);
387
- const result = await execute_default(code, state, options);
388
- await serialize_output_default(options, result, logger);
389
- const duration = printDuration(new Date().getTime() - start);
390
- logger.success(`Done in ${duration}! \u2728`);
387
+ try {
388
+ const result = await execute_default(code, state, options);
389
+ await serialize_output_default(options, result, logger);
390
+ const duration = printDuration(new Date().getTime() - start);
391
+ logger.success(`Done in ${duration}! \u2728`);
392
+ } catch (error) {
393
+ logger.error(error);
394
+ const duration = printDuration(new Date().getTime() - start);
395
+ logger.error(`Took ${duration}.`);
396
+ process.exitCode = 1;
397
+ }
391
398
  };
392
399
  var handler_default = executeHandler;
393
400
 
@@ -625,7 +632,7 @@ var assertPath = (basePath) => {
625
632
  process.on("message", ({ init, basePath, opts }) => {
626
633
  if (init) {
627
634
  commands_default(basePath, opts).then(() => {
628
- process.send({ done: true });
635
+ process.send({ done: true, exitCode: process.exitCode });
629
636
  });
630
637
  }
631
638
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/cli",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "description": "CLI devtools for the openfn toolchain.",
5
5
  "engines": {
6
6
  "node": ">=16",
@@ -36,10 +36,10 @@
36
36
  "typescript": "^4.7.4"
37
37
  },
38
38
  "dependencies": {
39
- "@openfn/compiler": "^0.0.19",
40
- "@openfn/describe-package": "^0.0.11",
41
- "@openfn/logger": "^0.0.8",
42
- "@openfn/runtime": "^0.0.13",
39
+ "@openfn/compiler": "0.0.20",
40
+ "@openfn/describe-package": "0.0.13",
41
+ "@openfn/logger": "0.0.8",
42
+ "@openfn/runtime": "0.0.13",
43
43
  "fast-safe-stringify": "^2.1.1",
44
44
  "rimraf": "^3.0.2",
45
45
  "treeify": "^1.1.0",