b-gsdk 0.0.1 → 0.0.4

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,27 @@
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
+ }
package/dist/index.js CHANGED
@@ -1,32 +1,59 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
4
  };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/index.ts
20
- var src_exports = {};
21
- __export(src_exports, {
22
- helloWorld: () => helloWorld
23
- });
24
- module.exports = __toCommonJS(src_exports);
25
- var hey = "hola mundo";
26
- function helloWorld() {
27
- return hey + "ma";
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.main = void 0;
7
+ const cli_1 = require("@graphql-codegen/cli");
8
+ const resolve_pkg_1 = __importDefault(require("resolve-pkg"));
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const path_1 = __importDefault(require("path"));
11
+ const get_b_gsdk_directory_path_1 = require("./util/get-b-gsdk-directory-path");
12
+ async function main() {
13
+ console.log("starting main");
14
+ const bgsdkDirectoryPath = (0, get_b_gsdk_directory_path_1.getBGsdkDirectoryPath)(process.cwd());
15
+ console.log("got dir path:", bgsdkDirectoryPath);
16
+ if (!bgsdkDirectoryPath) {
17
+ throw new Error("Make sure you have a b-gsdk directory in the root of your project.");
18
+ }
19
+ console.log("attempting codegen");
20
+ const [schemaCodegen, sdkCodegen] = await (0, cli_1.generate)({
21
+ schema: {
22
+ "https://mr-beast-2.myshopify.com/api/2021-10/graphql": {
23
+ headers: {
24
+ "x-shopify-storefront-access-token": "374a3639228aeb7798d99d88002c4b2e",
25
+ },
26
+ },
27
+ },
28
+ generates: {
29
+ [__dirname + "/generated/index.ts"]: {
30
+ documents: [
31
+ bgsdkDirectoryPath + "/**/*.{gql,graphql}",
32
+ bgsdkDirectoryPath + "/*.{gql,graphql}",
33
+ ],
34
+ plugins: [
35
+ "typescript",
36
+ "typescript-operations",
37
+ "typescript-graphql-request",
38
+ ],
39
+ },
40
+ [__dirname + "/generated/graphql.schema.json"]: {
41
+ plugins: ["introspection"],
42
+ config: {
43
+ documentMode: "documentNode",
44
+ withHooks: true,
45
+ },
46
+ },
47
+ },
48
+ }, false);
49
+ console.log("ok COOl, i got it:", sdkCodegen);
50
+ console.log("attempting to write to file");
51
+ const resolved = (0, resolve_pkg_1.default)("@b-gsdk/client");
52
+ if (!resolved) {
53
+ throw new Error("Please install @b-gsdk/client");
54
+ }
55
+ fs_1.default.writeFileSync(path_1.default.join(resolved, "generated/graphql.schema.json"), schemaCodegen.content);
56
+ fs_1.default.writeFileSync(path_1.default.join(resolved, "generated/index.ts"), sdkCodegen.content);
57
+ console.log("done");
28
58
  }
29
- // Annotate the CommonJS export names for ESM import in node:
30
- 0 && (module.exports = {
31
- helloWorld
32
- });
59
+ exports.main = main;
@@ -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,17 @@
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;
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "b-gsdk",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "A GraphQL Codegen that outputs a TypeScript SDK.",
5
5
  "author": "Julian Benegas",
6
6
  "license": "MIT",
7
+ "main": "dist/index.js",
8
+ "bin": "dist/bin.js",
7
9
  "dependencies": {
8
10
  "@graphql-codegen/cli": "^2.6.2",
9
11
  "@graphql-codegen/core": "^2.5.1",
@@ -27,8 +29,6 @@
27
29
  "typescript": "^4.6.3"
28
30
  },
29
31
  "scripts": {
30
- "build": "esbuild ./src/index.ts --bundle --platform=node --target=node16 --outdir=dist",
31
- "watch": "yarn build --watch",
32
- "generate": "ts-node ./src/index.ts"
32
+ "build": "tsc"
33
33
  }
34
34
  }
