snapshot.mjs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env zx
  2. import "zx/globals";
  3. import { CLIENTS, PROJECTS, executeStep } from "./utils.mjs";
  4. $.verbose = false;
  5. // Parse CLI arguments.
  6. const selectedProjects = argv._;
  7. const projects =
  8. selectedProjects.length > 0
  9. ? Object.keys(PROJECTS).filter((project) =>
  10. selectedProjects.includes(project)
  11. )
  12. : Object.keys(PROJECTS);
  13. const runTests = !!argv.test;
  14. const scaffoldOnly = !!argv["scaffold-only"];
  15. // Resolve paths.
  16. const bin = path.resolve(__dirname, "../outfile.cjs");
  17. const projectsDirectory = path.resolve(__dirname, "../projects/");
  18. if (!fs.existsSync(projectsDirectory)) {
  19. fs.mkdirSync(projectsDirectory);
  20. }
  21. for (const projectName of projects) {
  22. // Go the submodules directory before creating each project.
  23. cd(projectsDirectory);
  24. // Log project start.
  25. echo(chalk.blue(chalk.bold(`${projectName}:`)));
  26. // Scaffold the project.
  27. const args = [projectName, ...PROJECTS[projectName]];
  28. await executeStep(
  29. "scaffold the project",
  30. () => $`node ${[bin, ...args, "--force", "--default"]}`
  31. );
  32. if (scaffoldOnly) continue;
  33. // Go inside the created project.
  34. const projectDirectory = path.resolve(projectsDirectory, projectName);
  35. cd(projectDirectory);
  36. const pkg = await fs.readJSON(path.resolve(projectDirectory, "package.json"));
  37. // Install project's dependencies.
  38. await executeStep("install NPM dependencies", async () => {
  39. await $`pnpm install`;
  40. });
  41. // Generate IDLs.
  42. if ("generate:idls" in pkg.scripts) {
  43. await executeStep("generate IDLs", async () => {
  44. await $`pnpm generate:idls`;
  45. });
  46. }
  47. // Generate clients.
  48. if ("generate:clients" in pkg.scripts) {
  49. await executeStep("generate clients", async () => {
  50. await $`pnpm generate:clients`;
  51. });
  52. }
  53. // Build programs.
  54. if ("programs:build" in pkg.scripts) {
  55. await executeStep("build programs", async () => {
  56. await $`pnpm programs:build`;
  57. });
  58. }
  59. if (!runTests) continue;
  60. // Test programs.
  61. if ("programs:test" in pkg.scripts) {
  62. await executeStep("test programs", async () => {
  63. await $`pnpm programs:test`;
  64. });
  65. }
  66. // Lint and test clients.
  67. for (const client of CLIENTS) {
  68. if (`clients:${client}:test` in pkg.scripts) {
  69. await executeStep(`lint ${client} client`, async () => {
  70. await $`pnpm clients:${client}:lint`;
  71. });
  72. await executeStep(`test ${client} client`, async () => {
  73. await $`pnpm clients:${client}:test`;
  74. });
  75. }
  76. }
  77. // Add line break between projects.
  78. echo("");
  79. }
  80. echo(chalk.green("All projects were created successfully!"));