浏览代码

unified javacsript script

Joe Caulfield 11 月之前
父节点
当前提交
11947d3381
共有 8 个文件被更改,包括 107 次插入70 次删除
  1. 4 4
      package.json
  2. 2 2
      scripts/helpers/utils.mts
  3. 99 0
      scripts/js.mts
  4. 0 8
      scripts/js/format.mts
  5. 0 8
      scripts/js/lint.mts
  6. 0 35
      scripts/js/publish.mts
  7. 0 12
      scripts/js/test.mts
  8. 2 1
      scripts/rust.mts

+ 4 - 4
package.json

@@ -8,10 +8,10 @@
     "validator:start": "tsx ./scripts/helpers/start-validator.mts",
     "validator:restart": "pnpm validator:start --restart",
     "validator:stop": "tsx ./scripts/helpers/stop-validator.mts",
-    "js:format": "tsx ./scripts/js/format.mts",
-    "js:lint": "tsx ./scripts/js/lint.mts",
-    "js:publish": "tsx ./scripts/js/publish.mts",
-    "js:test": "tsx ./scripts/js/test.mts",
+    "js:format": "tsx ./scripts/js.mts format clients/js",
+    "js:lint": "tsx ./scripts/js.mts lint clients/js",
+    "js:publish": "tsx ./scripts/js.mts publish clients/js",
+    "js:test": "tsx ./scripts/js.mts test clients/js",
     "rust:format": "tsx ./scripts/rust.mts format clients/rust",
     "rust:lint": "tsx ./scripts/rust.mts lint clients/rust",
     "rust:lint:clippy": "tsx ./scripts/rust.mts lint-clippy clients/rust",

+ 2 - 2
scripts/helpers/utils.mts

@@ -161,7 +161,7 @@ export function parseCliArguments(): { manifestPath: string; args: string[] } {
   };
 }
 
