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.

add create command to scaffold projects from templates or GitHub repos

+93 -1
+1
include/cli/pkg.h
··· 16 16 int pkg_cmd_info(int argc, char **argv); 17 17 int pkg_cmd_ls(int argc, char **argv); 18 18 int pkg_cmd_cache(int argc, char **argv); 19 + int pkg_cmd_create(int argc, char **argv); 19 20 20 21 bool pkg_script_exists(const char *package_json_path, const char *script_name); 21 22
+1 -1
meson/ant.version
··· 1 - 0.6.1 1 + 0.6.2
+90
src/cli/pkg.c
··· 1425 1425 return EXIT_FAILURE; 1426 1426 } 1427 1427 } 1428 + 1429 + int pkg_cmd_create(int argc, char **argv) { 1430 + if (argc < 2) { 1431 + printf("Usage: ant create <template> [dest] [...flags]\n"); 1432 + printf(" ant create <github-org/repo> [dest] [...flags]\n\n"); 1433 + printf("Scaffold a new project from a template.\n\n"); 1434 + printf("Templates:\n"); 1435 + printf(" NPM: Runs 'ant x create-<template>' with given arguments\n"); 1436 + printf(" GitHub: Clones repository contents as template\n\n"); 1437 + printf("Environment variables:\n"); 1438 + printf(" GITHUB_TOKEN Supply a token for private repos or higher rate limits\n"); 1439 + return EXIT_SUCCESS; 1440 + } 1441 + 1442 + const char *template = argv[1]; 1443 + bool is_github = (strchr(template, '/') != NULL); 1444 + 1445 + if (is_github) { 1446 + const char *dest = NULL; 1447 + 1448 + for (int i = 2; i < argc; i++) { 1449 + if (argv[i][0] != '-') { dest = argv[i]; break; } 1450 + } 1451 + 1452 + if (!dest) { 1453 + const char *slash = strrchr(template, '/'); 1454 + dest = slash ? slash + 1 : template; 1455 + } 1456 + 1457 + struct stat st; 1458 + if (stat(dest, &st) == 0) { 1459 + fprintf(stderr, "Error: directory '%s' already exists\n", dest); 1460 + return EXIT_FAILURE; 1461 + } 1462 + 1463 + char url[1024]; 1464 + if (strncmp(template, "https://", 8) == 0 || strncmp(template, "git@", 4) == 0) { 1465 + snprintf(url, sizeof(url), "%s", template); 1466 + } else snprintf(url, sizeof(url), "https://github.com/%s.git", template); 1467 + 1468 + printf("%s+%s Creating project from %s%s%s...\n", C_GREEN, C_RESET, C_BOLD, template, C_RESET); 1469 + 1470 + char cmd[2048]; 1471 + snprintf(cmd, sizeof(cmd), "git clone --depth 1 %s %s", url, dest); 1472 + int ret = system(cmd); 1473 + if (ret != 0) { 1474 + fprintf(stderr, "Error: failed to clone %s\n", url); 1475 + return EXIT_FAILURE; 1476 + } 1477 + 1478 + char git_dir[1024]; 1479 + snprintf(git_dir, sizeof(git_dir), "%s/.git", dest); 1480 + 1481 + char rm_cmd[1024]; 1482 + snprintf(rm_cmd, sizeof(rm_cmd), "rm -rf %s", git_dir); 1483 + system(rm_cmd); 1484 + 1485 + if (stat(dest, &st) == 0) { 1486 + char pkg_json[1024]; 1487 + snprintf(pkg_json, sizeof(pkg_json), "%s/package.json", dest); 1488 + if (stat(pkg_json, &st) == 0) { 1489 + printf("\n%sDone!%s Created %s%s%s\n", C_GREEN, C_RESET, C_BOLD, dest, C_RESET); 1490 + printf("\n cd %s\n ant install\n\n", dest); 1491 + } else printf("\n%sDone!%s Created %s%s%s\n", C_GREEN, C_RESET, C_BOLD, dest, C_RESET); 1492 + } 1493 + 1494 + return EXIT_SUCCESS; 1495 + } 1496 + 1497 + char create_pkg[512]; 1498 + snprintf(create_pkg, sizeof(create_pkg), "create-%s", template); 1499 + 1500 + int new_argc = argc; 1501 + char **new_argv = malloc(sizeof(char*) * (new_argc + 1)); 1502 + if (!new_argv) { 1503 + fprintf(stderr, "Error: out of memory\n"); 1504 + return EXIT_FAILURE; 1505 + } 1506 + 1507 + new_argv[0] = argv[0]; 1508 + new_argv[1] = create_pkg; 1509 + 1510 + for (int i = 2; i < argc; i++) new_argv[i] = argv[i]; 1511 + new_argv[new_argc] = NULL; 1512 + 1513 + int ret = pkg_cmd_exec(new_argc, new_argv); 1514 + free(new_argv); 1515 + 1516 + return ret; 1517 + }
+1
src/main.c
··· 72 72 {"info", NULL, "Show package information from registry", pkg_cmd_info}, 73 73 {"ls", "list", "List installed packages", pkg_cmd_ls}, 74 74 {"cache", NULL, "Manage the package cache", pkg_cmd_cache}, 75 + {"create", NULL, "Scaffold a project from a template", pkg_cmd_create}, 75 76 {NULL, NULL, NULL, NULL} 76 77 }; 77 78