-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinstall-deno-deps.js
More file actions
executable file
·214 lines (169 loc) · 6.29 KB
/
install-deno-deps.js
File metadata and controls
executable file
·214 lines (169 loc) · 6.29 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
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
function printHelp() {
console.log('Install Deno Dependencies\n');
console.log('Usage: ./install-deno-deps.js <functions-dir>\n');
console.log('Scans for folders with deno.json and runs "deno install".');
}
if (process.argv.includes('--help') || process.argv.includes('-h') || process.argv.length === 2) {
printHelp();
process.exit(0);
}
const FUNCTIONS_DIR = process.argv[2];
function installDependencies(folderPath, errorSummary) {
const folderName = path.basename(folderPath);
const denoJsonPath = path.join(folderPath, 'deno.json');
if (!fs.existsSync(denoJsonPath)) {
console.log(`⚠️ No deno.json found in ${folderName}`);
errorSummary.noDenoJson.push(folderName);
return false;
}
const nodeModulesPath = path.join(folderPath, 'node_modules');
if (fs.existsSync(nodeModulesPath)) {
fs.rmSync(nodeModulesPath, { recursive: true, force: true });
}
try {
console.log(`🔧 Installing dependencies for ${folderName}...`);
const entrypoint = fs.existsSync(path.join(folderPath, 'index.ts')) ? 'index.ts' : null;
if (entrypoint) {
execSync(`deno cache ${entrypoint}`, {
cwd: folderPath,
stdio: 'pipe',
encoding: 'utf8'
});
}
const result = execSync('deno install --node-modules-dir', {
cwd: folderPath,
stdio: 'pipe',
encoding: 'utf8'
});
console.log(`✅ Dependencies installed for ${folderName}`);
if (result.trim()) {
const lines = result.trim().split('\n');
const meaningfulLines = lines.filter(line =>
line.trim() &&
!line.includes('Download') &&
!line.includes('Check')
);
if (meaningfulLines.length > 0) {
console.log(` ℹ️ ${meaningfulLines.join(', ')}`);
}
}
const nmPath = path.join(folderPath, 'node_modules');
if (fs.existsSync(nmPath)) {
const nmContents = fs.readdirSync(nmPath).slice(0, 10);
console.log(` 📦 node_modules: ${nmContents.join(', ')}${nmContents.length >= 10 ? '...' : ''}`);
}
errorSummary.success.push(folderName);
return true;
} catch (error) {
console.error(`❌ Failed to install dependencies for ${folderName}`);
let errorMessage = error.message;
if (error.stderr) {
errorMessage = error.stderr.toString().trim() || error.message;
}
const errorLines = errorMessage.split('\n');
const mainError = errorLines.find(line =>
line.includes('error:') ||
line.includes('Error:') ||
line.includes('Failed')
) || errorLines[0];
console.error(` 💥 ${mainError}`);
errorSummary.installFailed.push({
folder: folderName,
error: mainError
});
return false;
}
}
function main() {
if (!FUNCTIONS_DIR) {
console.error('❌ Functions directory argument is required.');
console.error('Usage: ./install-deno-deps.js <functions-dir>');
process.exit(1);
}
console.log(`🔧 Installing Deno dependencies for all folders with deno.json in: ${FUNCTIONS_DIR}\n`);
const errorSummary = {
success: [],
noDenoJson: [],
installFailed: []
};
if (!fs.existsSync(FUNCTIONS_DIR)) {
console.error(`❌ Functions directory not found: ${FUNCTIONS_DIR}`);
process.exit(1);
}
const entries = fs.readdirSync(FUNCTIONS_DIR, { withFileTypes: true });
const folders = entries
.filter(entry => entry.isDirectory())
.map(entry => entry.name)
.filter(name => !name.startsWith('_'));
const foldersWithDenoJson = folders.filter(folderName => {
const folderPath = path.join(FUNCTIONS_DIR, folderName);
const denoJsonPath = path.join(folderPath, 'deno.json');
return fs.existsSync(denoJsonPath);
});
console.log(`Found ${foldersWithDenoJson.length} folders with deno.json files:\n`);
console.log(`${foldersWithDenoJson.join(', ')}\n`);
if (foldersWithDenoJson.length === 0) {
console.log(`No folders with deno.json files found in ${FUNCTIONS_DIR}.`);
process.exit(0);
}
foldersWithDenoJson.forEach(folderName => {
const folderPath = path.join(FUNCTIONS_DIR, folderName);
installDependencies(folderPath, errorSummary);
});
console.log('\n🎉 Dependency installation completed!');
console.log('\n📊 SUMMARY:');
if (errorSummary.success.length > 0) {
console.log(`✅ Successfully installed: ${errorSummary.success.length} folders`);
console.log(` ${errorSummary.success.join(', ')}`);
}
if (errorSummary.noDenoJson.length > 0) {
console.log(`\n⚠️ No deno.json found: ${errorSummary.noDenoJson.length} folders`);
console.log(` ${errorSummary.noDenoJson.join(', ')}`);
}
if (errorSummary.installFailed.length > 0) {
console.log(`\n❌ Installation failed: ${errorSummary.installFailed.length} folders`);
const errorGroups = {};
errorSummary.installFailed.forEach(({ folder, error }) => {
let errorType = error;
if (error.includes('Permission denied')) {
errorType = 'Permission denied';
} else if (error.includes('network')) {
errorType = 'Network error';
} else if (error.includes('not found')) {
errorType = 'Package not found';
} else if (error.includes('version')) {
errorType = 'Version conflict';
} else {
errorType = error.split('\n')[0].trim();
}
if (!errorGroups[errorType]) {
errorGroups[errorType] = [];
}
errorGroups[errorType].push(folder);
});
Object.entries(errorGroups).forEach(([errorType, folders]) => {
console.log(` ${errorType}`);
console.log(` Affected folders: ${folders.join(', ')}`);
});
}
const totalProcessed = foldersWithDenoJson.length;
const totalErrors = errorSummary.installFailed.length;
console.log(`\n📈 TOTALS:`);
console.log(` Folders with deno.json: ${totalProcessed}`);
console.log(` Successfully installed: ${errorSummary.success.length}`);
console.log(` Installation failures: ${totalErrors}`);
if (totalErrors === 0) {
console.log('\n🎊 All dependencies installed successfully!');
} else {
console.log(`\n🔧 ${totalErrors} folders need attention.`);
}
process.exit(totalErrors > 0 ? 1 : 0);
}
if (require.main === module) {
main();
}
module.exports = { installDependencies };