-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
85 lines (70 loc) · 3.32 KB
/
build.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
const fs = require('fs');
const path = require('path');
const {Configuration, OpenAIApi} = require("openai");
const configuration = new Configuration({
apiKey: process.env['openai_apikey'],
});
const openai = new OpenAIApi(configuration);
async function fetchFromOpenAI(system, prompt, previousMessages = []) {
let result = '';
try {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{role: 'system', content: system},
...previousMessages.map(({author, message}) => ({role: author, content: message})),
{role: 'user', content: prompt},
],
max_tokens: 600,
temperature: 0.1,
});
result = response.data.choices[0].message.content.trim();
return result;
} catch(e) {
console.log('Could not connect to OpenAI', e.message);
}
console.log('got result', {result});
return result;
}
async function transpileFile(sourceCode, sourcePath, destinationPath, targetExtension) {
console.log("Transpiling", sourceCode)
const transpiledCode = await fetchFromOpenAI(
`You are a JavaScript code generator. You will take pseucode and transform it into JavaScript which will run on NodeJS. Please only output valid JavaScript code and nothing else.`,
// TODO: Replace with SudoLang
`You are a JavaScript code generator. You will take pseucode and transform it into JavaScript.
Please only output valid JavaScript code and nothing else. Refrain from adding comments, instructions or explanations.
${sourceCode}
Please generate the JavaScript code for the pseudocode above. Please only output valid JavaScript code and nothing else. Refrain from adding comments, instructions or explanations.`
);
const destinationFile = path.join(destinationPath, path.basename(sourcePath, '.su') + targetExtension);
fs.writeFileSync(destinationFile, transpiledCode);
}
function extractInterfaces(sourcePath, destinationPath) {
const sourceCode = fs.readFileSync(sourcePath, 'utf-8');
const interfaceRegex = /@interfaces\s+([\w\s,]+)\s+from\s+["'](.+?)["']/;
const match = interfaceRegex.exec(sourceCode);
if (match) {
const interfaces = match[1].split(',').map(x => x.trim());
const interfaceFilePath = path.join(path.dirname(sourcePath), match[2]);
const interfaceFileContent = fs.readFileSync(interfaceFilePath, 'utf-8');
const replacedCode = sourceCode.replace(match[0], interfaceFileContent);
return replacedCode;
}
return sourceCode;
}
function buildSystem(sourceDir, destinationDir, targetLanguage) {
const targetExtension = targetLanguage === 'JavaScript' ? '.js' : '.py';
fs.readdirSync(sourceDir).forEach(file => {
if (path.extname(file) === '.su') {
const sourcePath = path.join(sourceDir, file);
const destinationPath = path.join(destinationDir, file);
const sourceCode = extractInterfaces(sourcePath, destinationPath);
console.log(sourceCode);
transpileFile(sourceCode,sourcePath, destinationDir, targetExtension);
}
});
}
const sourceDir = './src';
const destinationDir = './dist';
const targetLanguage = 'JavaScript';
buildSystem(sourceDir, destinationDir, targetLanguage);