create-kecare 1.0.0-beta.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.
Files changed (3) hide show
  1. package/index.mjs +172 -0
  2. package/package.json +21 -0
  3. package/tsconfig.json +27 -0
package/index.mjs ADDED
@@ -0,0 +1,172 @@
1
+ // oxlint-disable no-unused-vars
2
+ import { __VERSION__ } from './__VERSION__.mjs';
3
+
4
+ // Node.js
5
+ import os from 'node:os';
6
+ import { join } from 'node:path';
7
+ import { exit } from 'node:process';
8
+ import { Readable } from 'node:stream';
9
+ import { finished } from 'node:stream/promises';
10
+ import { execFileSync } from 'node:child_process';
11
+ import { mkdirSync, existsSync, createWriteStream, rmSync, mkdir } from 'node:fs';
12
+
13
+ // 第三方工具库
14
+ import { remove, move } from 'fs-extra';
15
+ import consola from 'consola';
16
+ import gradient from 'gradient-string';
17
+ import compressing from 'compressing';
18
+
19
+ const color = gradient(['cyan', '#2d9b87']);
20
+
21
+ (async () => {
22
+ const args = process.argv.slice(2);
23
+
24
+ let version = 'latest';
25
+ let installPath = undefined;
26
+
27
+ for (const arg of args) {
28
+ if (arg.startsWith('--version=')) {
29
+ version = arg.slice(10);
30
+ } else {
31
+ installPath = arg;
32
+ }
33
+ }
34
+ const workspace = process.platform === 'win32' ? join(process.env.USERPROFILE, '.kecare') : join(process.env.HOME, '.kecare');
35
+
36
+ const tempspace = join(workspace, '.temp');
37
+ if (!existsSync(workspace)) mkdirSync(workspace);
38
+ if (!existsSync(tempspace)) mkdirSync(tempspace);
39
+
40
+ const packageName = `kecare-${process.platform}-${os.arch()}`;
41
+ const selectedVersion = __VERSION__;
42
+
43
+ let selectMirror = '';
44
+ consola.start(color('Finding the appropriate mirror..'));
45
+
46
+ const mirrors = [
47
+ 'https://registry.npmjs.org/',
48
+ // "https://registry.npmmirror.com/", // binary removal blocked
49
+ 'https://mirrors.cloud.tencent.com/npm/',
50
+ 'https://cdn.jsdelivr.net/npm/',
51
+ ];
52
+ for (const mirror of mirrors) {
53
+ try {
54
+ consola.info(color(`Trying mirror ${mirror}`));
55
+ const controller = new AbortController();
56
+ const timeout = setTimeout(() => controller.abort(), 5000);
57
+ const response = await fetch(`${mirror}${packageName}`, {
58
+ signal: controller.signal,
59
+ });
60
+ clearTimeout(timeout);
61
+ if (!response.ok) continue;
62
+ const packageInfo = await response.json();
63
+ if (!packageInfo || !packageInfo['dist-tags'] || !packageInfo['dist-tags'].latest) continue;
64
+ selectMirror = mirror;
65
+ consola.success(color(`Found version ${selectedVersion} at ${mirror}`));
66
+ break;
67
+ } catch (error) {
68
+ consola.warn(color(`Mirror unavailable: ${error.message}`));
69
+ }
70
+ }
71
+ if (!selectMirror) {
72
+ consola.error(color('Failed to detect latest version from all mirrors'));
73
+ exit(1);
74
+ }
75
+
76
+ const downloadUrl = `${selectMirror}${packageName}/-/${packageName.split('/')[1]}-${selectedVersion}.tgz`;
77
+ consola.start(color(`Downloading package from ${downloadUrl}`));
78
+ try {
79
+ const res = await fetch(downloadUrl);
80
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
81
+ const destination = join(tempspace, `package.tgz`);
82
+ if (existsSync(destination)) await remove(destination);
83
+ const fileStream = createWriteStream(destination);
84
+ await finished(Readable.fromWeb(res.body).pipe(fileStream));
85
+ consola.success(color(`Package downloaded successfully`));
86
+ } catch (error) {
87
+ consola.error(color(`Download failed: ${error.message}`));
88
+ exit(1);
89
+ }
90
+ consola.start(color(`Extracting package ...`));
91
+ try {
92
+ await compressing.tgz.uncompress(join(tempspace, 'package.tgz'), tempspace);
93
+ consola.success(color('package extracted'));
94
+ } catch (error) {
95
+ consola.error(color(`Extraction failed: ${error.message}`));
96
+ exit(1);
97
+ }
98
+
99
+ const execName = process.platform === 'win32' ? 'kecare.exe' : 'kecare';
100
+ const execPath = join(tempspace, 'package', execName);
101
+ if (!existsSync(execPath)) {
102
+ consola.error(color('Executable not found in package'));
103
+ exit(1);
104
+ }
105
+
106
+ if (installPath) {
107
+ consola.start(color(`Installing to custom path: ${installPath}`));
108
+
109
+ if (!existsSync(installPath)) mkdir(installPath, { recursive: true });
110
+
111
+ await move(execPath, join(installPath, execName), { overwrite: true });
112
+ } else {
113
+ consola.start(color('Finding suitable installation location..'));
114
+ let targetPath = '';
115
+ if (process.platform === 'win32') {
116
+ targetPath = join(process.env.USERPROFILE, '.kecare');
117
+ if (!existsSync(targetPath)) mkdirSync(targetPath);
118
+ consola.info(color(`Installing to ${targetPath}`));
119
+ await move(execPath, join(targetPath, execName), { overwrite: true });
120
+ try {
121
+ execFileSync('powershell.exe', ['-Command', `[System.Environment]::SetEnvironmentVariable('PATH', $env:PATH + ';${targetPath}', 'User')`]);
122
+ consola.success(color(`Added to PATH (requires restart)`));
123
+ } catch {
124
+ consola.warn(color('Manual PATH configuration required'));
125
+ }
126
+ } else {
127
+ const searchPaths = [join(process.env.HOME, 'bin'), join(process.env.HOME, '.local', 'bin'), '/usr/local/bin'];
128
+ for (const path of searchPaths) {
129
+ if (existsSync(path)) {
130
+ targetPath = path;
131
+ break;
132
+ }
133
+ }
134
+ }
135
+ if (!targetPath) {
136
+ targetPath = join(process.env.HOME, 'bin');
137
+ mkdirSync(targetPath, { recursive: true });
138
+ }
139
+ consola.info(`Installing to ${targetPath}`);
140
+ const targetFile = join(targetPath, 'co');
141
+ if (existsSync(targetFile)) rmSync(targetFile);
142
+ try {
143
+ consola.info(color(`Moving executable to PATH: ${targetPath}`));
144
+ await move(execPath, targetFile, { overwrite: true });
145
+ } catch (error) {
146
+ consola.error(color(`Installation failed: ${error?.message}`));
147
+ exit(1);
148
+ }
149
+ try {
150
+ execFileSync('chmod', ['+x', targetFile]);
151
+ consola.success(color('Executable permissions set'));
152
+ } catch (error) {
153
+ consola.error(color(`Permission setting failed: ${error?.message}`));
154
+ exit(1);
155
+ }
156
+ }
157
+ consola.start(color('cleaning temporary files ...'));
158
+ try {
159
+ await remove(tempspace);
160
+ consola.success(color(`Installation complete`));
161
+ } catch (error) {
162
+ consola.warn(color(`Cleanup failed: ${error.message}`));
163
+ }
164
+
165
+ console.log('');
166
+ consola.success(color(`kecare Generator installed successfully!`));
167
+
168
+ consola.log(color('△ Try running: kecare --version'));
169
+ if (process.platform === 'win32') {
170
+ console.log(color('△ Note: You may need to restart your terminal for PATH changes to take effect'));
171
+ }
172
+ })();
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "create-kecare",
3
+ "version": "1.0.0-beta.1",
4
+ "main": "index.mjs",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/pamperkyumi/kecare.git"
8
+ },
9
+ "bin": {
10
+ "create-kecare": "index.mjs"
11
+ },
12
+ "scripts": {
13
+ "create-kecare": "./index.mjs"
14
+ },
15
+ "dependencies": {
16
+ "compressing": "^1.10.1",
17
+ "consola": "^3.4.0",
18
+ "fs-extra": "^11.2.0",
19
+ "gradient-string": "^3.0.0"
20
+ }
21
+ }
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
+ }