jsonata-w 1.0.1 → 1.0.3
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/dist/cli.js +18 -17
- package/dist/core/SourceLoader.js +7 -4
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -104,7 +104,8 @@ function pickSubset(data, template) {
|
|
|
104
104
|
})
|
|
105
105
|
.command('transform <file>', 'Transform JSON using a single JSONata file with embedded @config', (yargs) => {
|
|
106
106
|
return yargs
|
|
107
|
-
.positional('file', { describe: 'JSONata file with embedded @config', type: 'string', demandOption: true })
|
|
107
|
+
.positional('file', { describe: 'JSONata file with embedded @config', type: 'string', demandOption: true })
|
|
108
|
+
.option('dry-run', { type: 'boolean', default: false, describe: 'Execute and validate without writing output to disk' });
|
|
108
109
|
}, async (argv) => {
|
|
109
110
|
try {
|
|
110
111
|
const filePath = path_1.default.resolve(argv.file);
|
|
@@ -117,12 +118,14 @@ function pickSubset(data, template) {
|
|
|
117
118
|
const resolvePath = (p) => path_1.default.resolve(fileDir, p);
|
|
118
119
|
const inputPath = resolvePath(config.input);
|
|
119
120
|
const outputPath = resolvePath(config.output);
|
|
120
|
-
|
|
121
|
+
if (!argv['dry-run'])
|
|
122
|
+
console.log(`Loading input from ${inputPath}...`);
|
|
121
123
|
const json = loader.load(inputPath);
|
|
122
|
-
|
|
123
|
-
|
|
124
|
+
if (!argv['dry-run'])
|
|
125
|
+
console.log(`Executing transformation...`);
|
|
124
126
|
const result = await transformer.evaluate(json, fileContent);
|
|
125
|
-
|
|
127
|
+
if (!argv['dry-run'])
|
|
128
|
+
console.log(`Unflattening result...`);
|
|
126
129
|
const finalResult = unflatten(result);
|
|
127
130
|
if (config.schema) {
|
|
128
131
|
const schemaPath = resolvePath(config.schema);
|
|
@@ -179,25 +182,17 @@ function pickSubset(data, template) {
|
|
|
179
182
|
console.log('✅ Example validation passed.');
|
|
180
183
|
}
|
|
181
184
|
}
|
|
182
|
-
const dir = path_1.default.dirname(outputPath);
|
|
183
|
-
if (!fs_1.default.existsSync(dir))
|
|
184
|
-
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
185
185
|
// Determine output format based on file extension
|
|
186
186
|
const ext = path_1.default.extname(outputPath).toLowerCase();
|
|
187
187
|
let outputContent;
|
|
188
188
|
if (ext === '.yaml' || ext === '.yml') {
|
|
189
189
|
outputContent = js_yaml_1.default.dump(finalResult, { indent: 2, lineWidth: -1 });
|
|
190
|
-
console.log(`Writing YAML output to ${outputPath}...`);
|
|
191
190
|
}
|
|
192
191
|
else if (ext === '.json') {
|
|
193
192
|
outputContent = JSON.stringify(finalResult, null, 2);
|
|
194
|
-
console.log(`Writing JSON output to ${outputPath}...`);
|
|
195
193
|
}
|
|
196
194
|
else {
|
|
197
195
|
// For other extensions (e.g., .css, .txt, .js), output as string
|
|
198
|
-
// If the result is already a string, use it directly
|
|
199
|
-
// If it's an array of strings, join them
|
|
200
|
-
// Otherwise, convert to string representation
|
|
201
196
|
if (typeof finalResult === 'string') {
|
|
202
197
|
outputContent = finalResult;
|
|
203
198
|
}
|
|
@@ -208,13 +203,19 @@ function pickSubset(data, template) {
|
|
|
208
203
|
outputContent = String(finalResult);
|
|
209
204
|
}
|
|
210
205
|
else {
|
|
211
|
-
// For complex objects, still use JSON
|
|
212
206
|
outputContent = JSON.stringify(finalResult, null, 2);
|
|
213
207
|
}
|
|
214
|
-
console.log(`Writing string output to ${outputPath}...`);
|
|
215
208
|
}
|
|
216
|
-
|
|
217
|
-
|
|
209
|
+
if (argv['dry-run']) {
|
|
210
|
+
process.stdout.write(outputContent);
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
const dir = path_1.default.dirname(outputPath);
|
|
214
|
+
if (!fs_1.default.existsSync(dir))
|
|
215
|
+
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
216
|
+
fs_1.default.writeFileSync(outputPath, outputContent);
|
|
217
|
+
console.log(`Transformed ${config.input} -> ${config.output}`);
|
|
218
|
+
}
|
|
218
219
|
}
|
|
219
220
|
catch (e) {
|
|
220
221
|
console.error(e.message);
|
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.SourceLoader = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
8
9
|
class SourceLoader {
|
|
9
10
|
constructor() {
|
|
10
11
|
this.cache = new Map();
|
|
@@ -17,13 +18,15 @@ class SourceLoader {
|
|
|
17
18
|
throw new Error(`File not found: ${path}`);
|
|
18
19
|
}
|
|
19
20
|
const content = fs_1.default.readFileSync(path, 'utf-8');
|
|
21
|
+
const isYaml = path.endsWith('.yml') || path.endsWith('.yaml');
|
|
20
22
|
try {
|
|
21
|
-
const
|
|
22
|
-
this.cache.set(path,
|
|
23
|
-
return
|
|
23
|
+
const data = isYaml ? js_yaml_1.default.load(content) : JSON.parse(content);
|
|
24
|
+
this.cache.set(path, data);
|
|
25
|
+
return data;
|
|
24
26
|
}
|
|
25
27
|
catch (_e) {
|
|
26
|
-
|
|
28
|
+
const format = isYaml ? 'YAML' : 'JSON';
|
|
29
|
+
throw new Error(`Invalid ${format} in file: ${path}`);
|
|
27
30
|
}
|
|
28
31
|
}
|
|
29
32
|
clearCache() {
|