Created
October 27, 2022 17:46
-
-
Save ottomata/e21aaae6fd1be3f58ab59341a79cc2d7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Quick and hacky script that will use dyff to show the diff between | |
// Any modified materialized schema version and its previous version. | |
// | |
// Defaults to using https://github.com/homeport/dyff, so install that first. | |
// This could be cleaned up and incorporated into jsonschema-tools itself, and | |
// then shown in CI. | |
// | |
jsonschema_tools = require('@wikimedia/jsonschema-tools'); | |
const _ = require('lodash'); | |
path = require('path'); | |
const util = require('util'); | |
const exec = util.promisify(require('child_process').exec); | |
/** | |
* Runs (and logs) command in cwd. | |
* @param {string} command | |
* @param {string} execOptions options to pass to child_process.exec | |
* @param {Object} logger If given, will call logger.debug(command) before executing it. | |
* @return {Promise} result child_process#exec | |
*/ | |
function execCommand(command, execOptions, logger) { | |
if (execOptions) { | |
if (logger) { | |
logger.debug(`Running: \`${command}\` with `, execOptions); | |
} | |
return exec(command, execOptions); | |
} else { | |
if (logger) { | |
logger.debug(`Running: \`${command}\``); | |
} | |
return exec(command); | |
} | |
} | |
/** | |
* Finds the git root path relative to options.schemaBasePath | |
* @param {Object} options | |
* @return {string} | |
*/ | |
async function findGitRoot(options = {}) { | |
options = jsonschema_tools.readConfig(options); | |
return (await execCommand( | |
// Need to execute the git command in the schemaBasePath for it to find the | |
// .git directory somewhere above schemaBasePath | |
'git rev-parse --show-toplevel', { cwd: options.schemaBasePath }, options.log | |
)).stdout.trim(); | |
} | |
/** | |
* @param {Object} options | |
* @return {Array<string>} | |
*/ | |
async function gitChangedMaterializedSchemaPaths(options = {}) { | |
options = jsonschema_tools.readConfig(options); | |
const gitRoot = await findGitRoot(options); | |
const execOptions = { cwd: options.schemaBasePath }; | |
const command = `git diff --name-only --diff-filter=ACM HEAD^`; | |
const modifiedFiles = (await execCommand(command, execOptions, options.log)).stdout.trim().split('\n'); | |
return _.filter(modifiedFiles, file => { | |
file_basename = path.basename(file); | |
return path.extname(file_basename) === '.' + options.contentTypes[0] && | |
file_basename !== options.currentName && | |
file_basename != `latest.${options.contentTypes[0]}`; | |
}); | |
} | |
async function diffSchemas(sourceSchemaPath, destSchemaPath, options = { | |
diffCommand: 'dyff --color on between' | |
}) { | |
options = jsonschema_tools.readConfig(options); | |
const execOptions = { cwd: options.schemaBasePath }; | |
const diffCommand = options.diffCommand | |
const out = await execCommand( | |
`${diffCommand} ${sourceSchemaPath} ${destSchemaPath}`, | |
execOptions, | |
options.log | |
); | |
return out.stdout; | |
} | |
async function printMaterializedDiffGitHead(options={}) { | |
options = jsonschema_tools.readConfig(options); | |
schemasChanged = await gitChangedMaterializedSchemaPaths(options); | |
console.log(schemasChanged); | |
allSchemasByTitle = await jsonschema_tools.findSchemasByTitle(options); | |
filteredAllSchemasByTitle = {}; | |
Object.keys(allSchemasByTitle).forEach( title => { | |
filtered = allSchemasByTitle[title].filter( si => { | |
file = si.path; | |
return path.extname(path.basename(file)) === '.' + options.contentTypes[0] && path.basename(file) !== options.currentName | |
}); | |
if (filtered.length > 0) { | |
filteredAllSchemasByTitle[title] = filtered; | |
} | |
}); | |
Object.keys(filteredAllSchemasByTitle).forEach(async (title) => { | |
schemas = filteredAllSchemasByTitle[title]; | |
for (let i = 0; i < schemas.length -1; i++) { | |
prevSI = schemas[i]; | |
nextSI = schemas[i+1]; | |
// console.log(nextSI.path); | |
if (schemasChanged.includes(nextSI.path)) { | |
diff = await diffSchemas(prevSI.path, nextSI.path) | |
console.log(diff); | |
} | |
}; | |
}); | |
} | |
printMaterializedDiffGitHead(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: