@graphql-markdown/diff 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Grégory Heitz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # @graphql-markdown/diff
2
+
3
+ [![Latest Version](https://img.shields.io/npm/v/@graphql-markdown/diff?style=flat-square)](https://www.npmjs.com/package/@graphql-markdown/diff)
4
+ [![GitHub License](https://img.shields.io/github/license/graphql-markdown/graphql-markdown?style=flat-square)](https://raw.githubusercontent.com/graphql-markdown/graphql-markdown/main/LICENSE)
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@graphql-markdown/diff",
3
+ "homepage": "https://graphql-markdown.github.io",
4
+ "description": "GraphQL-Markdown diff package.",
5
+ "bugs": {
6
+ "url": "https://github.com/graphql-markdown/graphql-markdown/issues"
7
+ },
8
+ "version": "1.0.0",
9
+ "license": "MIT",
10
+ "main": "src/index.js",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/graphql-markdown/graphql-markdown.git"
14
+ },
15
+ "author": {
16
+ "name": "Gregory Heitz"
17
+ },
18
+ "scripts": {
19
+ "prepack": "pinst --disable",
20
+ "postpack": "pinst --enable",
21
+ "test": "jest",
22
+ "stryker": "stryker run"
23
+ },
24
+ "dependencies": {
25
+ "@graphql-inspector/core": "^3.4.0",
26
+ "@graphql-markdown/utils": "^1.0.0",
27
+ "@graphql-tools/graphql-file-loader": "^7.5.5",
28
+ "@graphql-tools/load": "^7.7.7"
29
+ },
30
+ "directories": {
31
+ "test": "tests"
32
+ },
33
+ "engines": {
34
+ "node": ">=16.14"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ }
39
+ }
package/src/index.js ADDED
@@ -0,0 +1,67 @@
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 { printSchema } = require("@graphql-markdown/utils").graphql;
9
+ const {
10
+ fs: { fileExists, readFile, saveFile },
11
+ } = require("@graphql-markdown/utils").helpers;
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
+ };