@benup/bensdk 1.7.0 → 1.8.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.
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
+
import { promises as fsPromises } from 'fs';
|
|
2
3
|
import { initialStates } from './lib/state-machine';
|
|
3
4
|
import { validateBenefitDefinition } from './validate';
|
|
5
|
+
import inquirer from 'inquirer';
|
|
4
6
|
|
|
5
7
|
async function main() {
|
|
6
8
|
const benefitDefinition = (await import(`${process.cwd()}/src/benefit-definition.ts`)).default;
|
|
@@ -24,12 +26,12 @@ async function main() {
|
|
|
24
26
|
{} as Record<string, string[]>
|
|
25
27
|
);
|
|
26
28
|
|
|
27
|
-
Object.keys(stateMachines)
|
|
29
|
+
for (const action of Object.keys(stateMachines)) {
|
|
28
30
|
const states = stateMachines[action];
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
for (const state of states) {
|
|
31
33
|
if (initialStates.includes(state)) {
|
|
32
|
-
|
|
34
|
+
continue;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
const fileName = `${state.toLocaleLowerCase()}.handler.ts`;
|
|
@@ -85,8 +87,22 @@ async function main() {
|
|
|
85
87
|
if (!fs.existsSync(handlersDir)) {
|
|
86
88
|
fs.mkdirSync(handlersDir, { recursive: true });
|
|
87
89
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
+
|
|
91
|
+
if (!fs.existsSync(`${handlersDir}/${fileName}`)) {
|
|
92
|
+
await fsPromises.writeFile(`${handlersDir}/${fileName}`, stateHandler, 'utf-8');
|
|
93
|
+
} else {
|
|
94
|
+
const answer = await inquirer.prompt({
|
|
95
|
+
type: 'list',
|
|
96
|
+
name: 'overwrite',
|
|
97
|
+
message: `The file ${fileName} already exists. Do you want to overwrite it?`,
|
|
98
|
+
choices: ['overwrite', 'skip']
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (answer.overwrite === 'overwrite') {
|
|
102
|
+
await fsPromises.writeFile(`${handlersDir}/${fileName}`, stateHandler, 'utf-8');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
90
106
|
|
|
91
107
|
const testsDir = `${process.cwd()}/__tests__`;
|
|
92
108
|
|
|
@@ -95,7 +111,7 @@ async function main() {
|
|
|
95
111
|
}
|
|
96
112
|
|
|
97
113
|
// Read the action test template
|
|
98
|
-
let actionTestTemplate =
|
|
114
|
+
let actionTestTemplate = await fsPromises.readFile(
|
|
99
115
|
process.cwd() + '/bin/cli/templates/action.test.template.ts',
|
|
100
116
|
'utf-8'
|
|
101
117
|
);
|
|
@@ -103,20 +119,49 @@ async function main() {
|
|
|
103
119
|
// Replaces the action placeholder with the current action name
|
|
104
120
|
actionTestTemplate = actionTestTemplate.replaceAll('CHANGE_ACTION_NAME_PLACEHOLDER', action);
|
|
105
121
|
|
|
106
|
-
|
|
122
|
+
const testFileName = `${action.toLowerCase()}.test.ts`;
|
|
123
|
+
if (!fs.existsSync(`${testsDir}/${testFileName}`)) {
|
|
124
|
+
await fsPromises.writeFile(`${testsDir}/${testFileName}`, actionTestTemplate, 'utf-8');
|
|
125
|
+
} else {
|
|
126
|
+
const answer = await inquirer.prompt({
|
|
127
|
+
type: 'list',
|
|
128
|
+
name: 'overwrite',
|
|
129
|
+
message: `The test file ${testFileName} already exists. Do you want to overwrite it?`,
|
|
130
|
+
choices: ['overwrite', 'skip']
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
if (answer.overwrite === 'overwrite') {
|
|
134
|
+
await fsPromises.writeFile(`${testsDir}/${testFileName}`, actionTestTemplate, 'utf-8');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
107
137
|
|
|
108
138
|
/**
|
|
109
139
|
* Creates the utils.ts for the tests if it doesn't exists
|
|
110
140
|
*/
|
|
111
141
|
if (!fs.existsSync(`${testsDir}/utils.ts`)) {
|
|
112
|
-
const utilsTestTemplate =
|
|
142
|
+
const utilsTestTemplate = await fsPromises.readFile(
|
|
113
143
|
process.cwd() + '/bin/cli/templates/utils.template.ts',
|
|
114
144
|
'utf-8'
|
|
115
145
|
);
|
|
116
146
|
|
|
117
|
-
|
|
147
|
+
await fsPromises.writeFile(`${testsDir}/utils.ts`, utilsTestTemplate, 'utf-8');
|
|
148
|
+
} else {
|
|
149
|
+
const answer = await inquirer.prompt({
|
|
150
|
+
type: 'list',
|
|
151
|
+
name: 'overwrite',
|
|
152
|
+
message: `The utils.ts file already exists. Do you want to overwrite it?`,
|
|
153
|
+
choices: ['overwrite', 'skip']
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
if (answer.overwrite === 'overwrite') {
|
|
157
|
+
const utilsTestTemplate = await fsPromises.readFile(
|
|
158
|
+
process.cwd() + '/bin/cli/templates/utils.template.ts',
|
|
159
|
+
'utf-8'
|
|
160
|
+
);
|
|
161
|
+
await fsPromises.writeFile(`${testsDir}/utils.ts`, utilsTestTemplate, 'utf-8');
|
|
162
|
+
}
|
|
118
163
|
}
|
|
119
|
-
}
|
|
164
|
+
}
|
|
120
165
|
}
|
|
121
166
|
|
|
122
167
|
main();
|
|
@@ -4,7 +4,9 @@ export const BenefitDefinitionSchema = z
|
|
|
4
4
|
.object({
|
|
5
5
|
benefitID: z.string().min(3),
|
|
6
6
|
availableActions: z.array(
|
|
7
|
-
z.enum(
|
|
7
|
+
z.enum(
|
|
8
|
+
['GRANT', 'REVOKE', 'BEFORE_ALL_RECHARGE', 'RECHARGE', 'AFTER_ALL_RECHARGE','DEDUCTION', 'GRANT_DEPENDENT', 'REVOKE_DEPENDENT']
|
|
9
|
+
)
|
|
8
10
|
),
|
|
9
11
|
|
|
10
12
|
stateMachine: z.object({
|
|
@@ -18,12 +20,24 @@ export const BenefitDefinitionSchema = z
|
|
|
18
20
|
REQUESTED_REVOKE: z.object({ next: z.string() })
|
|
19
21
|
})
|
|
20
22
|
.passthrough(),
|
|
23
|
+
BEFORE_ALL_RECHARGE: z
|
|
24
|
+
.object({
|
|
25
|
+
REQUESTED_BEFORE_ALL_RECHARGE: z.object({ next: z.string() })
|
|
26
|
+
})
|
|
27
|
+
.passthrough()
|
|
28
|
+
.optional(),
|
|
21
29
|
RECHARGE: z
|
|
22
30
|
.object({
|
|
23
31
|
REQUESTED_RECHARGE: z.object({ next: z.string() })
|
|
24
32
|
})
|
|
25
33
|
.passthrough()
|
|
26
34
|
.optional(),
|
|
35
|
+
AFTER_ALL_RECHARGE: z
|
|
36
|
+
.object({
|
|
37
|
+
REQUESTED_AFTER_ALL_RECHARGE: z.object({ next: z.string() })
|
|
38
|
+
})
|
|
39
|
+
.passthrough()
|
|
40
|
+
.optional(),
|
|
27
41
|
GRANT_DEPENDENT: z
|
|
28
42
|
.object({
|
|
29
43
|
REQUESTED_GRANT_DEPENDENT: z.object({ next: z.string() })
|
|
@@ -56,7 +70,9 @@ export const BenefitDefinitionSchema = z
|
|
|
56
70
|
log: z.object({
|
|
57
71
|
GRANT: z.object({}).passthrough(),
|
|
58
72
|
REVOKE: z.object({}).passthrough(),
|
|
73
|
+
BEFORE_ALL_RECHARGE: z.object({}).passthrough().optional(),
|
|
59
74
|
RECHARGE: z.object({}).passthrough().optional(),
|
|
75
|
+
AFTER_ALL_RECHARGE: z.object({}).passthrough().optional(),
|
|
60
76
|
DEDUCTION: z.object({}).passthrough().optional(),
|
|
61
77
|
GRANT_DEPENDENT: z.object({}).passthrough().optional(),
|
|
62
78
|
REVOKE_DEPENDENT: z.object({}).passthrough().optional()
|