package/src/bin.ts ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { main } from ".";
4
+ import { formatError } from "./util/format-error";
5
+
6
+ // Show usage and exit with code
7
+ function help(code: number) {
8
+ console.log(`Usage:
9
+ b-gsdk generate
10
+ `);
11
+ process.exit(code);
12
+ }
13
+
14
+ // Get CLI arguments
15
+ const [, , cmd] = process.argv;
16
+
17
+ // CLI commands
18
+ const cmds: { [key: string]: () => void } = {
19
+ generate: main,
20
+ };
21
+
22
+ // Run CLI
23
+ try {
24
+ // Run command or show usage for unknown command
25
+ cmds[cmd] ? cmds[cmd]() : help(0);
26
+ } catch (e) {
27
+ console.error(formatError(e).message);
28
+ process.exit(1);
29
+ }
package/src/index.ts CHANGED
@@ -2,8 +2,20 @@ import { generate } from "@graphql-codegen/cli";
2
2
  import resolvePkg from "resolve-pkg";
3
3
  import fs from "fs";
4
4
  import path from "path";
5
+ import { getBGsdkDirectoryPath } from "./util/get-b-gsdk-directory-path";
5
6
 
6
- async function main() {
7
+ export async function main() {
8
+ console.log("starting main");
9
+ const bgsdkDirectoryPath = getBGsdkDirectoryPath(process.cwd());
10
+
11
+ console.log("got dir path:", bgsdkDirectoryPath);
12
+ if (!bgsdkDirectoryPath) {
13
+ throw new Error(
14
+ "Make sure you have a b-gsdk directory in the root of your project."
15
+ );
16
+ }
17
+
18
+ console.log("attempting codegen");
7
19
  const [schemaCodegen, sdkCodegen] = await generate(
8
20
  {
9
21
  schema: {
@@ -17,8 +29,8 @@ async function main() {
17
29
  generates: {
18
30
  [__dirname + "/generated/index.ts"]: {
19
31
  documents: [
20
- __dirname + "/docs/**/*.{gql,graphql}",
21
- __dirname + "/docs/*.{gql,graphql}",
32
+ bgsdkDirectoryPath + "/**/*.{gql,graphql}",
33
+ bgsdkDirectoryPath + "/*.{gql,graphql}",
22
34
  ],
23
35
  plugins: [
24
36
  "typescript",
@@ -37,10 +49,11 @@ async function main() {
37
49
  },
38
50
  false
39
51
  );
52
+ console.log("ok COOl, i got it:", sdkCodegen);
53
+ console.log("attempting to write to file");
40
54
  const resolved = resolvePkg("@b-gsdk/client");
41
55
  if (!resolved) {
42
- console.error("Please install @b-gsdk/client");
43
- return;
56
+ throw new Error("Please install @b-gsdk/client");
44
57
  }
45
58
  fs.writeFileSync(
46
59
  path.join(resolved, "generated/graphql.schema.json"),
@@ -52,5 +65,3 @@ async function main() {
52
65
  );
53
66
  console.log("done");
54
67
  }
55
-
56
- main();
@@ -0,0 +1,15 @@
1
+ export const formatError = (error: unknown): Error => {
2
+ if (error instanceof Error) {
3
+ return error;
4
+ }
5
+
6
+ if (typeof error === "string") {
7
+ return new Error(error);
8
+ }
9
+
10
+ if (typeof error === "object") {
11
+ return new Error(JSON.stringify(error, null, 2));
12
+ }
13
+
14
+ return new Error(`Unknown error: ${error}`);
15
+ };
@@ -0,0 +1,12 @@
1
+ import fs from "fs";
2
+ import path from "path";
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");
7
+ if (fs.existsSync(schemaPath)) {
8
+ return schemaPath;
9
+ }
10
+
11
+ return null;
12
+ };
package/tsconfig.json CHANGED
@@ -1,14 +1,10 @@
1
1
  {
2
2
  "extends": "@tsconfig/node16/tsconfig.json",
3
-
4
- // Most ts-node options can be specified here using their programmatic names.
5
3
  "ts-node": {
6
4
  // It is faster to skip typechecking.
7
5
  // Remove if you want ts-node to do typechecking.
8
6
  "transpileOnly": true,
9
-
10
7
  "files": true,
11
-
12
8
  "compilerOptions": {
13
9
  // compilerOptions specified here will override those declared below,
14
10
  // but *only* in ts-node. Useful if you want ts-node and tsc to use
@@ -16,6 +12,7 @@
16
12
  }
17
13
  },
18
14
  "compilerOptions": {
19
- // typescript options here
15
+ "rootDir": "src",
16
+ "outDir": "dist"
20
17
  }
21
18
  }