@roadiehq/scaffolder-backend-module-utils 1.15.0 → 1.15.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 +1 -1
- package/dist/cjs/yaml-CKNTTZhZ.cjs.js +872 -0
- package/dist/cjs/yaml-CKNTTZhZ.cjs.js.map +1 -0
- package/dist/index.cjs.js +28 -869
- package/dist/index.cjs.js.map +1 -1
- package/dist/new-backend.cjs.js +51 -0
- package/dist/new-backend.cjs.js.map +1 -0
- package/dist/new-backend.d.ts +9 -0
- package/new-backend/package.json +6 -0
- package/package.json +10 -4
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var backendCommon = require('@backstage/backend-common');
|
|
4
|
+
var pluginScaffolderBackend = require('@backstage/plugin-scaffolder-backend');
|
|
5
|
+
var errors = require('@backstage/errors');
|
|
6
|
+
var AdmZip = require('adm-zip');
|
|
7
|
+
var fs = require('fs-extra');
|
|
8
|
+
var YAML = require('yaml');
|
|
9
|
+
var pluginScaffolderNode = require('@backstage/plugin-scaffolder-node');
|
|
10
|
+
var path = require('path');
|
|
11
|
+
var lodash = require('lodash');
|
|
12
|
+
var detectIndent = require('detect-indent');
|
|
13
|
+
var jsonata = require('jsonata');
|
|
14
|
+
|
|
15
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
16
|
+
|
|
17
|
+
var AdmZip__default = /*#__PURE__*/_interopDefaultCompat(AdmZip);
|
|
18
|
+
var fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
19
|
+
var YAML__default = /*#__PURE__*/_interopDefaultCompat(YAML);
|
|
20
|
+
var detectIndent__default = /*#__PURE__*/_interopDefaultCompat(detectIndent);
|
|
21
|
+
var jsonata__default = /*#__PURE__*/_interopDefaultCompat(jsonata);
|
|
22
|
+
|
|
23
|
+
function createZipAction() {
|
|
24
|
+
return pluginScaffolderBackend.createTemplateAction({
|
|
25
|
+
id: "roadiehq:utils:zip",
|
|
26
|
+
description: "Zips the content of the path",
|
|
27
|
+
supportsDryRun: true,
|
|
28
|
+
schema: {
|
|
29
|
+
input: {
|
|
30
|
+
required: ["path"],
|
|
31
|
+
type: "object",
|
|
32
|
+
properties: {
|
|
33
|
+
path: {
|
|
34
|
+
title: "Path",
|
|
35
|
+
description: "Relative path you would like to zip",
|
|
36
|
+
type: "string"
|
|
37
|
+
},
|
|
38
|
+
outputPath: {
|
|
39
|
+
title: "Output Path",
|
|
40
|
+
description: "The name of the result of the zip command",
|
|
41
|
+
type: "string"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
output: {
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: {
|
|
48
|
+
outputPath: {
|
|
49
|
+
title: "Zip Path",
|
|
50
|
+
type: "string"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
async handler(ctx) {
|
|
56
|
+
const zip = new AdmZip__default.default();
|
|
57
|
+
const sourceFilepath = backendCommon.resolveSafeChildPath(
|
|
58
|
+
ctx.workspacePath,
|
|
59
|
+
ctx.input.path
|
|
60
|
+
);
|
|
61
|
+
const destFilepath = backendCommon.resolveSafeChildPath(
|
|
62
|
+
ctx.workspacePath,
|
|
63
|
+
ctx.input.outputPath
|
|
64
|
+
);
|
|
65
|
+
if (!fs__default.default.existsSync(sourceFilepath)) {
|
|
66
|
+
throw new errors.InputError(
|
|
67
|
+
`File ${ctx.input.path} does not exist. Can't zip it.`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (fs__default.default.lstatSync(sourceFilepath).isDirectory()) {
|
|
71
|
+
zip.addLocalFolder(sourceFilepath);
|
|
72
|
+
} else if (fs__default.default.lstatSync(sourceFilepath).isFile()) {
|
|
73
|
+
zip.addLocalFile(sourceFilepath);
|
|
74
|
+
}
|
|
75
|
+
zip.writeZip(destFilepath);
|
|
76
|
+
ctx.output("outputPath", ctx.input.outputPath);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function createWriteFileAction() {
|
|
82
|
+
return pluginScaffolderBackend.createTemplateAction({
|
|
83
|
+
id: "roadiehq:utils:fs:write",
|
|
84
|
+
description: "Creates a file with the content on the given path",
|
|
85
|
+
supportsDryRun: true,
|
|
86
|
+
schema: {
|
|
87
|
+
input: {
|
|
88
|
+
required: ["path", "content"],
|
|
89
|
+
type: "object",
|
|
90
|
+
properties: {
|
|
91
|
+
path: {
|
|
92
|
+
title: "Path",
|
|
93
|
+
description: "Relative path",
|
|
94
|
+
type: "string"
|
|
95
|
+
},
|
|
96
|
+
content: {
|
|
97
|
+
title: "Content",
|
|
98
|
+
description: "This will be the content of the file",
|
|
99
|
+
type: "string"
|
|
100
|
+
},
|
|
101
|
+
preserveFormatting: {
|
|
102
|
+
title: "Preserve Formatting",
|
|
103
|
+
description: "Specify whether to preserve formatting for JSON content",
|
|
104
|
+
type: "boolean",
|
|
105
|
+
default: false
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
output: {
|
|
110
|
+
type: "object",
|
|
111
|
+
properties: {
|
|
112
|
+
path: {
|
|
113
|
+
title: "Path",
|
|
114
|
+
type: "string"
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
async handler(ctx) {
|
|
120
|
+
const destFilepath = backendCommon.resolveSafeChildPath(
|
|
121
|
+
ctx.workspacePath,
|
|
122
|
+
ctx.input.path
|
|
123
|
+
);
|
|
124
|
+
let formattedContent = ctx.input.content;
|
|
125
|
+
if (ctx.input.preserveFormatting) {
|
|
126
|
+
try {
|
|
127
|
+
const parsedContent = JSON.parse(ctx.input.content);
|
|
128
|
+
formattedContent = JSON.stringify(parsedContent, null, 2);
|
|
129
|
+
} catch (error) {
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
fs__default.default.outputFileSync(destFilepath, formattedContent);
|
|
133
|
+
ctx.output("path", destFilepath);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function createAppendFileAction() {
|
|
139
|
+
return pluginScaffolderBackend.createTemplateAction({
|
|
140
|
+
id: "roadiehq:utils:fs:append",
|
|
141
|
+
description: "Append content to the end of the given file, it will create the file if it does not exist.",
|
|
142
|
+
schema: {
|
|
143
|
+
input: {
|
|
144
|
+
type: "object",
|
|
145
|
+
required: ["content", "path"],
|
|
146
|
+
properties: {
|
|
147
|
+
path: {
|
|
148
|
+
title: "Path",
|
|
149
|
+
description: "Path to existing file to append.",
|
|
150
|
+
type: "string"
|
|
151
|
+
},
|
|
152
|
+
content: {
|
|
153
|
+
title: "Content",
|
|
154
|
+
description: "This will be appended to the file",
|
|
155
|
+
type: "string"
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
output: {
|
|
160
|
+
type: "object",
|
|
161
|
+
properties: {
|
|
162
|
+
path: {
|
|
163
|
+
title: "Path",
|
|
164
|
+
type: "string"
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
async handler(ctx) {
|
|
170
|
+
const sourceFilepath = backendCommon.resolveSafeChildPath(
|
|
171
|
+
ctx.workspacePath,
|
|
172
|
+
ctx.input.path
|
|
173
|
+
);
|
|
174
|
+
fs__default.default.appendFileSync(sourceFilepath, ctx.input.content);
|
|
175
|
+
ctx.output("path", sourceFilepath);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const parsers = {
|
|
181
|
+
yaml: (cnt) => YAML__default.default.parse(cnt),
|
|
182
|
+
json: (cnt) => JSON.parse(cnt),
|
|
183
|
+
multiyaml: (cnt) => YAML__default.default.parseAllDocuments(cnt).map((doc) => doc.toJSON())
|
|
184
|
+
};
|
|
185
|
+
function createParseFileAction() {
|
|
186
|
+
return pluginScaffolderBackend.createTemplateAction({
|
|
187
|
+
id: "roadiehq:utils:fs:parse",
|
|
188
|
+
description: "Reads a file from the workspace and optionally parses it",
|
|
189
|
+
supportsDryRun: true,
|
|
190
|
+
schema: {
|
|
191
|
+
input: {
|
|
192
|
+
type: "object",
|
|
193
|
+
required: ["path"],
|
|
194
|
+
properties: {
|
|
195
|
+
path: {
|
|
196
|
+
title: "Path",
|
|
197
|
+
description: "Path to the file to read.",
|
|
198
|
+
type: "string"
|
|
199
|
+
},
|
|
200
|
+
parser: {
|
|
201
|
+
title: "Parse",
|
|
202
|
+
description: "Optionally parse the content to an object.",
|
|
203
|
+
type: "string",
|
|
204
|
+
enum: ["yaml", "json", "multiyaml"]
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
output: {
|
|
209
|
+
type: "object",
|
|
210
|
+
properties: {
|
|
211
|
+
content: {
|
|
212
|
+
title: "Content of the file",
|
|
213
|
+
type: ["string", "object"]
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
async handler(ctx) {
|
|
219
|
+
const sourceFilepath = backendCommon.resolveSafeChildPath(
|
|
220
|
+
ctx.workspacePath,
|
|
221
|
+
ctx.input.path
|
|
222
|
+
);
|
|
223
|
+
const parserName = ctx.input.parser;
|
|
224
|
+
let parser = (content2) => content2;
|
|
225
|
+
if (parserName) {
|
|
226
|
+
parser = parsers[parserName];
|
|
227
|
+
}
|
|
228
|
+
const content = parser(
|
|
229
|
+
fs__default.default.readFileSync(sourceFilepath).toString()
|
|
230
|
+
);
|
|
231
|
+
ctx.output("content", content);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function createReplaceInFileAction() {
|
|
237
|
+
return pluginScaffolderNode.createTemplateAction({
|
|
238
|
+
id: "roadiehq:utils:fs:replace",
|
|
239
|
+
description: "Replaces content of a file with given values.",
|
|
240
|
+
supportsDryRun: true,
|
|
241
|
+
schema: {
|
|
242
|
+
input: {
|
|
243
|
+
required: ["files"],
|
|
244
|
+
type: "object",
|
|
245
|
+
properties: {
|
|
246
|
+
files: {
|
|
247
|
+
title: "Files",
|
|
248
|
+
description: "A list of files and replacements to be done",
|
|
249
|
+
type: "array",
|
|
250
|
+
items: {
|
|
251
|
+
type: "object",
|
|
252
|
+
required: [],
|
|
253
|
+
properties: {
|
|
254
|
+
file: {
|
|
255
|
+
type: "string",
|
|
256
|
+
title: "The source location of the file to be used to run replace against"
|
|
257
|
+
},
|
|
258
|
+
find: {
|
|
259
|
+
type: "string",
|
|
260
|
+
title: "A string to be replaced"
|
|
261
|
+
},
|
|
262
|
+
replaceWith: {
|
|
263
|
+
type: "string",
|
|
264
|
+
title: "Text to be used to replace the found lines with"
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
async handler(ctx) {
|
|
273
|
+
var _a;
|
|
274
|
+
if (!Array.isArray((_a = ctx.input) == null ? void 0 : _a.files)) {
|
|
275
|
+
throw new errors.InputError("files must be an Array");
|
|
276
|
+
}
|
|
277
|
+
for (const file of ctx.input.files) {
|
|
278
|
+
if (!file.file) {
|
|
279
|
+
throw new errors.InputError("Path to file needs to be defined");
|
|
280
|
+
}
|
|
281
|
+
if (!file.find || !file.replaceWith) {
|
|
282
|
+
throw new errors.InputError(
|
|
283
|
+
"each file must have a find and replaceWith property"
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
const sourceFilepath = backendCommon.resolveSafeChildPath(
|
|
287
|
+
ctx.workspacePath,
|
|
288
|
+
file.file
|
|
289
|
+
);
|
|
290
|
+
const content = fs__default.default.readFileSync(sourceFilepath).toString();
|
|
291
|
+
const replacedContent = content.replaceAll(file.find, file.replaceWith);
|
|
292
|
+
fs__default.default.writeFileSync(sourceFilepath, replacedContent);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const yamlOptionsSchema = {
|
|
299
|
+
title: "Options",
|
|
300
|
+
description: "YAML stringify options",
|
|
301
|
+
type: "object",
|
|
302
|
+
properties: {
|
|
303
|
+
indent: {
|
|
304
|
+
description: "(default: 2) - indentation width to use (in spaces)",
|
|
305
|
+
type: "number"
|
|
306
|
+
},
|
|
307
|
+
noArrayIndent: {
|
|
308
|
+
description: "(default: false) - when true, will not add an indentation level to array elements",
|
|
309
|
+
type: "boolean"
|
|
310
|
+
},
|
|
311
|
+
skipInvalid: {
|
|
312
|
+
description: "(default: false) - do not throw on invalid types (like function in the safe schema) and skip pairs and single values with such types",
|
|
313
|
+
type: "boolean"
|
|
314
|
+
},
|
|
315
|
+
flowLevel: {
|
|
316
|
+
description: "(default: -1) - specifies level of nesting, when to switch from block to flow style for collections. -1 means block style everwhere",
|
|
317
|
+
type: "number"
|
|
318
|
+
},
|
|
319
|
+
sortKeys: {
|
|
320
|
+
description: "(default: false) - if true, sort keys when dumping YAML. If a function, use the function to sort the keys",
|
|
321
|
+
type: "boolean"
|
|
322
|
+
},
|
|
323
|
+
lineWidth: {
|
|
324
|
+
description: "(default: 80) - set max line width. Set -1 for unlimited width",
|
|
325
|
+
type: "number"
|
|
326
|
+
},
|
|
327
|
+
noRefs: {
|
|
328
|
+
description: "(default: false) - if true, don't convert duplicate objects into references",
|
|
329
|
+
type: "boolean"
|
|
330
|
+
},
|
|
331
|
+
noCompatMode: {
|
|
332
|
+
description: `(default: false) - if true don't try to be compatible with older yaml versions. Currently: don't quote "yes", "no" and so on, as required for YAML 1.1`,
|
|
333
|
+
type: "boolean"
|
|
334
|
+
},
|
|
335
|
+
condenseFlow: {
|
|
336
|
+
description: `(default: false) - if true flow sequences will be condensed, omitting the space between a, b. Eg. '[a,b]', and omitting the space between key: value and quoting the key. Eg. '{"a":b}' Can be useful when using yaml for pretty URL query params as spaces are %-encoded.`,
|
|
337
|
+
type: "boolean"
|
|
338
|
+
},
|
|
339
|
+
quotingType: {
|
|
340
|
+
description: `(' or ", default: ') - strings will be quoted using this quoting style. If you specify single quotes, double quotes will still be used for non-printable characters.`,
|
|
341
|
+
type: "string"
|
|
342
|
+
},
|
|
343
|
+
forceQuotes: {
|
|
344
|
+
description: "(default: false) - if true, all non-key strings will be quoted even if they normally don't need to.",
|
|
345
|
+
type: "boolean"
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
function mergeArrayCustomiser(objValue, srcValue) {
|
|
351
|
+
if (lodash.isArray(objValue) && !lodash.isNull(objValue)) {
|
|
352
|
+
return Array.from(new Set(objValue.concat(srcValue)));
|
|
353
|
+
}
|
|
354
|
+
return void 0;
|
|
355
|
+
}
|
|
356
|
+
function createMergeJSONAction({ actionId }) {
|
|
357
|
+
return pluginScaffolderNode.createTemplateAction({
|
|
358
|
+
id: actionId || "roadiehq:utils:json:merge",
|
|
359
|
+
description: "Merge new data into an existing JSON file.",
|
|
360
|
+
supportsDryRun: true,
|
|
361
|
+
schema: {
|
|
362
|
+
input: {
|
|
363
|
+
type: "object",
|
|
364
|
+
required: ["content", "path"],
|
|
365
|
+
properties: {
|
|
366
|
+
path: {
|
|
367
|
+
title: "Path",
|
|
368
|
+
description: "Path to existing file to append.",
|
|
369
|
+
type: "string"
|
|
370
|
+
},
|
|
371
|
+
content: {
|
|
372
|
+
description: "This will be merged into to the file. Can be either an object or a string.",
|
|
373
|
+
title: "Content",
|
|
374
|
+
type: ["string", "object"]
|
|
375
|
+
},
|
|
376
|
+
mergeArrays: {
|
|
377
|
+
type: "boolean",
|
|
378
|
+
default: false,
|
|
379
|
+
title: "Merge Arrays?",
|
|
380
|
+
description: "Where a value is an array the merge function should concatenate the provided array value with the target array"
|
|
381
|
+
},
|
|
382
|
+
matchFileIndent: {
|
|
383
|
+
type: "boolean",
|
|
384
|
+
default: false,
|
|
385
|
+
title: "Match file indent?",
|
|
386
|
+
description: "Make the output file indentation match that of the specified input file."
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
output: {
|
|
391
|
+
type: "object",
|
|
392
|
+
properties: {
|
|
393
|
+
path: {
|
|
394
|
+
title: "Path",
|
|
395
|
+
type: "string"
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
},
|
|
400
|
+
async handler(ctx) {
|
|
401
|
+
const sourceFilepath = backendCommon.resolveSafeChildPath(
|
|
402
|
+
ctx.workspacePath,
|
|
403
|
+
ctx.input.path
|
|
404
|
+
);
|
|
405
|
+
let existingContent;
|
|
406
|
+
if (fs__default.default.existsSync(sourceFilepath)) {
|
|
407
|
+
existingContent = JSON.parse(
|
|
408
|
+
fs__default.default.readFileSync(sourceFilepath).toString()
|
|
409
|
+
);
|
|
410
|
+
} else {
|
|
411
|
+
ctx.logger.info(
|
|
412
|
+
`The file ${sourceFilepath} does not exist, creating it.`
|
|
413
|
+
);
|
|
414
|
+
existingContent = {};
|
|
415
|
+
}
|
|
416
|
+
const content = typeof ctx.input.content === "string" ? JSON.parse(ctx.input.content) : ctx.input.content;
|
|
417
|
+
let fileIndent = 2;
|
|
418
|
+
if (ctx.input.matchFileIndent) {
|
|
419
|
+
fileIndent = detectIndent__default.default(
|
|
420
|
+
fs__default.default.readFileSync(sourceFilepath, "utf8")
|
|
421
|
+
).amount;
|
|
422
|
+
if (!fileIndent) {
|
|
423
|
+
fileIndent = 2;
|
|
424
|
+
ctx.logger.info(
|
|
425
|
+
`Failed to detect source file indentation, using default value of 2.`
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
fs__default.default.writeFileSync(
|
|
430
|
+
sourceFilepath,
|
|
431
|
+
JSON.stringify(
|
|
432
|
+
lodash.mergeWith(
|
|
433
|
+
existingContent,
|
|
434
|
+
content,
|
|
435
|
+
ctx.input.mergeArrays ? mergeArrayCustomiser : void 0
|
|
436
|
+
),
|
|
437
|
+
null,
|
|
438
|
+
fileIndent
|
|
439
|
+
)
|
|
440
|
+
);
|
|
441
|
+
ctx.output("path", sourceFilepath);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
function createMergeAction() {
|
|
446
|
+
return pluginScaffolderNode.createTemplateAction({
|
|
447
|
+
id: "roadiehq:utils:merge",
|
|
448
|
+
description: "Merges data into an existing structured file.",
|
|
449
|
+
supportsDryRun: true,
|
|
450
|
+
schema: {
|
|
451
|
+
input: {
|
|
452
|
+
type: "object",
|
|
453
|
+
required: ["content", "path"],
|
|
454
|
+
properties: {
|
|
455
|
+
path: {
|
|
456
|
+
title: "Path",
|
|
457
|
+
description: "Path to existing file to append.",
|
|
458
|
+
type: "string"
|
|
459
|
+
},
|
|
460
|
+
content: {
|
|
461
|
+
description: "This will be merged into to the file. Can be either an object or a string.",
|
|
462
|
+
title: "Content",
|
|
463
|
+
type: ["string", "object"]
|
|
464
|
+
},
|
|
465
|
+
mergeArrays: {
|
|
466
|
+
type: "boolean",
|
|
467
|
+
default: false,
|
|
468
|
+
title: "Merge Arrays?",
|
|
469
|
+
description: "Where a value is an array the merge function should concatenate the provided array value with the target array"
|
|
470
|
+
},
|
|
471
|
+
options: {
|
|
472
|
+
...yamlOptionsSchema,
|
|
473
|
+
description: `${yamlOptionsSchema.description} (for YAML output only)`
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
output: {
|
|
478
|
+
type: "object",
|
|
479
|
+
properties: {
|
|
480
|
+
path: {
|
|
481
|
+
title: "Path",
|
|
482
|
+
type: "string"
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
async handler(ctx) {
|
|
488
|
+
const sourceFilepath = backendCommon.resolveSafeChildPath(
|
|
489
|
+
ctx.workspacePath,
|
|
490
|
+
ctx.input.path
|
|
491
|
+
);
|
|
492
|
+
if (!fs__default.default.existsSync(sourceFilepath)) {
|
|
493
|
+
ctx.logger.error(`The file ${sourceFilepath} does not exist.`);
|
|
494
|
+
throw new Error(`The file ${sourceFilepath} does not exist.`);
|
|
495
|
+
}
|
|
496
|
+
const originalContent = fs__default.default.readFileSync(sourceFilepath).toString();
|
|
497
|
+
let mergedContent;
|
|
498
|
+
switch (path.extname(sourceFilepath)) {
|
|
499
|
+
case ".json": {
|
|
500
|
+
const newContent = typeof ctx.input.content === "string" ? JSON.parse(ctx.input.content) : ctx.input.content;
|
|
501
|
+
mergedContent = JSON.stringify(
|
|
502
|
+
lodash.mergeWith(
|
|
503
|
+
YAML__default.default.parse(originalContent),
|
|
504
|
+
newContent,
|
|
505
|
+
ctx.input.mergeArrays ? mergeArrayCustomiser : void 0
|
|
506
|
+
),
|
|
507
|
+
null,
|
|
508
|
+
2
|
|
509
|
+
);
|
|
510
|
+
break;
|
|
511
|
+
}
|
|
512
|
+
case ".yml":
|
|
513
|
+
case ".yaml": {
|
|
514
|
+
const newContent = typeof ctx.input.content === "string" ? YAML__default.default.parse(ctx.input.content) : ctx.input.content;
|
|
515
|
+
mergedContent = YAML__default.default.stringify(
|
|
516
|
+
lodash.mergeWith(
|
|
517
|
+
YAML__default.default.parse(originalContent),
|
|
518
|
+
newContent,
|
|
519
|
+
ctx.input.mergeArrays ? mergeArrayCustomiser : void 0
|
|
520
|
+
),
|
|
521
|
+
ctx.input.options
|
|
522
|
+
);
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (!mergedContent) {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
fs__default.default.writeFileSync(sourceFilepath, mergedContent);
|
|
530
|
+
ctx.output("path", sourceFilepath);
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function createSleepAction(options) {
|
|
536
|
+
return pluginScaffolderBackend.createTemplateAction({
|
|
537
|
+
id: "roadiehq:utils:sleep",
|
|
538
|
+
description: "Halts the scaffolding for the given amount of seconds",
|
|
539
|
+
supportsDryRun: true,
|
|
540
|
+
schema: {
|
|
541
|
+
input: {
|
|
542
|
+
type: "object",
|
|
543
|
+
required: ["amount"],
|
|
544
|
+
properties: {
|
|
545
|
+
amount: {
|
|
546
|
+
title: "Sleep Amount",
|
|
547
|
+
description: "How much seconds should this step take.",
|
|
548
|
+
type: "number"
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
},
|
|
553
|
+
async handler(ctx) {
|
|
554
|
+
var _a;
|
|
555
|
+
if (isNaN((_a = ctx.input) == null ? void 0 : _a.amount)) {
|
|
556
|
+
throw new errors.InputError("amount must be a number");
|
|
557
|
+
} else if ((options == null ? void 0 : options.maxSleep) && ctx.input.amount > options.maxSleep) {
|
|
558
|
+
throw new errors.InputError(
|
|
559
|
+
`sleep amount can not be greater than maxSleep. amount: ${ctx.input.amount}, maxSleep: ${options.maxSleep}`
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
ctx.logger.info(`Waiting ${ctx.input.amount} seconds`);
|
|
563
|
+
await new Promise((resolve) => {
|
|
564
|
+
setTimeout(resolve, ctx.input.amount * 1e3);
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function createJSONataAction() {
|
|
571
|
+
return pluginScaffolderBackend.createTemplateAction({
|
|
572
|
+
id: "roadiehq:utils:jsonata",
|
|
573
|
+
description: "Allows performing JSONata operations and transformations on input objects and produces the output result as a step output.",
|
|
574
|
+
supportsDryRun: true,
|
|
575
|
+
schema: {
|
|
576
|
+
input: {
|
|
577
|
+
type: "object",
|
|
578
|
+
required: ["data", "expression"],
|
|
579
|
+
properties: {
|
|
580
|
+
data: {
|
|
581
|
+
title: "Data",
|
|
582
|
+
description: "Input data to be transformed",
|
|
583
|
+
type: [
|
|
584
|
+
"object",
|
|
585
|
+
"array",
|
|
586
|
+
"string",
|
|
587
|
+
"number",
|
|
588
|
+
"integer",
|
|
589
|
+
"boolean",
|
|
590
|
+
"null"
|
|
591
|
+
]
|
|
592
|
+
},
|
|
593
|
+
expression: {
|
|
594
|
+
title: "Expression",
|
|
595
|
+
description: "JSONata expression to perform on the input",
|
|
596
|
+
type: "string"
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
},
|
|
600
|
+
output: {
|
|
601
|
+
type: "object",
|
|
602
|
+
properties: {
|
|
603
|
+
result: {
|
|
604
|
+
title: "Output result from JSONata",
|
|
605
|
+
type: "object"
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
async handler(ctx) {
|
|
611
|
+
try {
|
|
612
|
+
const expression = jsonata__default.default(ctx.input.expression);
|
|
613
|
+
const result = expression.evaluate(ctx.input.data);
|
|
614
|
+
ctx.output("result", result);
|
|
615
|
+
} catch (e) {
|
|
616
|
+
const message = e.hasOwnProperty("message") ? e.message : "unknown JSONata evaluation error";
|
|
617
|
+
throw new Error(
|
|
618
|
+
`JSONata failed to evaluate the expression: ${message}`
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
function createYamlJSONataTransformAction() {
|
|
626
|
+
return pluginScaffolderBackend.createTemplateAction({
|
|
627
|
+
id: "roadiehq:utils:jsonata:yaml:transform",
|
|
628
|
+
description: "Allows performing JSONata operations and transformations on a YAML file in the workspace. The result can be read from the `result` step output.",
|
|
629
|
+
supportsDryRun: true,
|
|
630
|
+
schema: {
|
|
631
|
+
input: {
|
|
632
|
+
type: "object",
|
|
633
|
+
required: ["path", "expression"],
|
|
634
|
+
properties: {
|
|
635
|
+
path: {
|
|
636
|
+
title: "Path",
|
|
637
|
+
description: "Input path to read yaml file",
|
|
638
|
+
type: "string"
|
|
639
|
+
},
|
|
640
|
+
expression: {
|
|
641
|
+
title: "Expression",
|
|
642
|
+
description: "JSONata expression to perform on the input",
|
|
643
|
+
type: "string"
|
|
644
|
+
},
|
|
645
|
+
loadAll: {
|
|
646
|
+
title: "Load All",
|
|
647
|
+
description: "Use this if the yaml source file contains multiple yaml objects",
|
|
648
|
+
type: "boolean"
|
|
649
|
+
},
|
|
650
|
+
as: {
|
|
651
|
+
title: "Desired Result Type",
|
|
652
|
+
description: 'Permitted values are: "string" (default) and "object"',
|
|
653
|
+
type: "string",
|
|
654
|
+
enum: ["string", "object"]
|
|
655
|
+
},
|
|
656
|
+
options: yamlOptionsSchema
|
|
657
|
+
}
|
|
658
|
+
},
|
|
659
|
+
output: {
|
|
660
|
+
type: "object",
|
|
661
|
+
properties: {
|
|
662
|
+
result: {
|
|
663
|
+
title: "Output result from JSONata",
|
|
664
|
+
type: "object | string"
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
},
|
|
669
|
+
async handler(ctx) {
|
|
670
|
+
let resultHandler;
|
|
671
|
+
if (ctx.input.as === "object") {
|
|
672
|
+
resultHandler = (rz) => rz;
|
|
673
|
+
} else {
|
|
674
|
+
resultHandler = (rz) => YAML__default.default.stringify(rz, ctx.input.options);
|
|
675
|
+
}
|
|
676
|
+
const sourceFilepath = backendCommon.resolveSafeChildPath(
|
|
677
|
+
ctx.workspacePath,
|
|
678
|
+
ctx.input.path
|
|
679
|
+
);
|
|
680
|
+
let data;
|
|
681
|
+
if (ctx.input.loadAll) {
|
|
682
|
+
data = YAML__default.default.parseAllDocuments(
|
|
683
|
+
fs__default.default.readFileSync(sourceFilepath).toString()
|
|
684
|
+
).map((doc) => doc.toJSON());
|
|
685
|
+
} else {
|
|
686
|
+
data = YAML__default.default.parse(fs__default.default.readFileSync(sourceFilepath).toString());
|
|
687
|
+
}
|
|
688
|
+
const expression = jsonata__default.default(ctx.input.expression);
|
|
689
|
+
const result = expression.evaluate(data);
|
|
690
|
+
ctx.output("result", resultHandler(result));
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
function createJsonJSONataTransformAction() {
|
|
696
|
+
return pluginScaffolderBackend.createTemplateAction({
|
|
697
|
+
id: "roadiehq:utils:jsonata:json:transform",
|
|
698
|
+
description: "Allows performing JSONata operations and transformations on a JSON file in the workspace. The result can be read from the `result` step output.",
|
|
699
|
+
supportsDryRun: true,
|
|
700
|
+
schema: {
|
|
701
|
+
input: {
|
|
702
|
+
type: "object",
|
|
703
|
+
required: ["path", "expression"],
|
|
704
|
+
properties: {
|
|
705
|
+
path: {
|
|
706
|
+
title: "Path",
|
|
707
|
+
description: "Input path to read json file",
|
|
708
|
+
type: "string"
|
|
709
|
+
},
|
|
710
|
+
expression: {
|
|
711
|
+
title: "Expression",
|
|
712
|
+
description: "JSONata expression to perform on the input",
|
|
713
|
+
type: "string"
|
|
714
|
+
},
|
|
715
|
+
as: {
|
|
716
|
+
title: "Desired Result Type",
|
|
717
|
+
description: 'Permitted values are: "string" (default) and "object"',
|
|
718
|
+
type: "string",
|
|
719
|
+
enum: ["string", "object"]
|
|
720
|
+
},
|
|
721
|
+
replacer: {
|
|
722
|
+
title: "Replacer",
|
|
723
|
+
description: "Replacer array",
|
|
724
|
+
type: "array",
|
|
725
|
+
items: {
|
|
726
|
+
type: "string"
|
|
727
|
+
}
|
|
728
|
+
},
|
|
729
|
+
space: {
|
|
730
|
+
title: "Space",
|
|
731
|
+
description: "Space character",
|
|
732
|
+
type: "string"
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
},
|
|
736
|
+
output: {
|
|
737
|
+
type: "object",
|
|
738
|
+
properties: {
|
|
739
|
+
result: {
|
|
740
|
+
title: "Output result from JSONata",
|
|
741
|
+
type: "object | string"
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
},
|
|
746
|
+
async handler(ctx) {
|
|
747
|
+
let resultHandler;
|
|
748
|
+
if (ctx.input.as === "object") {
|
|
749
|
+
resultHandler = (rz) => rz;
|
|
750
|
+
} else {
|
|
751
|
+
resultHandler = (rz) => JSON.stringify(rz, ctx.input.replacer, ctx.input.space);
|
|
752
|
+
}
|
|
753
|
+
const sourceFilepath = backendCommon.resolveSafeChildPath(
|
|
754
|
+
ctx.workspacePath,
|
|
755
|
+
ctx.input.path
|
|
756
|
+
);
|
|
757
|
+
const data = JSON.parse(fs__default.default.readFileSync(sourceFilepath).toString());
|
|
758
|
+
const expression = jsonata__default.default(ctx.input.expression);
|
|
759
|
+
const result = expression.evaluate(data);
|
|
760
|
+
ctx.output("result", resultHandler(result));
|
|
761
|
+
}
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
function createSerializeJsonAction() {
|
|
766
|
+
return pluginScaffolderBackend.createTemplateAction({
|
|
767
|
+
id: "roadiehq:utils:serialize:json",
|
|
768
|
+
description: "Allows performing serialization on an object",
|
|
769
|
+
supportsDryRun: true,
|
|
770
|
+
schema: {
|
|
771
|
+
input: {
|
|
772
|
+
type: "object",
|
|
773
|
+
required: ["data"],
|
|
774
|
+
properties: {
|
|
775
|
+
data: {
|
|
776
|
+
title: "Data",
|
|
777
|
+
description: "Input data to perform seriazation on.",
|
|
778
|
+
type: "object"
|
|
779
|
+
},
|
|
780
|
+
replacer: {
|
|
781
|
+
title: "Replacer",
|
|
782
|
+
description: "Replacer array",
|
|
783
|
+
type: "array",
|
|
784
|
+
items: {
|
|
785
|
+
type: "string"
|
|
786
|
+
}
|
|
787
|
+
},
|
|
788
|
+
space: {
|
|
789
|
+
title: "Space",
|
|
790
|
+
description: "Space character",
|
|
791
|
+
type: "string"
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
},
|
|
795
|
+
output: {
|
|
796
|
+
type: "string",
|
|
797
|
+
properties: {
|
|
798
|
+
serialized: {
|
|
799
|
+
title: "Output result from serialization",
|
|
800
|
+
type: "string"
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
},
|
|
805
|
+
async handler(ctx) {
|
|
806
|
+
ctx.output(
|
|
807
|
+
"serialized",
|
|
808
|
+
JSON.stringify(ctx.input.data, ctx.input.replacer, ctx.input.space)
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
function createSerializeYamlAction() {
|
|
815
|
+
return pluginScaffolderNode.createTemplateAction({
|
|
816
|
+
id: "roadiehq:utils:serialize:yaml",
|
|
817
|
+
description: "Allows performing serialization on an object",
|
|
818
|
+
supportsDryRun: true,
|
|
819
|
+
schema: {
|
|
820
|
+
input: {
|
|
821
|
+
type: "object",
|
|
822
|
+
required: ["data"],
|
|
823
|
+
properties: {
|
|
824
|
+
data: {
|
|
825
|
+
title: "Data",
|
|
826
|
+
description: "Input data to perform seriazation on.",
|
|
827
|
+
type: "object"
|
|
828
|
+
},
|
|
829
|
+
replacer: {
|
|
830
|
+
title: "Replacer",
|
|
831
|
+
description: "Replacer array",
|
|
832
|
+
type: "array",
|
|
833
|
+
items: {
|
|
834
|
+
type: "string"
|
|
835
|
+
}
|
|
836
|
+
},
|
|
837
|
+
options: yamlOptionsSchema
|
|
838
|
+
}
|
|
839
|
+
},
|
|
840
|
+
output: {
|
|
841
|
+
type: "string",
|
|
842
|
+
properties: {
|
|
843
|
+
serialized: {
|
|
844
|
+
title: "Output result from serialization",
|
|
845
|
+
type: "string"
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
},
|
|
850
|
+
async handler(ctx) {
|
|
851
|
+
ctx.output(
|
|
852
|
+
"serialized",
|
|
853
|
+
YAML__default.default.stringify(ctx.input.data, ctx.input.options)
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
});
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
exports.createAppendFileAction = createAppendFileAction;
|
|
860
|
+
exports.createJSONataAction = createJSONataAction;
|
|
861
|
+
exports.createJsonJSONataTransformAction = createJsonJSONataTransformAction;
|
|
862
|
+
exports.createMergeAction = createMergeAction;
|
|
863
|
+
exports.createMergeJSONAction = createMergeJSONAction;
|
|
864
|
+
exports.createParseFileAction = createParseFileAction;
|
|
865
|
+
exports.createReplaceInFileAction = createReplaceInFileAction;
|
|
866
|
+
exports.createSerializeJsonAction = createSerializeJsonAction;
|
|
867
|
+
exports.createSerializeYamlAction = createSerializeYamlAction;
|
|
868
|
+
exports.createSleepAction = createSleepAction;
|
|
869
|
+
exports.createWriteFileAction = createWriteFileAction;
|
|
870
|
+
exports.createYamlJSONataTransformAction = createYamlJSONataTransformAction;
|
|
871
|
+
exports.createZipAction = createZipAction;
|
|
872
|
+
//# sourceMappingURL=yaml-CKNTTZhZ.cjs.js.map
|