Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion clients/js/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module.exports = {
extends: ['@solana/eslint-config-solana'],
ignorePatterns: ['.eslintrc.cjs', 'tsup.config.ts', 'env-shim.ts'],
ignorePatterns: [
'.eslintrc.cjs',
'tsup.config.ts',
'env-shim.ts',
'bin/cli.cjs',
],
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
Expand Down
5 changes: 5 additions & 0 deletions clients/js/bin/cli.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env -S node

const run = require('../dist/src/cli.js').run;

run(process.argv);
2 changes: 1 addition & 1 deletion clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
},
"bin": {
"program-metadata": "./dist/src/cli.js"
"program-metadata": "./bin/cli.cjs"
},
"files": [
"./dist/src",
Expand Down
17 changes: 16 additions & 1 deletion clients/js/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
export * from './program';
import { logDebug, logError } from './logs';
import { createProgram } from './program';

const program = createProgram();

export async function run(argv: readonly string[]) {
try {
await program.parseAsync(argv);
} catch (err) {
if (program.opts().debug) {
logDebug(`${(err as { stack: string }).stack}`);
}
logError((err as { message: string }).message);
process.exitCode = 1;
}
}
4 changes: 4 additions & 0 deletions clients/js/src/cli/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export function logError(message: string): void {
console.error(picocolors.red(`[Error] `) + message);
}

export function logDebug(message: string): void {
console.debug(picocolors.magenta(`[Debug] `) + message);
}

export function logErrorAndExit(message: string): never {
logError(message);
process.exit(1);
Expand Down
48 changes: 36 additions & 12 deletions clients/js/src/cli/program.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
#!/usr/bin/env node

import { setCommands } from './commands';
import { setGlobalOptions } from './options';
import { CustomCommand } from './utils';

// Define the CLI program.
const program = new CustomCommand();
program
.name('program-metadata')
.description('CLI to manage Solana program metadata and IDLs')
.version(__VERSION__)
.configureHelp({ showGlobalOptions: true })
.tap(setGlobalOptions)
.tap(setCommands)
.parse();
export async function programMetadata(
args: string[],
opts?: { suppressOutput?: boolean }
): Promise<void> {
await createProgram({
exitOverride: true,
suppressOutput: opts?.suppressOutput,
}).parseAsync(args, { from: 'user' });
}

export function createProgram(internalOptions?: {
exitOverride?: boolean;
suppressOutput?: boolean;
}): CustomCommand {
const program = new CustomCommand();
program
.name('program-metadata')
.description('CLI to manage Solana program metadata and IDLs')
.version(__VERSION__)
.configureHelp({ showGlobalOptions: true })
.tap(setGlobalOptions)
.tap(setCommands);

// Internal options.
if (internalOptions?.exitOverride) {
program.exitOverride();
}
if (internalOptions?.suppressOutput) {
program.configureOutput({
writeErr: () => {},
writeOut: () => {},
});
}

return program;
}
Loading