@c-time/frelio-data-json-generator 2.2.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { validateSiteRecipe, formatZodErrors } from '@c-time/frelio-types/schemas';
3
3
  import { findAffectedOutputs } from '@c-time/frelio-dependency-map';
4
4
  import { readFile } from 'fs/promises';
5
- import { existsSync, readFileSync, mkdirSync, unlinkSync, writeFileSync } from 'fs';
5
+ import { existsSync, readFileSync, mkdirSync, writeFileSync } from 'fs';
6
6
  import { glob } from 'glob';
7
7
  import { exec } from 'child_process';
8
8
  import { promisify, parseArgs } from 'util';
@@ -158,7 +158,7 @@ function calculateRegenerationScope(changes, dependencyMap, recipe) {
158
158
  const targets = [];
159
159
  const seenOutputPaths = /* @__PURE__ */ new Set();
160
160
  const addTarget = (target) => {
161
- const key = `${target.outputPath}:${target.contentId ?? ""}`;
161
+ const key = `${target.reason}:${target.outputPath}:${target.contentId ?? ""}`;
162
162
  if (!seenOutputPaths.has(key)) {
163
163
  seenOutputPaths.add(key);
164
164
  targets.push(target);
@@ -956,7 +956,7 @@ async function processListRecipes(contentTypeId, recipes, targets, context) {
956
956
  }
957
957
  return outputs;
958
958
  }
959
- async function processListWithoutPagination(contentTypeId, recipe, context) {
959
+ async function buildListPageData(contentTypeId, recipe, context) {
960
960
  let contents = context.contentsByType.get(contentTypeId) ?? [];
961
961
  contents = applyFilters(contents, recipe.filters);
962
962
  contents = applySort(contents, recipe.sort);
@@ -967,6 +967,10 @@ async function processListWithoutPagination(contentTypeId, recipe, context) {
967
967
  contents = contents.slice(0, recipe.limit);
968
968
  }
969
969
  const items = await processListItems(contents, recipe, context);
970
+ return { items, totalCount: items.length };
971
+ }
972
+ async function processListWithoutPagination(contentTypeId, recipe, context) {
973
+ const { items, totalCount } = await buildListPageData(contentTypeId, recipe, context);
970
974
  const outputPath = sanitizePath(recipe.outputPath);
971
975
  const pathError = validateOutputPath(outputPath);
972
976
  if (pathError) {
@@ -986,7 +990,7 @@ async function processListWithoutPagination(contentTypeId, recipe, context) {
986
990
  outputFile: toHtmlOutputPath(outputPath),
987
991
  data: {
988
992
  items,
989
- totalCount: items.length
993
+ totalCount
990
994
  }
991
995
  };
992
996
  return {
@@ -1338,19 +1342,42 @@ function extractRequiredContentTypes(recipe) {
1338
1342
  async function detectDeletedOutputs(generatedOutputs, outputRootPath, fileSystem, targets, fullRebuild, logger) {
1339
1343
  const deleteOutputs = [];
1340
1344
  if (!fullRebuild) {
1341
- const deleteTargets = targets.filter((t) => t.reason === "deleted");
1342
- for (const target of deleteTargets) {
1343
- const generated = generatedOutputs.some((o) => o.path === target.outputPath);
1344
- if (!generated) {
1345
- deleteOutputs.push({
1346
- path: target.outputPath,
1347
- content: {
1348
- type: "delete",
1349
- outputFile: toHtmlOutputPath(target.outputPath)
1350
- }
1351
- });
1352
- logger.debug(`Marked for deletion: ${target.outputPath}`);
1345
+ const deletedContentIds = new Set(
1346
+ targets.filter((t) => t.reason === "deleted" && t.contentId).map((t) => t.contentId)
1347
+ );
1348
+ if (deletedContentIds.size === 0) {
1349
+ return deleteOutputs;
1350
+ }
1351
+ const generatedPaths = new Set(generatedOutputs.map((o) => o.path));
1352
+ try {
1353
+ const existingFiles = await fileSystem.listFiles(`${outputRootPath}/**/*.json`);
1354
+ const existingOutputPaths = existingFiles.filter((f) => !f.endsWith("report.json")).map((f) => f.replace(`${outputRootPath}/`, ""));
1355
+ for (const existingPath of existingOutputPaths) {
1356
+ if (generatedPaths.has(existingPath)) {
1357
+ continue;
1358
+ }
1359
+ let contentId;
1360
+ try {
1361
+ const raw = await fileSystem.readFile(`${outputRootPath}/${existingPath}`);
1362
+ const parsed = JSON.parse(raw);
1363
+ const value = parsed?.data?.contentId;
1364
+ contentId = typeof value === "string" ? value : void 0;
1365
+ } catch {
1366
+ continue;
1367
+ }
1368
+ if (contentId && deletedContentIds.has(contentId)) {
1369
+ deleteOutputs.push({
1370
+ path: existingPath,
1371
+ content: {
1372
+ type: "delete",
1373
+ outputFile: toHtmlOutputPath(existingPath)
1374
+ }
1375
+ });
1376
+ logger.debug(`Marked for deletion (unpublished/deleted): ${existingPath}`);
1377
+ }
1353
1378
  }
1379
+ } catch {
1380
+ logger.debug("Could not list existing files for deletion detection");
1354
1381
  }
1355
1382
  return deleteOutputs;
1356
1383
  }
@@ -1563,17 +1590,9 @@ async function main() {
1563
1590
  if (!options.dryRun) {
1564
1591
  mkdirSync(OUTPUT_ROOT, { recursive: true });
1565
1592
  for (const output of result.outputs) {
1566
- if (output.content.type === "delete") {
1567
- const fullPath = join(OUTPUT_ROOT, output.path);
1568
- if (existsSync(fullPath)) unlinkSync(fullPath);
1569
- }
1570
- }
1571
- for (const output of result.outputs) {
1572
- if (output.content.type !== "delete") {
1573
- const fullPath = join(OUTPUT_ROOT, output.path);
1574
- mkdirSync(dirname(fullPath), { recursive: true });
1575
- writeFileSync(fullPath, JSON.stringify(output.content, null, 2));
1576
- }
1593
+ const fullPath = join(OUTPUT_ROOT, output.path);
1594
+ mkdirSync(dirname(fullPath), { recursive: true });
1595
+ writeFileSync(fullPath, JSON.stringify(output.content, null, 2));
1577
1596
  }
1578
1597
  }
1579
1598
  mkdirSync(dirname(REPORT_PATH), { recursive: true });