@graphql-markdown/diff 1.0.14 → 1.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.
@@ -0,0 +1,13 @@
1
+ import { type Change } from "@graphql-inspector/core";
2
+ import type { FunctionCheckSchemaChanges, GraphQLSchema } from "@graphql-markdown/types";
3
+ export declare const SCHEMA_HASH_FILE: ".schema";
4
+ export declare const SCHEMA_REF: "schema.graphql";
5
+ export declare enum CompareMethod {
6
+ DIFF = "SCHEMA-DIFF",
7
+ HASH = "SCHEMA-HASH",
8
+ FORCE = "FORCE",
9
+ NONE = "NONE"
10
+ }
11
+ export declare const getSchemaHash: (schema: GraphQLSchema) => string;
12
+ export declare const getDiff: (schemaNew: GraphQLSchema, schemaOldLocation: string) => Promise<Change[]>;
13
+ export declare const checkSchemaChanges: FunctionCheckSchemaChanges;
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkSchemaChanges = exports.getDiff = exports.getSchemaHash = exports.CompareMethod = exports.SCHEMA_REF = exports.SCHEMA_HASH_FILE = void 0;
4
+ const node_path_1 = require("node:path");
5
+ const node_crypto_1 = require("node:crypto");
6
+ const load_1 = require("@graphql-tools/load");
7
+ const core_1 = require("@graphql-inspector/core");
8
+ const graphql_file_loader_1 = require("@graphql-tools/graphql-file-loader");
9
+ const graphql_1 = require("@graphql-markdown/graphql");
10
+ const utils_1 = require("@graphql-markdown/utils");
11
+ exports.SCHEMA_HASH_FILE = ".schema";
12
+ exports.SCHEMA_REF = "schema.graphql";
13
+ var CompareMethod;
14
+ (function (CompareMethod) {
15
+ CompareMethod["DIFF"] = "SCHEMA-DIFF";
16
+ CompareMethod["HASH"] = "SCHEMA-HASH";
17
+ CompareMethod["FORCE"] = "FORCE";
18
+ CompareMethod["NONE"] = "NONE";
19
+ })(CompareMethod || (exports.CompareMethod = CompareMethod = {}));
20
+ const getSchemaHash = (schema) => {
21
+ const printedSchema = (0, graphql_1.printSchema)(schema);
22
+ return (0, node_crypto_1.createHash)("sha256").update(printedSchema).digest("hex");
23
+ };
24
+ exports.getSchemaHash = getSchemaHash;
25
+ const getDiff = async (schemaNew, schemaOldLocation) => {
26
+ const schemaOld = await (0, load_1.loadSchema)(schemaOldLocation, {
27
+ loaders: [new graphql_file_loader_1.GraphQLFileLoader()],
28
+ });
29
+ return (0, core_1.diff)(schemaOld, schemaNew);
30
+ };
31
+ exports.getDiff = getDiff;
32
+ const checkSchemaChanges = async (schema, outputDir, method = CompareMethod.DIFF) => {
33
+ if (method === CompareMethod.DIFF) {
34
+ const schemaRef = (0, node_path_1.join)(outputDir, exports.SCHEMA_REF);
35
+ if (await (0, utils_1.fileExists)(schemaRef)) {
36
+ const schemaDiff = await (0, exports.getDiff)(schema, schemaRef);
37
+ return schemaDiff.length > 0;
38
+ }
39
+ const schemaPrint = (0, graphql_1.printSchema)(schema);
40
+ await (0, utils_1.saveFile)(schemaRef, schemaPrint);
41
+ }
42
+ if (method === CompareMethod.HASH) {
43
+ const hashFile = (0, node_path_1.join)(outputDir, exports.SCHEMA_HASH_FILE);
44
+ const hashSchema = (0, exports.getSchemaHash)(schema);
45
+ if (await (0, utils_1.fileExists)(hashFile)) {
46
+ const hash = await (0, utils_1.readFile)(hashFile, { encoding: "utf8", flag: "r" });
47
+ return hashSchema !== hash;
48
+ }
49
+ await (0, utils_1.saveFile)(hashFile, hashSchema);
50
+ }
51
+ return true;
52
+ };
53
+ exports.checkSchemaChanges = checkSchemaChanges;
package/package.json CHANGED
@@ -5,28 +5,65 @@
5
5
  "bugs": {
6
6
  "url": "https://github.com/graphql-markdown/graphql-markdown/issues"
7
7
  },
8
- "version": "1.0.14",
8
+ "version": "1.1.0",
9
9
  "license": "MIT",
10
- "main": "src/index.js",
11
10
  "repository": {
12
11
  "type": "git",
13
- "url": "https://github.com/graphql-markdown/graphql-markdown.git"
12
+ "url": "git+https://github.com/graphql-markdown/graphql-markdown.git",
13
+ "directory": "packages/diff"
14
14
  },
