Last active
April 21, 2019 03:09
-
-
Save mernen/4b52651d6a979b0c95545204d7a60f5d to your computer and use it in GitHub Desktop.
Wrapper for `graphql-codegen` that only invokes the build if input files have been modified
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
// Only invoke `graphql-codegen` if input files have been modified | |
const childProcess = require('child_process'); | |
const fs = require('fs'); | |
const glob = require('glob'); | |
const {safeLoad} = require('js-yaml') | |
function main() { | |
let args = process.argv.slice(2); | |
let configPath = args[args.indexOf('--config') + 1]; | |
let config = safeLoad(fs.readFileSync(configPath, 'utf-8')); | |
if (!shouldGenerate(config)) { | |
console.log('Skipping generation'); | |
return; | |
} | |
childProcess.spawn('yarn', ['graphql-codegen', ...args], { | |
stdio: ['ignore', 'inherit', 'inherit'], | |
}); | |
} | |
function shouldGenerate(config) { | |
let outputFiles = Object.keys(config.generates); | |
try { | |
var newestGenerated = Math.min(...outputFiles.map(filename => fs.statSync(filename).mtime)); | |
} catch (err) { | |
console.error('Output file is missing', err); | |
return true; | |
} | |
let inputFiles = glob.sync(config.schema); | |
return inputFiles.some(inputFile => { | |
if (fs.statSync(inputFile).mtime >= newestGenerated) { | |
console.log('Schema file modified:', inputFile); | |
return true; | |
} | |
}); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment