import { JobManifestV1Model, SchemaVersions } from '../../../domains/manifest/ManifestModel'; import { ResourceNotFoundError } from '@squiz/dx-common-lib'; import { MANIFEST_MODELS } from '@squiz/dx-json-schema-lib'; import path from 'path'; export class JobManifestV1 { constructor( protected manifest: JobManifestV1Model, protected manifestPath: string, ) {} public static getJobSchemaVersionRegex(): RegExp { return /\/schemas\/(?JobV\d+)\.json#?$/; } public getSchemaVersion(): SchemaVersions { return 'JobV1'; } public getModel(): JobManifestV1Model { return this.manifest; } public getJobFunctionByName(fnName: string = ''): MANIFEST_MODELS.JobV1.JobFunction { const manifestModel = this.getModel(); if (!fnName) { if (manifestModel.mainFunction) { fnName = manifestModel.mainFunction; } else { throw new ResourceNotFoundError('Job main function is not defined'); } } const functionDefinition = manifestModel.functions.find((a) => a.name === fnName); if (!functionDefinition) { throw new ResourceNotFoundError( `"${fnName}" function for ${this.getName()} ${this.getVersion()} could not be found`, ); } return functionDefinition; } public async getJobFunctionEntryFilePath(jobFunction: MANIFEST_MODELS.JobV1.JobFunction): Promise { const jobDirectory = this.getJobDirectory(); const inputPath = path.resolve(path.join(jobDirectory, jobFunction.entry)); return inputPath; } public getJobDirectory() { return path.resolve(path.join(this.manifestPath, '..')); } public getManifestPath() { return this.manifestPath; } public getName(): string { return this.manifest.name; } public getVersion(): string { return this.manifest.version; } }