Skip to content

Commit 8180b70

Browse files
create output directory if it doesn't exist (#1452)
* fix(cli): create output directory if it doesn't exist (#1445) The compile and collections commands threw ENOENT when --output-path pointed to a directory that hadn't been created yet. Add mkdir({ recursive: true }) before writeFile in both handlers, matching the pattern already used in the execute command's serialize-output.ts. Fixes #859 * changeset --------- Co-authored-by: Lamine Gueye <51838776+lamine2000@users.noreply.github.com>
1 parent ebfddda commit 8180b70

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

.changeset/green-knives-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openfn/cli': patch
3+
---
4+
5+
Ensure output path exists on compile and collections commands

packages/cli/src/collections/handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path';
2-
import { readFile, writeFile } from 'node:fs/promises';
2+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
33

44
import { Logger } from '../util/logger';
55
import request from './request';
@@ -94,6 +94,7 @@ export const get = async (options: GetOptions, logger: Logger) => {
9494
null,
9595
options.pretty ? 2 : undefined
9696
);
97+
await mkdir(path.dirname(options.outputPath!), { recursive: true });
9798
await writeFile(options.outputPath!, content);
9899
logger.always(`Wrote items to ${options.outputPath}`);
99100
} else {

packages/cli/src/compile/handler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { writeFile } from 'node:fs/promises';
1+
import { mkdir, writeFile } from 'node:fs/promises';
2+
import { dirname } from 'node:path';
23
import type { CompileOptions } from './command';
34
import type { Logger } from '../util/logger';
45

@@ -22,6 +23,7 @@ const compileHandler = async (options: CompileOptions, logger: Logger) => {
2223
if (options.outputStdout) {
2324
logger.success('Result:\n\n' + result);
2425
} else {
26+
await mkdir(dirname(options.outputPath!), { recursive: true });
2527
await writeFile(options.outputPath!, result as string);
2628
logger.success(`Compiled to ${options.outputPath}`);
2729
}

0 commit comments

Comments
 (0)