Comparing version 1.4.0-next.10 to 1.4.0-next.11
import { __awaiter } from "tslib"; | ||
import { isOutputType } from 'graphql'; | ||
import * as path from 'path'; | ||
import { TYPEGEN_HEADER } from './lang'; | ||
import { nodeImports } from './node'; | ||
import { getOwnPackage, log, objValues, relativePathTo, typeScriptFileExtension } from './utils'; | ||
@@ -50,10 +50,6 @@ /** Any common types / constants that would otherwise be circular-imported */ | ||
}); | ||
const path = nodeImports().path; | ||
const typeSources = yield Promise.all(options.modules.map((source) => __awaiter(this, void 0, void 0, function* () { | ||
// Keeping all of this in here so if we don't have any sources | ||
// e.g. in the Playground, it doesn't break things. | ||
// Yeah, this doesn't exist in Node 6, but since this is a new | ||
// lib and Node 6 is close to EOL so if you really need it, open a PR :) | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
const readFile = util.promisify(fs.readFile); | ||
const { module: pathOrModule, glob = true, onlyTypes, alias, typeMatch } = source; | ||
@@ -72,3 +68,3 @@ if (path.isAbsolute(pathOrModule) && path.extname(pathOrModule) !== '.ts') { | ||
} | ||
fileContents = yield readFile(resolvedPath, 'utf-8'); | ||
fileContents = String(yield nodeImports().fs.promises.readFile(resolvedPath, 'utf-8')); | ||
} | ||
@@ -178,3 +174,3 @@ catch (e) { | ||
try { | ||
const typeDefPath = absolutePath.replace(path.extname(absolutePath), '.d.ts'); | ||
const typeDefPath = absolutePath.replace(nodeImports().path.extname(absolutePath), '.d.ts'); | ||
require.resolve(typeDefPath); | ||
@@ -181,0 +177,0 @@ return typeDefPath; |
import { __awaiter } from "tslib"; | ||
import * as path from 'path'; | ||
import { nodeImports } from './node'; | ||
// todo use Prettier.Options type instead of just `object` | ||
@@ -20,2 +20,3 @@ // but will this force us to make prettier a dep then since that type would be user-visible? | ||
let prettierConfigResolved; | ||
const path = nodeImports().path; | ||
if (typeof prettierConfig === 'string') { | ||
@@ -22,0 +23,0 @@ /* istanbul ignore if */ |
import { __awaiter } from "tslib"; | ||
import { lexicographicSortSchema } from 'graphql'; | ||
import * as path from 'path'; | ||
import { SDL_HEADER, TYPEGEN_HEADER } from './lang'; | ||
import { nodeImports } from './node'; | ||
import { printSchemaWithDirectives } from './printSchemaWithDirectives'; | ||
@@ -71,13 +71,6 @@ import { typegenAutoConfig } from './typegenAutoConfig'; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof filePath !== 'string' || !path.isAbsolute(filePath)) { | ||
if (typeof filePath !== 'string' || !nodeImports().path.isAbsolute(filePath)) { | ||
return Promise.reject(new Error(`Expected an absolute path to output the Nexus ${type}, saw ${filePath}`)); | ||
} | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
const [readFile, writeFile, removeFile, mkdir] = [ | ||
util.promisify(fs.readFile), | ||
util.promisify(fs.writeFile), | ||
util.promisify(fs.unlink), | ||
util.promisify(fs.mkdir), | ||
]; | ||
const fs = nodeImports().fs; | ||
const formattedOutput = typeof this.config.formatTypegen === 'function' ? yield this.config.formatTypegen(output, type) : output; | ||
@@ -87,7 +80,10 @@ const content = this.config.prettierConfig | ||
: formattedOutput; | ||
const [toSave, existing] = yield Promise.all([content, readFile(filePath, 'utf8').catch(() => '')]); | ||
const [toSave, existing] = yield Promise.all([ | ||
content, | ||
fs.promises.readFile(filePath, 'utf8').catch(() => ''), | ||
]); | ||
if (toSave !== existing) { | ||
const dirPath = path.dirname(filePath); | ||
const dirPath = nodeImports().path.dirname(filePath); | ||
try { | ||
yield mkdir(dirPath, { recursive: true }); | ||
yield fs.promises.mkdir(dirPath, { recursive: true }); | ||
} | ||
@@ -103,3 +99,3 @@ catch (e) { | ||
try { | ||
yield removeFile(filePath); | ||
yield fs.promises.unlink(filePath); | ||
} | ||
@@ -112,3 +108,3 @@ catch (e) { | ||
} | ||
return writeFile(filePath, toSave); | ||
return fs.promises.writeFile(filePath, toSave); | ||
} | ||
@@ -115,0 +111,0 @@ }); |
import { __rest } from "tslib"; | ||
import * as path from 'path'; | ||
import { nodeImports } from './node'; | ||
import { assertAbsolutePath, getOwnPackage, isProductionStage } from './utils'; | ||
/** Normalizes the builder config into the config we need for typegen */ | ||
export function resolveTypegenConfig(config) { | ||
const { outputs, shouldGenerateArtifacts = Boolean(!process.env.NODE_ENV || process.env.NODE_ENV !== 'production') } = config, rest = __rest(config, ["outputs", "shouldGenerateArtifacts"]); | ||
const defaultSDLFilePath = path.join(process.cwd(), 'schema.graphql'); | ||
let typegenFilePath = null; | ||
let sdlFilePath = null; | ||
if (outputs === undefined) { | ||
if (isProductionStage()) { | ||
sdlFilePath = defaultSDLFilePath; | ||
const { outputs, shouldGenerateArtifacts = defaultShouldGenerateArtifacts() } = config, rest = __rest(config, ["outputs", "shouldGenerateArtifacts"]); | ||
function getOutputPaths() { | ||
const defaultSDLFilePath = nodeImports().path.join(process.cwd(), 'schema.graphql'); | ||
let typegenFilePath = null; | ||
let sdlFilePath = null; | ||
if (outputs === undefined) { | ||
if (isProductionStage()) { | ||
sdlFilePath = defaultSDLFilePath; | ||
} | ||
} | ||
} | ||
else if (outputs === true) { | ||
sdlFilePath = defaultSDLFilePath; | ||
} | ||
else if (typeof outputs === 'object') { | ||
if (outputs.schema === true) { | ||
else if (outputs === true) { | ||
sdlFilePath = defaultSDLFilePath; | ||
} | ||
else if (typeof outputs.schema === 'string') { | ||
sdlFilePath = assertAbsolutePath(outputs.schema, 'outputs.schema'); | ||
else if (typeof outputs === 'object') { | ||
if (outputs.schema === true) { | ||
sdlFilePath = defaultSDLFilePath; | ||
} | ||
else if (typeof outputs.schema === 'string') { | ||
sdlFilePath = assertAbsolutePath(outputs.schema, 'outputs.schema'); | ||
} | ||
else if (outputs.schema === undefined && isProductionStage()) { | ||
} | ||
// handle typegen configuration | ||
if (typeof outputs.typegen === 'string') { | ||
typegenFilePath = { | ||
outputPath: assertAbsolutePath(outputs.typegen, 'outputs.typegen'), | ||
}; | ||
} | ||
else if (typeof outputs.typegen === 'object') { | ||
typegenFilePath = Object.assign(Object.assign({}, outputs.typegen), { outputPath: assertAbsolutePath(outputs.typegen.outputPath, 'outputs.typegen.outputPath') }); | ||
if (outputs.typegen.globalsPath) { | ||
typegenFilePath.globalsPath = assertAbsolutePath(outputs.typegen.globalsPath, 'outputs.typegen.globalsPath'); | ||
} | ||
} | ||
} | ||
else if (outputs.schema === undefined && isProductionStage()) { | ||
else if (outputs !== false) { | ||
console.warn(`You should specify a configuration value for outputs in Nexus' makeSchema. ` + | ||
`Provide one to remove this warning.`); | ||
} | ||
// handle typegen configuration | ||
if (typeof outputs.typegen === 'string') { | ||
typegenFilePath = { | ||
outputPath: assertAbsolutePath(outputs.typegen, 'outputs.typegen'), | ||
}; | ||
} | ||
else if (typeof outputs.typegen === 'object') { | ||
typegenFilePath = Object.assign(Object.assign({}, outputs.typegen), { outputPath: assertAbsolutePath(outputs.typegen.outputPath, 'outputs.typegen.outputPath') }); | ||
if (outputs.typegen.globalsPath) { | ||
typegenFilePath.globalsPath = assertAbsolutePath(outputs.typegen.globalsPath, 'outputs.typegen.globalsPath'); | ||
} | ||
} | ||
return { | ||
typegenFilePath, | ||
sdlFilePath, | ||
}; | ||
} | ||
else if (outputs !== false) { | ||
console.warn(`You should specify a configuration value for outputs in Nexus' makeSchema. ` + | ||
`Provide one to remove this warning.`); | ||
} | ||
return Object.assign(Object.assign({}, rest), { nexusSchemaImportId: getOwnPackage().name, outputs: { | ||
typegen: shouldGenerateArtifacts ? typegenFilePath : null, | ||
schema: shouldGenerateArtifacts ? sdlFilePath : null, | ||
typegen: shouldGenerateArtifacts ? getOutputPaths().typegenFilePath : null, | ||
schema: shouldGenerateArtifacts ? getOutputPaths().sdlFilePath : null, | ||
} }); | ||
} | ||
function defaultShouldGenerateArtifacts() { | ||
return Boolean(typeof process === 'object' && | ||
typeof process.cwd === 'function' && | ||
(!process.env.NODE_ENV || process.env.NODE_ENV !== 'production')); | ||
} | ||
//# sourceMappingURL=typegenUtils.js.map |
@@ -87,4 +87,2 @@ import { GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLInterfaceTypeConfig, GraphQLNamedType, GraphQLObjectType, GraphQLResolveInfo, GraphQLScalarType, GraphQLSchema, GraphQLType, GraphQLUnionType } from 'graphql'; | ||
export declare function casesHandled(x: never): never; | ||
/** Quickly log objects */ | ||
export declare function dump(x: any): void; | ||
export declare function resolveImportPath(rootType: TypingImport, typeName: string, outputPath: string): string; | ||
@@ -91,0 +89,0 @@ /** Given the right hand side of an arg definition, returns the underlying "named type" for us to add to the builder */ |
@@ -1,4 +0,2 @@ | ||
import * as fs from 'fs'; | ||
import { GraphQLScalarType, isAbstractType, isEnumType, isInputObjectType, isInterfaceType, isObjectType, isScalarType, isSpecifiedScalarType, isUnionType, isWrappingType, specifiedScalarTypes, } from 'graphql'; | ||
import * as Path from 'path'; | ||
import { decorateType } from './definitions/decorateType'; | ||
@@ -8,2 +6,3 @@ import { isNexusMetaType, resolveNexusMetaType } from './definitions/nexusMeta'; | ||
import { NexusTypes, withNexusSymbol, } from './definitions/_types'; | ||
import { nodeImports } from './node'; | ||
export const isInterfaceField = (type, fieldName) => { | ||
@@ -96,3 +95,3 @@ return type.getInterfaces().some((i) => Boolean(i.getFields()[fieldName])); | ||
export const assertAbsolutePath = (pathName, property) => { | ||
if (!Path.isAbsolute(pathName)) { | ||
if (!nodeImports().path.isAbsolute(pathName)) { | ||
throw new Error(`Expected path for "${property}" to be an absolute path, saw "${pathName}"`); | ||
@@ -158,3 +157,3 @@ } | ||
function makeRelativePathExplicitlyRelative(path) { | ||
if (Path.isAbsolute(path)) | ||
if (nodeImports().path.isAbsolute(path)) | ||
return path; | ||
@@ -180,2 +179,3 @@ if (path.startsWith('./')) | ||
export function relativePathTo(absolutePath, fromPath) { | ||
const Path = nodeImports().path; | ||
const filename = Path.basename(absolutePath); | ||
@@ -362,13 +362,10 @@ const relative = Path.relative(Path.dirname(fromPath), Path.dirname(absolutePath)); | ||
} | ||
/** Quickly log objects */ | ||
export function dump(x) { | ||
console.log(require('util').inspect(x, { depth: null })); | ||
} | ||
function isNodeModule(path) { | ||
// Avoid treating absolute windows paths as Node packages e.g. D:/a/b/c | ||
return !Path.isAbsolute(path) && /^([A-z0-9@])/.test(path); | ||
return !nodeImports().path.isAbsolute(path) && /^([A-z0-9@])/.test(path); | ||
} | ||
export function resolveImportPath(rootType, typeName, outputPath) { | ||
const rootTypePath = rootType.module; | ||
if (typeof rootTypePath !== 'string' || (!Path.isAbsolute(rootTypePath) && !isNodeModule(rootTypePath))) { | ||
if (typeof rootTypePath !== 'string' || | ||
(!nodeImports().path.isAbsolute(rootTypePath) && !isNodeModule(rootTypePath))) { | ||
throw new Error(`Expected an absolute path or Node package for the root typing path of the type "${typeName}", saw "${rootTypePath}"`); | ||
@@ -384,3 +381,3 @@ } | ||
} | ||
else if (!fs.existsSync(rootTypePath)) { | ||
else if (!nodeImports().fs.existsSync(rootTypePath)) { | ||
throw new Error(`Root typing path "${rootTypePath}" for the type "${typeName}" does not exist`); | ||
@@ -391,3 +388,3 @@ } | ||
} | ||
if (Path.isAbsolute(rootTypePath)) { | ||
if (nodeImports().path.isAbsolute(rootTypePath)) { | ||
return relativePathTo(rootTypePath, outputPath); | ||
@@ -394,0 +391,0 @@ } |
@@ -6,4 +6,4 @@ "use strict"; | ||
const graphql_1 = require("graphql"); | ||
const path = (0, tslib_1.__importStar)(require("path")); | ||
const lang_1 = require("./lang"); | ||
const node_1 = require("./node"); | ||
const utils_1 = require("./utils"); | ||
@@ -54,10 +54,6 @@ /** Any common types / constants that would otherwise be circular-imported */ | ||
}); | ||
const path = (0, node_1.nodeImports)().path; | ||
const typeSources = yield Promise.all(options.modules.map((source) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
// Keeping all of this in here so if we don't have any sources | ||
// e.g. in the Playground, it doesn't break things. | ||
// Yeah, this doesn't exist in Node 6, but since this is a new | ||
// lib and Node 6 is close to EOL so if you really need it, open a PR :) | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
const readFile = util.promisify(fs.readFile); | ||
const { module: pathOrModule, glob = true, onlyTypes, alias, typeMatch } = source; | ||
@@ -76,3 +72,3 @@ if (path.isAbsolute(pathOrModule) && path.extname(pathOrModule) !== '.ts') { | ||
} | ||
fileContents = yield readFile(resolvedPath, 'utf-8'); | ||
fileContents = String(yield (0, node_1.nodeImports)().fs.promises.readFile(resolvedPath, 'utf-8')); | ||
} | ||
@@ -183,3 +179,3 @@ catch (e) { | ||
try { | ||
const typeDefPath = absolutePath.replace(path.extname(absolutePath), '.d.ts'); | ||
const typeDefPath = absolutePath.replace((0, node_1.nodeImports)().path.extname(absolutePath), '.d.ts'); | ||
require.resolve(typeDefPath); | ||
@@ -186,0 +182,0 @@ return typeDefPath; |
@@ -5,3 +5,3 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const path = (0, tslib_1.__importStar)(require("path")); | ||
const node_1 = require("./node"); | ||
// todo use Prettier.Options type instead of just `object` | ||
@@ -24,2 +24,3 @@ // but will this force us to make prettier a dep then since that type would be user-visible? | ||
let prettierConfigResolved; | ||
const path = (0, node_1.nodeImports)().path; | ||
if (typeof prettierConfig === 'string') { | ||
@@ -26,0 +27,0 @@ /* istanbul ignore if */ |
@@ -6,4 +6,4 @@ "use strict"; | ||
const graphql_1 = require("graphql"); | ||
const path = (0, tslib_1.__importStar)(require("path")); | ||
const lang_1 = require("./lang"); | ||
const node_1 = require("./node"); | ||
const printSchemaWithDirectives_1 = require("./printSchemaWithDirectives"); | ||
@@ -75,13 +75,6 @@ const typegenAutoConfig_1 = require("./typegenAutoConfig"); | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { | ||
if (typeof filePath !== 'string' || !path.isAbsolute(filePath)) { | ||
if (typeof filePath !== 'string' || !(0, node_1.nodeImports)().path.isAbsolute(filePath)) { | ||
return Promise.reject(new Error(`Expected an absolute path to output the Nexus ${type}, saw ${filePath}`)); | ||
} | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
const [readFile, writeFile, removeFile, mkdir] = [ | ||
util.promisify(fs.readFile), | ||
util.promisify(fs.writeFile), | ||
util.promisify(fs.unlink), | ||
util.promisify(fs.mkdir), | ||
]; | ||
const fs = (0, node_1.nodeImports)().fs; | ||
const formattedOutput = typeof this.config.formatTypegen === 'function' ? yield this.config.formatTypegen(output, type) : output; | ||
@@ -91,7 +84,10 @@ const content = this.config.prettierConfig | ||
: formattedOutput; | ||
const [toSave, existing] = yield Promise.all([content, readFile(filePath, 'utf8').catch(() => '')]); | ||
const [toSave, existing] = yield Promise.all([ | ||
content, | ||
fs.promises.readFile(filePath, 'utf8').catch(() => ''), | ||
]); | ||
if (toSave !== existing) { | ||
const dirPath = path.dirname(filePath); | ||
const dirPath = (0, node_1.nodeImports)().path.dirname(filePath); | ||
try { | ||
yield mkdir(dirPath, { recursive: true }); | ||
yield fs.promises.mkdir(dirPath, { recursive: true }); | ||
} | ||
@@ -107,3 +103,3 @@ catch (e) { | ||
try { | ||
yield removeFile(filePath); | ||
yield fs.promises.unlink(filePath); | ||
} | ||
@@ -116,3 +112,3 @@ catch (e) { | ||
} | ||
return writeFile(filePath, toSave); | ||
return fs.promises.writeFile(filePath, toSave); | ||
} | ||
@@ -119,0 +115,0 @@ }); |
@@ -5,50 +5,61 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const path = (0, tslib_1.__importStar)(require("path")); | ||
const node_1 = require("./node"); | ||
const utils_1 = require("./utils"); | ||
/** Normalizes the builder config into the config we need for typegen */ | ||
function resolveTypegenConfig(config) { | ||
const { outputs, shouldGenerateArtifacts = Boolean(!process.env.NODE_ENV || process.env.NODE_ENV !== 'production') } = config, rest = (0, tslib_1.__rest)(config, ["outputs", "shouldGenerateArtifacts"]); | ||
const defaultSDLFilePath = path.join(process.cwd(), 'schema.graphql'); | ||
let typegenFilePath = null; | ||
let sdlFilePath = null; | ||
if (outputs === undefined) { | ||
if ((0, utils_1.isProductionStage)()) { | ||
sdlFilePath = defaultSDLFilePath; | ||
const { outputs, shouldGenerateArtifacts = defaultShouldGenerateArtifacts() } = config, rest = (0, tslib_1.__rest)(config, ["outputs", "shouldGenerateArtifacts"]); | ||
function getOutputPaths() { | ||
const defaultSDLFilePath = (0, node_1.nodeImports)().path.join(process.cwd(), 'schema.graphql'); | ||
let typegenFilePath = null; | ||
let sdlFilePath = null; | ||
if (outputs === undefined) { | ||
if ((0, utils_1.isProductionStage)()) { | ||
sdlFilePath = defaultSDLFilePath; | ||
} | ||
} | ||
} | ||
else if (outputs === true) { | ||
sdlFilePath = defaultSDLFilePath; | ||
} | ||
else if (typeof outputs === 'object') { | ||
if (outputs.schema === true) { | ||
else if (outputs === true) { | ||
sdlFilePath = defaultSDLFilePath; | ||
} | ||
else if (typeof outputs.schema === 'string') { | ||
sdlFilePath = (0, utils_1.assertAbsolutePath)(outputs.schema, 'outputs.schema'); | ||
else if (typeof outputs === 'object') { | ||
if (outputs.schema === true) { | ||
sdlFilePath = defaultSDLFilePath; | ||
} | ||
else if (typeof outputs.schema === 'string') { | ||
sdlFilePath = (0, utils_1.assertAbsolutePath)(outputs.schema, 'outputs.schema'); | ||
} | ||
else if (outputs.schema === undefined && (0, utils_1.isProductionStage)()) { | ||
} | ||
// handle typegen configuration | ||
if (typeof outputs.typegen === 'string') { | ||
typegenFilePath = { | ||
outputPath: (0, utils_1.assertAbsolutePath)(outputs.typegen, 'outputs.typegen'), | ||
}; | ||
} | ||
else if (typeof outputs.typegen === 'object') { | ||
typegenFilePath = Object.assign(Object.assign({}, outputs.typegen), { outputPath: (0, utils_1.assertAbsolutePath)(outputs.typegen.outputPath, 'outputs.typegen.outputPath') }); | ||
if (outputs.typegen.globalsPath) { | ||
typegenFilePath.globalsPath = (0, utils_1.assertAbsolutePath)(outputs.typegen.globalsPath, 'outputs.typegen.globalsPath'); | ||
} | ||
} | ||
} | ||
else if (outputs.schema === undefined && (0, utils_1.isProductionStage)()) { | ||
else if (outputs !== false) { | ||
console.warn(`You should specify a configuration value for outputs in Nexus' makeSchema. ` + | ||
`Provide one to remove this warning.`); | ||
} | ||
// handle typegen configuration | ||
if (typeof outputs.typegen === 'string') { | ||
typegenFilePath = { | ||
outputPath: (0, utils_1.assertAbsolutePath)(outputs.typegen, 'outputs.typegen'), | ||
}; | ||
} | ||
else if (typeof outputs.typegen === 'object') { | ||
typegenFilePath = Object.assign(Object.assign({}, outputs.typegen), { outputPath: (0, utils_1.assertAbsolutePath)(outputs.typegen.outputPath, 'outputs.typegen.outputPath') }); | ||
if (outputs.typegen.globalsPath) { | ||
typegenFilePath.globalsPath = (0, utils_1.assertAbsolutePath)(outputs.typegen.globalsPath, 'outputs.typegen.globalsPath'); | ||
} | ||
} | ||
return { | ||
typegenFilePath, | ||
sdlFilePath, | ||
}; | ||
} | ||
else if (outputs !== false) { | ||
console.warn(`You should specify a configuration value for outputs in Nexus' makeSchema. ` + | ||
`Provide one to remove this warning.`); | ||
} | ||
return Object.assign(Object.assign({}, rest), { nexusSchemaImportId: (0, utils_1.getOwnPackage)().name, outputs: { | ||
typegen: shouldGenerateArtifacts ? typegenFilePath : null, | ||
schema: shouldGenerateArtifacts ? sdlFilePath : null, | ||
typegen: shouldGenerateArtifacts ? getOutputPaths().typegenFilePath : null, | ||
schema: shouldGenerateArtifacts ? getOutputPaths().sdlFilePath : null, | ||
} }); | ||
} | ||
exports.resolveTypegenConfig = resolveTypegenConfig; | ||
function defaultShouldGenerateArtifacts() { | ||
return Boolean(typeof process === 'object' && | ||
typeof process.cwd === 'function' && | ||
(!process.env.NODE_ENV || process.env.NODE_ENV !== 'production')); | ||
} | ||
//# sourceMappingURL=typegenUtils.js.map |
@@ -87,4 +87,2 @@ import { GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLInterfaceTypeConfig, GraphQLNamedType, GraphQLObjectType, GraphQLResolveInfo, GraphQLScalarType, GraphQLSchema, GraphQLType, GraphQLUnionType } from 'graphql'; | ||
export declare function casesHandled(x: never): never; | ||
/** Quickly log objects */ | ||
export declare function dump(x: any): void; | ||
export declare function resolveImportPath(rootType: TypingImport, typeName: string, outputPath: string): string; | ||
@@ -91,0 +89,0 @@ /** Given the right hand side of an arg definition, returns the underlying "named type" for us to add to the builder */ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.result = exports.ownProp = exports.isArray = exports.unpack = exports.graphql15InterfaceType = exports.graphql15InterfaceConfig = exports.Unreachable = exports.raiseProgrammerError = exports.isProductionStage = exports.invariantGuard = exports.getNexusNamedType = exports.getArgNamedType = exports.resolveImportPath = exports.dump = exports.casesHandled = exports.getOwnPackage = exports.pathToArray = exports.UNKNOWN_TYPE_SCALAR = exports.venn = exports.log = exports.consoleWarn = exports.runAbstractTypeRuntimeChecks = exports.assertNoMissingTypes = exports.printedGenTyping = exports.PrintedGenTyping = exports.printedGenTypingImport = exports.PrintedGenTypingImport = exports.relativePathTo = exports.formatPathForModuleImport = exports.typeScriptFileExtension = exports.isPromiseLike = exports.firstDefined = exports.isUnknownType = exports.groupTypes = exports.assertAbsolutePath = exports.isObject = exports.eachObj = exports.mapValues = exports.mapObj = exports.objValues = exports.suggestionList = exports.isInterfaceField = void 0; | ||
const tslib_1 = require("tslib"); | ||
const fs = (0, tslib_1.__importStar)(require("fs")); | ||
exports.result = exports.ownProp = exports.isArray = exports.unpack = exports.graphql15InterfaceType = exports.graphql15InterfaceConfig = exports.Unreachable = exports.raiseProgrammerError = exports.isProductionStage = exports.invariantGuard = exports.getNexusNamedType = exports.getArgNamedType = exports.resolveImportPath = exports.casesHandled = exports.getOwnPackage = exports.pathToArray = exports.UNKNOWN_TYPE_SCALAR = exports.venn = exports.log = exports.consoleWarn = exports.runAbstractTypeRuntimeChecks = exports.assertNoMissingTypes = exports.printedGenTyping = exports.PrintedGenTyping = exports.printedGenTypingImport = exports.PrintedGenTypingImport = exports.relativePathTo = exports.formatPathForModuleImport = exports.typeScriptFileExtension = exports.isPromiseLike = exports.firstDefined = exports.isUnknownType = exports.groupTypes = exports.assertAbsolutePath = exports.isObject = exports.eachObj = exports.mapValues = exports.mapObj = exports.objValues = exports.suggestionList = exports.isInterfaceField = void 0; | ||
const graphql_1 = require("graphql"); | ||
const Path = (0, tslib_1.__importStar)(require("path")); | ||
const decorateType_1 = require("./definitions/decorateType"); | ||
@@ -12,2 +9,3 @@ const nexusMeta_1 = require("./definitions/nexusMeta"); | ||
const _types_1 = require("./definitions/_types"); | ||
const node_1 = require("./node"); | ||
const isInterfaceField = (type, fieldName) => { | ||
@@ -107,3 +105,3 @@ return type.getInterfaces().some((i) => Boolean(i.getFields()[fieldName])); | ||
const assertAbsolutePath = (pathName, property) => { | ||
if (!Path.isAbsolute(pathName)) { | ||
if (!(0, node_1.nodeImports)().path.isAbsolute(pathName)) { | ||
throw new Error(`Expected path for "${property}" to be an absolute path, saw "${pathName}"`); | ||
@@ -174,3 +172,3 @@ } | ||
function makeRelativePathExplicitlyRelative(path) { | ||
if (Path.isAbsolute(path)) | ||
if ((0, node_1.nodeImports)().path.isAbsolute(path)) | ||
return path; | ||
@@ -197,2 +195,3 @@ if (path.startsWith('./')) | ||
function relativePathTo(absolutePath, fromPath) { | ||
const Path = (0, node_1.nodeImports)().path; | ||
const filename = Path.basename(absolutePath); | ||
@@ -392,14 +391,10 @@ const relative = Path.relative(Path.dirname(fromPath), Path.dirname(absolutePath)); | ||
exports.casesHandled = casesHandled; | ||
/** Quickly log objects */ | ||
function dump(x) { | ||
console.log(require('util').inspect(x, { depth: null })); | ||
} | ||
exports.dump = dump; | ||
function isNodeModule(path) { | ||
// Avoid treating absolute windows paths as Node packages e.g. D:/a/b/c | ||
return !Path.isAbsolute(path) && /^([A-z0-9@])/.test(path); | ||
return !(0, node_1.nodeImports)().path.isAbsolute(path) && /^([A-z0-9@])/.test(path); | ||
} | ||
function resolveImportPath(rootType, typeName, outputPath) { | ||
const rootTypePath = rootType.module; | ||
if (typeof rootTypePath !== 'string' || (!Path.isAbsolute(rootTypePath) && !isNodeModule(rootTypePath))) { | ||
if (typeof rootTypePath !== 'string' || | ||
(!(0, node_1.nodeImports)().path.isAbsolute(rootTypePath) && !isNodeModule(rootTypePath))) { | ||
throw new Error(`Expected an absolute path or Node package for the root typing path of the type "${typeName}", saw "${rootTypePath}"`); | ||
@@ -415,3 +410,3 @@ } | ||
} | ||
else if (!fs.existsSync(rootTypePath)) { | ||
else if (!(0, node_1.nodeImports)().fs.existsSync(rootTypePath)) { | ||
throw new Error(`Root typing path "${rootTypePath}" for the type "${typeName}" does not exist`); | ||
@@ -422,3 +417,3 @@ } | ||
} | ||
if (Path.isAbsolute(rootTypePath)) { | ||
if ((0, node_1.nodeImports)().path.isAbsolute(rootTypePath)) { | ||
return relativePathTo(rootTypePath, outputPath); | ||
@@ -425,0 +420,0 @@ } |
{ | ||
"name": "nexus", | ||
"version": "1.4.0-next.10", | ||
"version": "1.4.0-next.11", | ||
"description": "Scalable, strongly typed GraphQL schema development", | ||
@@ -82,2 +82,3 @@ "keywords": [ | ||
"dripip": "^0.10.0", | ||
"esbuild": "^0.14.48", | ||
"eslint": "^6.6.0", | ||
@@ -84,0 +85,0 @@ "get-port": "^5.1.1", |
import { GraphQLNamedType, GraphQLSchema, isOutputType } from 'graphql' | ||
import * as path from 'path' | ||
import type { TypegenInfo } from './builder' | ||
import type { TypingImport } from './definitions/_types' | ||
import { TYPEGEN_HEADER } from './lang' | ||
import { nodeImports } from './node' | ||
import { getOwnPackage, log, objValues, relativePathTo, typeScriptFileExtension } from './utils' | ||
@@ -132,2 +132,3 @@ | ||
const path = nodeImports().path | ||
const typeSources = await Promise.all( | ||
@@ -138,7 +139,2 @@ options.modules.map(async (source) => { | ||
// Yeah, this doesn't exist in Node 6, but since this is a new | ||
// lib and Node 6 is close to EOL so if you really need it, open a PR :) | ||
const fs = require('fs') as typeof import('fs') | ||
const util = require('util') as typeof import('util') | ||
const readFile = util.promisify(fs.readFile) | ||
const { module: pathOrModule, glob = true, onlyTypes, alias, typeMatch } = source | ||
@@ -159,3 +155,3 @@ if (path.isAbsolute(pathOrModule) && path.extname(pathOrModule) !== '.ts') { | ||
} | ||
fileContents = await readFile(resolvedPath, 'utf-8') | ||
fileContents = String(await nodeImports().fs.promises.readFile(resolvedPath, 'utf-8')) | ||
} catch (e) { | ||
@@ -283,3 +279,3 @@ if (e instanceof Error && e.message.indexOf('Cannot find module') !== -1) { | ||
try { | ||
const typeDefPath = absolutePath.replace(path.extname(absolutePath), '.d.ts') | ||
const typeDefPath = absolutePath.replace(nodeImports().path.extname(absolutePath), '.d.ts') | ||
require.resolve(typeDefPath) | ||
@@ -286,0 +282,0 @@ return typeDefPath |
@@ -1,3 +0,3 @@ | ||
import * as path from 'path' | ||
import type * as Prettier from 'prettier' | ||
import { nodeImports } from './node' | ||
@@ -25,2 +25,4 @@ export type TypegenFormatFn = (content: string, type: 'types' | 'schema') => string | Promise<string> | ||
const path = nodeImports().path | ||
if (typeof prettierConfig === 'string') { | ||
@@ -27,0 +29,0 @@ /* istanbul ignore if */ |
import { GraphQLSchema, lexicographicSortSchema } from 'graphql' | ||
import * as path from 'path' | ||
import type { BuilderConfigInput, TypegenInfo } from './builder' | ||
@@ -7,2 +6,3 @@ import type { ConfiguredTypegen } from './core' | ||
import { SDL_HEADER, TYPEGEN_HEADER } from './lang' | ||
import { nodeImports } from './node' | ||
import { printSchemaWithDirectives } from './printSchemaWithDirectives' | ||
@@ -79,3 +79,3 @@ import { typegenAutoConfig } from './typegenAutoConfig' | ||
async writeFile(type: 'schema' | 'types', output: string, filePath: string) { | ||
if (typeof filePath !== 'string' || !path.isAbsolute(filePath)) { | ||
if (typeof filePath !== 'string' || !nodeImports().path.isAbsolute(filePath)) { | ||
return Promise.reject( | ||
@@ -85,10 +85,3 @@ new Error(`Expected an absolute path to output the Nexus ${type}, saw ${filePath}`) | ||
} | ||
const fs = require('fs') as typeof import('fs') | ||
const util = require('util') as typeof import('util') | ||
const [readFile, writeFile, removeFile, mkdir] = [ | ||
util.promisify(fs.readFile), | ||
util.promisify(fs.writeFile), | ||
util.promisify(fs.unlink), | ||
util.promisify(fs.mkdir), | ||
] | ||
const fs = nodeImports().fs | ||
const formattedOutput = | ||
@@ -100,7 +93,10 @@ typeof this.config.formatTypegen === 'function' ? await this.config.formatTypegen(output, type) : output | ||
const [toSave, existing] = await Promise.all([content, readFile(filePath, 'utf8').catch(() => '')]) | ||
const [toSave, existing] = await Promise.all([ | ||
content, | ||
fs.promises.readFile(filePath, 'utf8').catch(() => ''), | ||
]) | ||
if (toSave !== existing) { | ||
const dirPath = path.dirname(filePath) | ||
const dirPath = nodeImports().path.dirname(filePath) | ||
try { | ||
await mkdir(dirPath, { recursive: true }) | ||
await fs.promises.mkdir(dirPath, { recursive: true }) | ||
} catch (e) { | ||
@@ -115,3 +111,3 @@ if (e.code !== 'EEXIST') { | ||
try { | ||
await removeFile(filePath) | ||
await fs.promises.unlink(filePath) | ||
} catch (e) { | ||
@@ -123,3 +119,3 @@ /* istanbul ignore next */ | ||
} | ||
return writeFile(filePath, toSave) | ||
return fs.promises.writeFile(filePath, toSave) | ||
} | ||
@@ -126,0 +122,0 @@ } |
@@ -1,4 +0,4 @@ | ||
import * as path from 'path' | ||
import type { BuilderConfigInput } from './builder' | ||
import type { ConfiguredTypegen } from './core' | ||
import { nodeImports } from './node' | ||
import type { TypegenMetadataConfig } from './typegenMetadata' | ||
@@ -9,48 +9,50 @@ import { assertAbsolutePath, getOwnPackage, isProductionStage } from './utils' | ||
export function resolveTypegenConfig(config: BuilderConfigInput): TypegenMetadataConfig { | ||
const { | ||
outputs, | ||
shouldGenerateArtifacts = Boolean(!process.env.NODE_ENV || process.env.NODE_ENV !== 'production'), | ||
...rest | ||
} = config | ||
const { outputs, shouldGenerateArtifacts = defaultShouldGenerateArtifacts(), ...rest } = config | ||
const defaultSDLFilePath = path.join(process.cwd(), 'schema.graphql') | ||
function getOutputPaths() { | ||
const defaultSDLFilePath = nodeImports().path.join(process.cwd(), 'schema.graphql') | ||
let typegenFilePath: ConfiguredTypegen | null = null | ||
let sdlFilePath: string | null = null | ||
let typegenFilePath: ConfiguredTypegen | null = null | ||
let sdlFilePath: string | null = null | ||
if (outputs === undefined) { | ||
if (isProductionStage()) { | ||
if (outputs === undefined) { | ||
if (isProductionStage()) { | ||
sdlFilePath = defaultSDLFilePath | ||
} | ||
} else if (outputs === true) { | ||
sdlFilePath = defaultSDLFilePath | ||
} | ||
} else if (outputs === true) { | ||
sdlFilePath = defaultSDLFilePath | ||
} else if (typeof outputs === 'object') { | ||
if (outputs.schema === true) { | ||
sdlFilePath = defaultSDLFilePath | ||
} else if (typeof outputs.schema === 'string') { | ||
sdlFilePath = assertAbsolutePath(outputs.schema, 'outputs.schema') | ||
} else if (outputs.schema === undefined && isProductionStage()) { | ||
} | ||
// handle typegen configuration | ||
if (typeof outputs.typegen === 'string') { | ||
typegenFilePath = { | ||
outputPath: assertAbsolutePath(outputs.typegen, 'outputs.typegen'), | ||
} else if (typeof outputs === 'object') { | ||
if (outputs.schema === true) { | ||
sdlFilePath = defaultSDLFilePath | ||
} else if (typeof outputs.schema === 'string') { | ||
sdlFilePath = assertAbsolutePath(outputs.schema, 'outputs.schema') | ||
} else if (outputs.schema === undefined && isProductionStage()) { | ||
} | ||
} else if (typeof outputs.typegen === 'object') { | ||
typegenFilePath = { | ||
...outputs.typegen, | ||
outputPath: assertAbsolutePath(outputs.typegen.outputPath, 'outputs.typegen.outputPath'), | ||
} as ConfiguredTypegen | ||
if (outputs.typegen.globalsPath) { | ||
typegenFilePath.globalsPath = assertAbsolutePath( | ||
outputs.typegen.globalsPath, | ||
'outputs.typegen.globalsPath' | ||
) | ||
// handle typegen configuration | ||
if (typeof outputs.typegen === 'string') { | ||
typegenFilePath = { | ||
outputPath: assertAbsolutePath(outputs.typegen, 'outputs.typegen'), | ||
} | ||
} else if (typeof outputs.typegen === 'object') { | ||
typegenFilePath = { | ||
...outputs.typegen, | ||
outputPath: assertAbsolutePath(outputs.typegen.outputPath, 'outputs.typegen.outputPath'), | ||
} as ConfiguredTypegen | ||
if (outputs.typegen.globalsPath) { | ||
typegenFilePath.globalsPath = assertAbsolutePath( | ||
outputs.typegen.globalsPath, | ||
'outputs.typegen.globalsPath' | ||
) | ||
} | ||
} | ||
} else if (outputs !== false) { | ||
console.warn( | ||
`You should specify a configuration value for outputs in Nexus' makeSchema. ` + | ||
`Provide one to remove this warning.` | ||
) | ||
} | ||
} else if (outputs !== false) { | ||
console.warn( | ||
`You should specify a configuration value for outputs in Nexus' makeSchema. ` + | ||
`Provide one to remove this warning.` | ||
) | ||
return { | ||
typegenFilePath, | ||
sdlFilePath, | ||
} | ||
} | ||
@@ -62,6 +64,14 @@ | ||
outputs: { | ||
typegen: shouldGenerateArtifacts ? typegenFilePath : null, | ||
schema: shouldGenerateArtifacts ? sdlFilePath : null, | ||
typegen: shouldGenerateArtifacts ? getOutputPaths().typegenFilePath : null, | ||
schema: shouldGenerateArtifacts ? getOutputPaths().sdlFilePath : null, | ||
}, | ||
} | ||
} | ||
function defaultShouldGenerateArtifacts() { | ||
return Boolean( | ||
typeof process === 'object' && | ||
typeof process.cwd === 'function' && | ||
(!process.env.NODE_ENV || process.env.NODE_ENV !== 'production') | ||
) | ||
} |
@@ -1,2 +0,1 @@ | ||
import * as fs from 'fs' | ||
import { | ||
@@ -25,3 +24,2 @@ GraphQLEnumType, | ||
} from 'graphql' | ||
import * as Path from 'path' | ||
import { decorateType } from './definitions/decorateType' | ||
@@ -46,2 +44,3 @@ import { isNexusMetaType, NexusMetaType, resolveNexusMetaType } from './definitions/nexusMeta' | ||
} from './definitions/_types' | ||
import { nodeImports } from './node' | ||
@@ -155,3 +154,3 @@ export const isInterfaceField = (type: GraphQLObjectType, fieldName: string) => { | ||
export const assertAbsolutePath = (pathName: string, property: string) => { | ||
if (!Path.isAbsolute(pathName)) { | ||
if (!nodeImports().path.isAbsolute(pathName)) { | ||
throw new Error(`Expected path for "${property}" to be an absolute path, saw "${pathName}"`) | ||
@@ -227,3 +226,3 @@ } | ||
function makeRelativePathExplicitlyRelative(path: string) { | ||
if (Path.isAbsolute(path)) return path | ||
if (nodeImports().path.isAbsolute(path)) return path | ||
if (path.startsWith('./')) return path | ||
@@ -250,2 +249,3 @@ return `./${path}` | ||
export function relativePathTo(absolutePath: string, fromPath: string): string { | ||
const Path = nodeImports().path | ||
const filename = Path.basename(absolutePath) | ||
@@ -485,10 +485,5 @@ const relative = Path.relative(Path.dirname(fromPath), Path.dirname(absolutePath)) | ||
/** Quickly log objects */ | ||
export function dump(x: any) { | ||
console.log(require('util').inspect(x, { depth: null })) | ||
} | ||
function isNodeModule(path: string) { | ||
// Avoid treating absolute windows paths as Node packages e.g. D:/a/b/c | ||
return !Path.isAbsolute(path) && /^([A-z0-9@])/.test(path) | ||
return !nodeImports().path.isAbsolute(path) && /^([A-z0-9@])/.test(path) | ||
} | ||
@@ -499,3 +494,6 @@ | ||
if (typeof rootTypePath !== 'string' || (!Path.isAbsolute(rootTypePath) && !isNodeModule(rootTypePath))) { | ||
if ( | ||
typeof rootTypePath !== 'string' || | ||
(!nodeImports().path.isAbsolute(rootTypePath) && !isNodeModule(rootTypePath)) | ||
) { | ||
throw new Error( | ||
@@ -512,3 +510,3 @@ `Expected an absolute path or Node package for the root typing path of the type "${typeName}", saw "${rootTypePath}"` | ||
} | ||
} else if (!fs.existsSync(rootTypePath)) { | ||
} else if (!nodeImports().fs.existsSync(rootTypePath)) { | ||
throw new Error(`Root typing path "${rootTypePath}" for the type "${typeName}" does not exist`) | ||
@@ -521,3 +519,3 @@ } | ||
if (Path.isAbsolute(rootTypePath)) { | ||
if (nodeImports().path.isAbsolute(rootTypePath)) { | ||
return relativePathTo(rootTypePath, outputPath) | ||
@@ -524,0 +522,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
2337924
391
37045
18
27