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 = {} ): Promise { 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); }, }, ]); const collection_name: string = response.collection_name as string; 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)) { 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(); }