-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy patheval.ts
More file actions
197 lines (174 loc) · 5.88 KB
/
eval.ts
File metadata and controls
197 lines (174 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import fs from 'node:fs';
import {Args, Flags} from '@oclif/core';
import {InstanceCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {evaluateScript, type EvaluateScriptResult} from '@salesforce/b2c-tooling-sdk/operations/script';
import {getActiveCodeVersion} from '@salesforce/b2c-tooling-sdk/operations/code';
import {t} from '../../i18n/index.js';
export default class ScriptEval extends InstanceCommand<typeof ScriptEval> {
static args = {
expression: Args.string({
description: 'Script expression to evaluate',
required: false,
}),
};
static description = t(
'commands.script.eval.description',
'Evaluate a Script API expression on a B2C Commerce instance',
);
static enableJsonFlag = true;
static examples = [
// Inline expression
'<%= config.bin %> <%= command.id %> "dw.system.Site.getCurrent().getName()"',
// With server flag
'<%= config.bin %> <%= command.id %> --server my-sandbox.demandware.net "1+1"',
// From file
'<%= config.bin %> <%= command.id %> --file script.js',
// With site ID
'<%= config.bin %> <%= command.id %> --site RefArch "dw.system.Site.getCurrent().getName()"',
// JSON output
'<%= config.bin %> <%= command.id %> --json "dw.catalog.ProductMgr.getProduct(\'123\')"',
// Multi-statement via heredoc (shell)
`echo 'var site = dw.system.Site.getCurrent(); site.getName();' | <%= config.bin %> <%= command.id %>`,
];
static flags = {
...InstanceCommand.baseFlags,
file: Flags.string({
char: 'f',
description: 'Read expression from file',
}),
site: Flags.string({
description: 'Site ID to use for controller trigger (default: RefArch)',
default: 'RefArch',
}),
timeout: Flags.integer({
char: 't',
description: 'Timeout in seconds for waiting for breakpoint (default: 30)',
default: 30,
}),
};
async run(): Promise<EvaluateScriptResult> {
// Require both Basic auth (for SDAPI) and OAuth (for OCAPI)
this.requireWebDavCredentials();
this.requireOAuthCredentials();
const hostname = this.resolvedConfig.values.hostname!;
let codeVersion = this.resolvedConfig.values.codeVersion;
// If no code version specified, discover the active one
if (!codeVersion) {
if (!this.jsonEnabled()) {
this.log(
t('commands.script.eval.discoveringCodeVersion', 'No code version specified, discovering active version...'),
);
}
const activeVersion = await getActiveCodeVersion(this.instance);
if (!activeVersion?.id) {
this.error(
t('commands.script.eval.noActiveVersion', 'No active code version found. Specify one with --code-version.'),
);
}
codeVersion = activeVersion.id;
// Update the instance config
this.instance.config.codeVersion = codeVersion;
}
// Get expression from args, file, or stdin
const expression = await this.getExpression();
if (!expression || expression.trim() === '') {
this.error(
t(
'commands.script.eval.noExpression',
'No expression provided. Pass as argument, use --file, or pipe to stdin.',
),
);
}
if (!this.jsonEnabled()) {
this.log(
t('commands.script.eval.evaluating', 'Evaluating expression on {{hostname}} ({{codeVersion}})...', {
hostname,
codeVersion,
}),
);
}
try {
const result = await evaluateScript(this.instance, expression, {
siteId: this.flags.site,
timeout: this.flags.timeout * 1000,
});
if (result.success) {
if (!this.jsonEnabled()) {
this.log(t('commands.script.eval.result', 'Result:'));
// Output the raw result without additional formatting
process.stdout.write(result.result ?? 'undefined');
process.stdout.write('\n');
}
} else if (!this.jsonEnabled()) {
this.log(t('commands.script.eval.error', 'Error: {{error}}', {error: result.error ?? 'Unknown error'}));
}
return result;
} catch (error) {
if (error instanceof Error) {
this.error(t('commands.script.eval.failed', 'Evaluation failed: {{message}}', {message: error.message}));
}
throw error;
}
}
/**
* Gets the expression from various input sources.
*
* Priority:
* 1. --file flag (reads from file)
* 2. Positional argument (inline expression)
* 3. stdin (for heredocs/piping)
*/
private async getExpression(): Promise<string> {
// Priority 1: --file flag
if (this.flags.file) {
try {
return await fs.promises.readFile(this.flags.file, 'utf8');
} catch (error) {
this.error(
t('commands.script.eval.fileReadError', 'Failed to read file {{file}}: {{error}}', {
file: this.flags.file,
error: error instanceof Error ? error.message : String(error),
}),
);
}
}
// Priority 2: Positional argument
if (this.args.expression) {
return this.args.expression;
}
// Priority 3: stdin (check if stdin has data)
if (!process.stdin.isTTY) {
return this.readStdin();
}
return '';
}
/**
* Reads all data from stdin.
*/
private readStdin(): Promise<string> {
return new Promise((resolve, reject) => {
let data = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (chunk) => {
data += chunk;
});
process.stdin.on('end', () => {
resolve(data);
});
process.stdin.on('error', (err) => {
reject(err);
});
// Set a timeout for stdin reading
setTimeout(() => {
if (data === '') {
resolve('');
}
}, 100);
});
}
}