create-cookbook 1.0.0-alpha.67

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.
Files changed (3) hide show
  1. package/index.mjs +146 -0
  2. package/package.json +14 -0
  3. package/tsconfig.json +27 -0
package/index.mjs ADDED
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env node
2
+
3
+ import os from "node:os";
4
+ import { join } from "node:path";
5
+ import { exit } from "node:process";
6
+ import { Readable } from "node:stream";
7
+ import { finished } from "node:stream/promises";
8
+ import { execFileSync } from "node:child_process";
9
+ import { mkdirSync, existsSync, createWriteStream, rmSync } from "node:fs";
10
+ import { remove } from "fs-extra";
11
+ import consola from "consola";
12
+ import gradient from "gradient-string";
13
+ import * as tar from "tar";
14
+
15
+ const colorLong = gradient(["cyan", "green"]);
16
+ const color = gradient(["cyan", "#2d9b87"]);
17
+
18
+ (async () => {
19
+ let workspace = process.platform === "win32" ? join(process.env.USERPROFILE, ".cookbook") : join(process.env.HOME, ".cookbook");
20
+ let tempspace = process.platform === "win32" ? join(process.env.USERPROFILE, ".cookbook", ".temp") : join(process.env.HOME, ".cookbook", ".temp");
21
+ if (!existsSync(workspace)) mkdirSync(workspace);
22
+ if (!existsSync(tempspace)) mkdirSync(tempspace);
23
+
24
+ const name = `@milkio/cookbook-${process.platform}-${os.arch()}`;
25
+ let packageInfo;
26
+ for (const mirror of ["https://registry.npmjs.org/", "https://registry.npmmirror.com/", "https://mirrors.cloud.tencent.com/npm/", "https://cdn.jsdelivr.net/npm/"]) {
27
+ try {
28
+ consola.start(color(`Checking (${mirror}${name})..`));
29
+ const controller = new AbortController();
30
+ const timeout = setTimeout(() => controller.abort(), 8000);
31
+ const response = await fetch(`${mirror}${name}`, {
32
+ signal: controller.signal
33
+ });
34
+ clearTimeout(timeout);
35
+ if (response.status !== 200) continue;
36
+ const json = await response.json();
37
+ if (json.name !== name) continue;
38
+ packageInfo = {
39
+ mirror,
40
+ json,
41
+ };
42
+ break;
43
+ } catch (error) {
44
+ continue;
45
+ }
46
+ }
47
+ if (!packageInfo) {
48
+ consola.error(color("Network connection failed!"));
49
+ exit(1);
50
+ }
51
+
52
+ const url = `${packageInfo.mirror}${name}/-/cookbook-${process.platform}-${os.arch()}-${packageInfo.json["dist-tags"].latest}.tgz`;
53
+ console.log(url)
54
+ consola.start(url);
55
+ consola.start(color(`Downloading..`));
56
+ await utils.downloadFile(url, tempspace, "cookbook.tgz");
57
+ consola.success(color("Downloaded!"));
58
+
59
+ consola.start(color(`Extracting..`));
60
+ await tar.extract({
61
+ file: join(tempspace, "cookbook.tgz"),
62
+ cwd: tempspace,
63
+ });
64
+ await utils.mvToPathAndClean(join(tempspace, "package"), process.platform === "win32" ? "co.exe" : "co", tempspace);
65
+ consola.success(color("Extracted!"));
66
+
67
+ console.log("");
68
+ consola.info(color(`Try run: co --version`));
69
+ consola.info(colorLong(`If you find that the co command does not exist, try restarting your Terminal or System`));
70
+ })();
71
+
72
+ const utils = {
73
+ downloadFile: async (url, workspace, filename) => {
74
+ const res = await fetch(url);
75
+ if (!existsSync("downloads")) await mkdirSync("downloads");
76
+ if (existsSync(join(workspace, filename))) await remove(join(workspace, filename));
77
+ const destination = join(workspace, filename);
78
+ const fileStream = createWriteStream(destination, { flags: "wx" });
79
+ await finished(Readable.fromWeb(res.body).pipe(fileStream));
80
+ },
81
+ mvToPathAndClean: async (workspace, filename, tempspace) => {
82
+ if (process.platform === "win32") {
83
+ await new Promise((resolve) => setTimeout(resolve, 500));
84
+ 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")}`)) {
85
+ await utils.executePowershell(`[System.Environment]::SetEnvironmentVariable("PATH", [System.Environment]::GetEnvironmentVariable("PATH", "User") + ";${join(process.env.USERPROFILE, ".cookbook")}", "User");`);
86
+ }
87
+ if (!existsSync(process.env.USERPROFILE, ".cookbook")) mkdirSync(process.env.USERPROFILE, ".cookbook");
88
+ if (existsSync(join(process.env.USERPROFILE, ".cookbook", filename))) rmSync(join(process.env.USERPROFILE, ".cookbook", filename));
89
+ await utils.executePowershell(`Move-Item -Path "${join(workspace, filename)}" -Destination "${join(process.env.USERPROFILE, ".cookbook")}";`);
90
+ await utils.executePowershell(`Remove-Item -Recurse -Force "${join(tempspace)}";`);
91
+ return;
92
+ }
93
+ if (process.platform === "linux") {
94
+ const paths = [join(process.env.HOME, ".bin"), "/usr/bin/", "/usr/sbin"];
95
+ let pathChecked = "";
96
+ for (const path of paths) {
97
+ if (process.env.PATH.includes(`${path}:`) || process.env.PATH.includes(`:${path}`) || process.env.PATH === `${path}`) {
98
+ pathChecked = path;
99
+ break;
100
+ }
101
+ }
102
+ if (!pathChecked) {
103
+ consola.error(color("No path found!"));
104
+ exit(0);
105
+ }
106
+ if (!existsSync(pathChecked)) mkdirSync(pathChecked);
107
+ if (existsSync(join(pathChecked, filename))) {
108
+ if (pathChecked.startsWith("/home")) await utils.executeBash(`rm -f ${join(pathChecked, filename)}`);
109
+ else await utils.executeBash(`sudo rm -f ${join(pathChecked, filename)}`);
110
+ }
111
+ if (pathChecked.startsWith("/home")) await utils.executeBash(`mv ${join(workspace, filename)} ${pathChecked} && chmod +x ${join(pathChecked, filename)} && rm -rf ${join(tempspace)}`);
112
+ else await utils.executeBash(`sudo mv ${join(workspace, filename)} ${pathChecked} && sudo chmod +x ${join(pathChecked, filename)} && sudo rm -rf ${join(tempspace)}`);
113
+ }
114
+ if (process.platform === "darwin") {
115
+ const paths = [join(process.env.HOME, "bin"), join(process.env.HOME, ".bin"), join(process.env.HOME, ".local", "bin"), "/usr/local/bin"];
116
+ let pathChecked = "";
117
+ for (const path of paths) {
118
+ if (process.env.PATH.includes(`${path}:`) || process.env.PATH.includes(`:${path}`) || process.env.PATH === `${path}`) {
119
+ pathChecked = path;
120
+ break;
121
+ }
122
+ }
123
+ if (!pathChecked) {
124
+ consola.error(color("No path found!"));
125
+ exit(0);
126
+ }
127
+ if (!existsSync(pathChecked)) mkdirSync(pathChecked);
128
+ if (existsSync(join(pathChecked, filename))) {
129
+ if (pathChecked.startsWith("/Users")) await utils.executeBash(`rm -f ${join(pathChecked, filename)}`);
130
+ else await utils.executeBash(`sudo rm -f ${join(pathChecked, filename)}`);
131
+ }
132
+ if (pathChecked.startsWith("/Users")) await utils.executeBash(`mv ${join(workspace, filename)} ${pathChecked} && chmod +x ${join(pathChecked, filename)} && rm -rf ${join(tempspace)}`);
133
+ else await utils.executeBash(`sudo mv ${join(workspace, filename)} ${pathChecked} && sudo chmod +x ${join(pathChecked, filename)} && sudo rm -rf ${join(tempspace)}`);
134
+ }
135
+ },
136
+ executePowershell: async (script) => {
137
+ return execFileSync("powershell.exe", ["-c", script], {
138
+ stdio: "inherit",
139
+ });
140
+ },
141
+ executeBash: (script) => {
142
+ return execFileSync("bash", ["-c", script], {
143
+ stdio: "inherit",
144
+ });
145
+ },
146
+ };
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "create-cookbook",
3
+ "version": "1.0.0-alpha.67",
4
+ "main": "index.mjs",
5
+ "scripts": {
6
+ "postinstall": "./index.mjs"
7
+ },
8
+ "dependencies": {
9
+ "consola": "^3.2.3",
10
+ "fs-extra": "^11.2.0",
11
+ "gradient-string": "^3.0.0",
12
+ "tar": "^7.4.3"
13
+ }
14
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Enable latest features
4
+ "lib": ["ESNext", "DOM"],
5
+ "target": "ESNext",
6
+ "module": "ESNext",
7
+ "moduleDetection": "force",
8
+ "jsx": "react-jsx",
9
+ "allowJs": true,
10
+
11
+ // Bundler mode
12
+ "moduleResolution": "bundler",
13
+ "allowImportingTsExtensions": true,
14
+ "verbatimModuleSyntax": true,
15
+ "noEmit": true,
16
+
17
+ // Best practices
18
+ "strict": true,
19
+ "skipLibCheck": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+
22
+ // Some stricter flags (disabled by default)
23
+ "noUnusedLocals": false,
24
+ "noUnusedParameters": false,
25
+ "noPropertyAccessFromIndexSignature": false
26
+ }
27
+ }