@adobe/aio-cli-plugin-api-mesh 2.0.0 → 2.2.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/oclif.manifest.json +1 -1
- package/package.json +4 -3
- package/src/commands/__fixtures__/env_invalid +8 -0
- package/src/commands/__fixtures__/env_valid +3 -0
- package/src/commands/__fixtures__/sample_mesh_with_placeholder +17 -0
- package/src/commands/api-mesh/__tests__/create.test.js +351 -131
- package/src/commands/api-mesh/__tests__/delete.test.js +3 -3
- package/src/commands/api-mesh/__tests__/update.test.js +128 -128
- package/src/commands/api-mesh/create.js +69 -5
- package/src/commands/api-mesh/update.js +69 -5
- package/src/helpers.js +110 -0
- package/src/meshInterpolation.js +120 -0
- package/src/utils.js +8 -2
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*clears the process.env object
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
function clearEnv() {
|
|
6
|
+
for (const key in process.env) {
|
|
7
|
+
delete process.env[key];
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
*validates the environment file content
|
|
13
|
+
* @param {envContent}
|
|
14
|
+
* @returns {object} containing the status of validation . If validation is failed then the error property including the formatting errors is returned.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
function validateEnvFileFormat(envContent) {
|
|
18
|
+
//Key should start with a underscore or an alphabet followed by underscore/alphanumeric characters
|
|
19
|
+
const envKeyRegex = /^[a-zA-Z_]+[a-zA-Z0-9_]*$/;
|
|
20
|
+
|
|
21
|
+
const envValueRegex = /^(?:"(?:\\.|[^\\"])*"|'(?:\\.|[^\\'])*'|[^'"\s])+$/;
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
The above regex matches one or more of below :
|
|
25
|
+
(?:"(?:\\.|[^\\"])*"|'(?:\\.|[^\\'])*'|[^'"\s])
|
|
26
|
+
which is
|
|
27
|
+
1. ?:"(?:\\.|[^\\"])*" : Non capturing group starts and ends with '"'
|
|
28
|
+
*/
|
|
29
|
+
const envDict = {};
|
|
30
|
+
const lines = envContent.split(/\r?\n/);
|
|
31
|
+
const errors = [];
|
|
32
|
+
|
|
33
|
+
for (let index = 0; index < lines.length; index++) {
|
|
34
|
+
const line = lines[index];
|
|
35
|
+
const trimmedLine = line.trim();
|
|
36
|
+
if (trimmedLine.startsWith('#') || trimmedLine === '') {
|
|
37
|
+
// ignore comment or empty lines
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!trimmedLine.includes('=')) {
|
|
42
|
+
errors.push(`Invalid format << ${trimmedLine} >> on line ${index + 1}`);
|
|
43
|
+
} else {
|
|
44
|
+
const [key, value] = trimmedLine.split('=', 2);
|
|
45
|
+
if (!envKeyRegex.test(key) || !envValueRegex.test(value)) {
|
|
46
|
+
// invalid format: key or value does not match regex
|
|
47
|
+
errors.push(`Invalid format for key/value << ${trimmedLine} >> on line ${index + 1}`);
|
|
48
|
+
}
|
|
49
|
+
if (key in envDict) {
|
|
50
|
+
// duplicate key found
|
|
51
|
+
errors.push(`Duplicate key << ${key} >> on line ${index + 1}`);
|
|
52
|
+
}
|
|
53
|
+
envDict[key] = value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (errors.length) {
|
|
57
|
+
return {
|
|
58
|
+
valid: false,
|
|
59
|
+
error: errors.toString(),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
valid: true,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
*loads the pupa module dynamically and then interpolates the raw data from mesh file with object data
|
|
69
|
+
* @param {data}
|
|
70
|
+
* @param {obj}
|
|
71
|
+
* @returns {object} having interpolationStatus, missingKeys and interpolatedMesh
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
async function interpolateMesh(data, obj) {
|
|
75
|
+
let missingKeys = [];
|
|
76
|
+
let interpolatedMesh;
|
|
77
|
+
let pupa;
|
|
78
|
+
try {
|
|
79
|
+
pupa = (await import('pupa')).default;
|
|
80
|
+
} catch {
|
|
81
|
+
this.error('Error while loading pupa module');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
interpolatedMesh = pupa(data, obj, {
|
|
85
|
+
ignoreMissing: true,
|
|
86
|
+
transform: ({ value, key }) => {
|
|
87
|
+
if (key.startsWith('env.')) {
|
|
88
|
+
if (value) {
|
|
89
|
+
return value;
|
|
90
|
+
} else {
|
|
91
|
+
// missing value, add to list
|
|
92
|
+
missingKeys.push(key.split('.')[1]);
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
//ignore
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
if (missingKeys.length) {
|
|
103
|
+
return {
|
|
104
|
+
interpolationStatus: 'failed',
|
|
105
|
+
missingKeys: missingKeys,
|
|
106
|
+
interpolatedMesh: '',
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
interpolationStatus: 'success',
|
|
111
|
+
missingKeys: [],
|
|
112
|
+
interpolatedMeshData: interpolatedMesh,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
module.exports = {
|
|
117
|
+
interpolateMesh,
|
|
118
|
+
clearEnv,
|
|
119
|
+
validateEnvFileFormat,
|
|
120
|
+
};
|
package/src/utils.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const { Flags } = require('@oclif/core');
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Returns the string representation of the object's path.
|
|
3
5
|
* If the path evaluates to false, the default string is returned.
|
|
@@ -33,8 +35,6 @@ function objToString(obj, path = [], defaultString = '') {
|
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
37
|
|
|
36
|
-
const { Flags } = require('@oclif/core');
|
|
37
|
-
|
|
38
38
|
const ignoreCacheFlag = Flags.boolean({
|
|
39
39
|
char: 'i',
|
|
40
40
|
description: 'Ignore cache and force manual org -> project -> workspace selection',
|
|
@@ -53,9 +53,15 @@ const jsonFlag = Flags.boolean({
|
|
|
53
53
|
default: false,
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
+
const envFileFlag = Flags.string({
|
|
57
|
+
char: 'e',
|
|
58
|
+
description: 'Path to env file',
|
|
59
|
+
});
|
|
60
|
+
|
|
56
61
|
module.exports = {
|
|
57
62
|
objToString,
|
|
58
63
|
ignoreCacheFlag,
|
|
59
64
|
autoConfirmActionFlag,
|
|
60
65
|
jsonFlag,
|
|
66
|
+
envFileFlag,
|
|
61
67
|
};
|