create-cookbook 1.0.0-beta.8 → 1.0.0-beta.81
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/__VERSION__.mjs +1 -0
- package/index.mjs +171 -201
- package/package.json +1 -1
package/__VERSION__.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const __VERSION__ = "1.0.0-beta.81";
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
import { __VERSION__ } from "./__VERSION__.mjs";
|
|
3
4
|
import os from "node:os";
|
|
4
5
|
import { join } from "node:path";
|
|
5
6
|
import { exit } from "node:process";
|
|
@@ -7,217 +8,186 @@ import { Readable } from "node:stream";
|
|
|
7
8
|
import { finished } from "node:stream/promises";
|
|
8
9
|
import { execFileSync } from "node:child_process";
|
|
9
10
|
import { mkdirSync, existsSync, createWriteStream, rmSync } from "node:fs";
|
|
10
|
-
import { remove } from "fs-extra";
|
|
11
|
+
import { remove, move } from "fs-extra";
|
|
11
12
|
import consola from "consola";
|
|
12
13
|
import gradient from "gradient-string";
|
|
13
14
|
import compressing from "compressing";
|
|
14
15
|
|
|
15
|
-
const colorLong = gradient(["cyan", "green"]);
|
|
16
16
|
const color = gradient(["cyan", "#2d9b87"]);
|
|
17
17
|
|
|
18
18
|
(async () => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
const args = process.argv.slice(2);
|
|
20
|
+
let version = "latest";
|
|
21
|
+
let installPath = undefined;
|
|
22
|
+
for (const arg of args) {
|
|
23
|
+
if (arg.startsWith("--version=")) {
|
|
24
|
+
version = arg.slice(10);
|
|
25
|
+
} else {
|
|
26
|
+
installPath = arg;
|
|
24
27
|
}
|
|
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
|
-
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const workspace = process.platform === "win32"
|
|
31
|
+
? join(process.env.USERPROFILE, ".cookbook")
|
|
32
|
+
: join(process.env.HOME, ".cookbook");
|
|
33
|
+
|
|
34
|
+
const tempspace = join(workspace, ".temp");
|
|
35
|
+
if (!existsSync(workspace)) mkdirSync(workspace);
|
|
36
|
+
if (!existsSync(tempspace)) mkdirSync(tempspace);
|
|
37
|
+
|
|
38
|
+
const packageName = `@milkio/cookbook-${process.platform}-${os.arch()}`;
|
|
39
|
+
const selectedVersion = __VERSION__;
|
|
40
|
+
let selectedMirror = "";
|
|
41
|
+
|
|
42
|
+
consola.start(color("Finding the appropriate mirror.."));
|
|
43
|
+
const mirrors = [
|
|
44
|
+
"https://registry.npmjs.org/",
|
|
45
|
+
"https://registry.npmmirror.com/",
|
|
46
|
+
"https://mirrors.cloud.tencent.com/npm/",
|
|
47
|
+
"https://cdn.jsdelivr.net/npm/"
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
for (const mirror of mirrors) {
|
|
51
|
+
try {
|
|
52
|
+
consola.info(color(`Trying mirror: ${mirror}`));
|
|
53
|
+
const controller = new AbortController();
|
|
54
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
55
|
+
const response = await fetch(`${mirror}${packageName}`, {
|
|
56
|
+
signal: controller.signal,
|
|
57
|
+
});
|
|
58
|
+
clearTimeout(timeout);
|
|
59
|
+
|
|
60
|
+
if (!response.ok) continue;
|
|
61
|
+
|
|
62
|
+
const packageInfo = await response.json();
|
|
63
|
+
if (!packageInfo || !packageInfo["dist-tags"] || !packageInfo["dist-tags"].latest) continue;
|
|
64
|
+
|
|
65
|
+
selectedMirror = mirror;
|
|
66
|
+
consola.success(color(`Found version ${selectedVersion} at ${mirror}`));
|
|
67
|
+
break;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
consola.warn(color(`Mirror unavailable: ${error.message}`));
|
|
60
70
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!selectedMirror) {
|
|
74
|
+
consola.error(color("Failed to detect latest version from all mirrors"));
|
|
75
|
+
exit(1);
|
|
76
|
+
}
|
|
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);
|
|
94
|
+
}
|
|
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}`));
|
|
102
|
+
exit(1);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const execName = process.platform === "win32" ? "co.exe" : "co";
|
|
106
|
+
const execPath = join(tempspace, "package", execName);
|
|
107
|
+
|
|
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 = "";
|
|
121
|
+
if (process.platform === "win32") {
|
|
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"));
|
|
136
|
+
}
|
|
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;
|
|
147
|
+
break;
|
|
81
148
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (!targetPath) {
|
|
152
|
+
targetPath = join(process.env.HOME, "bin");
|
|
153
|
+
mkdirSync(targetPath, { recursive: true });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
consola.info(color(`Installing to ${targetPath}`));
|
|
157
|
+
const targetFile = join(targetPath, "co");
|
|
158
|
+
if (existsSync(targetFile)) rmSync(targetFile);
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
consola.info(color(`Moving executable to PATH: ${targetPath}`));
|
|
162
|
+
await move(execPath, targetFile, { overwrite: true });
|
|
163
|
+
} catch (error) {
|
|
164
|
+
consola.error(color(`Installation failed: ${error?.message}`));
|
|
85
165
|
exit(1);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
execFileSync("chmod", ["+x", targetFile]);
|
|
170
|
+
consola.success(color("Executable permissions set"));
|
|
171
|
+
} catch (error) {
|
|
172
|
+
consola.error(color(`Permission setting failed: ${error?.message}`));
|
|
173
|
+
exit(1);
|
|
174
|
+
}
|
|
86
175
|
}
|
|
176
|
+
}
|
|
87
177
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
await compressing.tgz.uncompress(join(tempspace, "cookbook.tgz"), tempspace);
|
|
105
|
-
}))();
|
|
106
|
-
|
|
107
|
-
consola.start(color("[1/2] Extracting.."));
|
|
108
|
-
await Promise.all([uiExtractPromise, cookbookExtractPromise]);
|
|
109
|
-
consola.success(color("[1/2] Extracted!"));
|
|
110
|
-
|
|
111
|
-
consola.success(color("[2/2] Installing.."));
|
|
112
|
-
await utils.mvToPathAndInstall(join(tempspace, "package"), process.platform === "win32" ? "co.exe" : "co", tempspace);
|
|
113
|
-
await utils.mvUIDir(join(tempspace, "ui", "package"));
|
|
114
|
-
await utils.tempspaceClean(tempspace);
|
|
115
|
-
consola.success(color("[2/2] Installed!"));
|
|
116
|
-
|
|
117
|
-
console.log("");
|
|
118
|
-
consola.info(color("Try run: co version"));
|
|
119
|
-
consola.info(colorLong("* If you find that the co command does not exist, try restarting your Terminal or System"));
|
|
120
|
-
})();
|
|
121
|
-
|
|
122
|
-
const utils = {
|
|
123
|
-
downloadFile: async (url, workspace, filename) => {
|
|
124
|
-
const res = await fetch(url);
|
|
125
|
-
if (existsSync(join(workspace, filename))) await remove(join(workspace, filename));
|
|
126
|
-
const destination = join(workspace, filename);
|
|
127
|
-
const fileStream = createWriteStream(destination, { flags: "wx" });
|
|
128
|
-
await finished(Readable.fromWeb(res.body).pipe(fileStream));
|
|
129
|
-
},
|
|
130
|
-
mvUIDir: async (tempspace) => {
|
|
131
|
-
if (!existsSync(process.env.HOME || process.env.USERPROFILE, ".cookbook")) mkdirSync(join(process.env.HOME || process.env.USERPROFILE, ".cookbook"));
|
|
132
|
-
if (process.platform === "win32") {
|
|
133
|
-
if (existsSync(join(process.env.USERPROFILE, ".cookbook", 'ui'))) await utils.executePowershell(`Remove-Item -Recurse -Force "${join(process.env.USERPROFILE, ".cookbook", 'ui')}";`);
|
|
134
|
-
await utils.executePowershell(`Move-Item -Path "${join(tempspace)}" -Destination "${join(process.env.USERPROFILE, ".cookbook", 'ui')}" -Force;`);
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
if (process.platform === "linux") {
|
|
138
|
-
if (existsSync(join(process.env.HOME, ".cookbook", 'ui'))) await utils.executeBash(`rm -rf "${join(process.env.HOME, ".cookbook", 'ui')}";`);
|
|
139
|
-
await utils.executeBash(`mv "${join(tempspace)}" "${join(process.env.HOME, ".cookbook", 'ui')}"`);
|
|
140
|
-
}
|
|
141
|
-
if (process.platform === "darwin") {
|
|
142
|
-
if (existsSync(join(process.env.HOME, ".cookbook", 'ui'))) await utils.executeBash(`rm -rf "${join(process.env.HOME, ".cookbook", 'ui')}";`);
|
|
143
|
-
await utils.executeBash(`mv "${join(tempspace)}" "${join(process.env.HOME, ".cookbook", 'ui')}"`);
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
tempspaceClean: async (tempspace) => {
|
|
147
|
-
if (process.platform === "win32") {
|
|
148
|
-
if (existsSync(join(tempspace))) await utils.executePowershell(`Remove-Item -Recurse -Force "${join(tempspace)}";`);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
if (process.platform === "linux") {
|
|
152
|
-
if (existsSync(join(tempspace))) await utils.executeBash(`rm -rf "${join(tempspace)}"`);
|
|
153
|
-
}
|
|
154
|
-
if (process.platform === "darwin") {
|
|
155
|
-
if (existsSync(join(tempspace))) await utils.executeBash(`rm -rf "${join(tempspace)}"`);
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
mvToPathAndInstall: async (workspace, filename, tempspace) => {
|
|
159
|
-
if (process.platform === "win32") {
|
|
160
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
161
|
-
if (!(process.env.PATH.includes(`${join(process.env.USERPROFILE, ".cookbook")};`) || process.env.PATH.includes(`;${join(process.env.USERPROFILE, ".cookbook")}`) || process.env.PATH === `${join(process.env.USERPROFILE, ".cookbook")}`)) {
|
|
162
|
-
await utils.executePowershell(`[System.Environment]::SetEnvironmentVariable("PATH", [System.Environment]::GetEnvironmentVariable("PATH", "User") + ";${join(process.env.USERPROFILE, ".cookbook")}", "User");`);
|
|
163
|
-
}
|
|
164
|
-
if (!existsSync(process.env.USERPROFILE, ".cookbook")) mkdirSync(process.env.USERPROFILE, ".cookbook");
|
|
165
|
-
if (existsSync(join(process.env.USERPROFILE, ".cookbook", filename))) rmSync(join(process.env.USERPROFILE, ".cookbook", filename));
|
|
166
|
-
await utils.executePowershell(`Move-Item -Path "${join(workspace, filename)}" -Destination "${join(process.env.USERPROFILE, ".cookbook")}";`);
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
if (process.platform === "linux") {
|
|
170
|
-
const paths = [join(process.env.HOME, ".bin"), "/usr/bin/", "/usr/sbin"];
|
|
171
|
-
let pathChecked = "";
|
|
172
|
-
for (const path of paths) {
|
|
173
|
-
if (process.env.PATH.includes(`${path}:`) || process.env.PATH.includes(`:${path}`) || process.env.PATH === `${path}`) {
|
|
174
|
-
pathChecked = path;
|
|
175
|
-
break;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (!pathChecked) {
|
|
179
|
-
consola.error(color("No path found!"));
|
|
180
|
-
exit(0);
|
|
181
|
-
}
|
|
182
|
-
if (!existsSync(pathChecked)) mkdirSync(pathChecked);
|
|
183
|
-
if (existsSync(join(pathChecked, filename))) {
|
|
184
|
-
if (pathChecked.startsWith("/home")) await utils.executeBash(`rm -f ${join(pathChecked, filename)}`);
|
|
185
|
-
else await utils.executeBash(`rm -f ${join(pathChecked, filename)}`);
|
|
186
|
-
}
|
|
187
|
-
if (pathChecked.startsWith("/home")) await utils.executeBash(`mv ${join(workspace, filename)} ${pathChecked} && chmod +x ${join(pathChecked, filename)}`);
|
|
188
|
-
else await utils.executeBash(`mv ${join(workspace, filename)} ${pathChecked} && chmod +x ${join(pathChecked, filename)}`);
|
|
189
|
-
}
|
|
190
|
-
if (process.platform === "darwin") {
|
|
191
|
-
const paths = [join(process.env.HOME, "bin"), join(process.env.HOME, ".bin"), join(process.env.HOME, ".local", "bin"), "/usr/local/bin"];
|
|
192
|
-
let pathChecked = "";
|
|
193
|
-
for (const path of paths) {
|
|
194
|
-
if (process.env.PATH.includes(`${path}:`) || process.env.PATH.includes(`:${path}`) || process.env.PATH === `${path}`) {
|
|
195
|
-
pathChecked = path;
|
|
196
|
-
break;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
if (!pathChecked) {
|
|
200
|
-
consola.error(color("No path found!"));
|
|
201
|
-
exit(0);
|
|
202
|
-
}
|
|
203
|
-
if (!existsSync(pathChecked)) mkdirSync(pathChecked);
|
|
204
|
-
if (existsSync(join(pathChecked, filename))) {
|
|
205
|
-
if (pathChecked.startsWith("/Users")) await utils.executeBash(`rm -f ${join(pathChecked, filename)}`);
|
|
206
|
-
else await utils.executeBash(`rm -f ${join(pathChecked, filename)}`);
|
|
207
|
-
}
|
|
208
|
-
if (pathChecked.startsWith("/Users")) await utils.executeBash(`mv ${join(workspace, filename)} ${pathChecked} && chmod +x ${join(pathChecked, filename)}`);
|
|
209
|
-
else await utils.executeBash(`mv ${join(workspace, filename)} ${pathChecked} && chmod +x ${join(pathChecked, filename)}`);
|
|
210
|
-
}
|
|
211
|
-
},
|
|
212
|
-
executePowershell: async (script) => {
|
|
213
|
-
return execFileSync("powershell.exe", ["-c", script], {
|
|
214
|
-
stdio: "inherit",
|
|
215
|
-
});
|
|
216
|
-
},
|
|
217
|
-
executeBash: (script) => {
|
|
218
|
-
|
|
219
|
-
return execFileSync("bash", ["-c", `if command -v sudo &>/dev/null; then sudo ${script}; else ${script}; fi`], {
|
|
220
|
-
stdio: "inherit",
|
|
221
|
-
});
|
|
222
|
-
},
|
|
223
|
-
};
|
|
178
|
+
consola.start(color("Cleaning temporary files.."));
|
|
179
|
+
try {
|
|
180
|
+
await remove(tempspace);
|
|
181
|
+
consola.success(color("Installation complete"));
|
|
182
|
+
} catch (error) {
|
|
183
|
+
consola.warn(color(`Cleanup failed: ${error.message}`));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
console.log("");
|
|
187
|
+
consola.success(color("Cookbook installed successfully!"))
|
|
188
|
+
|
|
189
|
+
console.log(color("△ Try running: co --help"));
|
|
190
|
+
if (process.platform === "win32") {
|
|
191
|
+
console.log(color("△ Note: You may need to restart your terminal for PATH changes to take effect"));
|
|
192
|
+
}
|
|
193
|
+
})();
|