@robinpath/cli 1.75.0 → 1.76.0

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/modules/child.js CHANGED
@@ -1,176 +1,176 @@
1
- /**
2
- * Native child process module for RobinPath.
3
- * Execute system commands and spawn processes.
4
- */
5
- import { exec as _exec, execSync as _execSync, spawn as _spawn } from 'node:child_process';
6
- import { toStr, toNum, requireArgs } from './_helpers.js';
7
-
8
- const _processes = new Map();
9
- let _nextId = 1;
10
-
11
- export const ChildFunctions = {
12
-
13
- exec: (args) => {
14
- requireArgs('child.exec', args, 1);
15
- const command = toStr(args[0]);
16
- const opts = {};
17
- if (args[1] && typeof args[1] === 'object') {
18
- if (args[1].cwd) opts.cwd = toStr(args[1].cwd);
19
- if (args[1].timeout) opts.timeout = toNum(args[1].timeout);
20
- if (args[1].encoding) opts.encoding = toStr(args[1].encoding);
21
- if (args[1].env) opts.env = { ...process.env, ...args[1].env };
22
- if (args[1].maxBuffer) opts.maxBuffer = toNum(args[1].maxBuffer);
23
- if (args[1].shell) opts.shell = toStr(args[1].shell);
24
- }
25
- opts.encoding = opts.encoding || 'utf-8';
26
-
27
- return new Promise((resolve, reject) => {
28
- _exec(command, opts, (error, stdout, stderr) => {
29
- resolve({
30
- stdout: stdout || '',
31
- stderr: stderr || '',
32
- code: error ? error.code ?? 1 : 0,
33
- error: error ? error.message : null
34
- });
35
- });
36
- });
37
- },
38
-
39
- execSync: (args) => {
40
- requireArgs('child.execSync', args, 1);
41
- const command = toStr(args[0]);
42
- const opts = { encoding: 'utf-8' };
43
- if (args[1] && typeof args[1] === 'object') {
44
- if (args[1].cwd) opts.cwd = toStr(args[1].cwd);
45
- if (args[1].timeout) opts.timeout = toNum(args[1].timeout);
46
- if (args[1].shell) opts.shell = toStr(args[1].shell);
47
- }
48
- try {
49
- return _execSync(command, opts);
50
- } catch (err) {
51
- return {
52
- stdout: err.stdout || '',
53
- stderr: err.stderr || '',
54
- code: err.status ?? 1,
55
- error: err.message
56
- };
57
- }
58
- },
59
-
60
- spawn: (args) => {
61
- requireArgs('child.spawn', args, 1);
62
- const command = toStr(args[0]);
63
- const spawnArgs = Array.isArray(args[1]) ? args[1].map(a => toStr(a)) : [];
64
- const opts = { shell: true };
65
- if (args[2] && typeof args[2] === 'object') {
66
- if (args[2].cwd) opts.cwd = toStr(args[2].cwd);
67
- if (args[2].env) opts.env = { ...process.env, ...args[2].env };
68
- if (args[2].shell !== undefined) opts.shell = args[2].shell;
69
- if (args[2].detached) opts.detached = true;
70
- }
71
-
72
- const child = _spawn(command, spawnArgs, opts);
73
- const id = `proc_${_nextId++}`;
74
- let stdout = '';
75
- let stderr = '';
76
-
77
- if (child.stdout) child.stdout.on('data', (d) => { stdout += d; });
78
- if (child.stderr) child.stderr.on('data', (d) => { stderr += d; });
79
-
80
- const resultPromise = new Promise((resolve) => {
81
- child.on('close', (code) => {
82
- _processes.delete(id);
83
- resolve({ id, stdout, stderr, code: code ?? 0 });
84
- });
85
- child.on('error', (err) => {
86
- _processes.delete(id);
87
- resolve({ id, stdout, stderr, code: 1, error: err.message });
88
- });
89
- });
90
-
91
- _processes.set(id, { child, resultPromise });
92
- return id;
93
- },
94
-
95
- wait: async (args) => {
96
- requireArgs('child.wait', args, 1);
97
- const id = toStr(args[0]);
98
- const proc = _processes.get(id);
99
- if (!proc) return { error: `Process ${id} not found` };
100
- return await proc.resultPromise;
101
- },
102
-
103
- kill: (args) => {
104
- requireArgs('child.kill', args, 1);
105
- const id = toStr(args[0]);
106
- const signal = toStr(args[1], 'SIGTERM');
107
- const proc = _processes.get(id);
108
- if (!proc) return false;
109
- proc.child.kill(signal);
110
- _processes.delete(id);
111
- return true;
112
- },
113
-
114
- running: () => {
115
- return Array.from(_processes.keys());
116
- }
117
- };
118
-
119
- export const ChildFunctionMetadata = {
120
- exec: {
121
- description: 'Execute a shell command and return output',
122
- parameters: [
123
- { name: 'command', dataType: 'string', description: 'Shell command to execute', formInputType: 'text', required: true },
124
- { name: 'options', dataType: 'object', description: 'Options: cwd, timeout, encoding, env, maxBuffer, shell', formInputType: 'json', required: false }
125
- ],
126
- returnType: 'object', returnDescription: 'Object with stdout, stderr, code, error', example: 'child.exec "ls -la"'
127
- },
128
- execSync: {
129
- description: 'Execute a shell command synchronously',
130
- parameters: [
131
- { name: 'command', dataType: 'string', description: 'Shell command', formInputType: 'text', required: true },
132
- { name: 'options', dataType: 'object', description: 'Options: cwd, timeout, shell', formInputType: 'json', required: false }
133
- ],
134
- returnType: 'string', returnDescription: 'Command output string', example: 'child.execSync "echo hello"'
135
- },
136
- spawn: {
137
- description: 'Spawn a child process (non-blocking)',
138
- parameters: [
139
- { name: 'command', dataType: 'string', description: 'Command to run', formInputType: 'text', required: true },
140
- { name: 'args', dataType: 'array', description: 'Command arguments', formInputType: 'json', required: false },
141
- { name: 'options', dataType: 'object', description: 'Options: cwd, env, shell, detached', formInputType: 'json', required: false }
142
- ],
143
- returnType: 'string', returnDescription: 'Process handle ID', example: 'child.spawn "node" ["server.js"]'
144
- },
145
- wait: {
146
- description: 'Wait for a spawned process to finish',
147
- parameters: [{ name: 'processId', dataType: 'string', description: 'Process handle ID from spawn', formInputType: 'text', required: true }],
148
- returnType: 'object', returnDescription: 'Object with stdout, stderr, code', example: 'child.wait $pid'
149
- },
150
- kill: {
151
- description: 'Kill a spawned process',
152
- parameters: [
153
- { name: 'processId', dataType: 'string', description: 'Process handle ID', formInputType: 'text', required: true },
154
- { name: 'signal', dataType: 'string', description: 'Signal (default: SIGTERM)', formInputType: 'text', required: false, defaultValue: 'SIGTERM' }
155
- ],
156
- returnType: 'boolean', returnDescription: 'true if killed', example: 'child.kill $pid'
157
- },
158
- running: {
159
- description: 'List all running spawned processes',
160
- parameters: [],
161
- returnType: 'array', returnDescription: 'Array of process handle IDs', example: 'child.running'
162
- }
163
- };
164
-
165
- export const ChildModuleMetadata = {
166
- description: 'Child process execution: exec, execSync, spawn, kill, and process management',
167
- methods: Object.keys(ChildFunctions)
168
- };
169
-
170
- export default {
171
- name: 'child',
172
- functions: ChildFunctions,
173
- functionMetadata: ChildFunctionMetadata,
174
- moduleMetadata: ChildModuleMetadata,
175
- global: false
176
- };
1
+ /**
2
+ * Native child process module for RobinPath.
3
+ * Execute system commands and spawn processes.
4
+ */
5
+ import { exec as _exec, execSync as _execSync, spawn as _spawn } from 'node:child_process';
6
+ import { toStr, toNum, requireArgs } from './_helpers.js';
7
+
8
+ const _processes = new Map();
9
+ let _nextId = 1;
10
+
11
+ export const ChildFunctions = {
12
+
13
+ exec: (args) => {
14
+ requireArgs('child.exec', args, 1);
15
+ const command = toStr(args[0]);
16
+ const opts = {};
17
+ if (args[1] && typeof args[1] === 'object') {
18
+ if (args[1].cwd) opts.cwd = toStr(args[1].cwd);
19
+ if (args[1].timeout) opts.timeout = toNum(args[1].timeout);
20
+ if (args[1].encoding) opts.encoding = toStr(args[1].encoding);
21
+ if (args[1].env) opts.env = { ...process.env, ...args[1].env };
22
+ if (args[1].maxBuffer) opts.maxBuffer = toNum(args[1].maxBuffer);
23
+ if (args[1].shell) opts.shell = toStr(args[1].shell);
24
+ }
25
+ opts.encoding = opts.encoding || 'utf-8';
26
+
27
+ return new Promise((resolve, reject) => {
28
+ _exec(command, opts, (error, stdout, stderr) => {
29
+ resolve({
30
+ stdout: stdout || '',
31
+ stderr: stderr || '',
32
+ code: error ? error.code ?? 1 : 0,
33
+ error: error ? error.message : null
34
+ });
35
+ });
36
+ });
37
+ },
38
+
39
+ execSync: (args) => {
40
+ requireArgs('child.execSync', args, 1);
41
+ const command = toStr(args[0]);
42
+ const opts = { encoding: 'utf-8' };
43
+ if (args[1] && typeof args[1] === 'object') {
44
+ if (args[1].cwd) opts.cwd = toStr(args[1].cwd);
45
+ if (args[1].timeout) opts.timeout = toNum(args[1].timeout);
46
+ if (args[1].shell) opts.shell = toStr(args[1].shell);
47
+ }
48
+ try {
49
+ return _execSync(command, opts);
50
+ } catch (err) {
51
+ return {
52
+ stdout: err.stdout || '',
53
+ stderr: err.stderr || '',
54
+ code: err.status ?? 1,
55
+ error: err.message
56
+ };
57
+ }
58
+ },
59
+
60
+ spawn: (args) => {
61
+ requireArgs('child.spawn', args, 1);
62
+ const command = toStr(args[0]);
63
+ const spawnArgs = Array.isArray(args[1]) ? args[1].map(a => toStr(a)) : [];
64
+ const opts = { shell: true };
65
+ if (args[2] && typeof args[2] === 'object') {
66
+ if (args[2].cwd) opts.cwd = toStr(args[2].cwd);
67
+ if (args[2].env) opts.env = { ...process.env, ...args[2].env };
68
+ if (args[2].shell !== undefined) opts.shell = args[2].shell;
69
+ if (args[2].detached) opts.detached = true;
70
+ }
71
+
72
+ const child = _spawn(command, spawnArgs, opts);
73
+ const id = `proc_${_nextId++}`;
74
+ let stdout = '';
75
+ let stderr = '';
76
+
77
+ if (child.stdout) child.stdout.on('data', (d) => { stdout += d; });
78
+ if (child.stderr) child.stderr.on('data', (d) => { stderr += d; });
79
+
80
+ const resultPromise = new Promise((resolve) => {
81
+ child.on('close', (code) => {
82
+ _processes.delete(id);
83
+ resolve({ id, stdout, stderr, code: code ?? 0 });
84
+ });
85
+ child.on('error', (err) => {
86
+ _processes.delete(id);
87
+ resolve({ id, stdout, stderr, code: 1, error: err.message });
88
+ });
89
+ });
90
+
91
+ _processes.set(id, { child, resultPromise });
92
+ return id;
93
+ },
94
+
95
+ wait: async (args) => {
96
+ requireArgs('child.wait', args, 1);
97
+ const id = toStr(args[0]);
98
+ const proc = _processes.get(id);
99
+ if (!proc) return { error: `Process ${id} not found` };
100
+ return await proc.resultPromise;
101
+ },
102
+
103
+ kill: (args) => {
104
+ requireArgs('child.kill', args, 1);
105
+ const id = toStr(args[0]);
106
+ const signal = toStr(args[1], 'SIGTERM');
107
+ const proc = _processes.get(id);
108
+ if (!proc) return false;
109
+ proc.child.kill(signal);
110
+ _processes.delete(id);
111
+ return true;
112
+ },
113
+
114
+ running: () => {
115
+ return Array.from(_processes.keys());
116
+ }
117
+ };
118
+
119
+ export const ChildFunctionMetadata = {
120
+ exec: {
121
+ description: 'Execute a shell command and return output',
122
+ parameters: [
123
+ { name: 'command', dataType: 'string', description: 'Shell command to execute', formInputType: 'text', required: true },
124
+ { name: 'options', dataType: 'object', description: 'Options: cwd, timeout, encoding, env, maxBuffer, shell', formInputType: 'json', required: false }
125
+ ],
126
+ returnType: 'object', returnDescription: 'Object with stdout, stderr, code, error', example: 'child.exec "ls -la"'
127
+ },
128
+ execSync: {
129
+ description: 'Execute a shell command synchronously',
130
+ parameters: [
131
+ { name: 'command', dataType: 'string', description: 'Shell command', formInputType: 'text', required: true },
132
+ { name: 'options', dataType: 'object', description: 'Options: cwd, timeout, shell', formInputType: 'json', required: false }
133
+ ],
134
+ returnType: 'string', returnDescription: 'Command output string', example: 'child.execSync "echo hello"'
135
+ },
136
+ spawn: {
137
+ description: 'Spawn a child process (non-blocking)',
138
+ parameters: [
139
+ { name: 'command', dataType: 'string', description: 'Command to run', formInputType: 'text', required: true },
140
+ { name: 'args', dataType: 'array', description: 'Command arguments', formInputType: 'json', required: false },
141
+ { name: 'options', dataType: 'object', description: 'Options: cwd, env, shell, detached', formInputType: 'json', required: false }
142
+ ],
143
+ returnType: 'string', returnDescription: 'Process handle ID', example: 'child.spawn "node" ["server.js"]'
144
+ },
145
+ wait: {
146
+ description: 'Wait for a spawned process to finish',
147
+ parameters: [{ name: 'processId', dataType: 'string', description: 'Process handle ID from spawn', formInputType: 'text', required: true }],
148
+ returnType: 'object', returnDescription: 'Object with stdout, stderr, code', example: 'child.wait $pid'
149
+ },
150
+ kill: {
151
+ description: 'Kill a spawned process',
152
+ parameters: [
153
+ { name: 'processId', dataType: 'string', description: 'Process handle ID', formInputType: 'text', required: true },
154
+ { name: 'signal', dataType: 'string', description: 'Signal (default: SIGTERM)', formInputType: 'text', required: false, defaultValue: 'SIGTERM' }
155
+ ],
156
+ returnType: 'boolean', returnDescription: 'true if killed', example: 'child.kill $pid'
157
+ },
158
+ running: {
159
+ description: 'List all running spawned processes',
160
+ parameters: [],
161
+ returnType: 'array', returnDescription: 'Array of process handle IDs', example: 'child.running'
162
+ }
163
+ };
164
+
165
+ export const ChildModuleMetadata = {
166
+ description: 'Child process execution: exec, execSync, spawn, kill, and process management',
167
+ methods: Object.keys(ChildFunctions)
168
+ };
169
+
170
+ export default {
171
+ name: 'child',
172
+ functions: ChildFunctions,
173
+ functionMetadata: ChildFunctionMetadata,
174
+ moduleMetadata: ChildModuleMetadata,
175
+ global: false
176
+ };