-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
172 lines (144 loc) · 4.01 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env node
const { Command } = require('commander');
const fs = require('fs-extra');
const path = require('path');
const inquirer = require('inquirer');
const glob = require('glob');
const fetch = require('node-fetch');
const os = require('os');
const program = new Command();
const API_KEY_PATH = path.join(os.homedir(), '.sparkengine_api_key');
const copyTemplate = (targetDir) => {
const sourceDir = path.join(__dirname, 'template');
fs.copySync(sourceDir, targetDir);
console.log(`Project created at ${targetDir}`);
};
const configureFavicon = (directory) => {
const faviconPath = path.join(__dirname, 'favicon.ico');
const targetFaviconPath = path.join(directory, 'favicon.ico');
fs.copySync(faviconPath, targetFaviconPath);
console.log(`Favicon configured at ${targetFaviconPath}`);
};
const configureAllSpkFiles = () => {
const homeDir = require('os').homedir();
const pattern = `${homeDir}/**/*.spk`;
glob(pattern, (err, files) => {
if (err) {
console.error('Error searching for .spk files:', err);
return;
}
files.forEach(file => {
const fileDir = path.dirname(file);
configureFavicon(fileDir);
});
console.log(`Configured ${files.length} .spk files to use our favicon.`);
});
};
const askToConfigureAllSpkFiles = () => {
inquirer.prompt([
{
type: 'confirm',
name: 'configureAll',
message: 'Would you like to configure all .spk files on your computer to use our favicon?',
default: true,
},
]).then((answers) => {
if (answers.configureAll) {
configureAllSpkFiles();
}
});
};
const setApiKey = (key) => {
fs.writeFileSync(API_KEY_PATH, key);
console.log('API key added successfully.');
};
const replaceApiKey = (key) => {
setApiKey(key);
console.log('API key replaced successfully.');
};
const removeApiKey = () => {
if (fs.existsSync(API_KEY_PATH)) {
fs.removeSync(API_KEY_PATH);
console.log('API key removed successfully.');
} else {
console.error('API key not found.');
}
};
const getApiKey = () => {
if (fs.existsSync(API_KEY_PATH)) {
return fs.readFileSync(API_KEY_PATH, 'utf-8');
}
console.error('API key not found. Please add your API key first.');
process.exit(1);
};
const sendPromptToProject = (projectId, prompt) => {
const apiKey = getApiKey();
fetch("https://sparkengine.ai/api/engine/completion", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
apiKey: apiKey,
ProjectId: projectId,
prompt: prompt
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
};
const promptUserForText = (projectId) => {
inquirer.prompt([
{
type: 'input',
name: 'prompt',
message: 'Enter your prompt:',
},
]).then((answers) => {
sendPromptToProject(projectId, answers.prompt);
});
};
program
.version('1.0.0')
.argument('<directory>', 'Directory to create the project in')
.action((directory) => {
copyTemplate(directory);
});
program
.command('configure-favicon <directory>')
.description('Configure .spk files in a specific directory to use our favicon')
.action((directory) => {
configureFavicon(directory);
});
program
.command('configure-all-spk')
.description('Configure all .spk files on the user\'s computer to use our favicon')
.action(() => {
configureAllSpkFiles();
});
program
.command('key add <key>')
.description('Add your Spark Engine API key')
.action((key) => {
setApiKey(key);
});
program
.command('key replace <key>')
.description('Replace your Spark Engine API key')
.action((key) => {
replaceApiKey(key);
});
program
.command('key remove')
.description('Remove your Spark Engine API key')
.action(() => {
removeApiKey();
});
program
.command('run <projectId>')
.description('Send a prompt to a Spark Engine project')
.action((projectId) => {
promptUserForText(projectId);
});
program.parse(process.argv);