Comparing version 0.0.12 to 0.0.13
@@ -15,3 +15,4 @@ #! /usr/bin/env node | ||
.option('-p --port [port]', 'server port, defaults to 4000.', parseInt) | ||
.action((schemaTextFileName, options) => graph(schemaTextFileName, options.port)) | ||
.option('-o --open [open]', 'opens the server endpoint in the default browser.') | ||
.action((schemaTextFileName, options) => graph(schemaTextFileName, options.port, options.open)) | ||
.parse(process.argv); | ||
@@ -18,0 +19,0 @@ |
@@ -14,4 +14,6 @@ #! /usr/bin/env node | ||
].join('\n')) | ||
.option('-m --mocks [mocks]', 'JSON file with mock data, defaults to the schema file name with ".json" extension.') | ||
.option('-p --port [port]', 'server port, defaults to 3000.', parseInt) | ||
.action((schemaTextFileName, options) => server(schemaTextFileName, options.port)) | ||
.option('-o --open [open]', 'opens the server endpoint in the default browser.') | ||
.action((schemaTextFileName, options) => server(schemaTextFileName, options.mocks, options.port, options.open)) | ||
.parse(process.argv); | ||
@@ -18,0 +20,0 @@ |
@@ -8,3 +8,3 @@ var http = require('http'); | ||
module.exports = function (schemaTextFileName, port) { | ||
module.exports = function (schemaTextFileName, port, openInBrowser) { | ||
port = port || 4000; | ||
@@ -44,3 +44,5 @@ | ||
openUrl(' http://localhost:' + port); | ||
if (openInBrowser) { | ||
openUrl(' http://localhost:' + port); | ||
} | ||
} | ||
@@ -47,0 +49,0 @@ }; |
{ | ||
"name": "gql-tools", | ||
"description": "GraphQL Tools for schema handling.", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"author": "Alberto Mijares <almilo@almilo.es>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
var fs = require('fs'); | ||
var path = require('path'); | ||
var graphql = require('graphql').graphql; | ||
@@ -8,20 +9,54 @@ var graphqlTools = require('graphql-tools'); | ||
module.exports = function (schemaTextFileName, port) { | ||
module.exports = function (schemaTextFileName, mocksFileName, port, openInBrowser) { | ||
port = port || 3000; | ||
mocksFileName = mocksFileName || changeExtension(schemaTextFileName, '.json'); | ||
var graphqlEndpoint = '/graphql'; | ||
var schemaText = fs.readFileSync(schemaTextFileName, 'utf-8'); | ||
var schema = graphqlTools.buildSchemaFromTypeDefinitions(schemaText); | ||
graphqlTools.addMockFunctionsToSchema({schema: schema}); | ||
express() | ||
.use(express.static(__dirname)) | ||
.use(graphqlEndpoint, graphqlHTTP({ | ||
schema: schema, | ||
pretty: true, | ||
graphiql: true | ||
.use(graphqlEndpoint, graphqlHTTP(function () { | ||
return { | ||
schema: createSchema(schemaTextFileName, mocksFileName), | ||
pretty: true, | ||
graphiql: true | ||
}; | ||
})) | ||
.listen(port, onServerReady); | ||
function createSchema(schemaTextFileName, mocksFileName) { | ||
var schemaText = fs.readFileSync(schemaTextFileName, 'utf-8'); | ||
var schema = graphqlTools.buildSchemaFromTypeDefinitions(schemaText); | ||
var mocks = getMocks(mocksFileName); | ||
graphqlTools.addMockFunctionsToSchema({schema: schema, mocks: mocks}); | ||
return schema; | ||
function getMocks(mocksFileName) { | ||
if (mocksFileName) { | ||
if (fs.existsSync(mocksFileName)) { | ||
var resolvedPath = path.resolve('./', mocksFileName); | ||
delete require.cache[resolvedPath]; | ||
var jsonMocks = require(resolvedPath); | ||
return Object.keys(jsonMocks).reduce(wrap, {}); | ||
function wrap(jsMocks, propertyName) { | ||
jsMocks[propertyName] = function () { | ||
return jsonMocks[propertyName]; | ||
}; | ||
return jsMocks; | ||
} | ||
} else { | ||
console.error('Mocks file: "' + mocksFileName + '" not found.'); | ||
} | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
} | ||
function onServerReady(error) { | ||
@@ -36,4 +71,12 @@ if (error) { | ||
openUrl(' http://localhost:' + port); | ||
if (openInBrowser) { | ||
openUrl(' http://localhost:' + port); | ||
} | ||
} | ||
}; | ||
function changeExtension(filename, newExtension) { | ||
var extension = path.extname(filename); | ||
return filename.replace(extension, newExtension); | ||
} |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
16189
268
4