/
prompt-generator.js
121 lines (102 loc) · 4.23 KB
/
prompt-generator.js
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
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const DATA_DIR = path.join(__dirname, '../data');
const PROMPT_TEMPLATES_DIR = path.join(__dirname, '../prompt-templates');
const PROMPT_TEMPLATES_BUNDLED_FILE_NAME = 'bundled-prompt-templates';
const PROMPT_TEMPLATES_INDEX_FILE_NAME = 'prompt-index.json';
const PROMPT_TEMPLATES_INDEX_FILE = path.join(PROMPT_TEMPLATES_DIR, PROMPT_TEMPLATES_INDEX_FILE_NAME);
const PROMPT_TEMPLATES_JSON_FILE = path.join(DATA_DIR, `${PROMPT_TEMPLATES_BUNDLED_FILE_NAME}.json`);
const readPromptIndex = () => {
console.log(`\t* Reading Prompt Index File @ ${PROMPT_TEMPLATES_INDEX_FILE}`);
const promptIndex = fs.readFileSync(PROMPT_TEMPLATES_INDEX_FILE, 'utf8');
return JSON.parse(promptIndex);
};
const writePromptIndex = (promptIndex, filePath) => {
fs.writeFileSync(PROMPT_TEMPLATES_INDEX_FILE, JSON.stringify(promptIndex, null, 4));
};
const sortPromptIndex = (promptIndex) => {
return promptIndex.sort((a, b) => a.label.localeCompare(b.label));
};
const saveBundledPromptTemplates = (bundledPromptTemplates) => {
console.log('\t* Writing the Bundled Prompt Templates');
fs.writeFileSync(PROMPT_TEMPLATES_JSON_FILE, JSON.stringify(bundledPromptTemplates, null, 4));
};
const formatPromptKeys = (prompt) => {
Object.keys(prompt).forEach((key) => {
const newKey = key.charAt(0).toUpperCase() + key.slice(1);
prompt[newKey] = prompt[key];
delete prompt[key];
});
};
const createBundledPromptTemplates = (promptIndex) => {
const bundledPromptTemplates = {
total: 0,
offset: 0,
limit: 0,
data: [],
':type': 'sheet',
};
bundledPromptTemplates.limit = promptIndex.length;
bundledPromptTemplates.total = promptIndex.length;
return bundledPromptTemplates;
};
const getGitBranchName = async () => {
return new Promise((resolve) => {
exec('git branch --show-current', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return 'main';
}
resolve(stdout.replace(/(\r\n|\n|\r)/gm, ''));
});
});
};
const startProgram = async () => {
console.log('- Prompt Generator Starting...');
try {
// Read the prompt templates index file
const promptIndex = readPromptIndex();
const sortedPromptIndex = sortPromptIndex(promptIndex);
writePromptIndex(sortedPromptIndex);
// Create or update the bundled prompt templates file
console.log(`\t* Creating Bundled Prompt Templates File @ ${PROMPT_TEMPLATES_JSON_FILE}`);
const bundledPromptTemplates = createBundledPromptTemplates(sortedPromptIndex);
// Add the prompt templates to the target files
for await (const prompt of promptIndex) {
console.log(`\t\t* Adding ${prompt.label}`);
// Try to read the prompt template file
// If it fails, log the error and continue
try {
const promptTemplate = fs.readFileSync(path.join(PROMPT_TEMPLATES_DIR, prompt.file), 'utf8');
const branchName = await getGitBranchName();
prompt.template = promptTemplate.replace(/GIT_BRANCH/g, branchName);
delete prompt.file;
// Add the prompt to the bundled prompt templates file
formatPromptKeys(prompt);
bundledPromptTemplates.data.push(prompt);
} catch (err) {
console.log(`\t\t\t! Error: ${err}`);
console.log(`\t\t\t! Skipping ${prompt.label}`);
}
}
// Write the prompt templates to the target files
saveBundledPromptTemplates(bundledPromptTemplates);
} catch (error) {
console.error('Unexpected error encountered:', error);
}
console.log('- Prompt Generator Complete!');
};
// Start the program
startProgram();