|
|
@@ -9,7 +9,6 @@ import {
|
|
|
removeBasePaths,
|
|
|
removeCwd,
|
|
|
replaceFileExtension,
|
|
|
- slugifyString,
|
|
|
} from "./util.js"
|
|
|
import path from "path"
|
|
|
import process from "node:process"
|
|
|
@@ -30,16 +29,74 @@ export async function getConfig() {
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
+async function runTask({ meta, config, jobId }) {
|
|
|
+ const log = getLogger(meta.opts.logLevel, jobId)
|
|
|
+ log.trace(`meta: ${JSON.stringify(meta, null, 2)}`)
|
|
|
+ log.trace(`config: ${JSON.stringify(config, null, 2)}`)
|
|
|
|
|
|
-export async function processFiles(config, meta) {
|
|
|
- const includes = meta.opts?.include?.[config.name] || []
|
|
|
- const patternsToInclude = [...config.inputFiles, ...includes]
|
|
|
+ const stateObject = {
|
|
|
+ meta,
|
|
|
+ config,
|
|
|
+ }
|
|
|
+ const cache = (meta.opts.cacheDir && !config.skipCache) ?
|
|
|
+ await checkCache(
|
|
|
+ jobId,
|
|
|
+ stateObject,
|
|
|
+ meta.opts,
|
|
|
+ )
|
|
|
+ :
|
|
|
+ { disabled: true, reason: "Cache disabled" }
|
|
|
+
|
|
|
+ if (cache && cache.hit) {
|
|
|
+ log.debug(`Loaded cache for ${jobId}`)
|
|
|
+ return { ...cache.taskResult, fromCache: true }
|
|
|
+ }
|
|
|
+ log.debug(`Cache miss for ${jobId} (${cache.reason})`)
|
|
|
+
|
|
|
+ const state = meta.opts.cacheDir
|
|
|
+ ? createTrackedObject(stateObject)
|
|
|
+ : { proxy: stateObject }
|
|
|
+
|
|
|
+ const {
|
|
|
+ detail,
|
|
|
+ paths = [],
|
|
|
+ deps: processorDeps,
|
|
|
+ ref,
|
|
|
+ } = await config.processor(state.proxy)
|
|
|
+
|
|
|
+ const taskResult = {
|
|
|
+ detail,
|
|
|
+ paths: paths.map(fileOutputPath => fileOutputPath.replace(meta.opts.outDir, "")),
|
|
|
+ ref,
|
|
|
+ }
|
|
|
+ log.debug(`Wrote ${taskResult.paths.length} files for ${jobId}`)
|
|
|
+ if (cache && !cache.disabled) {
|
|
|
+ log.debug(`Updating cache for ${jobId}`)
|
|
|
+ const processorPathDeps = processorDeps?.paths || []
|
|
|
+ const processorStateDeps = processorDeps?.state || []
|
|
|
+ const configPathDeps = config.deps?.paths || []
|
|
|
+ const configStateDeps = config.deps?.state || []
|
|
|
+ await updateCache(
|
|
|
+ meta.opts.cacheDir,
|
|
|
+ jobId,
|
|
|
+ removeCwd([
|
|
|
+ ...configPathDeps, ...processorPathDeps, config?.filePath,
|
|
|
+ ].filter(item => !!item)),
|
|
|
+ [
|
|
|
+ ...configStateDeps, ...processorStateDeps, ...(state?.accessed || []),
|
|
|
+ ].filter(item => !!item),
|
|
|
+ taskResult,
|
|
|
+ cache.updates,
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ return taskResult
|
|
|
+}
|
|
|
+
|
|
|
+async function processFileTask(patternsToInclude, config, meta) {
|
|
|
const filesToProcess = await readFilesByGlob(patternsToInclude)
|
|
|
const pathsToStrip = config.stripPaths || []
|
|
|
const outputDir = config.outputDir || ""
|
|
|
- const configPathDeps = config.deps?.paths || []
|
|
|
- const configStateDeps = config.deps?.state || []
|
|
|
- const log = getLogger(meta.opts.logLevel)
|
|
|
return await Promise.all(
|
|
|
filesToProcess.map(async filePath => {
|
|
|
const fileOutputPath = path.join(
|
|
|
@@ -52,66 +109,23 @@ export async function processFiles(config, meta) {
|
|
|
)
|
|
|
|
|
|
const fileOutputDir = path.dirname(fileOutputPath)
|
|
|
-
|
|
|
- const stateObject = {
|
|
|
- filePath,
|
|
|
- meta,
|
|
|
- fileOutputDir,
|
|
|
- fileOutputPath,
|
|
|
- config,
|
|
|
- }
|
|
|
-
|
|
|
- let cache = {}
|
|
|
- if (meta.opts.cacheDir) {
|
|
|
- cache = await checkCache(
|
|
|
- slugifyString(filePath),
|
|
|
- stateObject,
|
|
|
- meta.opts,
|
|
|
- )
|
|
|
-
|
|
|
- if (cache && cache.hit) {
|
|
|
- log.debug(`Loaded cache for ${filePath}`)
|
|
|
- return { ...cache.taskResult, fromCache: true }
|
|
|
- }
|
|
|
- log.debug(`Cache miss for ${filePath} (${cache.reason})`)
|
|
|
+ const jobConfig = {
|
|
|
+ ...config,
|
|
|
+ filePath, fileOutputDir, fileOutputPath,
|
|
|
}
|
|
|
+ return runTask({ meta, config: jobConfig, jobId: `${config.name} @ ${filePath}` })
|
|
|
+ }),
|
|
|
+ )}
|
|
|
|
|
|
- const state = meta.opts.cacheDir
|
|
|
- ? createTrackedObject(stateObject)
|
|
|
- : { proxy: stateObject }
|
|
|
-
|
|
|
- const {
|
|
|
- detail,
|
|
|
- paths,
|
|
|
- deps: processorDeps,
|
|
|
- ref,
|
|
|
- } = await config.processor(state.proxy)
|
|
|
+export async function processTask(config, meta) {
|
|
|
+ const includes = meta.opts?.include?.[config.name] || []
|
|
|
+ const patternsToInclude = [...(config?.inputFiles || []), ...includes]
|
|
|
|
|
|
- const taskResult = {
|
|
|
- detail,
|
|
|
- paths: paths.map(fileOutputPath => fileOutputPath.replace(meta.opts.outDir, "")),
|
|
|
- ref,
|
|
|
- }
|
|
|
- log.debug(`Wrote ${taskResult.paths.length} files for ${filePath}`)
|
|
|
- if (meta.opts.cacheDir) {
|
|
|
- log.debug(`Updating cache for ${filePath}`)
|
|
|
- const processorPathDeps = processorDeps?.paths || []
|
|
|
- const processorStateDeps = processorDeps?.state || []
|
|
|
- await updateCache(
|
|
|
- meta.opts.cacheDir,
|
|
|
- slugifyString(filePath),
|
|
|
- removeCwd([
|
|
|
- ...configPathDeps, ...processorPathDeps, filePath,
|
|
|
- ]),
|
|
|
- [
|
|
|
- ...configStateDeps, ...processorStateDeps, ...state.accessed,
|
|
|
- ],
|
|
|
- taskResult,
|
|
|
- cache.updates,
|
|
|
- )
|
|
|
- }
|
|
|
+ if (patternsToInclude.length) {
|
|
|
+ return processFileTask(patternsToInclude, config, meta)
|
|
|
+ }
|
|
|
|
|
|
- return taskResult
|
|
|
- }),
|
|
|
- )
|
|
|
+ const jobId = config.jobId || config.name
|
|
|
+ const taskResult = await runTask({ meta, config, jobId })
|
|
|
+ return [taskResult]
|
|
|
}
|