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
5 changes: 5 additions & 0 deletions .changeset/busy-pots-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@codama/cli': minor
---

add the `--gill` flag to the CLI allowing easy generation of gill based config files
6 changes: 6 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pnpm codama init

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

To initialize a [gill based Codama](https://gill.site/docs/guides/codama) configuration file, run the `init` command with the `--gill` flag like so:

```sh
pnpm codama init --gill
```

## `codama run`

Once you have your codama config file, you can run your Codama scripts using the `codama run` command as follows:
Expand Down
27 changes: 21 additions & 6 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,40 @@ export function setInitCommand(program: Command): void {
.argument('[output]', 'Optional path used to output the configuration file')
.option('-d, --default', 'Bypass prompts and select all defaults options')
.option('--js', 'Forces the output to be a JavaScript file')
.option('--gill', 'Forces the output to be a gill based JavaScript file')
.action(doInit);
}

type InitOptions = {
default?: boolean;
js?: boolean;
gill?: boolean;
};

type ConfigFileType = 'gill' | 'js' | 'json';

async function doInit(explicitOutput: string | undefined, options: InitOptions) {
const output = getOutputPath(explicitOutput, options);
const useJsFile = options.js || output.endsWith('.js');
let configFileType: ConfigFileType = output.endsWith('.js') ? 'js' : 'json';
if (options.gill) configFileType = 'gill';
else if (options.js) configFileType = 'js';

if (await canRead(output)) {
throw new Error(`Configuration file already exists at "${output}".`);
}

logBanner();
const result = await getPromptResult(options);
const content = getContentFromPromptResult(result, useJsFile);
const content = getContentFromPromptResult(result, configFileType);
await writeFile(output, content);
logSuccess(`Configuration file created at "${output}".`);
}

function getOutputPath(explicitOutput: string | undefined, options: Pick<InitOptions, 'js'>): string {
function getOutputPath(explicitOutput: string | undefined, options: Pick<InitOptions, 'gill' | 'js'>): string {
if (explicitOutput) {
return resolveRelativePath(explicitOutput);
}
return resolveRelativePath(options.js ? 'codama.js' : 'codama.json');
return resolveRelativePath(options.js || options.gill ? 'codama.js' : 'codama.json');
}

type PromptResult = {
Expand Down Expand Up @@ -114,7 +121,7 @@ function getDefaultPromptResult(): PromptResult {
};
}

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

if (!useJsFile) {
if (configFileType == 'json') {
return JSON.stringify(content, null, 4);
} else if (configFileType == 'gill') {
return `import { createCodamaConfig } from "gill";\n\n` +
`export default createCodamaConfig({ \n\t` +
`idl: "${result.idlPath}", \n\t` +
`clientJs: "${result.jsPath}", \n` +
result.scripts.includes('rust')
? `clientRust: "${result.rustPath}", \n`
: `` + `});`;
}

return (
Expand Down