b-gsdk 0.0.5 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 16
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # b-gsdk
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: {
@@ -45,7 +45,18 @@ async function main() {
45
45
  createDirIfDoesNotExist(`${bgsdkDirectoryPath}/generated`);
46
46
  fs_1.default.writeFileSync(path_1.default.join(bgsdkDirectoryPath, "generated/graphql.schema.json"), schemaCodegen.content);
47
47
  fs_1.default.writeFileSync(path_1.default.join(bgsdkDirectoryPath, "generated/index.ts"), sdkCodegen.content);
48
- fs_1.default.writeFileSync(path_1.default.join(bgsdkDirectoryPath, "sdk.ts"), sdkFileContents);
48
+ const skdFilePath = path_1.default.join(bgsdkDirectoryPath, "sdk.ts");
49
+ if (!fs_1.default.existsSync(skdFilePath)) {
50
+ fs_1.default.writeFileSync(skdFilePath, sdkFileContents);
51
+ }
52
+ const gitignorePath = path_1.default.join(process.cwd(), ".gitignore");
53
+ if (fs_1.default.existsSync(gitignorePath)) {
54
+ const gitignore = fs_1.default.readFileSync(gitignorePath, "utf8");
55
+ if (!gitignore.includes("generated")) {
56
+ fs_1.default.appendFileSync(gitignorePath, "\ngenerated/");
57
+ console.log('Added "generated/" to .gitignore');
58
+ }
59
+ }
49
60
  console.log("Done ✨");
50
61
  }
51
62
  exports.main = main;
@@ -75,4 +86,9 @@ export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
75
86
 
76
87
  return { ...generatedSdk, rawClient: graphQLClient }
77
88
  }
89
+
90
+ // You can then create the sdk with the endpoint and headers set up and export it.
91
+ // For example like this:
92
+ // export const bgsdk = createBGsdk({ })
93
+
78
94
  `;
package/package.json CHANGED
@@ -1,18 +1,23 @@
1
1
  {
2
2
  "name": "b-gsdk",
3
- "version": "0.0.5",
3
+ "version": "0.1.0",
4
4
  "description": "A GraphQL Codegen that outputs a TypeScript SDK.",
5
5
  "author": "Julian Benegas",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
8
  "bin": "dist/bin.js",
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "prepublish": "yarn build"
12
+ },
9
13
  "dependencies": {
10
14
  "@graphql-codegen/cli": "^2.6.2",
11
- "@graphql-codegen/core": "^2.5.1",
12
15
  "@graphql-codegen/introspection": "^2.1.0",
13
16
  "@graphql-codegen/typescript": "^2.4.1",
14
17
  "@graphql-codegen/typescript-graphql-request": "^4.3.2",
15
- "@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"
16
21
  },
17
22
  "peerDependencies": {
18
23
  "graphql": "*",
@@ -26,7 +31,7 @@
26
31
  "ts-node": "^10.7.0",
27
32
  "typescript": "^4.6.3"
28
33
  },
29
- "scripts": {
30
- "build": "tsc"
34
+ "engines": {
35
+ "node": ">=16.0.0"
31
36
  }
32
- }
37
+ }
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: {
@@ -55,7 +59,20 @@ export async function main() {
55
59
  path.join(bgsdkDirectoryPath, "generated/index.ts"),
56
60
  sdkCodegen.content
57
61
  );
58
- fs.writeFileSync(path.join(bgsdkDirectoryPath, "sdk.ts"), sdkFileContents);
62
+ const skdFilePath = path.join(bgsdkDirectoryPath, "sdk.ts");
63
+ if (!fs.existsSync(skdFilePath)) {
64
+ fs.writeFileSync(skdFilePath, sdkFileContents);
65
+ }
66
+
67
+ const gitignorePath = path.join(process.cwd(), ".gitignore");
68
+ if (fs.existsSync(gitignorePath)) {
69
+ const gitignore = fs.readFileSync(gitignorePath, "utf8");
70
+ if (!gitignore.includes("generated")) {
71
+ fs.appendFileSync(gitignorePath, "\ngenerated/");
72
+ console.log('Added "generated/" to .gitignore');
73
+ }
74
+ }
75
+
59
76
  console.log("Done ✨");
60
77
  }
61
78
 
@@ -86,4 +103,9 @@ export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
86
103
 
87
104
  return { ...generatedSdk, rawClient: graphQLClient }
88
105
  }
106
+
107
+ // You can then create the sdk with the endpoint and headers set up and export it.
108
+ // For example like this:
109
+ // export const bgsdk = createBGsdk({ })
110
+
89
111
  `;
@@ -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
  }
package/dist/bin.js DELETED
@@ -1,27 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- const _1 = require(".");
5
- const format_error_1 = require("./util/format-error");
6
- // Show usage and exit with code
7
- function help(code) {
8
- console.log(`Usage:
9
- b-gsdk generate
10
- `);
11
- process.exit(code);
12
- }
13
- // Get CLI arguments
14
- const [, , cmd] = process.argv;
15
- // CLI commands
16
- const cmds = {
17
- generate: _1.main,
18
- };
19
- // Run CLI
20
- try {
21
- // Run command or show usage for unknown command
22
- cmds[cmd] ? cmds[cmd]() : help(0);
23
- }
24
- catch (e) {
25
- console.error((0, format_error_1.formatError)(e).message);
26
- process.exit(1);
27
- }
@@ -1,16 +0,0 @@
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;
@@ -1,17 +0,0 @@
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
- // taken from prisma https://github.com/prisma/prisma/blob/8f6b7c7c99c1c720cf5bfed5d563423e71c1b84f/packages/sdk/src/cli/getSchema.ts#L46-L49
10
- const getBGsdkDirectoryPath = (cwd) => {
11
- const schemaPath = path_1.default.join(cwd, "b-gsdk");
12
- if (fs_1.default.existsSync(schemaPath)) {
13
- return schemaPath;
14
- }
15
- return null;
16
- };
17
- exports.getBGsdkDirectoryPath = getBGsdkDirectoryPath;