Skip to content

Commit

Permalink
Prevent doc to generate if no schema change
Browse files Browse the repository at this point in the history
  • Loading branch information
edno committed Jun 27, 2020
1 parent fae4742 commit d837a23
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
53 changes: 27 additions & 26 deletions src/lib/generator.js
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}".`));
}
})
};
34 changes: 31 additions & 3 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const slugify = require('slugify');
const { kebabCase } = require('lodash');
const { kebabCase, startCase, round } = require('lodash');
const crypto = require('crypto');
const { printSchema } = require('graphql')
const fs = require('fs-extra')
const path = require('path')

const { startCase } = require('lodash');
const SCHEMA_HASH_FILE ='.schema';

function toSlug(str) {
return slugify(kebabCase(str));
Expand All @@ -12,4 +16,28 @@ function toArray(param) {
return undefined;
}

module.exports = { startCase, toSlug, toArray };
function getSchemaHash(schema) {
let printedSchema = printSchema(schema, { commentDescriptions: true })
let sum = crypto.createHash('sha256');
sum.update(printedSchema);
return sum.digest('hex');
}

function checkSchemaChanges(schema, outputDir) {
const hashFile = path.join(outputDir, SCHEMA_HASH_FILE);
const hashSchema = getSchemaHash(schema);
let hasDiff = true;
if (fs.existsSync(hashFile)) {
const hash = fs.readFileSync(hashFile);
hasDiff = hashSchema != hash;
}
return hasDiff;
}

function saveSchemaHash(schema, outputDir) {
const hashFile = path.join(outputDir, SCHEMA_HASH_FILE);
const hashSchema = getSchemaHash(schema);
fs.outputFileSync(hashFile, hashSchema);
}

module.exports = { round, startCase, toSlug, toArray, getSchemaHash, checkSchemaChanges, saveSchemaHash };

0 comments on commit d837a23

Please sign in to comment.