snapshot.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Test clients.
  67. for (const client of CLIENTS) {
  68. if (`clients:${client}:test` in pkg.scripts) {
  69. await executeStep(`test ${client} client`, async () => {
  70. await $`pnpm clients:${client}:test`;
  71. });
  72. }
  73. }
  74. // Additional JavaScript client tests.
  75. if (`clients:js:test` in pkg.scripts) {
  76. await executeStep(`lint js client`, async () => {
  77. cd(path.join(projectDirectory, 'clients', 'js'));
  78. await $`pnpm lint`;
  79. await $`pnpm format`;
  80. cd(projectDirectory);
  81. });
  82. }
  83. // Add line break between projects.
  84. echo('');
  85. }
  86. echo(chalk.green('All projects were created successfully!'));