-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent doc to generate if no schema change
- Loading branch information
Showing
2 changed files
with
58 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,40 @@ | ||
const chalk = require('chalk'); | ||
const { getSchemaMap, loadSchema, GraphQLFileLoader, UrlLoader, JsonFileLoader } = require('./graphql'); | ||
const Renderer = require('./renderer'); | ||
const Printer = require('./printer'); | ||
const chalk = require('chalk'); | ||
const { round } = require('lodash'); | ||
const { checkSchemaChanges, saveSchemaHash, round } = require('./utils') | ||
|
||
const time = process.hrtime(); | ||
|
||
module.exports = async function generateDocFromSchema(baseURL, schemaLocation, outputDir, homepageLocation) { | ||
let r = undefined; | ||
let pages = []; | ||
let schema = undefined; | ||
let rootTypes = []; | ||
return Promise.resolve( | ||
loadSchema(schemaLocation, { | ||
loaders: [new GraphQLFileLoader(), new UrlLoader(), new JsonFileLoader()], | ||
}), | ||
) | ||
.then((s) => (schema = s)) | ||
.then(() => (r = new Renderer(new Printer(schema, baseURL), outputDir, baseURL))) | ||
.then(() => (rootTypes = getSchemaMap(schema))) | ||
.then(() => | ||
Promise.all(Object.keys(rootTypes).map((typeName) => r.renderRootTypes(typeName, rootTypes[typeName]))), | ||
) | ||
return Promise.resolve( | ||
loadSchema(schemaLocation, { | ||
loaders: [new GraphQLFileLoader(), new UrlLoader(), new JsonFileLoader()], | ||
}), | ||
) | ||
.then((schema) => { | ||
if (checkSchemaChanges(schema, outputDir)) { | ||
let pages = []; | ||
const r = new Renderer(new Printer(schema, baseURL), outputDir, baseURL); | ||
const rootTypes = getSchemaMap(schema); | ||
await Promise.all(Object.keys(rootTypes).map((typeName) => r.renderRootTypes(typeName, rootTypes[typeName]))) | ||
.then((p) => { | ||
pages = p.reduce((r, i) => [].concat(r, i), []); | ||
pages = p.reduce((r, i) => [].concat(r, i), []); | ||
}) | ||
.then(() => r.renderHomepage(homepageLocation)) | ||
.then(() => r.renderSidebar(pages)) | ||
.then((sidebarPath) => { | ||
const [sec, msec] = process.hrtime(time); | ||
const duration = round(sec + msec / 1000000000, 3); | ||
console.info( | ||
chalk.green(`Documentation succesfully generated in "${outputDir}" with base URL "${baseURL}"`), | ||
); | ||
console.log(chalk.blue(`${pages.length} pages generated in ${duration}s from schema "${schemaLocation}"`)); | ||
console.info(chalk.blue.bold(`Remember to update your Docusaurus site's sidebars with "${sidebarPath}"`)); | ||
const [sec, msec] = process.hrtime(time); | ||
const duration = round(sec + msec / 1000000000, 3); | ||
console.info( | ||
chalk.green(`Documentation succesfully generated in "${outputDir}" with base URL "${baseURL}".`), | ||
); | ||
console.log(chalk.blue(`${pages.length} pages generated in ${duration}s from schema "${schemaLocation}".`)); | ||
console.info(chalk.blue.bold(`Remember to update your Docusaurus site's sidebars with "${sidebarPath}".`)); | ||
}); | ||
}; | ||
saveSchemaHash(schema, outputDir); | ||
} else { | ||
console.info(chalk.blue(`No changes detected in schema "${schemaLocation}".`)); | ||
} | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters