b-gsdk 0.0.7 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
package/dist/bin.js ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const _1 = require(".");
8
+ const format_error_1 = require("./util/format-error");
9
+ const arg_1 = __importDefault(require("arg"));
10
+ // Show usage and exit with code
11
+ function help(code) {
12
+ console.log(`Usage:
13
+
14
+ b-gsdk generate
15
+
16
+ `);
17
+ process.exit(code);
18
+ }
19
+ // Get CLI arguments
20
+ const [, , cmd] = process.argv;
21
+ const args = (0, arg_1.default)({
22
+ // types
23
+ "--dir": String,
24
+ // aliases
25
+ "-d": "--dir",
26
+ }, { permissive: true });
27
+ // CLI commands
28
+ const cmds = {
29
+ generate: _1.main,
30
+ };
31
+ // Run CLI
32
+ try {
33
+ // Run command or show usage for unknown command
34
+ cmds[cmd] ? cmds[cmd](args) : help(0);
35
+ }
36
+ catch (e) {
37
+ console.error((0, format_error_1.formatError)(e).message);
38
+ process.exit(1);
39
+ }
package/dist/index.js CHANGED
@@ -7,18 +7,18 @@ exports.main = void 0;
7
7
  const cli_1 = require("@graphql-codegen/cli");
8
8
  const fs_1 = __importDefault(require("fs"));
9
9
  const path_1 = __importDefault(require("path"));
10
+ const get_b_gsdk_config_1 = require("./util/get-b-gsdk-config");
10
11
  const get_b_gsdk_directory_path_1 = require("./util/get-b-gsdk-directory-path");
