Skip to content

Commit

Permalink
Merge pull request #60 from AnnikenYT/main
Browse files Browse the repository at this point in the history
feat: ✨ add quickgen command
  • Loading branch information
Stun3R committed Nov 13, 2022
2 parents 1820aeb + 1ca7c56 commit c08e0e7
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { quickGen } from './commands/quick';
import { Command } from "commander";

import { generateTypes } from "./commands/generate";
Expand Down Expand Up @@ -25,4 +26,15 @@ program
await generateTypes();
});

program
.command("quickgen")
.description("quickly generate Typescript types based on your GraphQL Schema using Arguments instead of manual prompts. Useful for use in npm scripts.")
.argument('<url>', "Your Strapi URL")
.option('-p, --path <location>', 'File path where you want to save the generated types', './models/')
.option('-n, --file-name <filename>', 'File name of the generated types', 'types.ts')
.action(async (url, options) => {
await quickGen(url, options.path, options.fileName);
});


program.parse(process.argv);
89 changes: 89 additions & 0 deletions src/commands/quick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Module dependencies
*/

// Node.js core.
import { join } from "path";

// Extra feature for fs Node.js core module.
import { ensureDir, outputFile } from "fs-extra";

// CLI color mode.
import { blue, green } from "chalk";

// Codegen core.
import { codegen } from "@graphql-codegen/core";
import { Types } from "@graphql-codegen/plugin-helpers/types";

// GraphQL Dependencies in order to use Codegen.
import * as typescriptPlugin from "@graphql-codegen/typescript";
import { printSchema, parse, GraphQLSchema } from "graphql";
import { loadSchema } from "@graphql-tools/load";
import { UrlLoader } from "@graphql-tools/url-loader";

// Options get by the prompt.
export interface GenerateOptions {
host: string;
name: string;
dir: string;
}

/**
* `$ strapi-sdk generate`
*
* Generate Typescript types based on your GraphQL Schema.
*/

export const quickGen = async (url: string, dir: string, name: string): Promise<void> => {
try {

// Build initial scope.
const scope = {
host: url,
outputDir: dir,
outputName: name,
filePath: join(`${dir}/${name}`),
};
/* remove "/" from scope.host in case it exists */
scope.host = scope.host.replace(/\/$/, "");

console.log(`${blue("Info")}: Generating types from GraphQL at ${green(scope.host + "/graphql")}.`);

// Load schema from Strapi API
const schema: GraphQLSchema = await loadSchema(`${scope.host}/graphql`, {
loaders: [new UrlLoader()],
});

// Build Codegen config.
const config: Types.GenerateOptions = {
schema: parse(printSchema(schema)),
filename: scope.outputName,
documents: [],
config: {},
plugins: [
{
typescript: {},
},
],
pluginMap: {
typescript: typescriptPlugin,
},
};

// Generate types
const output: string = await codegen(config);

// Create output directorie recursively.
await ensureDir(scope.outputDir);

// Write the generated type.
await outputFile(scope.filePath, output);

// Log the success.
console.log(`${blue("Info")}: Generated your types at ${green(scope.filePath)}.`);
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
};

0 comments on commit c08e0e7

Please sign in to comment.