Skip to content

Commit

Permalink
chore: improve sync-preset-schemas to only apply it on staged schem…
Browse files Browse the repository at this point in the history
…a files
  • Loading branch information
tinesoft committed Jun 26, 2024
1 parent 23be1a6 commit 293a14d
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions tools/scripts/sync-preset-schemas.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>;
required: string[];
properties: Record<string, unknown>;
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<SchemaFile>(presetSchemaFile);
const projetSchemaJson = readJsonFile<SchemaFile>(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<SchemaFile>(presetSchemaFile);
const projetSchemaJson = readJsonFile<SchemaFile>(
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}'`
);
});

0 comments on commit 293a14d

Please sign in to comment.