Skip to content

Commit 3e117a9

Browse files
committed
Auto-Code Freebie
1 parent d4532e6 commit 3e117a9

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

freebies/auto-coder/setup.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
const fs = require('fs');
2+
const fsp = require('fs/promises');
3+
const path = require('path');
4+
const { exec } = require('child_process');
5+
const https = require('https');
6+
const { promisify } = require('util');
7+
8+
const execAsync = promisify(exec);
9+
10+
async function installPackages() {
11+
try {
12+
console.log('Installing packages...');
13+
const { stdout, stderr } = await execAsync('npm install @langchain/community');
14+
if (stderr) {
15+
console.error(`Error installing packages: ${stderr}`);
16+
return;
17+
}
18+
console.log(`Packages installed successfully:\n${stdout}`);
19+
} catch (error) {
20+
console.error(`Error installing packages: ${error.message}`);
21+
}
22+
}
23+
24+
async function createExampleCodeFile() {
25+
const filePath = path.join(process.cwd(), 'example_code.txt');
26+
const defaultContent = 'This is the default content of example_code.txt';
27+
28+
try {
29+
await fsp.access(filePath);
30+
console.log(`File already exists: ${filePath}`);
31+
} catch (error) {
32+
console.log(`Creating file: ${filePath}`);
33+
await fsp.writeFile(filePath, defaultContent, 'utf8');
34+
console.log(`File created successfully with default content: ${filePath}`);
35+
}
36+
}
37+
38+
async function createConfigFile() {
39+
const configPath = path.join(process.cwd(), 'config.json');
40+
const defaultConfig = {
41+
language: "JavaScript",
42+
functionality: "example functionality",
43+
exampleCodePath: path.join(process.cwd(), 'example_code.txt'),
44+
iterations: 5,
45+
codeIntention: "Create a sample function",
46+
focusArea: "Initial focus on code structure and functionality",
47+
nextSteps: "Begin by implementing basic functionality and ensuring code structure is sound."
48+
};
49+
50+
try {
51+
await fsp.access(configPath);
52+
console.log(`Config file already exists: ${configPath}`);
53+
} catch (error) {
54+
console.log(`Creating config file: ${configPath}`);
55+
await fsp.writeFile(configPath, JSON.stringify(defaultConfig, null, 2), 'utf8');
56+
console.log(`Config file created successfully: ${configPath}`);
57+
}
58+
}
59+
60+
async function downloadFile(url, dest) {
61+
const file = fs.createWriteStream(dest);
62+
return new Promise((resolve, reject) => {
63+
https.get(url, (response) => {
64+
response.pipe(file);
65+
file.on('finish', () => {
66+
file.close(resolve);
67+
});
68+
}).on('error', (err) => {
69+
fs.unlink(dest);
70+
reject(err.message);
71+
});
72+
});
73+
}
74+
75+
async function downloadAutoMainScript() {
76+
const url = 'https://raw.githubusercontent.com/ctavolazzi/NovaSystem/main/freebies/auto-coder/auto-coder.js';
77+
const dest = path.join(process.cwd(), 'auto-coder.js');
78+
79+
try {
80+
await downloadFile(url, dest);
81+
console.log('auto-coder.js downloaded successfully.');
82+
} catch (error) {
83+
console.error(`Error downloading auto-coder.js: ${error}`);
84+
}
85+
}
86+
87+
async function createPackageJson() {
88+
const packageJsonPath = path.join(process.cwd(), 'package.json');
89+
let packageJson = {
90+
name: "setup_example",
91+
version: "1.0.0",
92+
type: "module",
93+
dependencies: {
94+
"@langchain/community": "^0.0.1"
95+
}
96+
};
97+
98+
try {
99+
const existingPackageJson = JSON.parse(await fsp.readFile(packageJsonPath, 'utf8'));
100+
packageJson = {
101+
...existingPackageJson,
102+
...packageJson,
103+
dependencies: {
104+
...existingPackageJson.dependencies,
105+
...packageJson.dependencies
106+
}
107+
};
108+
console.log(`Updating existing package.json at ${packageJsonPath}`);
109+
} catch (error) {
110+
console.log(`Creating new package.json at ${packageJsonPath}`);
111+
}
112+
113+
await fsp.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8');
114+
console.log(`package.json file created/updated successfully.`);
115+
}
116+
117+
async function setup() {
118+
await createPackageJson();
119+
await installPackages();
120+
await createExampleCodeFile();
121+
await createConfigFile();
122+
await downloadAutoMainScript();
123+
console.log('Setup completed successfully.');
124+
}
125+
126+
setup();

0 commit comments

Comments
 (0)