Selaa lähdekoodia

Merge test and snapshot scripts

Loris Leiva 1 vuosi sitten
vanhempi
sitoutus
13162b45f4
3 muutettua tiedostoa jossa 75 lisäystä ja 52 poistoa
  1. 2 2
      package.json
  2. 73 13
      scripts/snapshot.mjs
  3. 0 37
      scripts/test.mjs

+ 2 - 2
package.json

@@ -17,9 +17,9 @@
   "scripts": {
     "format": "prettier --write .",
     "build": "zx ./scripts/build.mjs",
+    "presnapshot": "pnpm build",
     "snapshot": "zx ./scripts/snapshot.mjs",
-    "pretest": "pnpm build snapshot",
-    "test": "zx ./scripts/test.mjs"
+    "test": "pnpm snapshot --test"
   },
   "repository": {
     "type": "git",

+ 73 - 13
scripts/snapshot.mjs

@@ -1,13 +1,25 @@
 #!/usr/bin/env zx
 import "zx/globals";
 
-// $.verbose = false;
+$.verbose = false;
 
-const projects = {
+const CLIENTS = ["js", "rust"];
+const PROJECTS = {
   "counter-shank": ["--shank"],
   "counter-anchor": ["--shank"],
 };
 
+// Parse CLI arguments.
+const selectedProjects = argv._;
+const projects =
+  selectedProjects.length > 0
+    ? Object.keys(PROJECTS).filter((project) =>
+        selectedProjects.includes(project)
+      )
+    : Object.keys(PROJECTS);
+const runTests = !!argv.test;
+
+// Resolve paths.
 const bin = path.resolve(__dirname, "../outfile.cjs");
 const submodulesDirectory = path.resolve(__dirname, "../submodules/");
 
@@ -15,7 +27,7 @@ if (!fs.existsSync(submodulesDirectory)) {
   fs.mkdirSync(submodulesDirectory);
 }
 
-for (const projectName in projects) {
+for (const projectName of projects) {
   // Go the submodules directory before creating each project.
   cd(submodulesDirectory);
 
@@ -25,24 +37,72 @@ for (const projectName in projects) {
     fs.rmSync(projectName, { recursive: true, force: true });
   }
 
-  // Create the project.
-  const verb = projectExists ? "Re-creating" : "Creating";
-  echo(`${verb} project ${projectName}...`);
-  const projectArgs = projects[projectName];
-  await $`node ${[bin, projectName, ...projectArgs, "--force", "--default"]}`;
+  // Log project start.
+  echo(chalk.blue(chalk.bold(`${projectName}:`)));
+
+  // Scaffold the project.
+  const args = [projectName, ...PROJECTS[projectName]];
+  await executeStep(
+    "scaffold the project",
+    () => $`node ${[bin, ...args, "--force", "--default"]}`
+  );
 
   // Go inside the created project.
   const projectDirectory = path.resolve(submodulesDirectory, projectName);
   cd(projectDirectory);
   const pkg = require(path.resolve(projectDirectory, "package.json"));
 
-  // Install the project's dependencies.
-  await $`pnpm install`;
+  // Install project's dependencies.
+  await executeStep("install NPM dependencies", async () => {
+    await $`pnpm install`;
+  });
+
+  // Generate IDLs.
+  if ("generate:idls" in pkg.scripts) {
+    await executeStep("generate IDLs", async () => {
+      await $`pnpm generate:idls`;
+    });
+  }
 
-  // Generate the clients.
-  if ("generate" in pkg.scripts) {
-    await $`pnpm generate`;
+  // Generate clients.
+  if ("generate:clients" in pkg.scripts) {
+    await executeStep("generate clients", async () => {
+      await $`pnpm generate:clients`;
+    });
   }
+
+  if (runTests) {
+    // Test programs.
+    if ("programs:test" in pkg.scripts) {
+      await executeStep("test programs", async () => {
+        await $`pnpm programs:test`;
+      });
+    }
+
+    // Test clients.
+    for (const client of CLIENTS) {
+      if (`clients:${client}:test` in pkg.scripts) {
+        await executeStep(`test ${client} clients`, async () => {
+          await $`pnpm clients:${client}:test`;
+        });
+      }
+    }
+  }
+
+  // Add line break between projects.
+  echo("");
 }
 
 echo(chalk.green("All projects were created successfully!"));
+
+async function executeStep(title, fn) {
+  try {
+    const capitalizedTitle = title.charAt(0).toUpperCase() + title.slice(1);
+    await spinner(`${capitalizedTitle}...`, fn);
+    echo(chalk.green("✔︎") + ` ${capitalizedTitle}.`);
+  } catch (e) {
+    echo(chalk.red("✘") + ` Failed to ${title}.\n`);
+    echo(e);
+    process.exit(1);
+  }
+}

+ 0 - 37
scripts/test.mjs

@@ -1,37 +0,0 @@
-#!/usr/bin/env zx
-import "zx/globals";
-
-const submodulesDirectory = path.resolve(__dirname, "../submodules/");
-const clients = ["js", "rust"];
-
-let projects = fs
-  .readdirSync(submodulesDirectory, { withFileTypes: true })
-  .filter((dirent) => dirent.isDirectory())
-  .map((dirent) => dirent.name)
-  .filter((name) => !name.startsWith(".") && name !== "node_modules");
-
-if (process.argv[2])
-  projects = projects.filter((project) => project.includes(process.argv[2]));
-
-cd(submodulesDirectory);
-console.log("Installing playground dependencies");
-await $`pnpm install`;
-
-for (const projectName of projects) {
-  // Go inside the project.
-  const projectDirectory = path.resolve(submodulesDirectory, projectName);
-  cd(projectDirectory);
-  const pkg = require(path.resolve(projectDirectory, "package.json"));
-
-  // Test programs.
-  if ("programs:test" in pkg.scripts) {
-    await $`pnpm programs:test`;
-  }
-
-  // Test clients.
-  for (const client of clients) {
-    if (`clients:${client}:test` in pkg.scripts) {
-      await $`pnpm clients:${client}:test`;
-    }
-  }
-}