|
|
@@ -7,6 +7,7 @@ import {
|
|
|
removeCwd,
|
|
|
replaceFileExtension,
|
|
|
getValueAtPath,
|
|
|
+ getHref,
|
|
|
expandTilde,
|
|
|
} from "./util/index.js"
|
|
|
import path from "path"
|
|
|
@@ -80,6 +81,19 @@ function buildPaginationMeta(page, totalPages, basePath) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+function getIndexPropertyLabel(propertyPath) {
|
|
|
+ if (!propertyPath) return "Index"
|
|
|
+ let label = propertyPath
|
|
|
+ label = label.replace(/^resources\./, "")
|
|
|
+ label = label.replace(/\.detail$/, "")
|
|
|
+ label = label.replace(/\.items$/, "")
|
|
|
+ const parts = label.split(".")
|
|
|
+ label = parts[parts.length - 1] || label
|
|
|
+ label = label.replace(/-taxonomy$/, "")
|
|
|
+ label = label.replace(/[-_]+/g, " ")
|
|
|
+ return label.replace(/\b\w/g, char => char.toUpperCase())
|
|
|
+}
|
|
|
+
|
|
|
export async function getConfig() {
|
|
|
const args = process.argv.slice(2)
|
|
|
const defaultPath = path.join(process.cwd(), "rhedyn.config.js")
|
|
|
@@ -259,6 +273,22 @@ async function expandStateTask(stateToExpand, task, jobConfig, actionConfig, met
|
|
|
const stateToProcess = selectState(stateToExpand, meta)
|
|
|
const pathsToStrip = getPathsToStrip(jobConfig)
|
|
|
const itemsPerPage = jobConfig.itemsPerPage ?? meta.opts.itemsPerPage
|
|
|
+ const shouldSetIndexTitle = jobConfig.buildFilePath && actionConfig.writeOut
|
|
|
+ const shouldBuildIndexList =
|
|
|
+ jobConfig.buildIndexList !== false &&
|
|
|
+ shouldSetIndexTitle
|
|
|
+ const indexListHref = shouldBuildIndexList
|
|
|
+ ? getHref(
|
|
|
+ buildOutputPath(
|
|
|
+ meta.opts.outDir,
|
|
|
+ jobConfig.outputDir,
|
|
|
+ pathsToStrip,
|
|
|
+ "index",
|
|
|
+ jobConfig.outputFileExtension,
|
|
|
+ ),
|
|
|
+ meta,
|
|
|
+ )
|
|
|
+ : null
|
|
|
|
|
|
// Build jobs, potentially with pagination
|
|
|
const jobs = stateToProcess.flatMap((stateJob, index) => {
|
|
|
@@ -294,12 +324,21 @@ async function expandStateTask(stateToExpand, task, jobConfig, actionConfig, met
|
|
|
? buildPaginationMeta(page, totalPages, basePath.replace(meta.opts.outDir, "/"))
|
|
|
: null
|
|
|
|
|
|
+ const propertyLabel = getIndexPropertyLabel(stateJob.property)
|
|
|
+ const indexOn = propertyLabel.toLowerCase()
|
|
|
+ const titleValue = stateJob.key != null ? String(stateJob.key) : String(index)
|
|
|
const decorations = {
|
|
|
...(Array.isArray(stateJob.value)
|
|
|
? { inputs: pageItems }
|
|
|
: { ...stateJob.value?.detail, inputs: pageItems }),
|
|
|
...(jobConfig.buildFilePath ? { fileOutputPath } : {}),
|
|
|
...(pagination ? { pagination } : {}),
|
|
|
+ ...(shouldSetIndexTitle && actionConfig.title == null
|
|
|
+ ? { title: `${propertyLabel}: ${titleValue}` }
|
|
|
+ : {}),
|
|
|
+ ...(indexListHref
|
|
|
+ ? { indexOn, indexPage: indexListHref }
|
|
|
+ : {}),
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
@@ -312,8 +351,7 @@ async function expandStateTask(stateToExpand, task, jobConfig, actionConfig, met
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- return await Promise.all(
|
|
|
- jobs.map(async ({ stateJob, page, decorations }) => {
|
|
|
+ const jobPromises = jobs.map(async ({ stateJob, page, decorations }) => {
|
|
|
const fileOutputDir = decorations.fileOutputPath
|
|
|
? path.dirname(decorations.fileOutputPath)
|
|
|
: undefined
|
|
|
@@ -332,8 +370,66 @@ async function expandStateTask(stateToExpand, task, jobConfig, actionConfig, met
|
|
|
actionConfig: jobActionConfig,
|
|
|
jobId: `${task.name} @ ${stateJob.property}:${stateJob.index}${pageInfo}`,
|
|
|
})
|
|
|
- }),
|
|
|
- )
|
|
|
+ })
|
|
|
+
|
|
|
+ if (shouldBuildIndexList) {
|
|
|
+ const indexPropertyLabel = getIndexPropertyLabel(stateToProcess[0]?.property)
|
|
|
+ const indexPages = stateToProcess.map((stateJob, index) => {
|
|
|
+ const key = stateJob.key ?? String(index)
|
|
|
+ const items = Array.isArray(stateJob.value)
|
|
|
+ ? stateJob.value
|
|
|
+ : stateJob.value?.detail || []
|
|
|
+ const totalPages = Array.isArray(items) && itemsPerPage > 0
|
|
|
+ ? Math.ceil(items.length / itemsPerPage)
|
|
|
+ : 1
|
|
|
+ const fileOutputPath = buildOutputPath(
|
|
|
+ meta.opts.outDir,
|
|
|
+ jobConfig.outputDir,
|
|
|
+ pathsToStrip,
|
|
|
+ key,
|
|
|
+ jobConfig.outputFileExtension,
|
|
|
+ )
|
|
|
+ return {
|
|
|
+ key,
|
|
|
+ title: String(key),
|
|
|
+ href: getHref(fileOutputPath, meta),
|
|
|
+ fileOutputPath,
|
|
|
+ count: Array.isArray(items) ? items.length : 0,
|
|
|
+ totalPages,
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ const indexFileOutputPath = buildOutputPath(
|
|
|
+ meta.opts.outDir,
|
|
|
+ jobConfig.outputDir,
|
|
|
+ pathsToStrip,
|
|
|
+ "index",
|
|
|
+ jobConfig.outputFileExtension,
|
|
|
+ )
|
|
|
+ const indexJobActionConfig = {
|
|
|
+ ...actionConfig,
|
|
|
+ inputs: indexPages,
|
|
|
+ indexPages,
|
|
|
+ isIndexList: true,
|
|
|
+ fileOutputPath: indexFileOutputPath,
|
|
|
+ fileOutputDir: path.dirname(indexFileOutputPath),
|
|
|
+ outputFileExtension: jobConfig.outputFileExtension,
|
|
|
+ }
|
|
|
+ if (indexJobActionConfig.title == null) {
|
|
|
+ indexJobActionConfig.title = `By: ${indexPropertyLabel}`
|
|
|
+ }
|
|
|
+ jobPromises.push(
|
|
|
+ runTask({
|
|
|
+ meta,
|
|
|
+ action: task.action,
|
|
|
+ jobConfig,
|
|
|
+ actionConfig: indexJobActionConfig,
|
|
|
+ jobId: `${task.name} @ index-list`,
|
|
|
+ }),
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ return await Promise.all(jobPromises)
|
|
|
}
|
|
|
|
|
|
export async function expandAndRunTask(meta, task) {
|
|
|
@@ -353,6 +449,7 @@ export async function expandAndRunTask(meta, task) {
|
|
|
...actionConfig,
|
|
|
inputs,
|
|
|
outputFileExtension: jobConfig.outputFileExtension,
|
|
|
+ ...(actionConfig.isIndexList == null ? { isIndexList: true } : {}),
|
|
|
}
|
|
|
const taskResult = await runTask({
|
|
|
meta,
|
|
|
@@ -402,6 +499,7 @@ export async function expandAndRunTask(meta, task) {
|
|
|
inputs,
|
|
|
...(fileOutputDir ? { fileOutputDir } : {}),
|
|
|
outputFileExtension: jobConfig.outputFileExtension,
|
|
|
+ ...(actionConfig.isIndexList == null ? { isIndexList: true } : {}),
|
|
|
}
|
|
|
const jobId = jobConfig.jobId || task.name
|
|
|
const taskResult = await runTask({
|
|
|
@@ -430,6 +528,7 @@ export async function expandAndRunTask(meta, task) {
|
|
|
inputs,
|
|
|
...(fileOutputDir ? { fileOutputDir } : {}),
|
|
|
outputFileExtension: jobConfig.outputFileExtension,
|
|
|
+ ...(actionConfig.isIndexList == null ? { isIndexList: true } : {}),
|
|
|
}
|
|
|
const jobId = jobConfig.jobId || task.name
|
|
|
const taskResult = await runTask({
|
|
|
@@ -476,6 +575,7 @@ export async function expandAndRunTask(meta, task) {
|
|
|
inputs: pageItems,
|
|
|
...(fileOutputDir ? { fileOutputDir } : {}),
|
|
|
outputFileExtension: jobConfig.outputFileExtension,
|
|
|
+ ...(actionConfig.isIndexList == null ? { isIndexList: true } : {}),
|
|
|
}
|
|
|
const pageInfo = page > 1 ? `:page-${page}` : ""
|
|
|
return runTask({
|