juxscript 1.1.0 → 1.1.3

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.
@@ -0,0 +1,347 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import * as acorn from 'acorn';
4
+ import { simple } from 'acorn-walk';
5
+ import { glob } from 'glob';
6
+ import { loadAndStripTypes, isTypeScript } from './ts-shim.js';
7
+
8
+ const vendors = new Set();
9
+ const processedFiles = new Set();
10
+ const rels = new Map();
11
+ const allFiles = new Set();
12
+ const cssFiles = new Set();
13
+ const assetFiles = new Set();
14
+
15
+ // ✅ Define asset extensions to track
16
+ const ASSET_EXTENSIONS = [
17
+ '.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.bmp', '.ico',
18
+ '.mp4', '.mov', '.avi', '.webm', '.ogv',
19
+ '.mp3', '.wav', '.ogg', '.m4a',
20
+ '.woff', '.woff2', '.ttf', '.otf', '.eot',
21
+ '.json', '.xml', '.csv', '.txt',
22
+ '.pdf', '.doc', '.docx', '.zip'
23
+ ];
24
+
25
+ function countAllSourceFiles(directories) {
26
+ const allFiles = new Set();
27
+
28
+ for (const dir of directories) {
29
+ const dirPath = path.resolve(dir);
30
+ if (!fs.existsSync(dirPath)) {
31
+ console.warn(`⚠️ Directory not found: ${dirPath}`);
32
+ continue;
33
+ }
34
+
35
+ const files = findSourceFiles(dirPath);
36
+ files.forEach(f => allFiles.add(f));
37
+ }
38
+
39
+ return allFiles;
40
+ }
41
+
42
+ function findSourceFiles(dir, fileList = []) {
43
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
44
+
45
+ for (const entry of entries) {
46
+ const fullPath = path.join(dir, entry.name);
47
+
48
+ if (entry.isDirectory()) {
49
+ if (!entry.name.startsWith('.') && entry.name !== 'node_modules') {
50
+ findSourceFiles(fullPath, fileList);
51
+ }
52
+ } else if (entry.isFile() && (entry.name.endsWith('.jux') || entry.name.endsWith('.js') || entry.name.endsWith('.ts'))) {
53
+ fileList.push(fullPath);
54
+ }
55
+ }
56
+
57
+ return fileList;
58
+ }
59
+
60
+ const getAllCode = (srcDir) => {
61
+ const pattern = path.join(srcDir, '**/*.{js,jux,ts}');
62
+ const files = glob.sync(pattern);
63
+ const codeMap = {};
64
+
65
+ files.forEach(filePath => {
66
+ const rawCode = fs.readFileSync(filePath, 'utf-8');
67
+ const code = loadAndStripTypes(filePath, rawCode);
68
+ codeMap[filePath] = code;
69
+ });
70
+
71
+ return codeMap;
72
+ };
73
+
74
+ const findAllRels = (code, sourceFile) => {
75
+ try {
76
+ const ast = acorn.parse(code, {
77
+ ecmaVersion: 'latest',
78
+ sourceType: 'module'
79
+ });
80
+
81
+ simple(ast, {
82
+ ImportDeclaration(node) {
83
+ const { value } = node.source;
84
+ if (/^[./]/.test(value)) {
85
+ if (!rels.has(sourceFile)) {
86
+ rels.set(sourceFile, new Set());
87
+ }
88
+ rels.get(sourceFile).add(value);
89
+ }
90
+ },
91
+ CallExpression(node) {
92
+ if (node.callee.type === 'MemberExpression' &&
93
+ node.callee.object.name === 'jux' &&
94
+ node.callee.property.name === 'include' &&
95
+ node.arguments.length > 0 &&
96
+ node.arguments[0].type === 'Literal') {
97
+
98
+ const includePath = node.arguments[0].value;
99
+
100
+ if (!rels.has(sourceFile)) {
101
+ rels.set(sourceFile, new Set());
102
+ }
103
+ rels.get(sourceFile).add(includePath);
104
+ }
105
+ },
106
+ Literal(node) {
107
+ if (typeof node.value === 'string') {
108
+ const val = node.value;
109
+
110
+ if (/^[./]/.test(val)) {
111
+ const ext = path.extname(val).toLowerCase();
112
+
113
+ if (ASSET_EXTENSIONS.includes(ext)) {
114
+ if (!rels.has(sourceFile)) {
115
+ rels.set(sourceFile, new Set());
116
+ }
117
+ rels.get(sourceFile).add(val);
118
+ }
119
+ }
120
+ }
121
+ }
122
+ });
123
+ } catch (err) {
124
+ // Silent fail
125
+ }
126
+ };
127
+
128
+ const findVendors = (code) => {
129
+ try {
130
+ const ast = acorn.parse(code, {
131
+ ecmaVersion: 'latest',
132
+ sourceType: 'module'
133
+ });
134
+
135
+ simple(ast, {
136
+ ImportDeclaration(node) {
137
+ const { value } = node.source;
138
+ if (!/^[./]/.test(value)) {
139
+ vendors.add(value);
140
+ }
141
+ }
142
+ });
143
+ } catch (err) {
144
+ // Silent fail
145
+ }
146
+ };
147
+
148
+ function processCodeDirs(directories) {
149
+ const allCodeMaps = {};
150
+ for (const dir of directories) {
151
+ const codeMap = getAllCode(dir);
152
+ Object.assign(allCodeMaps, codeMap);
153
+ }
154
+
155
+ Object.keys(allCodeMaps).forEach(filePath => {
156
+ allFiles.add(path.resolve(filePath));
157
+ });
158
+
159
+ for (const filePath in allCodeMaps) {
160
+ if (!processedFiles.has(filePath)) {
161
+ const code = allCodeMaps[filePath];
162
+ findVendors(code);
163
+ findAllRels(code, filePath);
164
+ processedFiles.add(filePath);
165
+ }
166
+ }
167
+
168
+ resolveAllRelativeImports();
169
+ }
170
+
171
+ function resolveAllRelativeImports() {
172
+ const visited = new Set();
173
+ const toProcess = Array.from(rels.keys());
174
+
175
+ while (toProcess.length > 0) {
176
+ const currentFile = toProcess.shift();
177
+
178
+ if (visited.has(currentFile)) continue;
179
+ visited.add(currentFile);
180
+
181
+ allFiles.add(currentFile);
182
+
183
+ const imports = rels.get(currentFile);
184
+ if (!imports) continue;
185
+
186
+ const sourceDir = path.dirname(currentFile);
187
+
188
+ imports.forEach(relImport => {
189
+ const basePath = path.resolve(sourceDir, relImport);
190
+
191
+ const ext = path.extname(relImport).toLowerCase();
192
+
193
+ if (ext === '.css') {
194
+ if (fs.existsSync(basePath)) {
195
+ const absolutePath = path.resolve(basePath);
196
+ allFiles.add(absolutePath);
197
+ cssFiles.add(absolutePath);
198
+ }
199
+ return;
200
+ }
201
+
202
+ if (ASSET_EXTENSIONS.includes(ext)) {
203
+ if (fs.existsSync(basePath)) {
204
+ const absolutePath = path.resolve(basePath);
205
+ allFiles.add(absolutePath);
206
+ assetFiles.add(absolutePath);
207
+ }
208
+ return;
209
+ }
210
+
211
+ const extensions = ['', '.js', '.jux', '.ts'];
212
+
213
+ for (const extSuffix of extensions) {
214
+ const fullPath = basePath + extSuffix;
215
+
216
+ if (fs.existsSync(fullPath)) {
217
+ const absolutePath = path.resolve(fullPath);
218
+ allFiles.add(absolutePath);
219
+
220
+ if (!visited.has(fullPath) && !processedFiles.has(fullPath)) {
221
+ try {
222
+ const content = fs.readFileSync(fullPath, 'utf-8');
223
+ const code = loadAndStripTypes(fullPath, content);
224
+
225
+ findVendors(code);
226
+ findAllRels(code, fullPath);
227
+ processedFiles.add(fullPath);
228
+
229
+ if (rels.has(fullPath)) {
230
+ toProcess.push(fullPath);
231
+ }
232
+ } catch (err) {
233
+ // Silent fail
234
+ }
235
+ }
236
+
237
+ break;
238
+ }
239
+ }
240
+ });
241
+ }
242
+ }
243
+
244
+ // ✅ LEGACY EXPORTS (for backward compatibility)
245
+ export const analyzeVendors = (files) => {
246
+ vendors.clear();
247
+ for (const filePath in files) {
248
+ if (filePath.endsWith('.js') || filePath.endsWith('.jux') || filePath.endsWith('.ts')) {
249
+ const rawCode = files[filePath];
250
+ const code = loadAndStripTypes(filePath, rawCode);
251
+ findVendors(code);
252
+ }
253
+ }
254
+ return Array.from(vendors);
255
+ }
256
+
257
+ export const clearVendors = () => {
258
+ vendors.clear();
259
+ }
260
+
261
+ export const getVendors = () => {
262
+ return Array.from(vendors);
263
+ }
264
+
265
+ // ✅ NEW: Main analysis function that returns everything
266
+ export function analyzeProject(directories = ['./jux', './lib']) {
267
+ // Clear previous state
268
+ vendors.clear();
269
+ processedFiles.clear();
270
+ rels.clear();
271
+ allFiles.clear();
272
+ cssFiles.clear();
273
+ assetFiles.clear();
274
+
275
+ // Run analysis
276
+ processCodeDirs(directories);
277
+
278
+ // Return comprehensive analysis results
279
+ return {
280
+ vendors: Array.from(vendors),
281
+ allFiles: Array.from(allFiles),
282
+ cssFiles: Array.from(cssFiles),
283
+ assetFiles: Array.from(assetFiles),
284
+ relativeImports: rels,
285
+
286
+ // Categorized files
287
+ juxFiles: Array.from(allFiles).filter(f => f.endsWith('.jux')),
288
+ jsFiles: Array.from(allFiles).filter(f => f.endsWith('.js')),
289
+ tsFiles: Array.from(allFiles).filter(f => f.endsWith('.ts')),
290
+
291
+ // Asset breakdown
292
+ assets: {
293
+ images: Array.from(assetFiles).filter(f => {
294
+ const ext = path.extname(f).toLowerCase();
295
+ return ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.bmp', '.ico'].includes(ext);
296
+ }),
297
+ videos: Array.from(assetFiles).filter(f => {
298
+ const ext = path.extname(f).toLowerCase();
299
+ return ['.mp4', '.mov', '.avi', '.webm', '.ogv'].includes(ext);
300
+ }),
301
+ audio: Array.from(assetFiles).filter(f => {
302
+ const ext = path.extname(f).toLowerCase();
303
+ return ['.mp3', '.wav', '.ogg', '.m4a'].includes(ext);
304
+ }),
305
+ fonts: Array.from(assetFiles).filter(f => {
306
+ const ext = path.extname(f).toLowerCase();
307
+ return ['.woff', '.woff2', '.ttf', '.otf', '.eot'].includes(ext);
308
+ }),
309
+ data: Array.from(assetFiles).filter(f => {
310
+ const ext = path.extname(f).toLowerCase();
311
+ return ['.json', '.xml', '.csv', '.txt'].includes(ext);
312
+ })
313
+ },
314
+
315
+ // Summary stats
316
+ stats: {
317
+ totalFiles: allFiles.size,
318
+ totalVendors: vendors.size,
319
+ totalCSS: cssFiles.size,
320
+ totalAssets: assetFiles.size,
321
+ filesWithImports: rels.size
322
+ }
323
+ };
324
+ }
325
+
326
+ // ✅ Export constants
327
+ export { ASSET_EXTENSIONS };
328
+
329
+ export default {
330
+ analyzeProject,
331
+ analyzeVendors,
332
+ clearVendors,
333
+ getVendors,
334
+ ASSET_EXTENSIONS
335
+ };
336
+
337
+ function main() {
338
+ const analysis = analyzeProject(['./jux', './lib']);
339
+
340
+ console.log('External vendors:', analysis.vendors);
341
+ console.log('Total files:', analysis.stats.totalFiles);
342
+ }
343
+
344
+ // Only run main if executed directly
345
+ if (import.meta.url === `file://${process.argv[1]}`) {
346
+ main();
347
+ }