Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,22 @@ make-cli scenarios create \
--scheduling='{"type":"on-demand"}' \
--blueprint='{"name":"My Scenario","flow":[],"metadata":{}}'

# Create a scenario using a blueprint stored in a file
make-cli scenarios create --team-id=123 --scheduling='{"type":"on-demand"}' --blueprint=@./scenario-blueprint.json

# Output formatting
make-cli scenarios list --team-id=123 --output=table
```

### JSON inputs from files

Options whose schema expects a JSON object or array accept either inline JSON or a file reference. Prefix the file path with `@`. The path is resolved from the command's current working directory. This is useful for large scenario blueprints and avoids command-line length limits on Windows.

```bash
make-cli scenarios create --team-id=123 --blueprint=@./scenario-blueprint.json
make-cli scenarios update 456 --blueprint="@./scenario blueprints/production.json"
```

### Resource IDs

Resource-level actions (`get`, `update`, `delete`, and similar) accept the resource's own ID as a **positional argument**. The long-form flag is still available for scripting or when you prefer being explicit — both forms are equivalent:
Expand Down
43 changes: 36 additions & 7 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFileSync } from 'node:fs';
import type { Command, OptionValues } from 'commander';
import { Command as CommandClass } from 'commander';
import type { MakeTool, JSONSchema } from '@makehq/sdk/tools';
Expand Down Expand Up @@ -67,27 +68,55 @@ export function coerceValue(value: string, schema: JSONSchema): JSONValue {
case 'boolean':
return value === 'true' || value === '1';
case 'object':
case 'array':
case 'array': {
let jsonValue = value;
let filePath: string | undefined;

if (value.startsWith('@')) {
filePath = value.slice(1);
if (!filePath) {
throw new Error('Expected a file path after "@" for JSON input');
}

try {
jsonValue = readFileSync(filePath, 'utf8');
} catch (err) {
const reason = err instanceof Error ? err.message : String(err);
throw new Error(`Unable to read JSON input file "${filePath}": ${reason}`);
}
}

try {
const parsed = JSON.parse(value);
const parsed = JSON.parse(jsonValue);
const inputDescription = filePath ? `file "${filePath}"` : value;

if (type === 'array') {
if (!Array.isArray(parsed)) {
throw new Error(`Expected JSON array for schema type "array", got: ${value}`);
}
} else {
if (parsed === null || Array.isArray(parsed) || typeof parsed !== 'object') {
throw new Error(`Expected JSON object for schema type "object", got: ${value}`);
throw new Error(
`Expected JSON array for schema type "array", got: ${inputDescription}`,
);
}
} else if (
parsed === null ||
Array.isArray(parsed) ||
typeof parsed !== 'object'
) {
throw new Error(
`Expected JSON object for schema type "object", got: ${inputDescription}`,
);
}

return parsed as JSONValue;
} catch (err) {
if (err instanceof SyntaxError) {
if (filePath) {
throw new Error(`Expected valid JSON in file "${filePath}"`);
}
throw new Error(`Expected valid JSON, got: ${value}`);
}
throw err;
}
}
default:
return value;
}
Expand Down
57 changes: 57 additions & 0 deletions test/commands.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { describe, expect, it, beforeEach, afterEach, jest } from '@jest/globals';
import { Command, Option } from 'commander';
import { deriveActionName, coerceValue, buildCommands, deriveSelfIdentifier } from '../src/commands.js';
Expand Down Expand Up @@ -106,6 +109,60 @@ describe('CLI: coerceValue', () => {
});
});

describe('CLI: coerceValue JSON files', () => {
it('should read object and array values from @-prefixed files', () => {
const directory = mkdtempSync(join(tmpdir(), 'make-cli-json-'));
const objectPath = join(directory, 'scenario blueprint.json');
const arrayPath = join(directory, 'modules.json');

try {
writeFileSync(objectPath, '{"name":"My Scenario","flow":[]}', 'utf8');
writeFileSync(arrayPath, '[1,2,3]', 'utf8');

expect(coerceValue(`@${objectPath}`, { type: 'object' })).toEqual({
name: 'My Scenario',
flow: [],
});
expect(coerceValue(`@${arrayPath}`, { type: 'array' })).toEqual([1, 2, 3]);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});

it('should report when a JSON input file cannot be read', () => {
const directory = mkdtempSync(join(tmpdir(), 'make-cli-json-'));
const filePath = join(directory, 'missing.json');

try {
expect(() => coerceValue(`@${filePath}`, { type: 'object' })).toThrow(
`Unable to read JSON input file "${filePath}"`,
);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});

it('should report invalid JSON in a file', () => {
const directory = mkdtempSync(join(tmpdir(), 'make-cli-json-'));
const filePath = join(directory, 'invalid.json');

try {
writeFileSync(filePath, '{invalid json', 'utf8');
expect(() => coerceValue(`@${filePath}`, { type: 'object' })).toThrow(
`Expected valid JSON in file "${filePath}"`,
);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});

it('should require a path after the @ prefix', () => {
expect(() => coerceValue('@', { type: 'object' })).toThrow(
'Expected a file path after "@" for JSON input',
);
});
});

describe('CLI: resolveAuth', () => {
beforeEach(() => {
delete process.env.MAKE_API_KEY;
Expand Down