Skip to content

Commit bac0950

Browse files
authored
unify rust and js scripts (#32)
* unified rust script * unified javacsript script * rename helpers
1 parent f1c7cc1 commit bac0950

File tree

16 files changed

+295
-251
lines changed

16 files changed

+295
-251
lines changed

package.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@
88
"validator:start": "tsx ./scripts/helpers/start-validator.mts",
99
"validator:restart": "pnpm validator:start --restart",
1010
"validator:stop": "tsx ./scripts/helpers/stop-validator.mts",
11-
"js:format": "tsx ./scripts/js/format.mts",
12-
"js:lint": "tsx ./scripts/js/lint.mts",
13-
"js:publish": "tsx ./scripts/js/publish.mts",
14-
"js:test": "tsx ./scripts/js/test.mts",
15-
"rust:format": "tsx ./scripts/rust/format.mts clients/rust",
16-
"rust:lint": "tsx ./scripts/rust/lint.mts clients/rust",
17-
"rust:lint:clippy": "tsx ./scripts/rust/lint-clippy.mts clients/rust",
18-
"rust:lint:docs": "tsx ./scripts/rust/lint-docs.mts clients/rust",
19-
"rust:lint:features": "tsx ./scripts/rust/lint-features.mts clients/rust",
20-
"rust:publish": "tsx ./scripts/rust/publish.mts clients/rust",
21-
"rust:test": "tsx ./scripts/rust/test.mts clients/rust",
22-
"interface:format": "tsx ./scripts/rust/format.mts interface",
23-
"interface:lint": "tsx ./scripts/rust/lint.mts interface",
24-
"interface:lint:clippy": "tsx ./scripts/rust/lint-clippy.mts interface",
25-
"interface:lint:docs": "tsx ./scripts/rust/lint-docs.mts interface",
26-
"interface:lint:features": "tsx ./scripts/rust/lint-features.mts interface",
27-
"interface:publish": "tsx ./scripts/rust/publish.mts interface",
28-
"interface:test": "tsx ./scripts/rust/test.mts interface",
29-
"interface:wasm": "tsx ./scripts/rust/wasm.mts interface",
11+
"js:format": "tsx ./scripts/js.mts format clients/js",
12+
"js:lint": "tsx ./scripts/js.mts lint clients/js",
13+
"js:publish": "tsx ./scripts/js.mts publish clients/js",
14+
"js:test": "tsx ./scripts/js.mts test clients/js",
15+
"rust:format": "tsx ./scripts/rust.mts format clients/rust",
16+
"rust:lint": "tsx ./scripts/rust.mts lint clients/rust",
17+
"rust:lint:clippy": "tsx ./scripts/rust.mts lint-clippy clients/rust",
18+
"rust:lint:docs": "tsx ./scripts/rust.mts lint-docs clients/rust",
19+
"rust:lint:features": "tsx ./scripts/rust.mts lint-features clients/rust",
20+
"rust:publish": "tsx ./scripts/rust.mts publish clients/rust",
21+
"rust:test": "tsx ./scripts/rust.mts test clients/rust",
22+
"interface:format": "tsx ./scripts/rust.mts format interface",
23+
"interface:lint": "tsx ./scripts/rust.mts lint interface",
24+
"interface:lint:clippy": "tsx ./scripts/rust.mts lint-clippy interface",
25+
"interface:lint:docs": "tsx ./scripts/rust.mts lint-docs interface",
26+
"interface:lint:features": "tsx ./scripts/rust.mts lint-features interface",
27+
"interface:publish": "tsx ./scripts/rust.mts publish interface",
28+
"interface:test": "tsx ./scripts/rust.mts test interface",
29+
"interface:wasm": "tsx ./scripts/rust.mts wasm interface",
3030
"template:upgrade": "tsx ./scripts/helpers/upgrade-template.ts"
3131
},
3232
"devDependencies": {

scripts/helpers/utils.mts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ export function partitionArguments(
119119
: [args, []];
120120
}
121121

122+
export function partitionArgumentsWithDefaultArgs(
123+
args: string[],
124+
delimiter: string,
125+
defaultArgs?: string[],
126+
): [string[], string[]] {
127+
const [providedCargoArgs, providedCommandArgs] = partitionArguments(args, delimiter);
128+
if (defaultArgs) {
129+
const [defaultCargoArgs, defaultCommandArgs] = partitionArguments(defaultArgs, delimiter);
130+
return [
131+
[...defaultCargoArgs, ...providedCargoArgs],
132+
[...defaultCommandArgs, ...providedCommandArgs],
133+
];
134+
}
135+
return [providedCargoArgs, providedCommandArgs];
136+
}
137+
122138
export async function getInstalledSolanaVersion(): Promise<string | undefined> {
123139
try {
124140
const { stdout } = await $`solana --version`.quiet();
@@ -128,9 +144,10 @@ export async function getInstalledSolanaVersion(): Promise<string | undefined> {
128144
}
129145
}
130146

131-
export function parseCliArguments(): { manifestPath: string; args: string[] } {
132-
// Command-line arguments.
133-
const args = cliArguments();
147+
export function parseCliArguments(): { command: string, libraryPath: string; args: string[] } {
148+
const command = process.argv[2];
149+
const args = process.argv.slice(3);
150+
134151
// Extract the relative crate directory from the command-line arguments. This
135152
// is the only required argument.
136153
const relativePath = args.shift();
@@ -140,7 +157,8 @@ export function parseCliArguments(): { manifestPath: string; args: string[] } {
140157
}
141158

142159
return {
143-
manifestPath: path.join(workingDirectory, relativePath, 'Cargo.toml'),
160+
command,
161+
libraryPath: path.join(workingDirectory, relativePath),
144162
args,
145163
};
146164
}

scripts/js.mts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env zx
2+
3+
// Script for working with JavaScript projects.
4+
5+
import 'zx/globals';
6+
import {
7+
parseCliArguments,
8+
partitionArgumentsWithDefaultArgs,
9+
} from './helpers/utils.mts';
10+
11+
enum Command {
12+
Format = 'format',
13+
Lint = 'lint',
14+
Test = 'test',
15+
Publish = 'publish',
16+
}
17+
18+
const { command, libraryPath, args } = parseCliArguments();
19+
20+
async function pnpm(
21+
command: string,
22+
build = false,
23+
) {
24+
const [pnpmArgs, commandArgs] = partitionArgumentsWithDefaultArgs(args, '--');
25+
cd(libraryPath);
26+
await $`pnpm install`;
27+
if (build) {
28+
await $`pnpm build`;
29+
}
30+
await $`pnpm ${command} ${pnpmArgs} -- ${commandArgs}`;
31+
}
32+
33+
async function format() {
34+
return pnpm('format');
35+
}
36+
37+
async function lint() {
38+
return pnpm('lint');
39+
}
40+
41+
async function test() {
42+
// Start the local validator, or restart it if it is already running.
43+
await $`pnpm validator:restart`;
44+
45+
// Build the client and run the tests.
46+
return pnpm('test', true);
47+
}
48+
49+
async function publish() {
50+
const [level, tag = 'latest'] = args;
51+
if (!level) {
52+
throw new Error('A version level — e.g. "path" — must be provided.');
53+
}
54+
55+
// Go to the directory and install the dependencies.
56+
cd(libraryPath);
57+
await $`pnpm install`;
58+
59+
// Update the version.
60+
const versionArgs = [
61+
'--no-git-tag-version',
62+
...(level.startsWith('pre') ? [`--preid ${tag}`] : []),
63+
];
64+
let { stdout } = await $`pnpm version ${level} ${versionArgs}`;
65+
const newVersion = stdout.slice(1).trim();
66+
67+
// Expose the new version to CI if needed.
68+
if (process.env.CI) {
69+
await $`echo "new_version=${newVersion}" >> $GITHUB_OUTPUT`;
70+
}
71+
72+
// Publish the package.
73+
// This will also build the package before publishing (see prepublishOnly script).
74+
await $`pnpm publish --no-git-checks --tag ${tag}`;
75+
76+
// Commit the new version.
77+
await $`git commit -am "Publish JS client v${newVersion}"`;
78+
79+
// Tag the new version.
80+
await $`git tag -a js@v${newVersion} -m "JS client v${newVersion}"`;
81+
}
82+
83+
84+
switch (command) {
85+
case Command.Format:
86+
await format();
87+
break;
88+
case Command.Lint:
89+
await lint();
90+
break;
91+
case Command.Test:
92+
await test();
93+
break;
94+
case Command.Publish:
95+
await publish();
96+
break;
97+
default:
98+
throw new Error(`Unknown command: ${command}`);
99+
}

scripts/js/format.mts

Lines changed: 0 additions & 8 deletions
This file was deleted.

scripts/js/lint.mts

Lines changed: 0 additions & 8 deletions
This file was deleted.

scripts/js/publish.mts

Lines changed: 0 additions & 35 deletions
This file was deleted.

scripts/js/test.mts

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)