-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
executable file
·99 lines (86 loc) · 2.95 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
import 'anylogger-console'
import path from 'path'
import fs from 'fs'
import * as module from 'module'
import * as url from 'url'
import { Command } from 'commander'
import * as parser from './lib/parser.js'
import ChecksCollection from './lib/checksCollection.js'
import { removeFilePart } from './lib/utils.js'
import * as validate from './lib/manifest.js'
import { log } from './lib/log.js'
const require = module.createRequire(url.fileURLToPath(import.meta.url))
const { version } = JSON.parse(fs.readFileSync(require.resolve('./package.json')))
const program = new Command()
program.version(version)
async function validatePipeline(file, options) {
const pipelineFile = path.resolve(file)
const pipelineDir = removeFilePart(pipelineFile)
const checks = new ChecksCollection()
let pipelines
try {
const pipelineGraph = await parser.readGraph(pipelineFile, checks)
pipelines = parser.getIdentifiers(pipelineGraph, checks, options.pipeline)
const codelinks = parser.getAllCodeLinks(pipelines)
const dependencies = parser.getDependencies(codelinks, pipelineDir)
const operationProperties = await parser.getAllOperationProperties(dependencies, checks)
parser.validateSteps({ pipelines, properties: operationProperties }, checks)
const pipelineProperties = parser.getPipelineProperties(pipelineGraph, Object.keys(pipelines))
parser.validatePipelines(pipelines, operationProperties, pipelineProperties, checks)
} catch (err) {
if (options.debug) {
log.error(err)
}
}
checks.print(options.levels)
if (!process.stdout.isTTY) {
log.log(checks.filterToJSON(options.levels))
}
if (checks.countIssues(options.strict)) {
process.exit(-1)
}
}
async function validateManifest(file, options) {
const checks = new ChecksCollection()
try {
await validate({ file, checks })
} catch (err) {
if (options.debug) {
log.error(err)
}
}
checks.print(options.levels)
if (!process.stdout.isTTY) {
log.log(checks.filterToJSON(options.levels))
}
if (checks.countIssues(options.strict)) {
process.exit(-1)
}
}
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(async (file, options) => {
options.levels = ['error']
if (!options.quiet) {
options.levels.push('warning')
}
if (options.verbose) {
options.levels.push('info')
}
if (options.strict) {
options.verbose = true
}
if (!options.manifest) {
validatePipeline(file, options)
} else {
await validateManifest(file, options)
}
})
program.parse()