Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Merge e5b4024 into a925931
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Feb 25, 2021
2 parents a925931 + e5b4024 commit a23dc93
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const parser = require('./lib/parser')
const { version } = require(path.join(__dirname, 'package.json'))
const ChecksCollection = require('./lib/checksCollection.js')
const { removeFilePart } = require('./lib/utils')
const validateManifest = require('./lib/manifest')

const program = new Command()
program.version(version)

async function main (file, options) {
async function validatePipeline (file, options) {
const pipelineFile = path.resolve(file)
const pipelineDir = removeFilePart(pipelineFile)
const checks = new ChecksCollection()
Expand Down Expand Up @@ -46,11 +47,12 @@ async function main (file, options) {
program
.arguments('<pipelineFile>')
.option('-d, --debug', 'Shows debug information', false)
.option('-m, --manifest', 'Validate a manifest.ttl instead of a pipeline', false)
.option('-p, --pipeline <pipelineIRI>', 'Pipeline IRI', null)
.option('-q, --quiet', 'Report errors only', false)
.option('-s, --strict', 'Produce an error exit status on warnings', false)
.option('-v, --verbose', 'Include successful validation checks in output', false)
.action((pipelineFile, options) => {
.action((file, options) => {
options.levels = ['error']
if (!options.quiet) {
options.levels.push('warning')
Expand All @@ -61,6 +63,11 @@ program
if (options.strict) {
options.verbose = true
}
main(pipelineFile, options)
if (!options.manifest) {
validatePipeline(file, options)
}
else {
validateManifest(file, options)
}
})
program.parse()
69 changes: 69 additions & 0 deletions lib/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const path = require('path')
const ChecksCollection = require('./checksCollection')
const parser = require('./parser')
const namespace = require('@rdfjs/namespace')
const { removeFilePart } = require('./utils')
const iriResolve = require('rdf-loader-code/lib/iriResolve')

const ns = {
schema: namespace('http://schema.org/'),
rdf: namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
p: namespace('https://pipeline.described.at/'),
code: namespace('https://code.described.at/')
}

async function validateManifest (file, options) {
const manifestPath = path.resolve(file)
const manifestDir = removeFilePart(manifestPath)
const manifestDirPart = manifestDir.substr(manifestDir.lastIndexOf('/') + 1)
const checks = new ChecksCollection()

try {
const manifestGraph = await parser.readGraph(manifestPath, checks)
const operations = manifestGraph.has(ns.rdf.type, ns.p.Operation)
.map(operation => {
const implementedBy = operation.out(ns.code.implementedBy)
const codeLink = implementedBy.out(ns.code.link)
const identifier = codeLink.term

const { filename, method } = iriResolve(identifier.value)
return { operation: operation.term.value, filename, method }
})
.map(({ operation, filename, method }) => {
filename = manifestDir + filename.replace(manifestDirPart, '')
method = method || 'default'
return { operation, filename, method }
})

operations.reduce(async (promise, { operation, filename, method }) => {
await promise
let imported
try {
imported = await import(filename)
}
catch (err) {
console.error(`Cannot import ${filename} for operation ${operation}`)
}
if (typeof imported[method] !== 'function') {
console.error(`Fn ${method} not found in ${filename} for operation ${operation}`)
}
}, Promise.resolve())
}
catch (err) {
if (options.debug) {
console.error(err)
}
}

checks.print(options.levels)

if (!process.stdout.isTTY) {
console.log(checks.filterToJSON(options.levels))
}

if (checks.countIssues(options.strict)) {
process.exit(-1)
}
}

module.exports = validateManifest
Empty file removed lib/manifest.ttl
Empty file.

0 comments on commit a23dc93

Please sign in to comment.