From ecd17484bbd2403fd0d07941e3269dee1f4dfa1b Mon Sep 17 00:00:00 2001 From: Sam Seay Date: Thu, 21 Jul 2022 19:37:59 +1200 Subject: [PATCH] Generate a JSON file with changes from code-analyzer (#33955) --- .gitignore | 3 ++ .../src/commands/analyzer/index.ts | 33 ++++++++++++++----- tools/code-analyzer/src/utils.ts | 15 +++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 40bafa5978b..6ebfd7d97ac 100644 --- a/.gitignore +++ b/.gitignore @@ -88,6 +88,9 @@ phpcs-report.xml allure-report allure-results +# Code analyzer output +changes.json + # Playwright output & working files /plugins/woocommerce/e2e/output /plugins/woocommerce/e2e/report diff --git a/tools/code-analyzer/src/commands/analyzer/index.ts b/tools/code-analyzer/src/commands/analyzer/index.ts index 2f24df613c7..b42d047df05 100644 --- a/tools/code-analyzer/src/commands/analyzer/index.ts +++ b/tools/code-analyzer/src/commands/analyzer/index.ts @@ -22,9 +22,11 @@ import { areSchemasEqual, getHookDescription, getHookChangeType, + generateJSONFile, } from '../../utils'; import { cloneRepo, generateDiff, generateSchemaDiff } from '../../git'; import { execSync } from 'child_process'; +import { OutputFlags } from '@oclif/core/lib/interfaces'; /** * Analyzer class @@ -67,6 +69,11 @@ export default class Analyzer extends Command { description: 'Git repo url or local path to a git repo.', default: process.cwd(), } ), + file: Flags.string( { + char: 'f', + description: 'Filename for change description JSON.', + default: 'changes.json', + } ), plugin: Flags.string( { char: 'p', description: 'Plugin to check for', @@ -133,9 +140,9 @@ export default class Analyzer extends Command { CliUx.ux.action.stop(); - this.scanChanges( diff, pluginData[ 0 ], flags.output, schemaDiff ); + this.scanChanges( diff, pluginData[ 0 ], flags, schemaDiff ); } else { - this.scanChanges( diff, pluginData[ 0 ], flags.output ); + this.scanChanges( diff, pluginData[ 0 ], flags ); } // Clean up the temporary repo. @@ -204,15 +211,16 @@ export default class Analyzer extends Command { /** * Scan patches for changes in templates, hooks and database schema * - * @param {string} content Patch content. - * @param {string} version Current product version. - * @param {string} output Output style. - * @param {boolean} schemaEquality if schemas are equal between branches. + * @param {string} content Patch content. + * @param {string} version Current product version. + * @param {string} output Output style. + * @param {string} changesFileName Name of a file to output change information to. + * @param {boolean} schemaEquality if schemas are equal between branches. */ - private scanChanges( + private async scanChanges( content: string, version: string, - output: string, + flags: OutputFlags< typeof Analyzer[ 'flags' ] >, schemaDiff: { [ key: string ]: { description: string; @@ -222,12 +230,19 @@ export default class Analyzer extends Command { areEqual: boolean; }; } | void - ): void { + ) { + const { output, file } = flags; CliUx.ux.action.start( 'Generating changes' ); const templates = this.scanTemplates( content, version ); const hooks = this.scanHooks( content, version, output ); const databaseUpdates = this.scanDatabases( content ); + await generateJSONFile( join( process.cwd(), file ), { + templates: Object.fromEntries( templates.entries() ), + hooks: Object.fromEntries( hooks.entries() ), + schema: databaseUpdates || {}, + } ); + if ( templates.size ) { printTemplateResults( templates, diff --git a/tools/code-analyzer/src/utils.ts b/tools/code-analyzer/src/utils.ts index de363e7fc74..7e6a7dd9460 100644 --- a/tools/code-analyzer/src/utils.ts +++ b/tools/code-analyzer/src/utils.ts @@ -4,6 +4,7 @@ import { createServer, Server } from 'net'; import { execSync } from 'child_process'; import { join } from 'path'; +import { writeFile } from 'fs/promises'; /** * Format version string for regex. @@ -234,3 +235,17 @@ export const getHookChangeType = ( diff: string ): 'Updated' | 'New' => { // If there is more than one 'since' in the diff, it means that line was updated meaning the hook already exists. return sinces.length > 1 ? 'Updated' : 'New'; }; + +export const generateJSONFile = ( filePath: string, data: unknown ) => { + const json = JSON.stringify( + data, + function replacer( key, value ) { + if ( value instanceof Map ) { + return Array.from( value.entries() ); + } + return value; + }, + 2 + ); + return writeFile( filePath, json ); +};