create-cookbook 1.0.0-beta.71 → 1.0.0-beta.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +131 -177
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -12,7 +12,6 @@ import consola from "consola";
|
|
|
12
12
|
import gradient from "gradient-string";
|
|
13
13
|
import compressing from "compressing";
|
|
14
14
|
|
|
15
|
-
const colorLong = gradient(["cyan", "green"]);
|
|
16
15
|
const color = gradient(["cyan", "#2d9b87"]);
|
|
17
16
|
|
|
18
17
|
(async () => {
|
|
@@ -27,206 +26,161 @@ const color = gradient(["cyan", "#2d9b87"]);
|
|
|
27
26
|
}
|
|
28
27
|
}
|
|
29
28
|
|
|
30
|
-
const workspace = process.platform === "win32"
|
|
31
|
-
|
|
29
|
+
const workspace = process.platform === "win32"
|
|
30
|
+
? join(process.env.USERPROFILE, ".cookbook")
|
|
31
|
+
: join(process.env.HOME, ".cookbook");
|
|
32
|
+
|
|
33
|
+
const tempspace = join(workspace, ".temp");
|
|
32
34
|
if (!existsSync(workspace)) mkdirSync(workspace);
|
|
33
35
|
if (!existsSync(tempspace)) mkdirSync(tempspace);
|
|
34
36
|
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
let
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
const packageName = `@milkio/cookbook-${process.platform}-${os.arch()}`;
|
|
38
|
+
let selectedVersion = version;
|
|
39
|
+
let selectedMirror = "";
|
|
40
|
+
|
|
41
|
+
consola.start(color("Finding the appropriate mirror.."));
|
|
42
|
+
const mirrors = [
|
|
43
|
+
"https://registry.npmjs.org/",
|
|
44
|
+
"https://registry.npmmirror.com/",
|
|
45
|
+
"https://mirrors.cloud.tencent.com/npm/",
|
|
46
|
+
"https://cdn.jsdelivr.net/npm/"
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
for (const mirror of mirrors) {
|
|
41
50
|
try {
|
|
42
|
-
consola.
|
|
51
|
+
consola.info(color(`Trying mirror: ${mirror}`));
|
|
43
52
|
const controller = new AbortController();
|
|
44
|
-
const timeout = setTimeout(() => controller.abort(),
|
|
45
|
-
const response = await fetch(`${mirror}${
|
|
53
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
54
|
+
const response = await fetch(`${mirror}${packageName}`, {
|
|
46
55
|
signal: controller.signal,
|
|
47
56
|
});
|
|
48
57
|
clearTimeout(timeout);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
58
|
+
|
|
59
|
+
if (!response.ok) continue;
|
|
60
|
+
|
|
61
|
+
const packageInfo = await response.json();
|
|
62
|
+
if (!packageInfo || !packageInfo["dist-tags"] || !packageInfo["dist-tags"].latest) continue;
|
|
63
|
+
|
|
64
|
+
if (!version || version === "latest") selectedVersion = packageInfo["dist-tags"].latest;
|
|
65
|
+
selectedMirror = mirror;
|
|
66
|
+
consola.success(color(`Found version ${selectedVersion} at ${mirror}`));
|
|
56
67
|
break;
|
|
57
68
|
} catch (error) {
|
|
58
|
-
|
|
59
|
-
continue;
|
|
69
|
+
consola.warn(color(`Mirror unavailable: ${error.message}`));
|
|
60
70
|
}
|
|
61
71
|
}
|
|
62
|
-
|
|
63
|
-
|
|
72
|
+
|
|
73
|
+
if (!selectedMirror) {
|
|
74
|
+
consola.error(color("Failed to detect latest version from all mirrors"));
|
|
64
75
|
exit(1);
|
|
65
76
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
} catch (error) {
|
|
84
|
-
// biome-ignore lint/correctness/noUnnecessaryContinue: <explanation>
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
77
|
+
|
|
78
|
+
const downloadUrl = `${selectedMirror}${packageName}/-/${packageName.split('/')[1]}-${selectedVersion}.tgz`;
|
|
79
|
+
consola.start(color(`Downloading package from ${downloadUrl}`));
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const res = await fetch(downloadUrl);
|
|
83
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
84
|
+
|
|
85
|
+
const destination = join(tempspace, "package.tgz");
|
|
86
|
+
if (existsSync(destination)) await remove(destination);
|
|
87
|
+
|
|
88
|
+
const fileStream = createWriteStream(destination);
|
|
89
|
+
await finished(Readable.fromWeb(res.body).pipe(fileStream));
|
|
90
|
+
consola.success(color("Package downloaded successfully"));
|
|
91
|
+
} catch (error) {
|
|
92
|
+
consola.error(color(`Download failed: ${error.message}`));
|
|
93
|
+
exit(1);
|
|
87
94
|
}
|
|
88
|
-
|
|
89
|
-
|
|
95
|
+
|
|
96
|
+
consola.start(color("Extracting package.."));
|
|
97
|
+
try {
|
|
98
|
+
await compressing.tgz.uncompress(join(tempspace, "package.tgz"), tempspace);
|
|
99
|
+
consola.success(color("Package extracted"));
|
|
100
|
+
} catch (error) {
|
|
101
|
+
consola.error(color(`Extraction failed: ${error.message}`));
|
|
90
102
|
exit(1);
|
|
91
103
|
}
|
|
92
104
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// consola.start(color(`[1/2] Downloading Cookbook UI (${uiUrl})..`));
|
|
96
|
-
// await utils.downloadFile(uiUrl, tempspace, "ui.tgz");
|
|
97
|
-
// consola.success(color("[1/2] Downloaded!"));
|
|
98
|
-
// const uiExtractPromise = (async () => {
|
|
99
|
-
// if (!existsSync(join(tempspace, "ui"))) mkdirSync(join(tempspace, "ui"));
|
|
100
|
-
// await compressing.tgz.uncompress(join(tempspace, "ui.tgz"), join(tempspace, "ui"));
|
|
101
|
-
// })();
|
|
102
|
-
|
|
103
|
-
const cookbookUrl = `${cookbookPackageInfo.mirror}${cookbookName}/-/cookbook-${process.platform}-${os.arch()}-${version === "latest" ? cookbookPackageInfo.json["dist-tags"].latest : version}.tgz`;
|
|
104
|
-
consola.start(cookbookUrl);
|
|
105
|
-
consola.start(color(`[2/2] Downloading Cookbook Core (${cookbookUrl})..`));
|
|
106
|
-
await utils.downloadFile(cookbookUrl, tempspace, "cookbook.tgz");
|
|
107
|
-
consola.success(color("[2/2] Downloaded!"));
|
|
108
|
-
const cookbookExtractPromise = (async () => {
|
|
109
|
-
await compressing.tgz.uncompress(join(tempspace, "cookbook.tgz"), tempspace);
|
|
110
|
-
})();
|
|
111
|
-
|
|
112
|
-
// consola.start(color("[1/2] Extracting.."));
|
|
113
|
-
// await Promise.all([uiExtractPromise, cookbookExtractPromise]);
|
|
114
|
-
// consola.success(color("[1/2] Extracted!"));
|
|
115
|
-
|
|
116
|
-
consola.success(color("[2/2] Installing.."));
|
|
117
|
-
await utils.mvToPathAndInstall(installPath, join(tempspace, "package"), process.platform === "win32" ? "co.exe" : "co");
|
|
118
|
-
// await utils.mvUIDir(join(tempspace, "ui", "package"));
|
|
119
|
-
await utils.tempspaceClean(tempspace);
|
|
120
|
-
consola.success(color("[2/2] Installed!"));
|
|
105
|
+
const execName = process.platform === "win32" ? "co.exe" : "co";
|
|
106
|
+
const execPath = join(tempspace, "package", execName);
|
|
121
107
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
mvUIDir: async (tempspace) => {
|
|
136
|
-
if (!existsSync(process.env.HOME || process.env.USERPROFILE, ".cookbook")) mkdirSync(join(process.env.HOME || process.env.USERPROFILE, ".cookbook"));
|
|
137
|
-
if (process.platform === "win32") {
|
|
138
|
-
if (existsSync(join(process.env.USERPROFILE, ".cookbook", "ui"))) await utils.executePowershell(`Remove-Item -Recurse -Force "${join(process.env.USERPROFILE, ".cookbook", "ui")}";`);
|
|
139
|
-
await utils.executePowershell(`Move-Item -Path "${join(tempspace)}" -Destination "${join(process.env.USERPROFILE, ".cookbook", "ui")}" -Force;`);
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
if (process.platform === "linux") {
|
|
143
|
-
if (existsSync(join(process.env.HOME, ".cookbook", "ui"))) await utils.executeBash(`rm -rf "${join(process.env.HOME, ".cookbook", "ui")}";`);
|
|
144
|
-
await utils.executeBash(`mv "${join(tempspace)}" "${join(process.env.HOME, ".cookbook", "ui")}"`);
|
|
145
|
-
}
|
|
146
|
-
if (process.platform === "darwin") {
|
|
147
|
-
if (existsSync(join(process.env.HOME, ".cookbook", "ui"))) await utils.executeBash(`rm -rf "${join(process.env.HOME, ".cookbook", "ui")}";`);
|
|
148
|
-
await utils.executeBash(`mv "${join(tempspace)}" "${join(process.env.HOME, ".cookbook", "ui")}"`);
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
tempspaceClean: async (tempspace) => {
|
|
152
|
-
if (process.platform === "win32") {
|
|
153
|
-
if (existsSync(join(tempspace))) await utils.executePowershell(`Remove-Item -Recurse -Force "${join(tempspace)}";`);
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
if (process.platform === "linux") {
|
|
157
|
-
if (existsSync(join(tempspace))) await utils.executeBash(`rm -rf "${join(tempspace)}"`);
|
|
158
|
-
}
|
|
159
|
-
if (process.platform === "darwin") {
|
|
160
|
-
if (existsSync(join(tempspace))) await utils.executeBash(`rm -rf "${join(tempspace)}"`);
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
mvToPathAndInstall: async (installPath, workspace, filename) => {
|
|
164
|
-
if (installPath) {
|
|
165
|
-
if (!existsSync(installPath)) mkdirSync(installPath, { recursive: true });
|
|
166
|
-
await move(join(workspace, filename), join(installPath, filename), { overwrite: true });
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
108
|
+
if (!existsSync(execPath)) {
|
|
109
|
+
consola.error(color("Executable not found in package"));
|
|
110
|
+
exit(1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (installPath) {
|
|
114
|
+
consola.start(color(`Installing to custom path: ${installPath}`));
|
|
115
|
+
if (!existsSync(installPath)) mkdirSync(installPath, { recursive: true });
|
|
116
|
+
await move(execPath, join(installPath, execName), { overwrite: true });
|
|
117
|
+
} else {
|
|
118
|
+
consola.start(color("Finding suitable installation location.."));
|
|
119
|
+
|
|
120
|
+
let targetPath = "";
|
|
169
121
|
if (process.platform === "win32") {
|
|
170
|
-
|
|
171
|
-
if (!(
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
pathChecked = path;
|
|
185
|
-
break;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
if (!pathChecked) {
|
|
189
|
-
consola.error(color("No path found!"));
|
|
190
|
-
exit(0);
|
|
191
|
-
}
|
|
192
|
-
if (!existsSync(pathChecked)) mkdirSync(pathChecked);
|
|
193
|
-
if (existsSync(join(pathChecked, filename))) {
|
|
194
|
-
if (pathChecked.startsWith("/home")) await utils.executeBash(`rm -f ${join(pathChecked, filename)}`);
|
|
195
|
-
else await utils.executeBash(`rm -f ${join(pathChecked, filename)}`);
|
|
122
|
+
targetPath = join(process.env.USERPROFILE, ".cookbook");
|
|
123
|
+
if (!existsSync(targetPath)) mkdirSync(targetPath);
|
|
124
|
+
|
|
125
|
+
consola.info(color(`Installing to ${targetPath}`));
|
|
126
|
+
await move(execPath, join(targetPath, execName), { overwrite: true });
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
execFileSync("powershell", [
|
|
130
|
+
"-Command",
|
|
131
|
+
`[System.Environment]::SetEnvironmentVariable('PATH', $env:PATH + ';${targetPath}', 'User')`
|
|
132
|
+
]);
|
|
133
|
+
consola.success(color("Added to PATH (requires restart)"));
|
|
134
|
+
} catch {
|
|
135
|
+
consola.warn(color("Manual PATH configuration required"));
|
|
196
136
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
137
|
+
} else {
|
|
138
|
+
const searchPaths = [
|
|
139
|
+
join(process.env.HOME, "bin"),
|
|
140
|
+
join(process.env.HOME, ".local", "bin"),
|
|
141
|
+
"/usr/local/bin"
|
|
142
|
+
];
|
|
143
|
+
|
|
144
|
+
for (const path of searchPaths) {
|
|
145
|
+
if (existsSync(path)) {
|
|
146
|
+
targetPath = path;
|
|
206
147
|
break;
|
|
207
148
|
}
|
|
208
149
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
150
|
+
|
|
151
|
+
if (!targetPath) {
|
|
152
|
+
targetPath = join(process.env.HOME, "bin");
|
|
153
|
+
mkdirSync(targetPath, { recursive: true });
|
|
212
154
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
155
|
+
|
|
156
|
+
consola.info(color(`Installing to ${targetPath}`));
|
|
157
|
+
const targetFile = join(targetPath, "co");
|
|
158
|
+
if (existsSync(targetFile)) rmSync(targetFile);
|
|
159
|
+
|
|
160
|
+
await move(execPath, targetFile, { overwrite: true });
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
execFileSync("chmod", ["+x", targetFile]);
|
|
164
|
+
consola.success(color("Executable permissions set"));
|
|
165
|
+
} catch (error) {
|
|
166
|
+
consola.warn(color(`Permission setting failed: ${error.message}`));
|
|
217
167
|
}
|
|
218
|
-
if (pathChecked.startsWith("/Users")) await utils.executeBash(`mv ${join(workspace, filename)} ${pathChecked} && chmod +x ${join(pathChecked, filename)}`);
|
|
219
|
-
else await utils.executeBash(`mv ${join(workspace, filename)} ${pathChecked} && chmod +x ${join(pathChecked, filename)}`);
|
|
220
168
|
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
consola.start(color("Cleaning temporary files"));
|
|
172
|
+
try {
|
|
173
|
+
await remove(tempspace);
|
|
174
|
+
consola.success(color("Installation complete"));
|
|
175
|
+
} catch (error) {
|
|
176
|
+
consola.warn(color(`Cleanup failed: ${error.message}`));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
console.log("");
|
|
180
|
+
consola.success(color("Cookbook installed successfully!"))
|
|
181
|
+
|
|
182
|
+
console.log(color("△ Try running: co --help"));
|
|
183
|
+
if (process.platform === "win32") {
|
|
184
|
+
console.log(color("△ Note: You may need to restart your terminal for PATH changes to take effect"));
|
|
185
|
+
}
|
|
186
|
+
})();
|