-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (154 loc) · 5.04 KB
/
index.js
File metadata and controls
181 lines (154 loc) · 5.04 KB
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
const axios = require("axios");
const fs = require("fs");
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
async function getDependencies(packageName, cleanOutput = false) {
try {
// Remove any URL parts if provided
packageName = packageName.replace(
/^https?:\/\/www\.npmjs\.com\/package\//,
""
);
packageName = packageName.replace(/^npmjs\.com\/package\//, "");
const response = await axios.get(
`https://registry.npmjs.org/${packageName}`
);
const packageData = response.data;
const latestVersion = packageData["dist-tags"]?.latest;
const versionData = packageData.versions[latestVersion];
if (!versionData) {
return cleanOutput
? ""
: `No version data found for package: ${packageName}`;
}
if (cleanOutput) {
// Clean output: only dependencies and dev dependencies
let output = "";
if (versionData.dependencies) {
output += Object.keys(versionData.dependencies).join("\n") + "\n";
}
if (versionData.devDependencies) {
output += Object.keys(versionData.devDependencies).join("\n") + "\n";
}
return output;
} else {
// Full output with package info
let output = `\nPackage: ${packageName}\n`;
output += `Latest version: ${latestVersion}\n`;
if (versionData.dependencies) {
output += "\nDependencies:\n";
output += Object.keys(versionData.dependencies).join("\n") + "\n";
} else {
output += "\nNo regular dependencies.\n";
}
if (versionData.devDependencies) {
output += "\nDev Dependencies:\n";
output += Object.keys(versionData.devDependencies).join("\n") + "\n";
} else {
output += "\nNo dev dependencies.\n";
}
return output;
}
} catch (error) {
return cleanOutput
? ""
: `Error fetching package data for ${packageName}: ${error.message}`;
}
}
async function processPackages(inputFile, outputFile, cleanOutput = false) {
try {
// Read input file
const inputContent = fs.readFileSync(inputFile, "utf8");
const packageNames = inputContent
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0);
if (packageNames.length === 0) {
console.log("No package names found in input file.");
return;
}
console.log(`Processing ${packageNames.length} packages...`);
let output = "";
if (!cleanOutput) {
output = `Dependency Extractor Results\n`;
output += `Generated on: ${new Date().toISOString()}\n`;
output += `=====================================\n`;
}
// Process each package
for (const packageName of packageNames) {
console.log(`Processing: ${packageName}`);
const result = await getDependencies(packageName, cleanOutput);
output += result;
if (!cleanOutput) {
output += "=====================================\n";
}
}
// Write to output file
fs.writeFileSync(outputFile, output);
console.log(`Results written to: ${outputFile}`);
} catch (error) {
console.error("Error processing packages:", error.message);
}
}
async function interactiveMode() {
readline.question("Enter npm package name or URL: ", async (input) => {
const result = await getDependencies(input.trim());
console.log(result);
readline.close();
});
}
// Check command line arguments
const args = process.argv.slice(2);
if (args.length === 0) {
// Interactive mode: no arguments
console.log("Interactive mode - enter package name:");
interactiveMode();
} else if (args.length === 2) {
// File mode: input.txt output.txt
const inputFile = args[0];
const outputFile = args[1];
// Check if input file exists
if (!fs.existsSync(inputFile)) {
console.error(`Input file not found: ${inputFile}`);
process.exit(1);
}
processPackages(inputFile, outputFile, false);
} else if (args.length === 3) {
// Clean output mode: input.txt output.txt clean
const inputFile = args[0];
const outputFile = args[1];
const cleanFlag = args[2];
if (cleanFlag.toLowerCase() !== "clean") {
console.log("Usage:");
console.log(
" node index.js # Interactive mode"
);
console.log(
" node index.js input.txt output.txt # File mode with full info"
);
console.log(
" node index.js input.txt output.txt clean # File mode with clean output"
);
process.exit(1);
}
// Check if input file exists
if (!fs.existsSync(inputFile)) {
console.error(`Input file not found: ${inputFile}`);
process.exit(1);
}
processPackages(inputFile, outputFile, true);
} else {
console.log("Usage:");
console.log(
" node index.js # Interactive mode"
);
console.log(
" node index.js input.txt output.txt # File mode with full info"
);
console.log(
" node index.js input.txt output.txt clean # File mode with clean output"
);
process.exit(1);
}