@vim3d/cli 0.1.0 → 0.2.1

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,3 +1,21 @@
1
1
  # @vim3d/cli
2
2
 
3
- This package is reserved for Vim3D command-line tools.
3
+ Check Spec 2 scenes and evaluate renderer-independent JSON snapshots:
4
+
5
+ ```sh
6
+ npx --package @vim3d/cli vim3d-cli check path/to/scene.vim3d
7
+ npx --package @vim3d/cli vim3d-cli eval path/to/scene.vim3d --frame 12
8
+ npx --package @vim3d/cli vim3d-cli eval path/to/scene.vim3d --frames 0:12 -o frames.json
9
+ ```
10
+
11
+ `check` compiles the scene without evaluating frames and is silent on success.
12
+ `eval` writes one snapshot (frame 0 by default) or an array for the inclusive
13
+ `--frames START:END` range. JSON goes to stdout unless you pass `--output FILE`
14
+ (`-o FILE`). A range is written frame by frame only after all frames evaluate
15
+ successfully.
16
+
17
+ Both commands accept `--fps FPS` (default 30) and `--frame-count COUNT` to
18
+ configure the clip length. Frames are zero-based and must be below the clip
19
+ length when set. Includes resolve relative to the including file; relative
20
+ channel asset paths resolve from the root scene's directory. Diagnostics go to
21
+ stderr and exit with a nonzero status.
package/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/cli.js ADDED
@@ -0,0 +1,136 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const node_fs_1 = require("node:fs");
5
+ const node_path_1 = require("node:path");
6
+ const promises_1 = require("node:stream/promises");
7
+ const node_util_1 = require("node:util");
8
+ const core_1 = require("@vim3d/core");
9
+ const usage = `Usage:
10
+ vim3d-cli check <scene> [--fps <fps>] [--frame-count <count>]
11
+ vim3d-cli eval <scene> [--frame <n> | --frames <start:end>]
12
+ [--fps <fps>] [--frame-count <count>] [-o, --output <file>]`;
13
+ function readAsset(path) {
14
+ try {
15
+ return (0, node_fs_1.readFileSync)(path);
16
+ }
17
+ catch (error) {
18
+ const { code } = error;
19
+ if (code === 'ENOENT' || code === 'ENOTDIR')
20
+ return null;
21
+ throw error;
22
+ }
23
+ }
24
+ function report(diagnostics) {
25
+ for (const { source, code, message, dependencyPath } of diagnostics) {
26
+ const location = source ? `${source.file}:${source.line}: ` : '';
27
+ const chain = dependencyPath?.length ? ` (${dependencyPath.join(' -> ')})` : '';
28
+ console.error(`${location}[${code}] ${message}${chain}`);
29
+ }
30
+ return 1;
31
+ }
32
+ function frameNumber(value) {
33
+ const frame = Number(value);
34
+ if (!/^\d+$/.test(value) || !Number.isSafeInteger(frame)) {
35
+ throw new Error(`Invalid frame number: ${value}`);
36
+ }
37
+ return frame;
38
+ }
39
+ async function writeRange(scene, first, end, output) {
40
+ const indent = (snapshot) => ' ' + JSON.stringify(snapshot, null, 2).replaceAll('\n', '\n ');
41
+ const chunks = ['[\n' + indent(first)];
42
+ for (let frame = first.frame + 1; frame <= end; frame++) {
43
+ const snapshot = (0, core_1.evaluateFrame)(scene, frame);
44
+ if ('diagnostics' in snapshot)
45
+ return report(snapshot.diagnostics);
46
+ chunks.push(',\n' + indent(snapshot.value));
47
+ }
48
+ chunks.push('\n]\n');
49
+ if (output === undefined) {
50
+ await (0, promises_1.pipeline)(chunks, process.stdout, { end: false });
51
+ }
52
+ else {
53
+ const file = (0, node_fs_1.openSync)(output, 'w');
54
+ try {
55
+ for (const chunk of chunks)
56
+ (0, node_fs_1.writeFileSync)(file, chunk);
57
+ }
58
+ finally {
59
+ (0, node_fs_1.closeSync)(file);
60
+ }
61
+ }
62
+ return 0;
63
+ }
64
+ async function run([command, ...args]) {
65
+ if ((command === '--help' || command === '-h') && !args.length) {
66
+ console.log(usage);
67
+ return 0;
68
+ }
69
+ if (command !== 'check' && command !== 'eval')
70
+ throw new Error(usage);
71
+ const { values, positionals } = (0, node_util_1.parseArgs)({
72
+ args,
73
+ allowPositionals: true,
74
+ options: {
75
+ help: { type: 'boolean', short: 'h' },
76
+ fps: { type: 'string' },
77
+ 'frame-count': { type: 'string' },
78
+ ...(command === 'eval' ? {
79
+ frame: { type: 'string' },
80
+ frames: { type: 'string' },
81
+ output: { type: 'string', short: 'o' },
82
+ } : {}),
83
+ },
84
+ });
85
+ if (values.help) {
86
+ console.log(usage);
87
+ return 0;
88
+ }
89
+ if (positionals.length !== 1)
90
+ throw new Error(usage);
91
+ let start = 0;
92
+ let end = 0;
93
+ if (values.frames !== undefined) {
94
+ if (values.frame !== undefined)
95
+ throw new Error('Use either --frame or --frames, not both');
96
+ const bounds = values.frames.split(':');
97
+ if (bounds.length !== 2)
98
+ throw new Error('Expected --frames <start:end>');
99
+ [start, end] = bounds.map(frameNumber);
100
+ if (start > end)
101
+ throw new Error('Frame range must be in ascending order');
102
+ }
103
+ else if (values.frame !== undefined) {
104
+ start = end = frameNumber(values.frame);
105
+ }
106
+ const path = (0, node_path_1.resolve)(positionals[0]);
107
+ const base = (0, node_path_1.dirname)(path);
108
+ const text = new TextDecoder().decode((0, node_fs_1.readFileSync)(path));
109
+ const scene = (0, core_1.compileScene)({ path, text }, {
110
+ fps: values.fps === undefined ? undefined : Number(values.fps),
111
+ frameCount: values['frame-count'] === undefined ? undefined : frameNumber(values['frame-count']),
112
+ readFile: file => readAsset((0, node_path_1.resolve)(base, file)),
113
+ });
114
+ if ('diagnostics' in scene)
115
+ return report(scene.diagnostics);
116
+ if (command === 'check')
117
+ return 0;
118
+ const { frameCount } = scene.value;
119
+ const snapshot = (0, core_1.evaluateFrame)(scene.value, frameCount !== undefined && end >= frameCount ? end : start);
120
+ if ('diagnostics' in snapshot)
121
+ return report(snapshot.diagnostics);
122
+ if (values.frames !== undefined)
123
+ return writeRange(scene.value, snapshot.value, end, values.output);
124
+ const json = JSON.stringify(snapshot.value, null, 2) + '\n';
125
+ if (values.output !== undefined)
126
+ (0, node_fs_1.writeFileSync)(values.output, json);
127
+ else
128
+ process.stdout.write(json);
129
+ return 0;
130
+ }
131
+ void run(process.argv.slice(2)).then(code => {
132
+ process.exitCode = code;
133
+ }, error => {
134
+ console.error(error instanceof Error ? error.message : String(error));
135
+ process.exitCode = 1;
136
+ });
package/esm/cli.js ADDED
@@ -0,0 +1,134 @@
1
+ #!/usr/bin/env node
2
+ import { closeSync, openSync, readFileSync, writeFileSync } from 'node:fs';
3
+ import { dirname, resolve } from 'node:path';
4
+ import { pipeline } from 'node:stream/promises';
5
+ import { parseArgs } from 'node:util';
6
+ import { compileScene, evaluateFrame } from '@vim3d/core';
7
+ const usage = `Usage:
8
+ vim3d-cli check <scene> [--fps <fps>] [--frame-count <count>]
9
+ vim3d-cli eval <scene> [--frame <n> | --frames <start:end>]
10
+ [--fps <fps>] [--frame-count <count>] [-o, --output <file>]`;
11
+ function readAsset(path) {
12
+ try {
13
+ return readFileSync(path);
14
+ }
15
+ catch (error) {
16
+ const { code } = error;
17
+ if (code === 'ENOENT' || code === 'ENOTDIR')
18
+ return null;
19
+ throw error;
20
+ }
21
+ }
22
+ function report(diagnostics) {
23
+ for (const { source, code, message, dependencyPath } of diagnostics) {
24
+ const location = source ? `${source.file}:${source.line}: ` : '';
25
+ const chain = dependencyPath?.length ? ` (${dependencyPath.join(' -> ')})` : '';
26
+ console.error(`${location}[${code}] ${message}${chain}`);
27
+ }
28
+ return 1;
29
+ }
30
+ function frameNumber(value) {
31
+ const frame = Number(value);
32
+ if (!/^\d+$/.test(value) || !Number.isSafeInteger(frame)) {
33
+ throw new Error(`Invalid frame number: ${value}`);
34
+ }
35
+ return frame;
36
+ }
37
+ async function writeRange(scene, first, end, output) {
38
+ const indent = (snapshot) => ' ' + JSON.stringify(snapshot, null, 2).replaceAll('\n', '\n ');
39
+ const chunks = ['[\n' + indent(first)];
40
+ for (let frame = first.frame + 1; frame <= end; frame++) {
41
+ const snapshot = evaluateFrame(scene, frame);
42
+ if ('diagnostics' in snapshot)
43
+ return report(snapshot.diagnostics);
44
+ chunks.push(',\n' + indent(snapshot.value));
45
+ }
46
+ chunks.push('\n]\n');
47
+ if (output === undefined) {
48
+ await pipeline(chunks, process.stdout, { end: false });
49
+ }
50
+ else {
51
+ const file = openSync(output, 'w');
52
+ try {
53
+ for (const chunk of chunks)
54
+ writeFileSync(file, chunk);
55
+ }
56
+ finally {
57
+ closeSync(file);
58
+ }
59
+ }
60
+ return 0;
61
+ }
62
+ async function run([command, ...args]) {
63
+ if ((command === '--help' || command === '-h') && !args.length) {
64
+ console.log(usage);
65
+ return 0;
66
+ }
67
+ if (command !== 'check' && command !== 'eval')
68
+ throw new Error(usage);
69
+ const { values, positionals } = parseArgs({
70
+ args,
71
+ allowPositionals: true,
72
+ options: {
73
+ help: { type: 'boolean', short: 'h' },
74
+ fps: { type: 'string' },
75
+ 'frame-count': { type: 'string' },
76
+ ...(command === 'eval' ? {
77
+ frame: { type: 'string' },
78
+ frames: { type: 'string' },
79
+ output: { type: 'string', short: 'o' },
80
+ } : {}),
81
+ },
82
+ });
83
+ if (values.help) {
84
+ console.log(usage);
85
+ return 0;
86
+ }
87
+ if (positionals.length !== 1)
88
+ throw new Error(usage);
89
+ let start = 0;
90
+ let end = 0;
91
+ if (values.frames !== undefined) {
92
+ if (values.frame !== undefined)
93
+ throw new Error('Use either --frame or --frames, not both');
94
+ const bounds = values.frames.split(':');
95
+ if (bounds.length !== 2)
96
+ throw new Error('Expected --frames <start:end>');
97
+ [start, end] = bounds.map(frameNumber);
98
+ if (start > end)
99
+ throw new Error('Frame range must be in ascending order');
100
+ }
101
+ else if (values.frame !== undefined) {
102
+ start = end = frameNumber(values.frame);
103
+ }
104
+ const path = resolve(positionals[0]);
105
+ const base = dirname(path);
106
+ const text = new TextDecoder().decode(readFileSync(path));
107
+ const scene = compileScene({ path, text }, {
108
+ fps: values.fps === undefined ? undefined : Number(values.fps),
109
+ frameCount: values['frame-count'] === undefined ? undefined : frameNumber(values['frame-count']),
110
+ readFile: file => readAsset(resolve(base, file)),
111
+ });
112
+ if ('diagnostics' in scene)
113
+ return report(scene.diagnostics);
114
+ if (command === 'check')
115
+ return 0;
116
+ const { frameCount } = scene.value;
117
+ const snapshot = evaluateFrame(scene.value, frameCount !== undefined && end >= frameCount ? end : start);
118
+ if ('diagnostics' in snapshot)
119
+ return report(snapshot.diagnostics);
120
+ if (values.frames !== undefined)
121
+ return writeRange(scene.value, snapshot.value, end, values.output);
122
+ const json = JSON.stringify(snapshot.value, null, 2) + '\n';
123
+ if (values.output !== undefined)
124
+ writeFileSync(values.output, json);
125
+ else
126
+ process.stdout.write(json);
127
+ return 0;
128
+ }
129
+ void run(process.argv.slice(2)).then(code => {
130
+ process.exitCode = code;
131
+ }, error => {
132
+ console.error(error instanceof Error ? error.message : String(error));
133
+ process.exitCode = 1;
134
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vim3d/cli",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Command-line tools for Vim3D.",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/pyramation/vim3d/tree/master/js/packages/cli",
@@ -8,6 +8,9 @@
8
8
  "main": "index.js",
9
9
  "module": "esm/index.js",
10
10
  "types": "index.d.ts",
11
+ "bin": {
12
+ "vim3d-cli": "cli.js"
13
+ },
11
14
  "sideEffects": false,
12
15
  "publishConfig": {
13
16
  "access": "public",
@@ -20,6 +23,9 @@
20
23
  "bugs": {
21
24
  "url": "https://github.com/pyramation/vim3d/issues"
22
25
  },
26
+ "dependencies": {
27
+ "@vim3d/core": "^0.2.1"
28
+ },
23
29
  "devDependencies": {
24
30
  "makage": "^0.8.0"
25
31
  },