-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcli.ts
More file actions
221 lines (208 loc) · 6.98 KB
/
Copy pathcli.ts
File metadata and controls
221 lines (208 loc) · 6.98 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { argv } from "node:process";
import { styleText } from "node:util";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { Argument, Command } from "npm:commander@^13.1.0";
import { contextToSchema } from "./scripts/context_to_schema.ts";
import { shexcToSchema, shexjToSchema } from "./scripts/shex_to_schema.ts";
import { shaclToSchema } from "./scripts/shacl_to_schema.ts";
import { schemaToScript } from "./scripts/schema_to_script.ts";
import { schemaToPackage } from "./scripts/schema_to_package.ts";
const asciiArt = String.raw`
_ ____ _ _ _
| | | _ \| | _(_) |_
| | | | | | |/ / | __|
| |___| |_| | <| | |_
|_____|____/|_|\_\_|\__|
`;
const program = new Command();
program
.name("ldkit")
.description("LDkit CLI utilities for Linked Data")
.helpOption(false)
.showHelpAfterError()
.configureHelp({
styleCommandDescription: (str) => styleText("gray", str),
styleDescriptionText: (str) => styleText("gray", str),
styleCommandText: (str) => styleText("cyan", str),
styleArgumentText: (str) => styleText("yellow", str),
styleSubcommandText: (str) => styleText("cyan", str),
})
.configureOutput({
outputError: (str, write) => write(styleText("red", str)),
stripColor: (str) => str,
});
program.command("context-to-schema")
.description("Convert a JSON-LD context from a file or URL to a LDkit schema")
.addArgument(
new Argument("<method>", "type of input").choices([
"url",
"file",
"arg",
]),
)
.argument("<input>", "input JSON-LD context - file, URL, or string")
.action(async (method, input) => {
try {
const resolvedInput = await resolve(method, input);
const schema = await contextToSchema(JSON.parse(resolvedInput));
console.log(schemaToScript(schema));
} catch (error: unknown) {
console.error(styleText("red", `${(error as Error).message}`));
}
});
program.command("shexc-to-schema")
.description("Convert a ShExC schema from a file or URL to a LDkit schema")
.addArgument(
new Argument("<method>", "type of input").choices([
"url",
"file",
"arg",
]),
)
.argument("<input>", "input ShExC schema - file, URL, or string")
.action(async (method, input) => {
try {
const resolvedInput = await resolve(method, input);
const schema = shexcToSchema(resolvedInput);
console.log(schemaToScript(schema));
} catch (error: unknown) {
console.error(styleText("red", `${(error as Error).message}`));
}
});
program.command("shexj-to-schema")
.description("Convert a ShExJ schema from a file or URL to a LDkit schema")
.addArgument(
new Argument("<method>", "type of input").choices([
"url",
"file",
"arg",
]),
)
.argument("<input>", "input ShExJ schema - file, URL, or string")
.action(async (method, input) => {
try {
const resolvedInput = await resolve(method, input);
const schema = shexjToSchema(JSON.parse(resolvedInput));
console.log(schemaToScript(schema));
} catch (error: unknown) {
console.error(styleText("red", `${(error as Error).message}`));
}
});
program.command("shacl-to-schema")
.description(
"Convert a SHACL shapes graph from a file or URL to a LDkit schema",
)
.addArgument(
new Argument("<method>", "type of input").choices([
"url",
"file",
"arg",
]),
)
.argument("<input>", "input SHACL Turtle - file, URL, or string")
.option(
"--prefix-alias <mapping>",
"rename a SHACL prefix in generated schema names (format: prefix=Alias). Repeatable.",
(value: string, previous: string[]) => [...previous, value],
[] as string[],
)
.action(async (method, input, opts: { prefixAlias?: string[] }) => {
try {
const resolvedInput = await resolve(method, input);
const prefixAliases = parsePrefixAliases(opts.prefixAlias);
const { schemas, extraNamespaces } = shaclToSchema(resolvedInput, {
prefixAliases,
});
console.log(schemaToScript(schemas, extraNamespaces));
} catch (error: unknown) {
console.error(styleText("red", `${(error as Error).message}`));
}
});
program.command("shacl-to-package")
.description(
"Convert a SHACL shapes graph into a directory of per-namespace LDkit schema files (one .ts per prefix plus an index.ts barrel)",
)
.addArgument(
new Argument("<method>", "type of input").choices([
"url",
"file",
"arg",
]),
)
.argument("<input>", "input SHACL Turtle - file, URL, or string")
.argument("<outDir>", "output directory for the generated package")
.option(
"--prefix-alias <mapping>",
"rename a SHACL prefix in generated schema names AND in the per-namespace file name (format: prefix=Alias). Repeatable.",
(value: string, previous: string[]) => [...previous, value],
[] as string[],
)
.action(
async (
method,
input,
outDir,
opts: { prefixAlias?: string[] },
) => {
try {
const resolvedInput = await resolve(method, input);
const prefixAliases = parsePrefixAliases(opts.prefixAlias);
const { schemas, extraNamespaces, schemaSourcePrefixes } =
shaclToSchema(resolvedInput, { prefixAliases });
const { files } = schemaToPackage(schemas, extraNamespaces, {
prefixAliases,
schemaSourcePrefixes,
});
mkdirSync(outDir, { recursive: true });
for (const [base, contents] of files) {
writeFileSync(join(outDir, `${base}.ts`), contents);
}
} catch (error: unknown) {
console.error(styleText("red", `${(error as Error).message}`));
}
},
);
// Check if no arguments were provided
if (argv.length <= 2) {
console.log(styleText("red", asciiArt));
program.help(); // Automatically exits after printing help
} else {
program.parse(argv);
}
function parsePrefixAliases(
pairs: string[] | undefined,
): Record<string, string> {
if (!pairs || pairs.length === 0) return {};
const result: Record<string, string> = {};
for (const pair of pairs) {
const eq = pair.indexOf("=");
if (eq <= 0 || eq === pair.length - 1) {
throw new Error(
`Invalid --prefix-alias value "${pair}" (expected format: prefix=Alias)`,
);
}
const prefix = pair.substring(0, eq);
const alias = pair.substring(eq + 1);
result[prefix] = alias;
}
return result;
}
async function resolve(method: string, input: string): Promise<string> {
if (method === "url") {
try {
return await fetch(input).then((res) => res.text());
} catch (error) {
throw new Error(`Failed to fetch URL: ${input}.\n${error}`);
}
} else if (method === "file") {
try {
return readFileSync(input, "utf-8");
} catch (error) {
throw new Error(`Failed to read file: ${input}.\n${error}`);
}
} else if (method === "arg") {
return input;
}
throw new Error(`Unknown resolution method: ${method}`);
}