-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
279 lines (241 loc) · 9.11 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env node
import * as tar from 'tar';
import fs from 'fs-extra';
import axios from 'axios';
import path from 'path';
import os from 'os';
import chalk from 'chalk';
import ora from 'ora';
import yaml from 'js-yaml'; // Import js-yaml
import readline from 'readline';
const REGISTRY_URL = 'https://registry.npmjs.org';
const DEFAULT_PACKAGE_JSON = {
name: "my-project",
version: "1.0.0",
description: "A new project",
main: "index.js",
scripts: {
test: "echo \"Error: no test specified\" && exit 1"
},
keywords: [],
author: "Your Name",
license: "ISC",
};
const args = process.argv.slice(2);
const verbose = args.includes('--verbose');
// Function to log debug information with color if verbose mode is enabled
const debugLog = (message) => {
if (verbose) {
console.log(chalk.blue(`[DEBUG] ${message}`));
}
};
const printHelp = () => {
console.log(`
Usage: cnsh <command> [options] [package]
Commands:
add Install a package
remove Remove a package
install Install dependencies from package.json
init Initialize a new project
Options:
-g Install globally
-y Initialize with default settings
--help Display this help message
--version Display the version number
--verbose Display detailed debugging information
`);
};
const printVersion = async () => {
const packageJsonPath = path.join(process.cwd(), 'package.json');
if (fs.existsSync(packageJsonPath)) {
const packageJson = fs.readJsonSync(packageJsonPath);
console.log(`cnsh version ${packageJson.version || 'unknown'}`);
} else {
console.log('package.json not found.');
}
};
const initProject = async (args) => {
if (args.includes('-y')) {
console.log('Initializing project with default settings...');
fs.writeJsonSync(path.join(process.cwd(), 'package.json'), DEFAULT_PACKAGE_JSON, { spaces: 2 });
console.log('Default package.json created.');
} else {
console.log('Initializing project...');
const name = await prompt('Project name (default: my-project): ') || 'my-project';
const version = await prompt('Version (default: 1.0.0): ') || '1.0.0';
const description = await prompt('Description (default: A new project): ') || 'A new project';
const author = await prompt('Author (default: Your Name): ') || 'Your Name';
const packageJson = {
...DEFAULT_PACKAGE_JSON,
name,
version,
description,
author
};
fs.writeJsonSync(path.join(process.cwd(), 'package.json'), packageJson, { spaces: 2 });
console.log('package.json created. Modify it as needed.');
}
};
const prompt = (query) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question(query, (answer) => {
rl.close();
resolve(answer);
});
});
};
const getPackageInfo = async (packageName) => {
debugLog(`Fetching package info for: ${packageName}`);
try {
const response = await axios.get(`${REGISTRY_URL}/${packageName}/latest`);
const { dist, version } = response.data;
debugLog(`Tarball URL: ${dist.tarball}, Version: ${version}`);
return { tarballUrl: dist.tarball, version };
} catch (error) {
throw new Error(`Failed to fetch package metadata: ${error.message}`);
}
};
const downloadTarball = async (tarballUrl, tarballPath) => {
debugLog(`Downloading tarball from URL: ${tarballUrl}`);
const writer = fs.createWriteStream(tarballPath);
const response = await axios.get(tarballUrl, { responseType: 'stream' });
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => {
const fileSize = fs.statSync(tarballPath).size;
debugLog(`Downloaded tarball size: ${(fileSize / (1024 * 1024)).toFixed(2)} MB`);
if (fileSize < 1024) { // Adjust this threshold based on expected file sizes
reject(new Error('Downloaded tarball is too small, indicating a possible issue.'));
} else {
resolve();
}
});
writer.on('error', reject);
});
};
const installPackage = async (packageName) => {
const spinner = ora(`Installing ${packageName}...`).start();
debugLog(`Starting installation of package: ${packageName}`);
try {
const { tarballUrl, version } = await getPackageInfo(packageName);
const packageDir = path.join(installDir, packageName);
const tarballPath = path.join(packageDir, `${packageName}.tar.gz`);
// Ensure package directory exists
if (!fs.existsSync(packageDir)) {
fs.mkdirSync(packageDir, { recursive: true });
}
await downloadTarball(tarballUrl, tarballPath);
debugLog(`Extracting ${packageName}...`);
spinner.text = `Extracting ${packageName}...`;
await tar.x({ file: tarballPath, cwd: packageDir });
fs.removeSync(tarballPath); // Remove the tarball after extraction
// Update the lock file with tarball URL and version
updateLockFile(packageName, tarballUrl, version);
debugLog(`Lock file updated for package: ${packageName}`);
spinner.succeed(chalk.green(`${packageName} installed successfully!`));
debugLog(`Installation of package ${packageName} completed.`);
} catch (error) {
spinner.fail(chalk.red(`Failed to install ${packageName}: ${error.message}`));
}
};
const removePackage = (packageName) => {
const spinner = ora(`Removing ${packageName}...`).start();
const packagePath = path.join(installDir, packageName);
if (fs.existsSync(packagePath)) {
fs.removeSync(packagePath);
// Update the lock file
updateLockFile(packageName, null, null, false);
spinner.succeed(chalk.green(`${packageName} removed successfully!`));
} else {
spinner.fail(chalk.red(`${packageName} is not installed`));
}
};
const installDependencies = async () => {
const spinner = ora('Installing dependencies from package.json...').start();
try {
const packageJsonPath = path.join(process.cwd(), 'package.json');
if (!fs.existsSync(packageJsonPath)) {
throw new Error('package.json not found');
}
const packageJson = fs.readJsonSync(packageJsonPath);
const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies };
if (Object.keys(dependencies).length === 0) {
spinner.info(chalk.yellow('No dependencies to install.'));
return;
}
spinner.text = 'Installing dependencies...';
const installPromises = Object.keys(dependencies).map(dep => installPackage(dep));
await Promise.all(installPromises);
spinner.succeed(chalk.green('All dependencies installed successfully!'));
} catch (error) {
spinner.fail(chalk.red(`Failed to install dependencies: ${error.message}`));
}
};
// Function to update the cnsh.lock file
const updateLockFile = (packageName, tarballUrl = null, version = null, installed = true) => {
const lockFilePath = path.join(process.cwd(), 'cnsh.lock');
let lockData = {};
if (fs.existsSync(lockFilePath)) {
lockData = yaml.load(fs.readFileSync(lockFilePath, 'utf8')) || {};
}
if (installed) {
lockData[packageName] = {
...(tarballUrl && { tarballUrl }), // Only add tarballUrl if it's provided
...(version && { version }) // Only add version if it's provided
};
} else {
delete lockData[packageName];
}
fs.writeFileSync(lockFilePath, yaml.dump(lockData), 'utf8');
};
// Extract the command and package name
const command = args.find(arg => !arg.startsWith('--')) || '';
const packageArgs = args.filter(arg => !arg.startsWith('--') && arg !== command);
const packageName = packageArgs.length > 0 ? packageArgs[0] : '';
if (args.includes('--help')) {
printHelp();
process.exit(0);
}
if (args.includes('--version')) {
await printVersion();
process.exit(0);
}
if (command === 'init') {
await initProject(args);
process.exit(0);
}
// Remove `--verbose` from args if present to avoid it being mistaken as a package name
const isGlobal = args.includes('-g');
const installDir = isGlobal
? path.join(os.homedir(), '.cnsh-global', 'cnsh_lib')
: path.join(process.cwd(), 'cnsh_lib');
if (!fs.existsSync(installDir)) {
fs.mkdirSync(installDir, { recursive: true });
}
switch (command) {
case 'add':
if (packageName) {
await installPackage(packageName);
} else {
console.log(chalk.red('No package name provided.'));
}
break;
case 'remove':
if (packageName) {
removePackage(packageName);
} else {
console.log(chalk.red('No package name provided.'));
}
break;
case 'install':
await installDependencies();
break;
default:
console.log(chalk.red('Unknown command.'));
printHelp();
break;
}