b-gsdk 0.0.3 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js +47 -12
- package/dist/util/get-b-gsdk-directory-path.js +1 -1
- package/package.json +2 -5
- package/src/index.ts +53 -13
- package/src/util/get-b-gsdk-directory-path.ts +1 -1
package/dist/index.js
CHANGED
@@ -5,18 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.main = void 0;
|
7
7
|
const cli_1 = require("@graphql-codegen/cli");
|
8
|
-
const resolve_pkg_1 = __importDefault(require("resolve-pkg"));
|
9
8
|
const fs_1 = __importDefault(require("fs"));
|
10
9
|
const path_1 = __importDefault(require("path"));
|
11
10
|
const get_b_gsdk_directory_path_1 = require("./util/get-b-gsdk-directory-path");
|
12
11
|
async function main() {
|
13
|
-
console.log("starting main");
|
14
12
|
const bgsdkDirectoryPath = (0, get_b_gsdk_directory_path_1.getBGsdkDirectoryPath)(process.cwd());
|
15
|
-
console.log("got dir path:", bgsdkDirectoryPath);
|
16
13
|
if (!bgsdkDirectoryPath) {
|
17
14
|
throw new Error("Make sure you have a b-gsdk directory in the root of your project.");
|
18
15
|
}
|
19
|
-
console.log("attempting codegen");
|
20
16
|
const [schemaCodegen, sdkCodegen] = await (0, cli_1.generate)({
|
21
17
|
schema: {
|
22
18
|
"https://mr-beast-2.myshopify.com/api/2021-10/graphql": {
|
@@ -46,14 +42,53 @@ async function main() {
|
|
46
42
|
},
|
47
43
|
},
|
48
44
|
}, false);
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
45
|
+
createDirIfDoesNotExist(`${bgsdkDirectoryPath}/generated`);
|
46
|
+
fs_1.default.writeFileSync(path_1.default.join(bgsdkDirectoryPath, "generated/graphql.schema.json"), schemaCodegen.content);
|
47
|
+
fs_1.default.writeFileSync(path_1.default.join(bgsdkDirectoryPath, "generated/index.ts"), sdkCodegen.content);
|
48
|
+
const skdFilePath = path_1.default.join(bgsdkDirectoryPath, "sdk.ts");
|
49
|
+
if (!fs_1.default.existsSync(skdFilePath)) {
|
50
|
+
fs_1.default.writeFileSync(skdFilePath, sdkFileContents);
|
54
51
|
}
|
55
|
-
|
56
|
-
fs_1.default.
|
57
|
-
|
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
|
+
}
|
60
|
+
console.log("Done ✨");
|
58
61
|
}
|
59
62
|
exports.main = main;
|
63
|
+
function createDirIfDoesNotExist(p) {
|
64
|
+
if (!fs_1.default.existsSync(p)) {
|
65
|
+
fs_1.default.mkdirSync(p);
|
66
|
+
}
|
67
|
+
}
|
68
|
+
const sdkFileContents = `import { GraphQLClient } from 'graphql-request'
|
69
|
+
import { getSdk } from './generated'
|
70
|
+
|
71
|
+
export type CreateBGsdkClientParams = {
|
72
|
+
endpoint: string
|
73
|
+
headers?: string[][] | Record<string, string> | Headers
|
74
|
+
}
|
75
|
+
|
76
|
+
export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
|
77
|
+
const graphQLClient = new GraphQLClient(endpoint, {
|
78
|
+
headers: {
|
79
|
+
accept: 'application/json',
|
80
|
+
'Content-Type': 'application/json',
|
81
|
+
...headers
|
82
|
+
}
|
83
|
+
})
|
84
|
+
|
85
|
+
const generatedSdk = getSdk(graphQLClient)
|
86
|
+
|
87
|
+
return { ...generatedSdk, rawClient: graphQLClient }
|
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
|
+
|
94
|
+
`;
|
@@ -7,7 +7,7 @@ exports.getBGsdkDirectoryPath = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
8
8
|
const path_1 = __importDefault(require("path"));
|
9
9
|
// taken from prisma https://github.com/prisma/prisma/blob/8f6b7c7c99c1c720cf5bfed5d563423e71c1b84f/packages/sdk/src/cli/getSchema.ts#L46-L49
|
10
|
-
const getBGsdkDirectoryPath =
|
10
|
+
const getBGsdkDirectoryPath = (cwd) => {
|
11
11
|
const schemaPath = path_1.default.join(cwd, "b-gsdk");
|
12
12
|
if (fs_1.default.existsSync(schemaPath)) {
|
13
13
|
return schemaPath;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "b-gsdk",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.6",
|
4
4
|
"description": "A GraphQL Codegen that outputs a TypeScript SDK.",
|
5
5
|
"author": "Julian Benegas",
|
6
6
|
"license": "MIT",
|
@@ -8,12 +8,10 @@
|
|
8
8
|
"bin": "dist/bin.js",
|
9
9
|
"dependencies": {
|
10
10
|
"@graphql-codegen/cli": "^2.6.2",
|
11
|
-
"@graphql-codegen/core": "^2.5.1",
|
12
11
|
"@graphql-codegen/introspection": "^2.1.0",
|
13
12
|
"@graphql-codegen/typescript": "^2.4.1",
|
14
13
|
"@graphql-codegen/typescript-graphql-request": "^4.3.2",
|
15
|
-
"@graphql-codegen/typescript-operations": "^2.2.1"
|
16
|
-
"resolve-pkg": "^2.0.0"
|
14
|
+
"@graphql-codegen/typescript-operations": "^2.2.1"
|
17
15
|
},
|
18
16
|
"peerDependencies": {
|
19
17
|
"graphql": "*",
|
@@ -23,7 +21,6 @@
|
|
23
21
|
"devDependencies": {
|
24
22
|
"@tsconfig/node16": "^1.0.2",
|
25
23
|
"@types/node": "^17.0.24",
|
26
|
-
"esbuild": "^0.14.36",
|
27
24
|
"graphql": "^16.3.0",
|
28
25
|
"ts-node": "^10.7.0",
|
29
26
|
"typescript": "^4.6.3"
|
package/src/index.ts
CHANGED
@@ -1,21 +1,17 @@
|
|
1
1
|
import { generate } from "@graphql-codegen/cli";
|
2
|
-
import resolvePkg from "resolve-pkg";
|
3
2
|
import fs from "fs";
|
4
3
|
import path from "path";
|
5
4
|
import { getBGsdkDirectoryPath } from "./util/get-b-gsdk-directory-path";
|
6
5
|
|
7
6
|
export async function main() {
|
8
|
-
console.log("starting main");
|
9
7
|
const bgsdkDirectoryPath = getBGsdkDirectoryPath(process.cwd());
|
10
8
|
|
11
|
-
console.log("got dir path:", bgsdkDirectoryPath);
|
12
9
|
if (!bgsdkDirectoryPath) {
|
13
10
|
throw new Error(
|
14
11
|
"Make sure you have a b-gsdk directory in the root of your project."
|
15
12
|
);
|
16
13
|
}
|
17
14
|
|
18
|
-
console.log("attempting codegen");
|
19
15
|
const [schemaCodegen, sdkCodegen] = await generate(
|
20
16
|
{
|
21
17
|
schema: {
|
@@ -49,19 +45,63 @@ export async function main() {
|
|
49
45
|
},
|
50
46
|
false
|
51
47
|
);
|
52
|
-
|
53
|
-
|
54
|
-
const resolved = resolvePkg("@b-gsdk/client");
|
55
|
-
if (!resolved) {
|
56
|
-
throw new Error("Please install @b-gsdk/client");
|
57
|
-
}
|
48
|
+
|
49
|
+
createDirIfDoesNotExist(`${bgsdkDirectoryPath}/generated`);
|
58
50
|
fs.writeFileSync(
|
59
|
-
path.join(
|
51
|
+
path.join(bgsdkDirectoryPath, "generated/graphql.schema.json"),
|
60
52
|
schemaCodegen.content
|
61
53
|
);
|
62
54
|
fs.writeFileSync(
|
63
|
-
path.join(
|
55
|
+
path.join(bgsdkDirectoryPath, "generated/index.ts"),
|
64
56
|
sdkCodegen.content
|
65
57
|
);
|
66
|
-
|
58
|
+
const skdFilePath = path.join(bgsdkDirectoryPath, "sdk.ts");
|
59
|
+
if (!fs.existsSync(skdFilePath)) {
|
60
|
+
fs.writeFileSync(skdFilePath, sdkFileContents);
|
61
|
+
}
|
62
|
+
|
63
|
+
const gitignorePath = path.join(process.cwd(), ".gitignore");
|
64
|
+
if (fs.existsSync(gitignorePath)) {
|
65
|
+
const gitignore = fs.readFileSync(gitignorePath, "utf8");
|
66
|
+
if (!gitignore.includes("generated")) {
|
67
|
+
fs.appendFileSync(gitignorePath, "\ngenerated/");
|
68
|
+
console.log('Added "generated/" to .gitignore');
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
console.log("Done ✨");
|
73
|
+
}
|
74
|
+
|
75
|
+
function createDirIfDoesNotExist(p: string) {
|
76
|
+
if (!fs.existsSync(p)) {
|
77
|
+
fs.mkdirSync(p);
|
78
|
+
}
|
67
79
|
}
|
80
|
+
|
81
|
+
const sdkFileContents = `import { GraphQLClient } from 'graphql-request'
|
82
|
+
import { getSdk } from './generated'
|
83
|
+
|
84
|
+
export type CreateBGsdkClientParams = {
|
85
|
+
endpoint: string
|
86
|
+
headers?: string[][] | Record<string, string> | Headers
|
87
|
+
}
|
88
|
+
|
89
|
+
export const createBGsdk = ({ endpoint, headers }: CreateBGsdkClientParams) => {
|
90
|
+
const graphQLClient = new GraphQLClient(endpoint, {
|
91
|
+
headers: {
|
92
|
+
accept: 'application/json',
|
93
|
+
'Content-Type': 'application/json',
|
94
|
+
...headers
|
95
|
+
}
|
96
|
+
})
|
97
|
+
|
98
|
+
const generatedSdk = getSdk(graphQLClient)
|
99
|
+
|
100
|
+
return { ...generatedSdk, rawClient: graphQLClient }
|
101
|
+
}
|
102
|
+
|
103
|
+
// You can then create the sdk with the endpoint and headers set up and export it.
|
104
|
+
// For example like this:
|
105
|
+
// export const bgsdk = createBGsdk({ })
|
106
|
+
|
107
|
+
`;
|
@@ -2,7 +2,7 @@ import fs from "fs";
|
|
2
2
|
import path from "path";
|
3
3
|
|
4
4
|
// taken from prisma https://github.com/prisma/prisma/blob/8f6b7c7c99c1c720cf5bfed5d563423e71c1b84f/packages/sdk/src/cli/getSchema.ts#L46-L49
|
5
|
-
export const getBGsdkDirectoryPath =
|
5
|
+
export const getBGsdkDirectoryPath = (cwd: string) => {
|
6
6
|
const schemaPath = path.join(cwd, "b-gsdk");
|
7
7
|
if (fs.existsSync(schemaPath)) {
|
8
8
|
return schemaPath;
|