b-gsdk 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/bin.js +49 -0
- package/dist/index.js +1 -1
- package/dist/util/format-error.js +16 -0
- package/dist/util/get-b-gsdk-config.js +30 -0
- package/dist/util/get-b-gsdk-directory-path.js +16 -0
- package/package.json +6 -3
- package/src/index.ts +1 -1
package/dist/bin.js
ADDED
@@ -0,0 +1,49 @@
|
|
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
|
+
require("dotenv").config();
|
8
|
+
const _1 = require(".");
|
9
|
+
const format_error_1 = require("./util/format-error");
|
10
|
+
const arg_1 = __importDefault(require("arg"));
|
11
|
+
// Show usage and exit with code
|
12
|
+
function help(code) {
|
13
|
+
console.log(`Usage:
|
14
|
+
|
15
|
+
b-gsdk generate
|
16
|
+
|
17
|
+
`);
|
18
|
+
process.exit(code);
|
19
|
+
}
|
20
|
+
// Get CLI arguments
|
21
|
+
const [, , cmd] = process.argv;
|
22
|
+
const args = (0, arg_1.default)({
|
23
|
+
// types
|
24
|
+
"--dir": String,
|
25
|
+
// aliases
|
26
|
+
"-d": "--dir",
|
27
|
+
}, { permissive: true });
|
28
|
+
// CLI commands
|
29
|
+
const cmds = {
|
30
|
+
generate: _1.main,
|
31
|
+
};
|
32
|
+
// Run CLI
|
33
|
+
try {
|
34
|
+
// Run command or show usage for unknown command
|
35
|
+
cmds[cmd]
|
36
|
+
? cmds[cmd](args)
|
37
|
+
.then(() => {
|
38
|
+
process.exit(0);
|
39
|
+
})
|
40
|
+
.catch((error) => {
|
41
|
+
console.error((0, format_error_1.formatError)(error));
|
42
|
+
process.exit(1);
|
43
|
+
})
|
44
|
+
: help(0);
|
45
|
+
}
|
46
|
+
catch (e) {
|
47
|
+
console.error((0, format_error_1.formatError)(e).message);
|
48
|
+
process.exit(1);
|
49
|
+
}
|
package/dist/index.js
CHANGED
@@ -15,7 +15,7 @@ async function main(args) {
|
|
15
15
|
throw new Error("Make sure you have a b-gsdk directory in the root of your project.");
|
16
16
|
}
|
17
17
|
const config = (0, get_b_gsdk_config_1.getBGsdkConfig)(bgsdkDirectoryPath);
|
18
|
-
const [
|
18
|
+
const [sdkCodegen, schemaCodegen] = await (0, cli_1.generate)({
|
19
19
|
schema: {
|
20
20
|
[config.endpoint]: {
|
21
21
|
headers: config.headers,
|
@@ -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,30 @@
|
|
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
|
+
endpoint: zod_1.z.string(),
|
12
|
+
headers: zod_1.z.record(zod_1.z.string()).optional(),
|
13
|
+
});
|
14
|
+
const getBGsdkConfig = (directoryPath) => {
|
15
|
+
const bgsdkConfigJsonPath = path_1.default.join(directoryPath, "config.json");
|
16
|
+
const bgsdkConfigJSPath = path_1.default.join(directoryPath, "config.js");
|
17
|
+
let rawConfig = {};
|
18
|
+
if (fs_1.default.existsSync(bgsdkConfigJsonPath)) {
|
19
|
+
rawConfig = JSON.parse(fs_1.default.readFileSync(bgsdkConfigJsonPath, "utf8"));
|
20
|
+
}
|
21
|
+
else if (fs_1.default.existsSync(bgsdkConfigJSPath)) {
|
22
|
+
rawConfig = require(bgsdkConfigJSPath);
|
23
|
+
}
|
24
|
+
else {
|
25
|
+
throw new Error(`Could not find config.{json,js} in ${directoryPath}`);
|
26
|
+
}
|
27
|
+
const parsedConfig = configSchema.parse(rawConfig);
|
28
|
+
return parsedConfig;
|
29
|
+
};
|
30
|
+
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,15 +1,18 @@
|
|
1
1
|
{
|
2
2
|
"name": "b-gsdk",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.2.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
|
-
"bin":
|
8
|
+
"bin": {
|
9
|
+
"b-gsdk": "dist/bin.js"
|
10
|
+
},
|
9
11
|
"types": "src/index.d.ts",
|
10
12
|
"scripts": {
|
11
13
|
"build": "tsc",
|
12
|
-
"prepublish": "yarn build"
|
14
|
+
"prepublish": "yarn build",
|
15
|
+
"publish": "npm publish"
|
13
16
|
},
|
14
17
|
"dependencies": {
|
15
18
|
"@graphql-codegen/cli": "^2.6.2",
|
package/src/index.ts
CHANGED