-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateMustache.ts
110 lines (101 loc) · 3.84 KB
/
generateMustache.ts
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
import yaml from "yaml";
import mustache from "mustache";
import Ajv from "ajv";
type Variable = {
[key: string]: boolean | string;
};
const template = ` # Creates a new Microsoft Entra app to authenticate users if
# the environment variable that stores clientId is empty
- uses: aadApp/create
with:
# Note: when you run aadApp/update, the Microsoft Entra app name will be updated
# based on the definition in manifest. If you don't want to change the
# name, make sure the name in Microsoft Entra manifest is the same with the name
# defined here.
name: {{appName}}
# If the value is false, the action will not generate client secret for you
{{#skipClientSecret}}
generateClientSecret: false
{{/skipClientSecret}}
{{^skipClientSecret}}
generateClientSecret: true
{{/skipClientSecret}}
# Authenticate users with a Microsoft work or school account in your
# organization's Microsoft Entra tenant (for example, single tenant).
signInAudience: AzureADMyOrg
# Write the information of created resources into environment file for the
# specified environment variable(s).
writeToEnvironmentFile:
clientId: AAD_APP_CLIENT_ID
{{^skipClientSecret}}
# Environment variable that starts with \`SECRET_\` will be stored to the
# .env.{envName}.user environment file
clientSecret: SECRET_AAD_APP_CLIENT_SECRET
{{/skipClientSecret}}
objectId: AAD_APP_OBJECT_ID
tenantId: AAD_APP_TENANT_ID
authority: AAD_APP_OAUTH_AUTHORITY
authorityHost: AAD_APP_OAUTH_AUTHORITY_HOST`; // Your template string here
const result = ` # Creates a new Microsoft Entra app to authenticate users if
# the environment variable that stores clientId is empty
- uses: aadApp/create
with:
# Note: when you run aadApp/update, the Microsoft Entra app name will be updated
# based on the definition in manifest. If you don't want to change the
# name, make sure the name in Microsoft Entra manifest is the same with the name
# defined here.
name: {{appName}}\${{APP_SUFFIX}}
# If the value is false, the action will not generate client secret for you
generateClientSecret: true
# Authenticate users with a Microsoft work or school account in your
# organization's Microsoft Entra tenant (for example, single tenant).
signInAudience: AzureADMyOrg
# Write the information of created resources into environment file for the
# specified environment variable(s).
writeToEnvironmentFile:
clientId: AAD_APP_CLIENT_ID
objectId: AAD_APP_OBJECT_ID
tenantId: AAD_APP_TENANT_ID
authority: AAD_APP_OAUTH_AUTHORITY
authorityHost: AAD_APP_OAUTH_AUTHORITY_HOST`; // Your result string here
function extractRenderVariables(template: string) {
let variables = [{}];
const regex = /\{\{([^}]+)\}\}/g;
let match;
while ((match = regex.exec(template)) !== null) {
const variable = match[1].replace(/[#^\/]/g, "");
if (variable !== match[1]) {
if (!variables.some((v: Variable) => v[variable])) {
variables = [
...variables.map((v) => {
return { ...v, [variable]: true };
}),
...variables.map((v) => {
return { ...v, [variable]: false };
}),
];
}
} else {
variables = variables.map((v) => {
return { ...v, [variable]: variable };
});
}
}
return variables;
}
function getRenderValue(
variables: Variable[],
template: string,
result: string
) {
yaml.parse(result);
}
const variables = extractRenderVariables(template);
variables.forEach((v) => {
mustache.render(template, v);
});
// const variables2 = extractRenderVariables(result);
// console.log(variables2);
// const ymlDoc = yaml.parse(mustache.render(result, variables2));
// console.log(JSON.stringify(ymlDoc));
export {};