Просмотр исходного кода

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

Nick Frostbutter 4 месяцев назад
Родитель
Сommit
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.
 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`
 ## `codama run`
 
 
 Once you have your codama config file, you can run your Codama scripts using the `codama run` command as follows:
 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')
         .argument('[output]', 'Optional path used to output the configuration file')
         .option('-d, --default', 'Bypass prompts and select all defaults options')
         .option('-d, --default', 'Bypass prompts and select all defaults options')
         .option('--js', 'Forces the output to be a JavaScript file')
         .option('--js', 'Forces the output to be a JavaScript file')
+        .option('--gill', 'Forces the output to be a gill based JavaScript file')
         .action(doInit);
         .action(doInit);
 }
 }
 
 
 type InitOptions = {
 type InitOptions = {
     default?: boolean;
     default?: boolean;
     js?: boolean;
     js?: boolean;
+    gill?: boolean;
 };
 };
 
 
+type ConfigFileType = 'gill' | 'js' | 'json';
+
 async function doInit(explicitOutput: string | undefined, options: InitOptions) {
 async function doInit(explicitOutput: string | undefined, options: InitOptions) {
     const output = getOutputPath(explicitOutput, options);
     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)) {
     if (await canRead(output)) {
         throw new Error(`Configuration file already exists at "${output}".`);
         throw new Error(`Configuration file already exists at "${output}".`);
     }
     }
 
 
     logBanner();
     logBanner();
     const result = await getPromptResult(options);
     const result = await getPromptResult(options);
-    const content = getContentFromPromptResult(result, useJsFile);
+    const content = getContentFromPromptResult(result, configFileType);
     await writeFile(output, content);
     await writeFile(output, content);
     logSuccess(`Configuration file created at "${output}".`);
     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) {
     if (explicitOutput) {
         return resolveRelativePath(explicitOutput);
         return resolveRelativePath(explicitOutput);
     }
     }
-    return resolveRelativePath(options.js ? 'codama.js' : 'codama.json');
+    return resolveRelativePath(options.js || options.gill ? 'codama.js' : 'codama.json');
 }
 }
 
 
 type PromptResult = {
 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> = {};
     const scripts: Record<ScriptName, ScriptConfig> = {};
     if (result.scripts.includes('js')) {
     if (result.scripts.includes('js')) {
         scripts.js = {
         scripts.js = {
@@ -130,8 +137,16 @@ function getContentFromPromptResult(result: PromptResult, useJsFile: boolean): s
     }
     }
     const content: Config = { idl: result.idlPath, before: [], scripts };
     const content: Config = { idl: result.idlPath, before: [], scripts };
 
 
-    if (!useJsFile) {
+    if (configFileType == 'json') {
         return JSON.stringify(content, null, 4);
         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 (
     return (