Skip to content

Commit

Permalink
fix(quickgen): updates types usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Stun3R committed Nov 13, 2022
1 parent c08e0e7 commit 620d3c8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@commitlint/cli": "13.2.1",
"@commitlint/config-conventional": "13.2.0",
"@release-it/conventional-changelog": "3.3.0",
"@types/commander": "^2.12.2",
"@typescript-eslint/eslint-plugin": "5.42.1",
"@typescript-eslint/parser": "5.42.1",
"commitizen": "4.2.5",
Expand Down
28 changes: 21 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { quickGen } from './commands/quick';
import { quickGen } from "./commands/quick";
import { Command } from "commander";

import { generateTypes } from "./commands/generate";

import { version } from "../package.json";

type QuickGenOptions = {
path: string;
fileName: string;
};

// Initial program setup
const program = new Command();

Expand All @@ -28,13 +33,22 @@ program

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) => {
.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: string, options: QuickGenOptions) => {
await quickGen(url, options.path, options.fileName);
});


program.parse(process.argv);
19 changes: 14 additions & 5 deletions src/commands/quick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { blue, green } from "chalk";

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

// GraphQL Dependencies in order to use Codegen.
import * as typescriptPlugin from "@graphql-codegen/typescript";
Expand All @@ -34,9 +34,12 @@ export interface GenerateOptions {
* Generate Typescript types based on your GraphQL Schema.
*/

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

// Build initial scope.
const scope = {
host: url,
Expand All @@ -47,7 +50,11 @@ export const quickGen = async (url: string, dir: string, name: string): Promise<
/* 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")}.`);
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`, {
Expand Down Expand Up @@ -80,7 +87,9 @@ export const quickGen = async (url: string, dir: string, name: string): Promise<
await outputFile(scope.filePath, output);

// Log the success.
console.log(`${blue("Info")}: Generated your types at ${green(scope.filePath)}.`);
console.log(
`${blue("Info")}: Generated your types at ${green(scope.filePath)}.`
);
process.exit(0);
} catch (error) {
console.error(error);
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,13 @@
"@types/node" "*"
"@types/responselike" "^1.0.0"

"@types/commander@^2.12.2":
version "2.12.2"
resolved "https://registry.yarnpkg.com/@types/commander/-/commander-2.12.2.tgz#183041a23842d4281478fa5d23c5ca78e6fd08ae"
integrity sha512-0QEFiR8ljcHp9bAbWxecjVRuAMr16ivPiGOw6KFQBVrVd0RQIcM3xKdRisH2EDWgVWujiYtHwhSkSUoAAGzH7Q==
dependencies:
commander "*"

"@types/estree@*":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.0.tgz#5fb2e536c1ae9bf35366eed879e827fa59ca41c2"
Expand Down Expand Up @@ -2118,6 +2125,11 @@ combined-stream@^1.0.8:
dependencies:
delayed-stream "~1.0.0"

commander@*:
version "9.4.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-9.4.1.tgz#d1dd8f2ce6faf93147295c0df13c7c21141cfbdd"
integrity sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==

commander@8.3.0, commander@^8.2.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66"
Expand Down

0 comments on commit 620d3c8

Please sign in to comment.