MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

improve global listing code

+83 -140
+11 -2
include/pkg.h
··· 114 114 } pkg_lifecycle_script_t; 115 115 116 116 uint32_t pkg_get_added_count(const pkg_context_t *ctx); 117 + uint32_t pkg_count_installed(const char *node_modules_path); 118 + uint32_t pkg_get_lifecycle_script_count(const pkg_context_t *ctx); 117 119 118 120 pkg_error_t pkg_discover_lifecycle_scripts( 119 121 pkg_context_t *ctx, 120 122 const char *node_modules_path 121 123 ); 122 - 123 - uint32_t pkg_get_lifecycle_script_count(const pkg_context_t *ctx); 124 124 125 125 pkg_error_t pkg_get_lifecycle_script( 126 126 const pkg_context_t *ctx, ··· 315 315 void *user_data 316 316 ); 317 317 318 + uint32_t pkg_count_global(pkg_context_t *ctx); 319 + uint32_t pkg_count_local(pkg_context_t *ctx); 320 + 318 321 pkg_error_t pkg_list_global( 322 + pkg_context_t *ctx, 323 + pkg_global_list_callback callback, 324 + void *user_data 325 + ); 326 + 327 + pkg_error_t pkg_list_local( 319 328 pkg_context_t *ctx, 320 329 pkg_global_list_callback callback, 321 330 void *user_data
+40 -104
src/cli/pkg.c
··· 1255 1255 1256 1256 typedef struct { 1257 1257 int count; 1258 - } global_ls_ctx_t; 1258 + int total; 1259 + } pkg_ls_ctx_t; 1259 1260 1260 - static void print_global_package(const char *name, const char *version, void *user_data) { 1261 - global_ls_ctx_t *ctx = (global_ls_ctx_t *)user_data; 1262 - printf(" %s%s%s@%s%s%s\n", C_BOLD, name, C_RESET, C_DIM, version, C_RESET); 1261 + static void print_pkg_cb(const char *name, const char *version, void *user_data) { 1262 + pkg_ls_ctx_t *ctx = (pkg_ls_ctx_t *)user_data; 1263 1263 ctx->count++; 1264 + const char *prefix = (ctx->count == ctx->total) ? "โ””โ”€โ”€" : "โ”œโ”€โ”€"; 1265 + printf("%s%s%s %s%s%s@%s\n", C_DIM, prefix, C_RESET, C_BOLD, name, C_RESET, version); 1264 1266 } 1265 1267 1266 - static int cmd_ls_global(void) { 1268 + static int cmd_ls(bool is_global) { 1267 1269 pkg_options_t opts = { .verbose = pkg_verbose }; 1268 1270 pkg_context_t *ctx = pkg_init(&opts); 1269 1271 if (!ctx) { ··· 1271 1273 return EXIT_FAILURE; 1272 1274 } 1273 1275 1274 - printf("%sGlobal packages%s:\n", C_BOLD, C_RESET); 1275 - 1276 - global_ls_ctx_t ls_ctx = { .count = 0 }; 1277 - pkg_error_t err = pkg_list_global(ctx, print_global_package, &ls_ctx); 1276 + char path_buf[PATH_MAX]; 1277 + const char *base_path; 1278 + const char *nm_path; 1279 + char nm_full_path[PATH_MAX]; 1278 1280 1279 - if (err == PKG_NOT_FOUND || ls_ctx.count == 0) { 1280 - printf(" (none)\n"); 1281 - } else if (err != PKG_OK) { 1282 - fprintf(stderr, "Error: %s\n", pkg_error_string(ctx)); 1283 - pkg_free(ctx); 1284 - return EXIT_FAILURE; 1281 + if (is_global) { 1282 + base_path = get_global_dir(); 1283 + snprintf(nm_full_path, sizeof(nm_full_path), "%s/node_modules", base_path); 1284 + nm_path = nm_full_path; 1285 1285 } else { 1286 - printf("\n%s%d%s package%s\n", C_GREEN, ls_ctx.count, C_RESET, ls_ctx.count == 1 ? "" : "s"); 1287 - } 1288 - 1289 - pkg_free(ctx); 1290 - return EXIT_SUCCESS; 1291 - } 1292 - 1293 - static int cmd_ls(const char *nm_path, bool is_global) { 1294 - struct stat st; 1295 - if (stat(nm_path, &st) != 0) { 1296 - if (is_global) { 1297 - printf("No global packages installed.\n"); 1298 - } else { 1299 - printf("No packages installed. Run 'ant install' first.\n"); 1286 + if (!getcwd(path_buf, sizeof(path_buf))) { 1287 + fprintf(stderr, "Error: Could not get current directory\n"); 1288 + pkg_free(ctx); 1289 + return EXIT_FAILURE; 1300 1290 } 1301 - return EXIT_SUCCESS; 1291 + base_path = path_buf; 1292 + nm_path = "node_modules"; 1302 1293 } 1303 1294 1304 - if (is_global) { 1305 - printf("%sGlobal packages%s:\n", C_BOLD, C_RESET); 1306 - } else { 1307 - printf("%sInstalled packages%s:\n", C_BOLD, C_RESET); 1308 - } 1295 + uint32_t direct = is_global ? pkg_count_global(ctx) : pkg_count_local(ctx); 1296 + uint32_t total = pkg_count_installed(nm_path); 1309 1297 1310 - ls_ctx_t ctx = { .count = 0, .show_path = false, .nm_path = nm_path }; 1298 + printf("%s%s/node_modules%s", C_DIM, base_path, C_RESET); 1311 1299 1312 - if (is_global) { 1313 - const char *global_dir = get_global_dir(); 1314 - char pkg_json_path[4096]; 1315 - snprintf(pkg_json_path, sizeof(pkg_json_path), "%s/package.json", global_dir); 1316 - 1317 - yyjson_doc *doc = yyjson_read_file(pkg_json_path, 0, NULL, NULL); 1318 - if (!doc) { 1319 - printf(" (none)\n"); 1320 - return EXIT_SUCCESS; 1321 - } 1322 - 1323 - yyjson_val *root = yyjson_doc_get_root(doc); 1324 - yyjson_val *deps = yyjson_obj_get(root, "dependencies"); 1325 - 1326 - if (!deps || !yyjson_is_obj(deps)) { 1327 - yyjson_doc_free(doc); 1328 - printf(" (none)\n"); 1329 - return EXIT_SUCCESS; 1330 - } 1331 - 1332 - yyjson_obj_iter iter; 1333 - yyjson_obj_iter_init(deps, &iter); 1334 - yyjson_val *key; 1335 - while ((key = yyjson_obj_iter_next(&iter)) != NULL) { 1336 - const char *pkg_name = yyjson_get_str(key); 1337 - if (pkg_name) print_ls_package(pkg_name, &ctx); 1338 - } 1339 - 1340 - yyjson_doc_free(doc); 1341 - } else { 1342 - DIR *dir = opendir(nm_path); 1343 - if (!dir) { 1344 - printf(" (none)\n"); 1345 - return EXIT_SUCCESS; 1346 - } 1347 - 1348 - struct dirent *entry; 1349 - while ((entry = readdir(dir)) != NULL) { 1350 - if (entry->d_name[0] == '.') continue; 1351 - 1352 - if (entry->d_name[0] == '@') { 1353 - char scope_path[4096]; 1354 - snprintf(scope_path, sizeof(scope_path), "%s/%s", nm_path, entry->d_name); 1355 - DIR *scope_dir = opendir(scope_path); 1356 - if (scope_dir) { 1357 - struct dirent *scoped; 1358 - while ((scoped = readdir(scope_dir)) != NULL) { 1359 - if (scoped->d_name[0] == '.') continue; 1360 - char full_name[512]; 1361 - snprintf(full_name, sizeof(full_name), "%s/%s", entry->d_name, scoped->d_name); 1362 - print_ls_package(full_name, &ctx); 1363 - } 1364 - closedir(scope_dir); 1365 - } 1366 - } else { 1367 - print_ls_package(entry->d_name, &ctx); 1368 - } 1369 - } 1370 - closedir(dir); 1300 + if (direct == 0) { 1301 + printf("\n (no package.json)\n"); 1302 + pkg_free(ctx); 1303 + return EXIT_SUCCESS; 1371 1304 } 1372 1305 1373 - if (ctx.count == 0) { 1374 - printf(" (none)\n"); 1375 - } else { 1376 - printf("\n%s%d%s package%s\n", C_GREEN, ctx.count, C_RESET, ctx.count == 1 ? "" : "s"); 1306 + if (total == 0) { 1307 + printf("\n (empty)\n"); 1308 + pkg_free(ctx); 1309 + return EXIT_SUCCESS; 1377 1310 } 1378 1311 1312 + printf(" %s(%u)%s\n", C_DIM, total, C_RESET); 1313 + 1314 + pkg_ls_ctx_t ls_ctx = { .count = 0, .total = (int)direct }; 1315 + if (is_global) pkg_list_global(ctx, print_pkg_cb, &ls_ctx); 1316 + else pkg_list_local(ctx, print_pkg_cb, &ls_ctx); 1317 + 1318 + pkg_free(ctx); 1379 1319 return EXIT_SUCCESS; 1380 1320 } 1381 1321 ··· 1395 1335 } else if (nerrors > 0) { 1396 1336 arg_print_errors(stdout, end, "ant ls"); 1397 1337 exitcode = EXIT_FAILURE; 1398 - } else if (global->count > 0) { 1399 - exitcode = cmd_ls_global(); 1400 - } else { 1401 - exitcode = cmd_ls("node_modules", false); 1402 - } 1338 + } else exitcode = cmd_ls(global->count > 0); 1403 1339 1404 1340 arg_freetable(argtable, sizeof(argtable)/sizeof(argtable[0])); 1405 1341 return exitcode;
+32 -34
src/pkg/root.zig
··· 1 1 const std = @import("std"); 2 2 const builtin = @import("builtin"); 3 3 4 + pub const cli = @import("cli.zig"); 4 5 pub const lockfile = @import("lockfile.zig"); 5 6 pub const cache = @import("cache.zig"); 6 7 pub const fetcher = @import("fetcher.zig"); ··· 587 588 if (index >= c.added_packages.items.len) return .invalid_argument; 588 589 out.* = c.added_packages.items[index]; 589 590 return .ok; 591 + } 592 + 593 + export fn pkg_count_installed(node_modules_path: [*:0]const u8) u32 { 594 + var dir = std.fs.cwd().openDir(std.mem.span(node_modules_path), .{ .iterate = true }) catch return 0; 595 + defer dir.close(); return cli.count_dir(dir, true); 590 596 } 591 597 592 598 export fn pkg_discover_lifecycle_scripts( ··· 2720 2726 return .ok; 2721 2727 } 2722 2728 2723 - export fn pkg_list_global( 2729 + export fn pkg_count_local(ctx: ?*PkgContext) u32 { 2730 + var pd = cli.get_dependencies(ctx, null, true) orelse return 0; 2731 + defer pd.deinit(); return pd.count(); 2732 + } 2733 + 2734 + export fn pkg_count_global(ctx: ?*PkgContext) u32 { 2735 + const global_dir = getGlobalDir(global_allocator) catch return 0; 2736 + var pd = cli.get_dependencies(ctx, global_dir, false) orelse return 0; 2737 + defer pd.deinit(); return pd.count(); 2738 + } 2739 + 2740 + export fn pkg_list_local( 2724 2741 ctx: ?*PkgContext, 2725 2742 callback: ?*const fn (name: [*:0]const u8, version: [*:0]const u8, user_data: ?*anyopaque) callconv(.c) void, 2726 2743 user_data: ?*anyopaque, 2727 2744 ) PkgError { 2728 - const c = ctx orelse return .invalid_argument; 2729 - _ = c.arena_state.reset(.retain_capacity); 2730 - const arena_alloc = c.arena_state.allocator(); 2745 + var pd = cli.get_dependencies(ctx, null, true) orelse return .ok; 2746 + defer pd.deinit(); 2747 + if (callback) |cb| cli.list_dependencies(&pd, cb, user_data); 2748 + return .ok; 2749 + } 2731 2750 2732 - const global_dir = getGlobalDir(arena_alloc) catch return .invalid_argument; 2733 - const pkg_json_path = std.fmt.allocPrint(arena_alloc, "{s}/package.json", .{global_dir}) catch return .out_of_memory; 2734 - const nm_path = std.fmt.allocPrint(arena_alloc, "{s}/node_modules", .{global_dir}) catch return .out_of_memory; 2735 - 2736 - const content = std.fs.cwd().readFileAlloc(arena_alloc, pkg_json_path, 1024 * 1024) catch return .not_found; 2737 - 2738 - const parsed = std.json.parseFromSlice(std.json.Value, arena_alloc, content, .{}) catch return .invalid_argument; 2739 - defer parsed.deinit(); 2740 - 2741 - const deps = parsed.value.object.get("dependencies") orelse return .ok; 2742 - if (deps != .object) return .ok; 2743 - 2744 - const cb = callback orelse return .ok; 2745 - 2746 - for (deps.object.keys()) |dep_name| { 2747 - const dep_pkg_json = std.fmt.allocPrint(arena_alloc, "{s}/{s}/package.json", .{nm_path, dep_name}) catch continue; 2748 - 2749 - const dep_content = std.fs.cwd().readFileAlloc(arena_alloc, dep_pkg_json, 256 * 1024) catch continue; 2750 - const dep_parsed = std.json.parseFromSlice(std.json.Value, arena_alloc, dep_content, .{}) catch continue; 2751 - defer dep_parsed.deinit(); 2752 - 2753 - const version = if (dep_parsed.value.object.get("version")) |v| 2754 - if (v == .string) v.string else "?" else "?"; 2755 - 2756 - const name_z = arena_alloc.dupeZ(u8, dep_name) catch continue; 2757 - const version_z = arena_alloc.dupeZ(u8, version) catch continue; 2758 - 2759 - cb(name_z.ptr, version_z.ptr, user_data); 2760 - } 2761 - 2751 + export fn pkg_list_global( 2752 + ctx: ?*PkgContext, 2753 + callback: ?*const fn (name: [*:0]const u8, version: [*:0]const u8, user_data: ?*anyopaque) callconv(.c) void, 2754 + user_data: ?*anyopaque, 2755 + ) PkgError { 2756 + const global_dir = getGlobalDir(global_allocator) catch return .invalid_argument; 2757 + var pd = cli.get_dependencies(ctx, global_dir, false) orelse return .ok; 2758 + defer pd.deinit(); 2759 + if (callback) |cb| cli.list_dependencies(&pd, cb, user_data); 2762 2760 return .ok; 2763 2761 }