From 293a14dfc8b35b6e13682580ed97f1db0b2fd00f Mon Sep 17 00:00:00 2001 From: Tine Kondo Date: Wed, 26 Jun 2024 03:53:23 +0000 Subject: [PATCH] chore: improve `sync-preset-schemas` to only apply it on staged schema files --- tools/scripts/sync-preset-schemas.ts | 68 ++++++++++++++++++---------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/tools/scripts/sync-preset-schemas.ts b/tools/scripts/sync-preset-schemas.ts index 56cde428..33e88a60 100644 --- a/tools/scripts/sync-preset-schemas.ts +++ b/tools/scripts/sync-preset-schemas.ts @@ -1,33 +1,53 @@ -import { logger, readJsonFile, workspaceRoot, writeJsonFile } from "@nx/devkit"; -import { existsSync, readdirSync } from "fs"; -import { join, resolve } from "path"; - +import { logger, readJsonFile, workspaceRoot, writeJsonFile } from '@nx/devkit'; +import { execSync } from 'child_process'; +import { existsSync, readdirSync } from 'fs'; +import { join, resolve } from 'path'; +import { resolveModuleName } from 'typescript'; interface SchemaFile { - properties:Record; - required: string[]; + properties: Record; + required: string[]; } const packagesRoot = resolve(workspaceRoot, 'packages'); const presetSchemaFiles = readdirSync(packagesRoot) -.filter( folder => existsSync(join(packagesRoot, folder, '/src/generators/preset/schema.json'))) -.map(folder => join(packagesRoot, folder, '/src/generators/preset/schema.json')); + .filter((folder) => + existsSync(join(packagesRoot, folder, '/src/generators/preset/schema.json')) + ) + .map((folder) => + join(packagesRoot, folder, '/src/generators/preset/schema.json') + ); + +const changedFiles = execSync('git diff --cached --name-only') + .toString() + .trim() + .split(/\r?\n/); +const changedPresetSchemaFiles = changedFiles + .map((f) => join(workspaceRoot, f)) + .filter((f) => presetSchemaFiles.indexOf(f) != -1); // Sync the the schema.json file of 'preset' generators with the related 'project' generator's -presetSchemaFiles.forEach(presetSchemaFile => { - const presetSchemaJson = readJsonFile(presetSchemaFile); - const projetSchemaJson = readJsonFile(join(presetSchemaFile, '../../project/schema.json')); - - delete projetSchemaJson.properties['name']; - const {prjName} = presetSchemaJson.properties; - presetSchemaJson.properties = { - prjName, - ...projetSchemaJson.properties, - } - - presetSchemaJson.required = ['prjName', ...projetSchemaJson.required.filter(p=> p!== 'name')]; - - writeJsonFile(presetSchemaFile, presetSchemaJson); - - logger.info(`✅ Successfuly synced preset schema file at '${presetSchemaFile}'`); +changedPresetSchemaFiles.forEach((presetSchemaFile) => { + const presetSchemaJson = readJsonFile(presetSchemaFile); + const projetSchemaJson = readJsonFile( + join(presetSchemaFile, '../../project/schema.json') + ); + + delete projetSchemaJson.properties['name']; + const { prjName } = presetSchemaJson.properties; + presetSchemaJson.properties = { + prjName, + ...projetSchemaJson.properties, + }; + + presetSchemaJson.required = [ + 'prjName', + ...projetSchemaJson.required.filter((p) => p !== 'name'), + ]; + + writeJsonFile(presetSchemaFile, presetSchemaJson); + + logger.info( + `✅ Successfuly synced preset schema file at '${presetSchemaFile}'` + ); });