Page MenuHomeSealhub

add-collection.ts
No OneTemporary

add-collection.ts

import { resolve } from "path";
import _locreq from "locreq";
import { promises as fs, existsSync } from "fs";
import { collectionTemplate } from "./templates/collection.js";
import { generateCollections } from "./generate-collections.js";
import { toKebabCase, toPascalCase } from "js-convert-case";
const target_locreq = _locreq(process.cwd());
import prompts from "prompts";
import { formatWithPrettier } from "./utils/prettier.js";
export async function addCollection(
params: Record<string, string | boolean> = {}
): Promise<void> {
prompts.override(params);
const response = await prompts([
{
type: "text",
name: "collection_name",
message: "What's the name of the collection class: ",
validate: (s: string) =>
s.length > 3 ? true : "Should be at least 3 characters long",
format: function (action: string) {
return toPascalCase(action);
},
},
]);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const collection_name: string = response.collection_name;
if (!collection_name) {
throw new Error("Empty collection name, please try again");
}
const file_path = target_locreq.resolve(
`src/back/collections/${toKebabCase(collection_name)}.ts`
);
if (existsSync(file_path)) {
// eslint-disable-next-line no-console
console.error(`ERROR: File ${file_path} already exists.`);
return;
}
await fs.mkdir(resolve(file_path, "../"), { recursive: true });
await fs.writeFile(
file_path,
await formatWithPrettier(collectionTemplate(collection_name))
);
// eslint-disable-next-line no-console
console.log(`${file_path} created`);
await generateCollections();
}

File Metadata

Mime Type
text/x-java
Expires
Fri, Jan 24, 15:15 (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
598555
Default Alt Text
add-collection.ts (1 KB)

Event Timeline