11
- async function main() {
12
- const bgsdkDirectoryPath = (0, get_b_gsdk_directory_path_1.getBGsdkDirectoryPath)(process.cwd());
12
+ async function main(args) {
13
+ const bgsdkDirectoryPath = (0, get_b_gsdk_directory_path_1.getBGsdkDirectoryPath)(process.cwd(), args["--dir"]);
13
14
  if (!bgsdkDirectoryPath) {
14
15
  throw new Error("Make sure you have a b-gsdk directory in the root of your project.");
15
16
  }
17
+ const config = (0, get_b_gsdk_config_1.getBGsdkConfig)(bgsdkDirectoryPath);
16
18
  const [schemaCodegen, sdkCodegen] = await (0, cli_1.generate)({
17
19
  schema: {
18
- "https://mr-beast-2.myshopify.com/api/2021-10/graphql": {
19
- headers: {
20
- "x-shopify-storefront-access-token": "374a3639228aeb7798d99d88002c4b2e",
21
- },
20
+ [config.schemaURL]: {
21
+ headers: config.headers,
22
22
  },
23
23
  },
24
24
  generates: {
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatError = void 0;
4
+ const formatError = (error) => {
5
+ if (error instanceof Error) {
6
+ return error;
7
+ }
8
+ if (typeof error === "string") {
9
+ return new Error(error);
10
+ }
11
+ if (typeof error === "object") {
12
+ return new Error(JSON.stringify(error, null, 2));
13
+ }
14
+ return new Error(`Unknown error: ${error}`);
15
+ };
16
+ exports.formatError = formatError;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getBGsdkConfig = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const zod_1 = require("zod");
10
+ const configSchema = zod_1.z.object({
11
+ schemaURL: zod_1.z.string(),
12
+ headers: zod_1.z.record(zod_1.z.string()),
13
+ });
14
+ const getBGsdkConfig = (directoryPath) => {
15
+ const bgsdkConfigPath = path_1.default.join(directoryPath, "config.json");
16
+ if (!fs_1.default.existsSync(bgsdkConfigPath)) {
17
+ throw new Error(`Could not find config.json in ${directoryPath}! Please create one.`);
18
+ }
19
+ const rawConfig = JSON.parse(fs_1.default.readFileSync(bgsdkConfigPath, "utf8"));
20
+ const parsedConfig = configSchema.parse(rawConfig);
21
+ return parsedConfig;
22
+ };
23
+ exports.getBGsdkConfig = getBGsdkConfig;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getBGsdkDirectoryPath = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const getBGsdkDirectoryPath = (cwd, customDir) => {
10
+ const schemaPath = path_1.default.join(cwd, customDir || "b-gsdk");
11
+ if (fs_1.default.existsSync(schemaPath)) {
12
+ return schemaPath;
13
+ }
14
+ return null;
15
+ };
16
+ exports.getBGsdkDirectoryPath = getBGsdkDirectoryPath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "b-gsdk",
3
- "version": "0.0.7",
3
+ "version": "0.1.2",
4
4
  "description": "A GraphQL Codegen that outputs a TypeScript SDK.",
5
5
  "author": "Julian Benegas",
6
6
  "license": "MIT",
@@ -15,7 +15,9 @@
15
15
  "@graphql-codegen/introspection": "^2.1.0",
16
16
  "@graphql-codegen/typescript": "^2.4.1",
17
17
  "@graphql-codegen/typescript-graphql-request": "^4.3.2",
18
- "@graphql-codegen/typescript-operations": "^2.2.1"
18
+ "@graphql-codegen/typescript-operations": "^2.2.1",
19
+ "arg": "^5.0.1",
20
+ "zod": "^3.14.4"
19
21
  },
20
22
  "peerDependencies": {
21
23
  "graphql": "*",
package/src/bin.ts CHANGED
@@ -2,11 +2,14 @@
2
2
 
3
3
  import { main } from ".";
4
4
  import { formatError } from "./util/format-error";
5
+ import arg from "arg";
5
6
 
6
7
  // Show usage and exit with code
7
8
  function help(code: number) {
8
9
  console.log(`Usage:
10
+
9
11
  b-gsdk generate
12
+
10
13
  `);
11
14
  process.exit(code);
12
15
  }
@@ -14,16 +17,28 @@ function help(code: number) {
14
17
  // Get CLI arguments
15
18
  const [, , cmd] = process.argv;
16
19
 
20
+ const args = arg(
21
+ {
22
+ // types
23
+ "--dir": String,
24
+ // aliases
25
+ "-d": "--dir",
26
+ },
27
+ { permissive: true }
28
+ );
29
+
17
30
  // CLI commands
18
- const cmds: { [key: string]: () => void } = {
31
+ const cmds: { [key: string]: (args: Args) => void } = {
19
32
  generate: main,
20
33
  };
21
34
 
22
35
  // Run CLI
23
36
  try {
24
37
  // Run command or show usage for unknown command
25
- cmds[cmd] ? cmds[cmd]() : help(0);
38
+ cmds[cmd] ? cmds[cmd](args) : help(0);
26
39
  } catch (e) {
27
40
  console.error(formatError(e).message);
28
41
  process.exit(1);
29
42
  }
43
+
44
+ export type Args = typeof args;
package/src/index.ts CHANGED
@@ -1,10 +1,15 @@
1
1
  import { generate } from "@graphql-codegen/cli";
2
2
  import fs from "fs";
3
3
  import path from "path";
4
+ import { Args } from "./bin";
5
+ import { getBGsdkConfig } from "./util/get-b-gsdk-config";
4
6
  import { getBGsdkDirectoryPath } from "./util/get-b-gsdk-directory-path";
5
7
 
6
- export async function main() {
7
- const bgsdkDirectoryPath = getBGsdkDirectoryPath(process.cwd());
8
+ export async function main(args: Args) {
9
+ const bgsdkDirectoryPath = getBGsdkDirectoryPath(
10
+ process.cwd(),
11
+ args["--dir"]
12
+ );
8
13
 
9
14
  if (!bgsdkDirectoryPath) {
10
15
  throw new Error(
@@ -12,14 +17,13 @@ export async function main() {
12
17
  );
13
18
  }
14
19
 
20
+ const config = getBGsdkConfig(bgsdkDirectoryPath);
21
+
15
22
  const [schemaCodegen, sdkCodegen] = await generate(
16
23
  {
17
24
  schema: {
18
- "https://mr-beast-2.myshopify.com/api/2021-10/graphql": {
19
- headers: {
20
- "x-shopify-storefront-access-token":
21
- "374a3639228aeb7798d99d88002c4b2e",
22
- },
25
+ [config.schemaURL]: {
26
+ headers: config.headers,
23
27
  },
24
28
  },
25
29
  generates: {
@@ -0,0 +1,20 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { z } from "zod";
4
+
5
+ const configSchema = z.object({
6
+ schemaURL: z.string(),
7
+ headers: z.record(z.string()),
8
+ });
9
+
10
+ export const getBGsdkConfig = (directoryPath: string) => {
11
+ const bgsdkConfigPath = path.join(directoryPath, "config.json");
12
+ if (!fs.existsSync(bgsdkConfigPath)) {
13
+ throw new Error(
14
+ `Could not find config.json in ${directoryPath}! Please create one.`
15
+ );
16
+ }
17
+ const rawConfig = JSON.parse(fs.readFileSync(bgsdkConfigPath, "utf8"));
18
+ const parsedConfig = configSchema.parse(rawConfig);
19
+ return parsedConfig;
20
+ };
@@ -1,9 +1,8 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
3
 
4
- // taken from prisma https://github.com/prisma/prisma/blob/8f6b7c7c99c1c720cf5bfed5d563423e71c1b84f/packages/sdk/src/cli/getSchema.ts#L46-L49
5
- export const getBGsdkDirectoryPath = (cwd: string) => {
6
- const schemaPath = path.join(cwd, "b-gsdk");
4
+ export const getBGsdkDirectoryPath = (cwd: string, customDir?: string) => {
5
+ const schemaPath = path.join(cwd, customDir || "b-gsdk");
7
6
  if (fs.existsSync(schemaPath)) {
8
7
  return schemaPath;
9
8
  }
package/tsconfig.json CHANGED
@@ -13,6 +13,7 @@
13
13
  },
14
14
  "compilerOptions": {
15
15
  "rootDir": "src",
16
- "outDir": "dist"
16
+ "outDir": "dist",
17
+ "strict": true
17
18
  }
18
19
  }