@graphql-tools/json-file-loader
Advanced tools
Comparing version 5.0.1-alpha-cf1d5c6.0 to 5.0.1-alpha-cf46456.0
@@ -6,2 +6,5 @@ 'use strict'; | ||
const utils = require('@graphql-tools/utils'); | ||
const path = require('path'); | ||
const fsExtra = require('fs-extra'); | ||
const process = require('process'); | ||
@@ -14,11 +17,15 @@ const FILE_EXTENSIONS = ['.json']; | ||
async canLoad(pointer, options) { | ||
return this.canLoadSync(pointer, options); | ||
if (utils.isValidPath(pointer)) { | ||
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { | ||
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer); | ||
return new Promise(resolve => fsExtra.exists(normalizedFilePath, resolve)); | ||
} | ||
} | ||
return false; | ||
} | ||
canLoadSync(pointer, options) { | ||
if (utils.isValidPath(pointer) && options.path && options.fs) { | ||
const { resolve, isAbsolute } = options.path; | ||
if (utils.isValidPath(pointer)) { | ||
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer); | ||
const { existsSync } = options.fs; | ||
if (existsSync(normalizedFilePath)) { | ||
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer); | ||
if (fsExtra.existsSync(normalizedFilePath)) { | ||
return true; | ||
@@ -31,10 +38,15 @@ } | ||
async load(pointer, options) { | ||
return this.loadSync(pointer, options); | ||
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer); | ||
try { | ||
const jsonContent = await fsExtra.readFile(normalizedFilePath, { encoding: 'utf8' }); | ||
return utils.parseGraphQLJSON(pointer, jsonContent, options); | ||
} | ||
catch (e) { | ||
throw new Error(`Unable to read JSON file: ${normalizedFilePath}: ${e.message || e}`); | ||
} | ||
} | ||
loadSync(pointer, options) { | ||
const { resolve: resolvePath, isAbsolute } = options.path; | ||
const normalizedFilepath = isAbsolute(pointer) ? pointer : resolvePath(options.cwd || process.cwd(), pointer); | ||
const normalizedFilepath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer); | ||
try { | ||
const { readFileSync } = options.fs; | ||
const jsonContent = readFileSync(normalizedFilepath, 'utf8'); | ||
const jsonContent = fsExtra.readFileSync(normalizedFilepath, 'utf8'); | ||
return utils.parseGraphQLJSON(pointer, jsonContent, options); | ||
@@ -41,0 +53,0 @@ } |
import { Source, SchemaPointerSingle, DocumentLoader, SingleFileOptions } from '@graphql-tools/utils'; | ||
export interface JsonFileLoaderOptions extends SingleFileOptions { | ||
fs?: typeof import('fs'); | ||
path?: typeof import('path'); | ||
} | ||
@@ -6,0 +4,0 @@ export declare class JsonFileLoader implements DocumentLoader { |
import { isValidPath, parseGraphQLJSON } from '@graphql-tools/utils'; | ||
import { isAbsolute, resolve } from 'path'; | ||
import { exists, existsSync, readFile, readFileSync } from 'fs-extra'; | ||
import { cwd } from 'process'; | ||
@@ -9,10 +12,14 @@ const FILE_EXTENSIONS = ['.json']; | ||
async canLoad(pointer, options) { | ||
return this.canLoadSync(pointer, options); | ||
if (isValidPath(pointer)) { | ||
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer); | ||
return new Promise(resolve => exists(normalizedFilePath, resolve)); | ||
} | ||
} | ||
return false; | ||
} | ||
canLoadSync(pointer, options) { | ||
if (isValidPath(pointer) && options.path && options.fs) { | ||
const { resolve, isAbsolute } = options.path; | ||
if (isValidPath(pointer)) { | ||
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer); | ||
const { existsSync } = options.fs; | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer); | ||
if (existsSync(normalizedFilePath)) { | ||
@@ -26,9 +33,14 @@ return true; | ||
async load(pointer, options) { | ||
return this.loadSync(pointer, options); | ||
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer); | ||
try { | ||
const jsonContent = await readFile(normalizedFilePath, { encoding: 'utf8' }); | ||
return parseGraphQLJSON(pointer, jsonContent, options); | ||
} | ||
catch (e) { | ||
throw new Error(`Unable to read JSON file: ${normalizedFilePath}: ${e.message || e}`); | ||
} | ||
} | ||
loadSync(pointer, options) { | ||
const { resolve: resolvePath, isAbsolute } = options.path; | ||
const normalizedFilepath = isAbsolute(pointer) ? pointer : resolvePath(options.cwd || process.cwd(), pointer); | ||
const normalizedFilepath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer); | ||
try { | ||
const { readFileSync } = options.fs; | ||
const jsonContent = readFileSync(normalizedFilepath, 'utf8'); | ||
@@ -35,0 +47,0 @@ return parseGraphQLJSON(pointer, jsonContent, options); |
{ | ||
"name": "@graphql-tools/json-file-loader", | ||
"version": "5.0.1-alpha-cf1d5c6.0", | ||
"version": "5.0.1-alpha-cf46456.0", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -9,3 +9,4 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@graphql-tools/utils": "5.0.1-alpha-cf1d5c6.0", | ||
"@graphql-tools/utils": "5.0.1-alpha-cf46456.0", | ||
"fs-extra": "9.0.0", | ||
"tslib": "1.11.1" | ||
@@ -22,2 +23,5 @@ }, | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "8.1.0" | ||
}, | ||
"publishConfig": { | ||
@@ -24,0 +28,0 @@ "access": "public" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14444
116
4
1
3
+ Addedfs-extra@9.0.0
+ Added@graphql-tools/utils@5.0.1-alpha-cf46456.0(transitive)
+ Addedat-least-node@1.0.0(transitive)
+ Addedfs-extra@9.0.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addeduniversalify@1.0.02.0.1(transitive)
- Removed@graphql-tools/utils@5.0.1-alpha-cf1d5c6.0(transitive)
- Removedaggregate-error@3.0.1(transitive)
- Removedclean-stack@2.2.0(transitive)
- Removedindent-string@4.0.0(transitive)
- Removedlodash@4.17.15(transitive)