@shaclmate/cli 2.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.d.ts +2 -0
- package/cli.js +151 -0
- package/package.json +42 -0
package/cli.d.ts
ADDED
package/cli.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import PrefixMap from "@rdfjs/prefix-map/PrefixMap";
|
|
3
|
+
import { ShapesGraphToAstTransformer } from "@shaclmate/compiler/ShapesGraphToAstTransformer.js";
|
|
4
|
+
import * as generators from "@shaclmate/compiler/generators";
|
|
5
|
+
import { dashDataset } from "@shaclmate/compiler/vocabularies/dashDataset.js";
|
|
6
|
+
import { array, command, multioption, oneOf, option, restPositionals, run, string, subcommands, } from "cmd-ts";
|
|
7
|
+
import { ExistingPath } from "cmd-ts/dist/esm/batteries/fs.js";
|
|
8
|
+
import { DataFactory, Parser, Store } from "n3";
|
|
9
|
+
import pino from "pino";
|
|
10
|
+
import { ShapesGraph } from "shacl-ast";
|
|
11
|
+
const inputFilePaths = restPositionals({
|
|
12
|
+
displayName: "inputFilePaths",
|
|
13
|
+
description: "paths to RDF files containing SHACL shapes",
|
|
14
|
+
type: ExistingPath,
|
|
15
|
+
});
|
|
16
|
+
const logger = pino({
|
|
17
|
+
level: process.env["NODE_ENV"] === "development" ||
|
|
18
|
+
process.env["NODE_ENV"] === "test"
|
|
19
|
+
? "debug"
|
|
20
|
+
: "info",
|
|
21
|
+
}, pino["destination"] ? pino.destination(2) : undefined);
|
|
22
|
+
const outputFilePath = option({
|
|
23
|
+
defaultValue: () => "",
|
|
24
|
+
description: "path to a file to write output to; if not specified, write to stdout",
|
|
25
|
+
long: "output-path",
|
|
26
|
+
short: "o",
|
|
27
|
+
type: string,
|
|
28
|
+
});
|
|
29
|
+
function readInput(inputFilePaths) {
|
|
30
|
+
if (inputFilePaths.length === 0) {
|
|
31
|
+
throw new Error("must specify at least one input shapes graph file path");
|
|
32
|
+
}
|
|
33
|
+
const inputParser = new Parser();
|
|
34
|
+
const dataset = new Store();
|
|
35
|
+
for (const quad of dashDataset) {
|
|
36
|
+
dataset.add(quad);
|
|
37
|
+
}
|
|
38
|
+
const iriPrefixes = [];
|
|
39
|
+
for (const inputFilePath of inputFilePaths) {
|
|
40
|
+
dataset.addQuads(inputParser.parse(fs.readFileSync(inputFilePath).toString(), null, (prefix, prefixNode) => {
|
|
41
|
+
const existingIriPrefix = iriPrefixes.find((iriPrefix) => iriPrefix[0] === prefix || iriPrefix[1].equals(prefixNode));
|
|
42
|
+
if (existingIriPrefix) {
|
|
43
|
+
if (existingIriPrefix[0] !== prefix ||
|
|
44
|
+
!existingIriPrefix[1].equals(prefixNode)) {
|
|
45
|
+
logger.warn("conflicting prefix %s: %s", prefix, prefixNode.value);
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
iriPrefixes.push([prefix, prefixNode]);
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
const shapesGraph = ShapesGraph.fromDataset(dataset);
|
|
53
|
+
return new ShapesGraphToAstTransformer({
|
|
54
|
+
iriPrefixMap: new PrefixMap(iriPrefixes, { factory: DataFactory }),
|
|
55
|
+
shapesGraph,
|
|
56
|
+
})
|
|
57
|
+
.transform()
|
|
58
|
+
.ifLeft((error) => {
|
|
59
|
+
throw error;
|
|
60
|
+
})
|
|
61
|
+
.extract();
|
|
62
|
+
}
|
|
63
|
+
function writeOutput(output, outputFilePath) {
|
|
64
|
+
if (outputFilePath.length === 0) {
|
|
65
|
+
process.stdout.write(output);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
fs.writeFileSync(outputFilePath, output);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
run(subcommands({
|
|
72
|
+
cmds: {
|
|
73
|
+
generate: command({
|
|
74
|
+
name: "generate",
|
|
75
|
+
description: "generate TypeScript for the SHACL Shapes Graph AST",
|
|
76
|
+
args: {
|
|
77
|
+
dataFactoryImport: option({
|
|
78
|
+
defaultValue: () => generators.ts.TsGenerator.Configuration.Defaults
|
|
79
|
+
.dataFactoryImport,
|
|
80
|
+
description: "import line to get an RDF/JS DataFactory",
|
|
81
|
+
long: "data-factory-import",
|
|
82
|
+
type: string,
|
|
83
|
+
}),
|
|
84
|
+
dataFactoryVariable: option({
|
|
85
|
+
defaultValue: () => generators.ts.TsGenerator.Configuration.Defaults
|
|
86
|
+
.dataFactoryVariable,
|
|
87
|
+
description: "variable of the RDF/JS DataFactory that was imported",
|
|
88
|
+
long: "data-factory-variable",
|
|
89
|
+
type: string,
|
|
90
|
+
}),
|
|
91
|
+
features: multioption({
|
|
92
|
+
description: "generator features to enable",
|
|
93
|
+
long: "feature",
|
|
94
|
+
type: array(oneOf([
|
|
95
|
+
"class",
|
|
96
|
+
"equals",
|
|
97
|
+
"fromRdf",
|
|
98
|
+
"toRdf",
|
|
99
|
+
"sparql-graph-patterns",
|
|
100
|
+
])),
|
|
101
|
+
}),
|
|
102
|
+
inputFilePaths,
|
|
103
|
+
outputFilePath,
|
|
104
|
+
objectTypeDeclarationType: option({
|
|
105
|
+
defaultValue: () => generators.ts.TsGenerator.Configuration.Defaults
|
|
106
|
+
.objectTypeDeclarationType,
|
|
107
|
+
long: "object-type-declaration-type",
|
|
108
|
+
type: oneOf(["class", "interface"]),
|
|
109
|
+
}),
|
|
110
|
+
objectTypeDiscriminatorPropertyName: option({
|
|
111
|
+
defaultValue: () => generators.ts.TsGenerator.Configuration.Defaults
|
|
112
|
+
.objectTypeDiscriminatorPropertyName,
|
|
113
|
+
description: "name of a property to add to generated object types to discriminate them with a string enum",
|
|
114
|
+
long: "object-type-discriminator-property-name",
|
|
115
|
+
type: string,
|
|
116
|
+
}),
|
|
117
|
+
objectTypeIdentifierPropertyName: option({
|
|
118
|
+
defaultValue: () => generators.ts.TsGenerator.Configuration.Defaults
|
|
119
|
+
.objectTypeIdentifierPropertyName,
|
|
120
|
+
description: "name of a property to add to generated object types to discriminate them with a string enum",
|
|
121
|
+
long: "object-type-discriminator-property-name",
|
|
122
|
+
type: string,
|
|
123
|
+
}),
|
|
124
|
+
},
|
|
125
|
+
handler: async ({ dataFactoryImport, dataFactoryVariable, features, inputFilePaths, objectTypeDeclarationType, objectTypeDiscriminatorPropertyName, objectTypeIdentifierPropertyName, outputFilePath, }) => {
|
|
126
|
+
writeOutput(new generators.ts.TsGenerator(readInput(inputFilePaths), new generators.ts.TsGenerator.Configuration({
|
|
127
|
+
dataFactoryImport,
|
|
128
|
+
dataFactoryVariable,
|
|
129
|
+
features: new Set(features),
|
|
130
|
+
objectTypeDeclarationType: objectTypeDeclarationType,
|
|
131
|
+
objectTypeDiscriminatorPropertyName,
|
|
132
|
+
objectTypeIdentifierPropertyName,
|
|
133
|
+
})).generate(), outputFilePath);
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
"show-ast-json": command({
|
|
137
|
+
name: "show-ast-json",
|
|
138
|
+
description: "show AST JSON for the SHACL Shapes Graph AST",
|
|
139
|
+
args: {
|
|
140
|
+
inputFilePaths,
|
|
141
|
+
outputFilePath,
|
|
142
|
+
},
|
|
143
|
+
handler: async ({ inputFilePaths, outputFilePath }) => {
|
|
144
|
+
writeOutput(new generators.json.AstJsonGenerator(readInput(inputFilePaths)).generate(), outputFilePath);
|
|
145
|
+
},
|
|
146
|
+
}),
|
|
147
|
+
},
|
|
148
|
+
description: "shaclmate command line interface",
|
|
149
|
+
name: "shaclmate",
|
|
150
|
+
}), process.argv.slice(2));
|
|
151
|
+
//# sourceMappingURL=cli.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"bin": {
|
|
3
|
+
"shaclmate": "cli.js"
|
|
4
|
+
},
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@shaclmate/compiler": "2.0.12",
|
|
7
|
+
"cmd-ts": "^0.13.0",
|
|
8
|
+
"pino": "^9.1.0"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {},
|
|
11
|
+
"files": ["*.d.ts", "*.js"],
|
|
12
|
+
"main": "index.js",
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"name": "@shaclmate/cli",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -b",
|
|
17
|
+
"check": "biome check",
|
|
18
|
+
"check:write": "biome check --write",
|
|
19
|
+
"check:write:unsafe": "biome check --write --unsafe",
|
|
20
|
+
"clean": "rimraf -g **/*.d.ts* **/*.js **/*.js.map tsconfig.tsbuildinfo",
|
|
21
|
+
"format": "biome format",
|
|
22
|
+
"format:write": "biome format --write",
|
|
23
|
+
"format:write:unsafe": "biome format --write --unsafe",
|
|
24
|
+
"rebuild": "run-s clean build",
|
|
25
|
+
"link-dependencies": "npm link purify-ts-helpers rdfjs-resource",
|
|
26
|
+
"lint": "biome lint",
|
|
27
|
+
"lint:write": "biome lint --write",
|
|
28
|
+
"lint:write:unsafe": "biome lint --write --unsafe",
|
|
29
|
+
"test": "biome check && vitest run",
|
|
30
|
+
"test:coverage": "biome check && vitest run --coverage",
|
|
31
|
+
"test:watch": "vitest watch",
|
|
32
|
+
"unlink": "npm unlink -g @shaclmate/cli",
|
|
33
|
+
"watch": "tsc -w --preserveWatchOutput"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/minorg/shaclmate"
|
|
38
|
+
},
|
|
39
|
+
"type": "module",
|
|
40
|
+
"types": "index.d.ts",
|
|
41
|
+
"version": "2.0.12"
|
|
42
|
+
}
|