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}'` + ); });