feat: vendors seeding script
This commit is contained in:
@@ -18,6 +18,7 @@ const token =
|
||||
process.env.STRAPI_TOKEN ??
|
||||
process.env.STRAPI_API_TOKEN ??
|
||||
process.env.AUTH_TOKEN;
|
||||
const isDryRun = process.argv.includes("--dry-run");
|
||||
|
||||
function readCategorySource(slug) {
|
||||
const filePath = path.join(categoriesDir, `${slug}.json`);
|
||||
@@ -113,7 +114,7 @@ async function createCategory(input) {
|
||||
return data?.createCategory;
|
||||
}
|
||||
|
||||
async function seedCategories(slugs) {
|
||||
async function seedCategories(slugs, dryRun = false) {
|
||||
let createdCount = 0;
|
||||
let skippedCount = 0;
|
||||
|
||||
@@ -133,24 +134,22 @@ async function seedCategories(slugs) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const created = await createCategory({
|
||||
slug,
|
||||
title,
|
||||
description,
|
||||
group,
|
||||
});
|
||||
if (dryRun) {
|
||||
createdCount += 1;
|
||||
console.log(`DRY-RUN CREATE ${slug} -> ${title} [group=${group}]`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const created = await createCategory({ slug, title, description, group });
|
||||
createdCount += 1;
|
||||
console.log(
|
||||
`CREATE ${created.slug} -> ${created.title} [group=${created.group}]`
|
||||
);
|
||||
console.log(`CREATE ${created.slug} -> ${created.title} [group=${created.group}]`);
|
||||
}
|
||||
|
||||
return { createdCount, skippedCount };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log(`Seeding categories to ${endpoint}`);
|
||||
console.log(`Seeding categories to ${endpoint}${isDryRun ? " (dry-run)" : ""}`);
|
||||
if (!token) {
|
||||
console.warn(
|
||||
"No STRAPI token provided. If endpoint is protected, set STRAPI_TOKEN."
|
||||
@@ -158,7 +157,7 @@ async function main() {
|
||||
}
|
||||
|
||||
const slugs = discoverCategorySlugs();
|
||||
const { createdCount, skippedCount } = await seedCategories(slugs);
|
||||
const { createdCount, skippedCount } = await seedCategories(slugs, isDryRun);
|
||||
|
||||
console.log(
|
||||
`Done. created=${createdCount}, skipped=${skippedCount}, total=${createdCount + skippedCount}`
|
||||
|
||||
Reference in New Issue
Block a user