-export function parseCliArgumentsV2(): { command: string, manifestPath: string; args: string[] } {
+export function parseCliArgumentsV2(): { command: string, libraryPath: string; args: string[] } {
   const command = process.argv[2];
   const args = process.argv.slice(3);
   
@@ -175,7 +175,7 @@ export function parseCliArgumentsV2(): { command: string, manifestPath: string;
 
   return {
     command,
-    manifestPath: path.join(workingDirectory, relativePath, 'Cargo.toml'),
+    libraryPath: path.join(workingDirectory, relativePath),
     args,
   };
 }

+ 99 - 0
scripts/js.mts

@@ -0,0 +1,99 @@
+#!/usr/bin/env zx
+
+// Script for working with JavaScript projects.
+
+import 'zx/globals';
+import {
+    parseCliArgumentsV2,
+    partitionArgumentsV2,
+} from './helpers/utils.mts';
+
+enum Command {
+    Format = 'format',
+    Lint = 'lint',
+    Test = 'test',
+    Publish = 'publish',
+}
+
+const { command, libraryPath, args } = parseCliArgumentsV2();
+
+async function pnpm(
+    command: string,
+    build = false,
+) {
+    const [pnpmArgs, commandArgs] = partitionArgumentsV2(args, '--');
+    cd(libraryPath);
+    await $`pnpm install`;
+    if (build) {
+        await $`pnpm build`;
+    }
+    await $`pnpm ${command} ${pnpmArgs} -- ${commandArgs}`;
+}
+
+async function format() {
+    return pnpm('format');
+}
+
+async function lint() {
+    return pnpm('lint');
+}
+
+async function test() {
+    // Start the local validator, or restart it if it is already running.
+    await $`pnpm validator:restart`;
+
+    // Build the client and run the tests.
+    return pnpm('test', true);
+}
+
+async function publish() {
+    const [level, tag = 'latest'] = args;
+    if (!level) {
+      throw new Error('A version level — e.g. "path" — must be provided.');
+    }
+
+    // Go to the directory and install the dependencies.
+    cd(libraryPath);
+    await $`pnpm install`;
+
+    // Update the version.
+    const versionArgs = [
+        '--no-git-tag-version',
+        ...(level.startsWith('pre') ? [`--preid ${tag}`] : []),
+    ];
+    let { stdout } = await $`pnpm version ${level} ${versionArgs}`;
+    const newVersion = stdout.slice(1).trim();
+
+    // Expose the new version to CI if needed.
+    if (process.env.CI) {
+        await $`echo "new_version=${newVersion}" >> $GITHUB_OUTPUT`;
+    }
+    
+    // Publish the package.
+    // This will also build the package before publishing (see prepublishOnly script).
+    await $`pnpm publish --no-git-checks --tag ${tag}`;
+    
+    // Commit the new version.
+    await $`git commit -am "Publish JS client v${newVersion}"`;
+    
+    // Tag the new version.
+    await $`git tag -a js@v${newVersion} -m "JS client v${newVersion}"`;
+}
+
+
+switch (command) {
+    case Command.Format:
+        await format();
+        break;
+    case Command.Lint:
+        await lint();
+        break;
+    case Command.Test:
+        await test();
+        break;
+    case Command.Publish:
+        await publish();
+        break;
+    default:
+        throw new Error(`Unknown command: ${command}`);
+}

+ 0 - 8
scripts/js/format.mts

@@ -1,8 +0,0 @@
-#!/usr/bin/env zx
-import 'zx/globals';
-import { cliArguments, workingDirectory } from '../helpers/utils.mts';
-
-// Format the client using Prettier.
-cd(path.join(workingDirectory, 'clients', 'js'));
-await $`pnpm install`;
-await $`pnpm format ${cliArguments()}`;

+ 0 - 8
scripts/js/lint.mts

@@ -1,8 +0,0 @@
-#!/usr/bin/env zx
-import 'zx/globals';
-import { cliArguments, workingDirectory } from '../helpers/utils.mts';
-
-// Check the client using ESLint.
-cd(path.join(workingDirectory, 'clients', 'js'));
-await $`pnpm install`;
-await $`pnpm lint ${cliArguments()}`;

+ 0 - 35
scripts/js/publish.mts

@@ -1,35 +0,0 @@
-#!/usr/bin/env zx
-import 'zx/globals';
-import { cliArguments, workingDirectory } from '../helpers/utils.mts';
-
-const [level, tag = 'latest'] = cliArguments();
-if (!level) {
-  throw new Error('A version level — e.g. "path" — must be provided.');
-}
-
-// Go to the client directory and install the dependencies.
-cd(path.join(workingDirectory, 'clients', 'js'));
-await $`pnpm install`;
-
-// Update the version.
-const versionArgs = [
-  '--no-git-tag-version',
-  ...(level.startsWith('pre') ? [`--preid ${tag}`] : []),
-];
-let { stdout } = await $`pnpm version ${level} ${versionArgs}`;
-const newVersion = stdout.slice(1).trim();
-
-// Expose the new version to CI if needed.
-if (process.env.CI) {
-  await $`echo "new_version=${newVersion}" >> $GITHUB_OUTPUT`;
-}
-
-// Publish the package.
-// This will also build the package before publishing (see prepublishOnly script).
-await $`pnpm publish --no-git-checks --tag ${tag}`;
-
-// Commit the new version.
-await $`git commit -am "Publish JS client v${newVersion}"`;
-
-// Tag the new version.
-await $`git tag -a js@v${newVersion} -m "JS client v${newVersion}"`;

+ 0 - 12
scripts/js/test.mts

@@ -1,12 +0,0 @@
-#!/usr/bin/env zx
-import 'zx/globals';
-import { cliArguments, workingDirectory } from '../helpers/utils.mts';
-
-// Start the local validator, or restart it if it is already running.
-await $`pnpm validator:restart`;
-
-// Build the client and run the tests.
-cd(path.join(workingDirectory, 'clients', 'js'));
-await $`pnpm install`;
-await $`pnpm build`;
-await $`pnpm test ${cliArguments()}`;

+ 2 - 1
scripts/rust.mts

@@ -23,7 +23,8 @@ enum Command {
     Publish = 'publish',
 }
 
-const { command, manifestPath, args } = parseCliArgumentsV2();
+const { command, libraryPath, args } = parseCliArgumentsV2();
+const manifestPath = path.join(libraryPath, 'Cargo.toml');
 
 async function cargo(
     toolchain: string,