Restructure software catalog
This commit is contained in:
+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