create-cookbook 1.0.0-beta.99 → 1.0.1
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 -1
- package/index.mjs +201 -192
- package/package.json +5 -1
package/__VERSION__.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const __VERSION__ =
|
|
1
|
+
export const __VERSION__ = '1.0.1'
|
package/index.mjs
CHANGED
|
@@ -1,193 +1,202 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { __VERSION__ } from "./__VERSION__.mjs";
|
|
4
|
-
import os from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { exit } from "node:process";
|
|
7
|
-
import { Readable } from "node:stream";
|
|
8
|
-
import { finished } from "node:stream/promises";
|
|
9
|
-
import { execFileSync } from "node:child_process";
|
|
10
|
-
import { mkdirSync, existsSync, createWriteStream, rmSync } from "node:fs";
|
|
11
|
-
import { remove, move } from "fs-extra";
|
|
12
|
-
import consola from "consola";
|
|
13
|
-
import gradient from "gradient-string";
|
|
14
|
-
import compressing from "compressing";
|
|
15
|
-
|
|
16
|
-
const color = gradient(["cyan", "#2d9b87"]);
|
|
17
|
-
|
|
18
|
-
(async () => {
|
|
19
|
-
const args = process.argv.slice(2);
|
|
20
|
-
|
|
21
|
-
let
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (!existsSync(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"https://registry.
|
|
46
|
-
"https://
|
|
47
|
-
"https://
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
const
|
|
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
|
-
const
|
|
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
|
-
consola.
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
consola.
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { __VERSION__ } from "./__VERSION__.mjs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { exit } from "node:process";
|
|
7
|
+
import { Readable } from "node:stream";
|
|
8
|
+
import { finished } from "node:stream/promises";
|
|
9
|
+
import { execFileSync } from "node:child_process";
|
|
10
|
+
import { mkdirSync, existsSync, createWriteStream, rmSync } from "node:fs";
|
|
11
|
+
import { remove, move } from "fs-extra";
|
|
12
|
+
import consola from "consola";
|
|
13
|
+
import gradient from "gradient-string";
|
|
14
|
+
import compressing from "compressing";
|
|
15
|
+
|
|
16
|
+
const color = gradient(["cyan", "#2d9b87"]);
|
|
17
|
+
|
|
18
|
+
(async () => {
|
|
19
|
+
const args = process.argv.slice(2);
|
|
20
|
+
// oxlint-disable-next-line no-unused-vars
|
|
21
|
+
let version = "latest";
|
|
22
|
+
let installPath = undefined;
|
|
23
|
+
for (const arg of args) {
|
|
24
|
+
if (arg.startsWith("--version=")) {
|
|
25
|
+
version = arg.slice(10);
|
|
26
|
+
} else {
|
|
27
|
+
installPath = arg;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const workspace = process.platform === "win32"
|
|
32
|
+
? join(process.env.USERPROFILE, ".cookbook")
|
|
33
|
+
: join(process.env.HOME, ".cookbook");
|
|
34
|
+
|
|
35
|
+
const tempspace = join(workspace, ".temp");
|
|
36
|
+
if (!existsSync(workspace)) mkdirSync(workspace);
|
|
37
|
+
if (!existsSync(tempspace)) mkdirSync(tempspace);
|
|
38
|
+
|
|
39
|
+
const packageName = `@milkio/cookbook-${process.platform}-${os.arch()}`;
|
|
40
|
+
const selectedVersion = __VERSION__;
|
|
41
|
+
let selectedMirror = "";
|
|
42
|
+
|
|
43
|
+
consola.start(color("Finding the appropriate mirror.."));
|
|
44
|
+
const mirrors = [
|
|
45
|
+
"https://registry.npmjs.org/",
|
|
46
|
+
// "https://registry.npmmirror.com/", // binary removal blocked
|
|
47
|
+
"https://mirrors.cloud.tencent.com/npm/",
|
|
48
|
+
"https://cdn.jsdelivr.net/npm/"
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
for (const mirror of mirrors) {
|
|
52
|
+
try {
|
|
53
|
+
consola.info(color(`Trying mirror: ${mirror}`));
|
|
54
|
+
const controller = new AbortController();
|
|
55
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
56
|
+
const response = await fetch(`${mirror}${packageName}`, {
|
|
57
|
+
signal: controller.signal,
|
|
58
|
+
});
|
|
59
|
+
clearTimeout(timeout);
|
|
60
|
+
|
|
61
|
+
if (!response.ok) continue;
|
|
62
|
+
|
|
63
|
+
const packageInfo = await response.json();
|
|
64
|
+
if (!packageInfo || !packageInfo["dist-tags"] || !packageInfo["dist-tags"].latest) continue;
|
|
65
|
+
|
|
66
|
+
selectedMirror = mirror;
|
|
67
|
+
consola.success(color(`Found version ${selectedVersion} at ${mirror}`));
|
|
68
|
+
break;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
consola.warn(color(`Mirror unavailable: ${error.message}`));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!selectedMirror) {
|
|
75
|
+
consola.error(color("Failed to detect latest version from all mirrors"));
|
|
76
|
+
exit(1);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const downloadUrl = `${selectedMirror}${packageName}/-/${packageName.split('/')[1]}-${selectedVersion}.tgz`;
|
|
80
|
+
consola.start(color(`Downloading package from ${downloadUrl}`));
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
const res = await fetch(downloadUrl);
|
|
84
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
85
|
+
|
|
86
|
+
const destination = join(tempspace, "package.tgz");
|
|
87
|
+
if (existsSync(destination)) await remove(destination);
|
|
88
|
+
|
|
89
|
+
const fileStream = createWriteStream(destination);
|
|
90
|
+
await finished(Readable.fromWeb(res.body).pipe(fileStream));
|
|
91
|
+
consola.success(color("Package downloaded successfully"));
|
|
92
|
+
} catch (error) {
|
|
93
|
+
consola.error(color(`Download failed: ${error.message}`));
|
|
94
|
+
exit(1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
consola.start(color("Extracting package.."));
|
|
98
|
+
try {
|
|
99
|
+
await compressing.tgz.uncompress(join(tempspace, "package.tgz"), tempspace);
|
|
100
|
+
consola.success(color("Package extracted"));
|
|
101
|
+
} catch (error) {
|
|
102
|
+
consola.error(color(`Extraction failed: ${error.message}`));
|
|
103
|
+
exit(1);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const execName = process.platform === "win32" ? "co.exe" : "co";
|
|
107
|
+
const execPath = join(tempspace, "package", execName);
|
|
108
|
+
|
|
109
|
+
if (!existsSync(execPath)) {
|
|
110
|
+
consola.error(color("Executable not found in package"));
|
|
111
|
+
exit(1);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (installPath) {
|
|
115
|
+
consola.start(color(`Installing to custom path: ${installPath}`));
|
|
116
|
+
if (!existsSync(installPath)) mkdirSync(installPath, { recursive: true });
|
|
117
|
+
await move(execPath, join(installPath, execName), { overwrite: true });
|
|
118
|
+
} else {
|
|
119
|
+
consola.start(color("Finding suitable installation location.."));
|
|
120
|
+
|
|
121
|
+
let targetPath = "";
|
|
122
|
+
if (process.platform === "win32") {
|
|
123
|
+
targetPath = join(process.env.USERPROFILE, ".cookbook");
|
|
124
|
+
if (!existsSync(targetPath)) mkdirSync(targetPath);
|
|
125
|
+
|
|
126
|
+
consola.info(color(`Installing to ${targetPath}`));
|
|
127
|
+
await move(execPath, join(targetPath, execName), { overwrite: true });
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
const ps =
|
|
131
|
+
`${target}=' ${targetPath.replace(/'/g,"''")}'
|
|
132
|
+
$userPath = [Environment]::GetEnvironmentVariable('Path','User')
|
|
133
|
+
if($null -eq $userPath) { $userPath = ''}
|
|
134
|
+
$parts = $userPath -split ';' | Where-Object {$_ -and $_.Trim() -ne ''}
|
|
135
|
+
$partsLower = $parts | ForEach-Object { $_.Trim().ToLowerInvariant() }
|
|
136
|
+
if ($partsLower -notcontains $target.Trim().ToLowerInvariant()) {
|
|
137
|
+
$newPath = ($parts + $target) -join ';'
|
|
138
|
+
[Environment]::SetEnvironmentVariable('Path', $newPath, 'User')
|
|
139
|
+
}
|
|
140
|
+
`
|
|
141
|
+
execFileSync('powershell.exe', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', ps], { stdio: 'ignore' });
|
|
142
|
+
consola.success(color("Added to PATH (requires restart)"));
|
|
143
|
+
} catch {
|
|
144
|
+
consola.warn(color("Manual PATH configuration required"));
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
const searchPaths = [
|
|
148
|
+
join(process.env.HOME, "bin"),
|
|
149
|
+
join(process.env.HOME, ".local", "bin"),
|
|
150
|
+
"/usr/local/bin"
|
|
151
|
+
];
|
|
152
|
+
|
|
153
|
+
for (const path of searchPaths) {
|
|
154
|
+
if (existsSync(path)) {
|
|
155
|
+
targetPath = path;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (!targetPath) {
|
|
161
|
+
targetPath = join(process.env.HOME, "bin");
|
|
162
|
+
mkdirSync(targetPath, { recursive: true });
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
consola.info(color(`Installing to ${targetPath}`));
|
|
166
|
+
const targetFile = join(targetPath, "co");
|
|
167
|
+
if (existsSync(targetFile)) rmSync(targetFile);
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
consola.info(color(`Moving executable to PATH: ${targetPath}`));
|
|
171
|
+
await move(execPath, targetFile, { overwrite: true });
|
|
172
|
+
} catch (error) {
|
|
173
|
+
consola.error(color(`Installation failed: ${error?.message}`));
|
|
174
|
+
exit(1);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
try {
|
|
178
|
+
execFileSync("chmod", ["+x", targetFile]);
|
|
179
|
+
consola.success(color("Executable permissions set"));
|
|
180
|
+
} catch (error) {
|
|
181
|
+
consola.error(color(`Permission setting failed: ${error?.message}`));
|
|
182
|
+
exit(1);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
consola.start(color("Cleaning temporary files.."));
|
|
188
|
+
try {
|
|
189
|
+
await remove(tempspace);
|
|
190
|
+
consola.success(color("Installation complete"));
|
|
191
|
+
} catch (error) {
|
|
192
|
+
consola.warn(color(`Cleanup failed: ${error.message}`));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
console.log("");
|
|
196
|
+
consola.success(color("Cookbook installed successfully!"))
|
|
197
|
+
|
|
198
|
+
console.log(color("△ Try running: co --version"));
|
|
199
|
+
if (process.platform === "win32") {
|
|
200
|
+
console.log(color("△ Note: You may need to restart your terminal for PATH changes to take effect"));
|
|
201
|
+
}
|
|
193
202
|
})();
|
package/package.json
CHANGED