15
15
  "author": {
16
16
  "name": "Gregory Heitz"
17
17
  },
18
+ "main": "dist/index.js",
19
+ "types": "dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "require": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ },
26
+ "import": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "default": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ }
34
+ },
35
+ "./*": {
36
+ "require": {
37
+ "types": "./dist/index.d.ts",
38
+ "default": "./dist/index.js"
39
+ },
40
+ "import": {
41
+ "types": "./dist/index.d.ts",
42
+ "default": "./dist/index.js"
43
+ },
44
+ "default": {
45
+ "types": "./dist/index.d.ts",
46
+ "default": "./dist/index.js"
47
+ }
48
+ },
49
+ "./package.json": "./package.json"
50
+ },
18
51
  "scripts": {
19
52
  "prepack": "pinst --disable",
20
53
  "postpack": "pinst --enable",
21
- "test": "jest",
22
- "stryker": "stryker run"
54
+ "stryker": "stryker run",
55
+ "clean": "rm -rf ./dist"
23
56
  },
24
57
  "dependencies": {
25
58
  "@graphql-inspector/core": "^5.0.0",
26
- "@graphql-markdown/utils": "^1.5.1",
59
+ "@graphql-markdown/graphql": "^1.0.0",
60
+ "@graphql-markdown/utils": "^1.6.0",
27
61
  "@graphql-tools/graphql-file-loader": "^8.0.0",
28
62
  "@graphql-tools/load": "^8.0.0"
29
63
  },
64
+ "devDependencies": {
65
+ "@graphql-markdown/types": "^1.0.0"
66
+ },
30
67
  "directories": {
31
68
  "test": "tests"
32
69
  },
@@ -34,6 +71,13 @@
34
71
  "node": ">=16.14"
35
72
  },
36
73
  "publishConfig": {
74
+ "directory": "dist",
37
75
  "access": "public"
38
- }
76
+ },
77
+ "typescript": {
78
+ "definition": "dist/index.d.ts"
79
+ },
80
+ "files": [
81
+ "dist"
82
+ ]
39
83
  }
package/src/index.js DELETED
@@ -1,67 +0,0 @@
1
- const path = require("path");
2
- const crypto = require("crypto");
3
-
4
- const { loadSchema } = require("@graphql-tools/load");
5
- const { diff } = require("@graphql-inspector/core");
6
- const { GraphQLFileLoader } = require("@graphql-tools/graphql-file-loader");
7
-
8
- const {
9
- fs: { fileExists, readFile, saveFile },
10
- graphql: { printSchema },
11
- } = require("@graphql-markdown/utils");
12
-
13
- const SCHEMA_HASH_FILE = ".schema";
14
- const SCHEMA_REF = "schema.graphql";
15
- const COMPARE_METHOD = {
16
- DIFF: "SCHEMA-DIFF",
17
- HASH: "SCHEMA-HASH",
18
- FORCE: "FORCE",
19
- NONE: "NONE",
20
- };
21
-
22
- function getSchemaHash(schema) {
23
- const printedSchema = printSchema(schema, { commentDescriptions: true });
24
- return crypto.createHash("sha256").update(printedSchema).digest("hex");
25
- }
26
-
27
- async function getDiff(schemaNew, schemaOldLocation) {
28
- const schemaOld = await loadSchema(schemaOldLocation, {
29
- loaders: [new GraphQLFileLoader()],
30
- });
31
- return diff(schemaOld, schemaNew);
32
- }
33
-
34
- async function checkSchemaChanges(
35
- schema,
36
- outputDir,
37
- method = COMPARE_METHOD.DIFF,
38
- ) {
39
- if (method === COMPARE_METHOD.DIFF) {
40
- const schemaRef = path.join(outputDir, SCHEMA_REF);
41
- if (await fileExists(schemaRef)) {
42
- const schemaDiff = await getDiff(schema, schemaRef);
43
- return schemaDiff.length > 0;
44
- }
45
- const schemaPrint = printSchema(schema);
46
- await saveFile(schemaRef, schemaPrint);
47
- }
48
-
49
- if (method === COMPARE_METHOD.HASH) {
50
- const hashFile = path.join(outputDir, SCHEMA_HASH_FILE);
51
- const hashSchema = getSchemaHash(schema);
52
- if (await fileExists(hashFile)) {
53
- const hash = await readFile(hashFile, { encoding: "utf8", flag: "r" });
54
- return hashSchema !== hash;
55
- }
56
- await saveFile(hashFile, hashSchema);
57
- }
58
-
59
- return true;
60
- }
61
-
62
- module.exports = {
63
- checkSchemaChanges,
64
- COMPARE_METHOD,
65
- SCHEMA_HASH_FILE,
66
- SCHEMA_REF,
67
- };