-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
executable file
·109 lines (93 loc) · 3.14 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
#!/usr/bin/env node
import path from "path";
import { glob } from "glob";
import { readFileSync, writeFileSync, existsSync } from "fs";
import dotenv from "universal-dotenv";
import beautify from "js-beautify";
import { componentConverter } from "./src/converter.js";
import { fileSelector } from "./src/selector.js";
import inquirer from "inquirer";
import getModels from "./src/models.js";
const options = {
indent_size: 2,
jslint_happy: true,
end_with_newline: false,
e4x: true,
};
const resetOptions = {
e4x: true,
preserve_newlines: false,
indent_level: 0,
indent_size: 0,
};
dotenv.init();
const showLoading = (message) => {
const animationFrames = ["⠋", "⠉", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
let currentFrame = 0;
process.stdout.write("\x1B[?25l");
const loadingInterval = setInterval(() => {
const frame = "\x1b[35m\x1b[1m" + animationFrames[currentFrame] + "\x1b[0m";
process.stdout.write(`\r${frame} \x1b[1m${message}\x1b[0m`);
currentFrame = (currentFrame + 1) % animationFrames.length;
}, 100);
const stopLoading = (message, color) => {
clearInterval(loadingInterval);
process.stdout.write("\r\x1B[K\x1B[?25h");
process.stdout.write(`${color}\x1b[1m${message}\n\x1b[0m`);
};
return { stopLoading };
};
async function run() {
const configPath = path.resolve(process.cwd(), "storybook-genie.config.json");
let model;
let basePath;
let template;
if (existsSync(configPath)) {
const data = readFileSync(configPath);
const config = JSON.parse(data);
if (config.defaultModel) {
model = config.defaultModel;
basePath = config.defaultPath;
}
}
const templateFilePath = glob.sync("storybook-genie.template.*")[0];
if (templateFilePath) {
const data = readFileSync(templateFilePath, "utf8");
template = data.trim();
}
if (!model) {
const answer = await inquirer.prompt([
{
type: "list",
message: "Select the AI model you want to use",
name: "model",
choices: await getModels(),
},
]);
model = answer.model;
}
await fileSelector({
message: "Select the files containing the React components:",
basePath,
}).then(async (selectedFiles) => { // Adjusted to expect an array of selected files
if (!selectedFiles || selectedFiles.length === 0) return; // Check if any files are selected
selectedFiles.forEach(async (file) => { // Loop through selected files
const input = readFileSync(file, "utf-8");
const extension = path.extname(file);
const spinner = showLoading("Generating story...");
try {
const story = await componentConverter(input.replace(/^\s*[\r\n]/gm, "").trim(), model, template).then(
(story) => {
story = beautify(story, resetOptions);
return beautify(story, options);
}
);
writeFileSync(file.replace(extension, `.story${extension}`), story);
spinner.stopLoading(`Story generated for ${file}!`, "\x1b[32m");
} catch (error) {
spinner.stopLoading(`Error generating story for ${file}: ` + error.message, "\x1b[91m");
}
});
});
}
await run();