Restructure software catalog
This commit is contained in:
@@ -15,10 +15,11 @@ import {
|
||||
} from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import process from "node:process";
|
||||
import { parseDocument } from "yaml";
|
||||
import { isMap, parseDocument } from "yaml";
|
||||
|
||||
type CatalogFile = {
|
||||
path?: unknown;
|
||||
size?: unknown;
|
||||
sha256?: unknown;
|
||||
};
|
||||
|
||||
@@ -38,16 +39,21 @@ type FileReference = {
|
||||
yamlPath: Array<string | number>;
|
||||
};
|
||||
|
||||
type ResolvedCatalogFile = {
|
||||
path: string;
|
||||
size: number;
|
||||
};
|
||||
|
||||
const ignoredDirectories = new Set([".git", "node_modules", "files", "img"]);
|
||||
|
||||
function usage(): void {
|
||||
console.log(`Использование: pnpm sha256 [--check] [путь ...]
|
||||
console.log(`Usage: pnpm fill-metadata [--check] [path ...]
|
||||
|
||||
Без путей скрипт обходит текущий каталог. Путь может указывать на index.yaml
|
||||
или на папку, внутри которой нужно найти index.yaml.
|
||||
With no paths, the script scans the current directory. A path may point to an
|
||||
index.yaml file or a directory containing index.yaml files.
|
||||
|
||||
--check только проверить суммы, не изменяя YAML
|
||||
--help показать эту справку`);
|
||||
--check verify checksums and file sizes without modifying YAML
|
||||
--help show this help message`);
|
||||
}
|
||||
|
||||
function parseArguments(args: string[]): Options {
|
||||
@@ -61,7 +67,7 @@ function parseArguments(args: string[]): Options {
|
||||
usage();
|
||||
process.exit(0);
|
||||
} else if (argument.startsWith("-")) {
|
||||
throw new Error(`Неизвестный параметр: ${argument}`);
|
||||
throw new Error(`Unknown option: ${argument}`);
|
||||
} else {
|
||||
targets.push(argument);
|
||||
}
|
||||
@@ -76,14 +82,14 @@ async function findIndexes(target: string, result: Set<string>): Promise<void> {
|
||||
|
||||
if (targetStat.isFile()) {
|
||||
if (path.basename(absoluteTarget) !== "index.yaml") {
|
||||
throw new Error(`Ожидался index.yaml: ${target}`);
|
||||
throw new Error(`Expected an index.yaml file: ${target}`);
|
||||
}
|
||||
result.add(absoluteTarget);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!targetStat.isDirectory()) {
|
||||
throw new Error(`Путь не является файлом или каталогом: ${target}`);
|
||||
throw new Error(`Path is neither a file nor a directory: ${target}`);
|
||||
}
|
||||
|
||||
const entries = await readdir(absoluteTarget, { withFileTypes: true });
|
||||
@@ -119,17 +125,20 @@ function collectFileReferences(data: ProgramIndex): FileReference[] {
|
||||
return references;
|
||||
}
|
||||
|
||||
async function resolveCatalogFile(indexPath: string, value: unknown): Promise<string> {
|
||||
async function resolveCatalogFile(
|
||||
indexPath: string,
|
||||
value: unknown,
|
||||
): Promise<ResolvedCatalogFile> {
|
||||
if (typeof value !== "string" || value.length === 0) {
|
||||
throw new Error("поле path отсутствует или не является строкой");
|
||||
throw new Error("the path field is missing or is not a string");
|
||||
}
|
||||
if (path.isAbsolute(value) || value.includes("\\")) {
|
||||
throw new Error(`недопустимый путь: ${value}`);
|
||||
throw new Error(`invalid path: ${value}`);
|
||||
}
|
||||
|
||||
const parts = value.split("/");
|
||||
if (parts[0] !== "files" || parts.some((part) => part === ".." || part === "")) {
|
||||
throw new Error(`путь должен находиться внутри files/: ${value}`);
|
||||
throw new Error(`path must be inside files/: ${value}`);
|
||||
}
|
||||
|
||||
const programDirectory = path.dirname(indexPath);
|
||||
@@ -137,7 +146,7 @@ async function resolveCatalogFile(indexPath: string, value: unknown): Promise<st
|
||||
const filePath = path.resolve(programDirectory, ...parts);
|
||||
const relativePath = path.relative(filesDirectory, filePath);
|
||||
if (relativePath.startsWith("..") || path.isAbsolute(relativePath)) {
|
||||
throw new Error(`путь выходит за пределы files/: ${value}`);
|
||||
throw new Error(`path escapes files/: ${value}`);
|
||||
}
|
||||
|
||||
const [realFilesDirectory, realFilePath] = await Promise.all([
|
||||
@@ -146,13 +155,14 @@ async function resolveCatalogFile(indexPath: string, value: unknown): Promise<st
|
||||
]);
|
||||
const realRelativePath = path.relative(realFilesDirectory, realFilePath);
|
||||
if (realRelativePath.startsWith("..") || path.isAbsolute(realRelativePath)) {
|
||||
throw new Error(`символическая ссылка выходит за пределы files/: ${value}`);
|
||||
throw new Error(`symbolic link escapes files/: ${value}`);
|
||||
}
|
||||
if (!(await stat(realFilePath)).isFile()) {
|
||||
throw new Error(`путь не указывает на обычный файл: ${value}`);
|
||||
const fileStat = await stat(realFilePath);
|
||||
if (!fileStat.isFile()) {
|
||||
throw new Error(`path does not point to a regular file: ${value}`);
|
||||
}
|
||||
|
||||
return realFilePath;
|
||||
return { path: realFilePath, size: fileStat.size };
|
||||
}
|
||||
|
||||
async function calculateSha256(filePath: string): Promise<string> {
|
||||
@@ -179,6 +189,29 @@ async function writeAtomically(filePath: string, contents: string): Promise<void
|
||||
}
|
||||
}
|
||||
|
||||
function setFileSize(
|
||||
document: ReturnType<typeof parseDocument>,
|
||||
yamlPath: Array<string | number>,
|
||||
size: number,
|
||||
): void {
|
||||
document.setIn([...yamlPath, "size"], size);
|
||||
|
||||
const fileMap = document.getIn(yamlPath, true);
|
||||
if (!isMap(fileMap)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const keyName = (pair: (typeof fileMap.items)[number]): string => String(pair.key);
|
||||
const sizeIndex = fileMap.items.findIndex((pair) => keyName(pair) === "size");
|
||||
const sha256Index = fileMap.items.findIndex((pair) => keyName(pair) === "sha256");
|
||||
if (sizeIndex < 0 || sha256Index < 0 || sizeIndex < sha256Index) {
|
||||
return;
|
||||
}
|
||||
|
||||
const [sizePair] = fileMap.items.splice(sizeIndex, 1);
|
||||
fileMap.items.splice(sha256Index, 0, sizePair);
|
||||
}
|
||||
|
||||
async function processIndex(indexPath: string, check: boolean): Promise<number> {
|
||||
const source = await readFile(indexPath, "utf8");
|
||||
const document = parseDocument(source, { prettyErrors: true });
|
||||
@@ -198,8 +231,12 @@ async function processIndex(indexPath: string, check: boolean): Promise<number>
|
||||
const results = await Promise.all(
|
||||
references.map(async (reference) => {
|
||||
try {
|
||||
const filePath = await resolveCatalogFile(indexPath, reference.data.path);
|
||||
return { reference, digest: await calculateSha256(filePath) };
|
||||
const file = await resolveCatalogFile(indexPath, reference.data.path);
|
||||
return {
|
||||
reference,
|
||||
digest: await calculateSha256(file.path),
|
||||
size: file.size,
|
||||
};
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
errors.push(`${String(reference.data.path)}: ${message}`);
|
||||
@@ -213,21 +250,30 @@ async function processIndex(indexPath: string, check: boolean): Promise<number>
|
||||
}
|
||||
|
||||
for (const result of results) {
|
||||
if (result === undefined || result.reference.data.sha256 === result.digest) {
|
||||
if (result === undefined) {
|
||||
continue;
|
||||
}
|
||||
changed += 1;
|
||||
if (!check) {
|
||||
document.setIn([...result.reference.yamlPath, "sha256"], result.digest);
|
||||
|
||||
if (result.reference.data.size !== result.size) {
|
||||
changed += 1;
|
||||
if (!check) {
|
||||
setFileSize(document, result.reference.yamlPath, result.size);
|
||||
}
|
||||
}
|
||||
if (result.reference.data.sha256 !== result.digest) {
|
||||
changed += 1;
|
||||
if (!check) {
|
||||
document.setIn([...result.reference.yamlPath, "sha256"], result.digest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const displayPath = path.relative(process.cwd(), indexPath) || indexPath;
|
||||
if (changed > 0 && check) {
|
||||
console.error(`${displayPath}: требуется обновить SHA-256 (${changed})`);
|
||||
console.error(`${displayPath}: metadata update required (${changed} fields)`);
|
||||
} else if (changed > 0) {
|
||||
await writeAtomically(indexPath, document.toString());
|
||||
console.log(`${displayPath}: обновлено SHA-256 (${changed})`);
|
||||
await writeAtomically(indexPath, document.toString({ lineWidth: 0 }));
|
||||
console.log(`${displayPath}: updated file metadata (${changed} fields)`);
|
||||
}
|
||||
|
||||
return changed;
|
||||
@@ -255,16 +301,16 @@ async function main(): Promise<void> {
|
||||
}
|
||||
|
||||
if (failed > 0) {
|
||||
console.error(`Ошибок: ${failed}`);
|
||||
console.error(`Errors: ${failed}`);
|
||||
process.exitCode = 1;
|
||||
} else if (options.check && changed > 0) {
|
||||
console.error(`Файлов с несовпадениями: ${changed}`);
|
||||
console.error(`File metadata mismatches: ${changed}`);
|
||||
process.exitCode = 1;
|
||||
} else {
|
||||
console.log(
|
||||
changed > 0
|
||||
? `Готово, обновлено полей: ${changed}`
|
||||
: `Готово, все SHA-256 актуальны. Проверено index.yaml: ${indexes.size}`,
|
||||
? `Done. Updated fields: ${changed}`
|
||||
: `Done. All SHA-256 checksums and file sizes are up to date. Checked index.yaml files: ${indexes.size}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
+28
-18
@@ -10,6 +10,7 @@ type CatalogType = "category" | "program";
|
||||
type CatalogIndex = {
|
||||
type?: unknown;
|
||||
name?: unknown;
|
||||
order?: unknown;
|
||||
versions?: Array<{ version?: unknown; status?: unknown }>;
|
||||
};
|
||||
|
||||
@@ -21,6 +22,7 @@ type CatalogVersion = {
|
||||
type CatalogNode = {
|
||||
type: CatalogType;
|
||||
name: string;
|
||||
order: number;
|
||||
directory: string;
|
||||
versions: CatalogVersion[];
|
||||
children: CatalogNode[];
|
||||
@@ -38,14 +40,14 @@ const collator = new Intl.Collator("ru", {
|
||||
});
|
||||
|
||||
function usage(): void {
|
||||
console.log(`Использование: pnpm tree [параметры] [путь]
|
||||
console.log(`Usage: pnpm tree [options] [path]
|
||||
|
||||
Без пути выводится всё дерево каталога. Путь может указывать на папку категории,
|
||||
программы или на её index.yaml.
|
||||
With no path, the script prints the complete tree from catalog. A path may point
|
||||
to a category directory, a program directory, or its index.yaml file.
|
||||
|
||||
--versions вывести версии под каждой программой
|
||||
--paths вывести относительные пути каталогов
|
||||
--help показать эту справку`);
|
||||
--versions show versions under each program
|
||||
--paths show relative directory paths
|
||||
--help show this help message`);
|
||||
}
|
||||
|
||||
function parseArguments(args: string[]): Options {
|
||||
@@ -62,20 +64,20 @@ function parseArguments(args: string[]): Options {
|
||||
usage();
|
||||
process.exit(0);
|
||||
} else if (argument.startsWith("-")) {
|
||||
throw new Error(`Неизвестный параметр: ${argument}`);
|
||||
throw new Error(`Unknown option: ${argument}`);
|
||||
} else {
|
||||
targets.push(argument);
|
||||
}
|
||||
}
|
||||
|
||||
if (targets.length > 1) {
|
||||
throw new Error("Можно указать только один путь");
|
||||
throw new Error("Only one path may be specified");
|
||||
}
|
||||
|
||||
return {
|
||||
showPaths,
|
||||
showVersions,
|
||||
target: targets[0] ?? ".",
|
||||
target: targets[0] ?? "catalog",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -83,6 +85,9 @@ function compareNodes(left: CatalogNode, right: CatalogNode): number {
|
||||
if (left.type !== right.type) {
|
||||
return left.type === "category" ? -1 : 1;
|
||||
}
|
||||
if (left.type === "category" && left.order !== right.order) {
|
||||
return left.order - right.order;
|
||||
}
|
||||
return collator.compare(left.name, right.name);
|
||||
}
|
||||
|
||||
@@ -96,7 +101,7 @@ async function resolveDirectory(target: string): Promise<string> {
|
||||
if (targetStat.isFile() && path.basename(absoluteTarget) === "index.yaml") {
|
||||
return path.dirname(absoluteTarget);
|
||||
}
|
||||
throw new Error(`Ожидалась папка каталога или index.yaml: ${target}`);
|
||||
throw new Error(`Expected a catalog directory or index.yaml file: ${target}`);
|
||||
}
|
||||
|
||||
async function readCatalogNode(directory: string): Promise<CatalogNode> {
|
||||
@@ -107,7 +112,7 @@ async function readCatalogNode(directory: string): Promise<CatalogNode> {
|
||||
source = await readFile(indexPath, "utf8");
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : String(error);
|
||||
throw new Error(`${indexPath}: не удалось прочитать index.yaml: ${reason}`);
|
||||
throw new Error(`${indexPath}: failed to read index.yaml: ${reason}`);
|
||||
}
|
||||
|
||||
const document = parseDocument(source, { prettyErrors: true });
|
||||
@@ -118,24 +123,29 @@ async function readCatalogNode(directory: string): Promise<CatalogNode> {
|
||||
|
||||
const data = document.toJS() as CatalogIndex;
|
||||
if (data.type !== "category" && data.type !== "program") {
|
||||
throw new Error(`${indexPath}: неизвестный type`);
|
||||
throw new Error(`${indexPath}: unknown type`);
|
||||
}
|
||||
if (typeof data.name !== "string" || data.name.length === 0) {
|
||||
throw new Error(`${indexPath}: отсутствует название`);
|
||||
throw new Error(`${indexPath}: name is missing`);
|
||||
}
|
||||
if (data.type === "category" && data.order !== undefined && !Number.isInteger(data.order)) {
|
||||
throw new Error(`${indexPath}: category order must be an integer`);
|
||||
}
|
||||
|
||||
const order = data.type === "category" && typeof data.order === "number" ? data.order : 0;
|
||||
|
||||
const versions =
|
||||
data.type === "program"
|
||||
? (data.versions ?? []).map((entry, index) => {
|
||||
if (typeof entry.version !== "string" || entry.version.length === 0) {
|
||||
throw new Error(`${indexPath}: некорректная версия с индексом ${index}`);
|
||||
throw new Error(`${indexPath}: invalid version at index ${index}`);
|
||||
}
|
||||
if (
|
||||
entry.status !== undefined &&
|
||||
entry.status !== "current" &&
|
||||
entry.status !== "archived"
|
||||
) {
|
||||
throw new Error(`${indexPath}: неизвестный статус версии ${entry.version}`);
|
||||
throw new Error(`${indexPath}: unknown status for version ${entry.version}`);
|
||||
}
|
||||
return { version: entry.version, status: entry.status } as CatalogVersion;
|
||||
})
|
||||
@@ -165,7 +175,7 @@ async function readCatalogNode(directory: string): Promise<CatalogNode> {
|
||||
}
|
||||
|
||||
children.sort(compareNodes);
|
||||
return { type: data.type, name: data.name, directory, versions, children };
|
||||
return { type: data.type, name: data.name, order, directory, versions, children };
|
||||
}
|
||||
|
||||
function formatNode(node: CatalogNode, showPaths: boolean): string {
|
||||
@@ -190,7 +200,7 @@ function printChildren(
|
||||
if (options.showVersions && node.type === "program") {
|
||||
entries.push(
|
||||
...node.versions.map(({ version, status }) => ({
|
||||
label: `версия ${version}${status === undefined ? "" : ` [${status}]`}`,
|
||||
label: `version ${version}${status === undefined ? "" : ` [${status}]`}`,
|
||||
})),
|
||||
);
|
||||
}
|
||||
@@ -217,6 +227,6 @@ try {
|
||||
await main();
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.error(`Ошибка: ${message}`);
|
||||
console.error(`Error: ${message}`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user