Skip to content

Commit 5ddcfac

Browse files
authored
add gill support to the cli init command (#668)
1 parent a8b5455 commit 5ddcfac

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

.changeset/busy-pots-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/cli': minor
3+
---
4+
5+
add the `--gill` flag to the CLI allowing easy generation of gill based config files

packages/cli/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ pnpm codama init
2020

2121
You will be prompted for the path of your IDL and asked to select any script presets you would like to use.
2222

23+
To initialize a [gill based Codama](https://gill.site/docs/guides/codama) configuration file, run the `init` command with the `--gill` flag like so:
24+
25+
```sh
26+
pnpm codama init --gill
27+
```
28+
2329
## `codama run`
2430

2531
Once you have your codama config file, you can run your Codama scripts using the `codama run` command as follows:

packages/cli/src/commands/init.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,40 @@ export function setInitCommand(program: Command): void {
1010
.argument('[output]', 'Optional path used to output the configuration file')
1111
.option('-d, --default', 'Bypass prompts and select all defaults options')
1212
.option('--js', 'Forces the output to be a JavaScript file')
13+
.option('--gill', 'Forces the output to be a gill based JavaScript file')
1314
.action(doInit);
1415
}
1516

1617
type InitOptions = {
1718
default?: boolean;
1819
js?: boolean;
20+
gill?: boolean;
1921
};
2022

23+
type ConfigFileType = 'gill' | 'js' | 'json';
24+
2125
async function doInit(explicitOutput: string | undefined, options: InitOptions) {
2226
const output = getOutputPath(explicitOutput, options);
23-
const useJsFile = options.js || output.endsWith('.js');
27+
let configFileType: ConfigFileType = output.endsWith('.js') ? 'js' : 'json';
28+
if (options.gill) configFileType = 'gill';
29+
else if (options.js) configFileType = 'js';
30+
2431
if (await canRead(output)) {
2532
throw new Error(`Configuration file already exists at "${output}".`);
2633
}
2734

2835
logBanner();
2936
const result = await getPromptResult(options);
30-
const content = getContentFromPromptResult(result, useJsFile);
37+
const content = getContentFromPromptResult(result, configFileType);
3138
await writeFile(output, content);
3239
logSuccess(`Configuration file created at "${output}".`);
3340
}
3441

35-
function getOutputPath(explicitOutput: string | undefined, options: Pick<InitOptions, 'js'>): string {
42+
function getOutputPath(explicitOutput: string | undefined, options: Pick<InitOptions, 'gill' | 'js'>): string {
3643
if (explicitOutput) {
3744
return resolveRelativePath(explicitOutput);
3845
}
39-
return resolveRelativePath(options.js ? 'codama.js' : 'codama.json');
46+
return resolveRelativePath(options.js || options.gill ? 'codama.js' : 'codama.json');
4047
}
4148

4249
type PromptResult = {
@@ -114,7 +121,7 @@ function getDefaultPromptResult(): PromptResult {
114121
};
115122
}
116123

117-
function getContentFromPromptResult(result: PromptResult, useJsFile: boolean): string {
124+
function getContentFromPromptResult(result: PromptResult, configFileType: ConfigFileType): string {
118125
const scripts: Record<ScriptName, ScriptConfig> = {};
119126
if (result.scripts.includes('js')) {
120127
scripts.js = {
@@ -130,8 +137,16 @@ function getContentFromPromptResult(result: PromptResult, useJsFile: boolean): s
130137
}
131138
const content: Config = { idl: result.idlPath, before: [], scripts };
132139

133-
if (!useJsFile) {
140+
if (configFileType == 'json') {
134141
return JSON.stringify(content, null, 4);
142+
} else if (configFileType == 'gill') {
143+
return `import { createCodamaConfig } from "gill";\n\n` +
144+
`export default createCodamaConfig({ \n\t` +
145+
`idl: "${result.idlPath}", \n\t` +
146+
`clientJs: "${result.jsPath}", \n` +
147+
result.scripts.includes('rust')
148+
? `clientRust: "${result.rustPath}", \n`
149+
: `` + `});`;
135150
}
136151

137152
return (

0 commit comments

Comments
 (0)