瀏覽代碼

add gill support to the cli `init` command (#668)

Nick Frostbutter 4 月之前
父節點
當前提交
5ddcfaccb4
共有 3 個文件被更改,包括 32 次插入6 次删除
  1. 5 0
      .changeset/busy-pots-cough.md
  2. 6 0
      packages/cli/README.md
  3. 21 6
      packages/cli/src/commands/init.ts

+ 5 - 0
.changeset/busy-pots-cough.md

@@ -0,0 +1,5 @@
+---
+'@codama/cli': minor
+---
+
+add the `--gill` flag to the CLI allowing easy generation of gill based config files

+ 6 - 0
packages/cli/README.md

@@ -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:

+ 21 - 6
packages/cli/src/commands/init.ts

@@ -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 = {
@@ -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 = {
@@ -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 (