format-graphql
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -5,11 +5,6 @@ #!/usr/bin/env node | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _path = _interopRequireDefault(require("path")); | ||
var _yargs = _interopRequireDefault(require("yargs")); | ||
var _utilities = require("../utilities"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const argv = _yargs.default.env('FG').help().usage('$0 <sdl-path>', 'Sort GraphQL schema definition language (SDL) document.', command => { | ||
@@ -31,2 +26,7 @@ command.positional('sdl-path', { | ||
}, | ||
'sort-enums': { | ||
default: true, | ||
description: 'Sort on enums', | ||
type: 'boolean' | ||
}, | ||
'sort-fields': { | ||
@@ -43,7 +43,4 @@ default: true, | ||
}).parse(); | ||
const resolvedPath = _path.default.resolve(argv.sdlPath); | ||
const inputSdl = _fs.default.readFileSync(resolvedPath, 'utf8'); | ||
const { | ||
@@ -53,2 +50,3 @@ write, | ||
sortDefinitions, | ||
sortEnums, | ||
sortFields | ||
@@ -59,9 +57,9 @@ } = argv; | ||
sortDefinitions, | ||
sortEnums, | ||
sortFields | ||
}); | ||
if (write) { | ||
_fs.default.writeFileSync(resolvedPath, outputSdl); // eslint-disable-next-line no-console | ||
_fs.default.writeFileSync(resolvedPath, outputSdl); | ||
// eslint-disable-next-line no-console | ||
console.log('Target SDL document has been overriden with the formatted SDL.'); | ||
@@ -68,0 +66,0 @@ } else { |
@@ -12,4 +12,3 @@ "use strict"; | ||
}); | ||
var _utilities = require("./utilities"); | ||
//# sourceMappingURL=index.js.map |
@@ -7,15 +7,13 @@ "use strict"; | ||
exports.default = void 0; | ||
var _graphql = require("graphql"); | ||
var _optionalize = require("./optionalize"); | ||
const sortSchema = (key, value, options) => { | ||
const sortSchema = (key, node, options) => { | ||
const value = node[key]; | ||
const { | ||
sortArguments, | ||
sortDefinitions, | ||
sortEnums, | ||
sortFields | ||
} = options; | ||
if (sortDefinitions && key === 'definitions' || sortFields && key === 'fields' || sortArguments && key === 'arguments') { | ||
if (sortDefinitions && key === 'definitions' || sortEnums && node.kind === 'EnumTypeDefinition' && key === 'values' || sortFields && key === 'fields' || sortArguments && key === 'arguments') { | ||
return value.slice().sort((a, b) => { | ||
@@ -25,16 +23,15 @@ if (a.kind === 'SchemaDefinition') { | ||
} | ||
if (b.kind === 'SchemaDefinition') { | ||
return 1; | ||
} | ||
return a.name.value.localeCompare(b.name.value); | ||
}); | ||
} | ||
return value; | ||
}; | ||
/** | ||
* We only care about rearranging: | ||
* - definitions | ||
* - enums | ||
* - fields | ||
@@ -57,2 +54,7 @@ * - arguments | ||
* }, | ||
* { | ||
* values: [ | ||
* ... | ||
* ], | ||
* }, | ||
* ... | ||
@@ -66,5 +68,7 @@ * ] | ||
* <start> -> definitions -> fields -> arguments | ||
* | ||
* or, for enums: | ||
* | ||
* <start> -> definitions -> values | ||
*/ | ||
const walkAST = (node, options, key) => { | ||
@@ -75,25 +79,20 @@ // Map a node type to the child node type we should walk down next | ||
definitions: 'fields', | ||
EnumTypeDefinition: 'values', | ||
fields: 'arguments' | ||
}; | ||
if (!key) { | ||
return node; | ||
} | ||
if (!Array.isArray(node[key])) { | ||
return node; | ||
} | ||
node[key] = sortSchema(key, node[key], options).map(child => { | ||
return walkAST(child, options, nextKey[key]); | ||
node[key] = sortSchema(key, node, options).map(child => { | ||
return walkAST(child, options, nextKey[child.kind] || nextKey[key]); | ||
}); | ||
return node; | ||
}; | ||
const formatSdl = (schemaSdl, options) => { | ||
return (0, _graphql.print)(walkAST((0, _graphql.parse)(schemaSdl), (0, _optionalize.getOptions)(options), 'definitions')); | ||
}; | ||
var _default = formatSdl; | ||
exports.default = _default; | ||
var _default = exports.default = formatSdl; | ||
//# sourceMappingURL=formatSdl.js.map |
@@ -12,6 +12,4 @@ "use strict"; | ||
}); | ||
var _formatSdl = _interopRequireDefault(require("./formatSdl")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
//# sourceMappingURL=index.js.map |
@@ -6,4 +6,3 @@ "use strict"; | ||
}); | ||
exports.getOptions = exports.toBoolean = exports.isBoolean = void 0; | ||
exports.toBoolean = exports.isBoolean = exports.getOptions = void 0; | ||
// eslint-disable-next-line flowtype/no-weak-types | ||
@@ -13,5 +12,3 @@ const isBoolean = value => { | ||
}; | ||
exports.isBoolean = isBoolean; | ||
const toBoolean = (value, defaultValue) => { | ||
@@ -21,8 +18,5 @@ if (isBoolean(value)) { | ||
} | ||
return isBoolean(defaultValue) ? defaultValue : Boolean(value); | ||
}; | ||
exports.toBoolean = toBoolean; | ||
const getSortOptions = options => { | ||
@@ -32,2 +26,3 @@ const { | ||
sortDefinitions, | ||
sortEnums, | ||
sortFields | ||
@@ -38,6 +33,6 @@ } = options || {}; | ||
sortDefinitions: toBoolean(sortDefinitions, true), | ||
sortEnums: toBoolean(sortEnums, true), | ||
sortFields: toBoolean(sortFields, true) | ||
}; | ||
}; | ||
const getOptions = options => { | ||
@@ -47,4 +42,3 @@ const sortOptions = getSortOptions(options); | ||
}; | ||
exports.getOptions = getOptions; | ||
//# sourceMappingURL=optionalize.js.map |
@@ -51,7 +51,2 @@ { | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "npm run lint && npm run test && npm run build" | ||
} | ||
}, | ||
"keywords": [ | ||
@@ -90,3 +85,3 @@ "graphql", | ||
}, | ||
"version": "1.4.0" | ||
"version": "1.5.0" | ||
} |
@@ -88,2 +88,3 @@ # format-graphql | ||
--sort-definitions Sort on definitions [boolean] [default: true] | ||
--sort-enums Sort on enums [boolean] [default: true] | ||
--sort-fields Sort on fields [boolean] [default: true] | ||
@@ -115,2 +116,3 @@ --write Overrides contents of the SDL document. | ||
sortDefinitions?: boolean, | ||
sortEnums?: boolean, | ||
sortFields?: boolean, | ||
@@ -117,0 +119,0 @@ sortArguments?: boolean, |
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
29154
211
141