@ldo/cli 0.0.1-alpha.14
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/README.md +26 -0
- package/dist/build.d.ts +6 -0
- package/dist/build.js +46 -0
- package/dist/build.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +4 -0
- package/dist/init.js +47 -0
- package/dist/init.js.map +1 -0
- package/dist/templates/context.ejs +8 -0
- package/dist/templates/defaultShapes/foafProfile.ejs +19 -0
- package/dist/templates/schema.ejs +8 -0
- package/dist/templates/shapeTypes.ejs +24 -0
- package/dist/templates/typings.ejs +14 -0
- package/package.json +63 -0
- package/src/build.ts +69 -0
- package/src/index.ts +25 -0
- package/src/init.ts +78 -0
- package/src/templates/context.ejs +8 -0
- package/src/templates/defaultShapes/foafProfile.ejs +19 -0
- package/src/templates/schema.ejs +8 -0
- package/src/templates/shapeTypes.ejs +24 -0
- package/src/templates/typings.ejs +14 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# LDO-CLI
|
|
2
|
+
|
|
3
|
+
A command line interface for Linked Data Objects. LDO-CLI builds `.shex` shapes into LDO types.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
Install the CLI
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm i ldo-cli --save-dev
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Set up a shapes folder
|
|
13
|
+
```bash
|
|
14
|
+
mkdir shapes
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Place ShexC shapes inside `.shex` files
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
touch ./shapes/example.shex
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Build the shpaes
|
|
24
|
+
```bash
|
|
25
|
+
ldo build --input ./shapes --output ./ldo
|
|
26
|
+
```
|
package/dist/build.d.ts
ADDED
package/dist/build.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.build = void 0;
|
|
7
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const parser_1 = __importDefault(require("@shexjs/parser"));
|
|
10
|
+
const schema_converter_shex_1 = __importDefault(require("@ldo/schema-converter-shex"));
|
|
11
|
+
const ejs_1 = require("ejs");
|
|
12
|
+
const prettier_1 = __importDefault(require("prettier"));
|
|
13
|
+
const loading_cli_1 = __importDefault(require("loading-cli"));
|
|
14
|
+
async function build(options) {
|
|
15
|
+
const load = (0, loading_cli_1.default)("Peparing Environment");
|
|
16
|
+
load.start();
|
|
17
|
+
const shapeDir = await fs_extra_1.default.promises.readdir(options.input, {
|
|
18
|
+
withFileTypes: true,
|
|
19
|
+
});
|
|
20
|
+
const shexFiles = shapeDir.filter((file) => file.isFile() && file.name.endsWith(".shex"));
|
|
21
|
+
if (fs_extra_1.default.existsSync(options.output)) {
|
|
22
|
+
await fs_extra_1.default.promises.rm(options.output, { recursive: true });
|
|
23
|
+
}
|
|
24
|
+
await fs_extra_1.default.promises.mkdir(options.output);
|
|
25
|
+
load.text = "Generating LDO Documents";
|
|
26
|
+
await Promise.all(shexFiles.map(async (file) => {
|
|
27
|
+
const fileName = path_1.default.parse(file.name).name;
|
|
28
|
+
const shexC = await fs_extra_1.default.promises.readFile(path_1.default.join(options.input, file.name), "utf8");
|
|
29
|
+
const schema = parser_1.default
|
|
30
|
+
.construct("https://ldo.js.org/")
|
|
31
|
+
.parse(shexC);
|
|
32
|
+
const [typings, context] = await (0, schema_converter_shex_1.default)(schema);
|
|
33
|
+
await Promise.all(["context", "schema", "shapeTypes", "typings"].map(async (templateName) => {
|
|
34
|
+
const finalContent = await (0, ejs_1.renderFile)(path_1.default.join(__dirname, "./templates", `${templateName}.ejs`), {
|
|
35
|
+
typings: typings.typings,
|
|
36
|
+
fileName,
|
|
37
|
+
schema: JSON.stringify(schema, null, 2),
|
|
38
|
+
context: JSON.stringify(context, null, 2),
|
|
39
|
+
});
|
|
40
|
+
await fs_extra_1.default.promises.writeFile(path_1.default.join(options.output, `${fileName}.${templateName}.ts`), await prettier_1.default.format(finalContent, { parser: "typescript" }));
|
|
41
|
+
}));
|
|
42
|
+
}));
|
|
43
|
+
load.stop();
|
|
44
|
+
}
|
|
45
|
+
exports.build = build;
|
|
46
|
+
//# sourceMappingURL=build.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.js","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":";;;;;;AAAA,wDAA0B;AAC1B,gDAAwB;AAExB,4DAAoC;AACpC,uFAA6D;AAC7D,6BAAiC;AACjC,wDAAgC;AAChC,8DAAkC;AAO3B,KAAK,UAAU,KAAK,CAAC,OAAqB;IAC/C,MAAM,IAAI,GAAG,IAAA,qBAAO,EAAC,sBAAsB,CAAC,CAAC;IAC7C,IAAI,CAAC,KAAK,EAAE,CAAC;IACb,MAAM,QAAQ,GAAG,MAAM,kBAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE;QACxD,aAAa,EAAE,IAAI;KACpB,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,CAC/B,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CACvD,CAAC;IAEF,IAAI,kBAAE,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACjC,MAAM,kBAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,OAAO,CAAC,MAAM,EAA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CAAC,QAAQ,CAAC,SAAS,CACzB,cAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,QAAQ,IAAI,YAAY,KAAK,CAAC,EAC3D,MAAM,kBAAQ,CAAC,MAAM,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,CAC9D,CAAC;QACJ,CAAC,CACF,CACF,CAAC;IACJ,CAAC,CAAC,CACH,CAAC;IACF,IAAI,CAAC,IAAI,EAAE,CAAC;AACd,CAAC;AAtDD,sBAsDC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const commander_1 = require("commander");
|
|
5
|
+
const build_1 = require("./build");
|
|
6
|
+
const init_1 = require("./init");
|
|
7
|
+
commander_1.program
|
|
8
|
+
.name("LDO-CLI")
|
|
9
|
+
.description("CLI to some JavaScript string utilities")
|
|
10
|
+
.version("3.0.1");
|
|
11
|
+
commander_1.program
|
|
12
|
+
.command("build")
|
|
13
|
+
.description("Build contents of a shex folder into Shape Types")
|
|
14
|
+
.option("-i, --input <inputPath>", "Provide the input path", "./shapes")
|
|
15
|
+
.option("-o, --output <outputPath>", "Provide the output path", "./ldo")
|
|
16
|
+
.action(build_1.build);
|
|
17
|
+
commander_1.program
|
|
18
|
+
.command("init")
|
|
19
|
+
.option("-d --directory>", "A parent directory for ldo files")
|
|
20
|
+
.description("Initializes a project for LDO.")
|
|
21
|
+
.action(init_1.init);
|
|
22
|
+
commander_1.program.parse();
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAEA,yCAAoC;AACpC,mCAAgC;AAChC,iCAA8B;AAE9B,mBAAO;KACJ,IAAI,CAAC,SAAS,CAAC;KACf,WAAW,CAAC,yCAAyC,CAAC;KACtD,OAAO,CAAC,OAAO,CAAC,CAAC;AAEpB,mBAAO;KACJ,OAAO,CAAC,OAAO,CAAC;KAChB,WAAW,CAAC,kDAAkD,CAAC;KAC/D,MAAM,CAAC,yBAAyB,EAAE,wBAAwB,EAAE,UAAU,CAAC;KACvE,MAAM,CAAC,2BAA2B,EAAE,yBAAyB,EAAE,OAAO,CAAC;KACvE,MAAM,CAAC,aAAK,CAAC,CAAC;AAEjB,mBAAO;KACJ,OAAO,CAAC,MAAM,CAAC;KACf,MAAM,CAAC,iBAAiB,EAAE,kCAAkC,CAAC;KAC7D,WAAW,CAAC,gCAAgC,CAAC;KAC7C,MAAM,CAAC,WAAI,CAAC,CAAC;AAEhB,mBAAO,CAAC,KAAK,EAAE,CAAC"}
|
package/dist/init.d.ts
ADDED
package/dist/init.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.init = void 0;
|
|
7
|
+
const child_process_promise_1 = require("child-process-promise");
|
|
8
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const ejs_1 = require("ejs");
|
|
11
|
+
const DEFAULT_SHAPES_FOLDER = "./.shapes";
|
|
12
|
+
const DEFAULT_LDO_FOLDER = "./.ldo";
|
|
13
|
+
const POTENTIAL_PARENT_DIRECTORIES = ["src", "lib", "bin"];
|
|
14
|
+
async function init(initOptions) {
|
|
15
|
+
await (0, child_process_promise_1.exec)("npm install @ldo/ldo --save");
|
|
16
|
+
await (0, child_process_promise_1.exec)("npm install @ldo/cli @types/shexj @types/jsonld --save-dev");
|
|
17
|
+
let parentDirectory = initOptions.directory;
|
|
18
|
+
if (!parentDirectory) {
|
|
19
|
+
parentDirectory = "./";
|
|
20
|
+
const allDirectories = (await fs_extra_1.default.promises.readdir("./", {
|
|
21
|
+
withFileTypes: true,
|
|
22
|
+
})).filter((file) => file.isDirectory());
|
|
23
|
+
for (let i = 0; i < POTENTIAL_PARENT_DIRECTORIES.length; i++) {
|
|
24
|
+
if (allDirectories.some((dir) => dir.name === POTENTIAL_PARENT_DIRECTORIES[i])) {
|
|
25
|
+
parentDirectory = POTENTIAL_PARENT_DIRECTORIES[i];
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const shapesFolderPath = path_1.default.join(parentDirectory, DEFAULT_SHAPES_FOLDER);
|
|
31
|
+
await fs_extra_1.default.promises.mkdir(shapesFolderPath);
|
|
32
|
+
const defaultShapePaths = await fs_extra_1.default.promises.readdir(path_1.default.join(__dirname, "./templates/defaultShapes"));
|
|
33
|
+
await Promise.all(defaultShapePaths.map(async (shapePath) => {
|
|
34
|
+
const shapeContent = await (0, ejs_1.renderFile)(path_1.default.join(__dirname, "./templates/defaultShapes", shapePath), {});
|
|
35
|
+
await fs_extra_1.default.promises.writeFile(path_1.default.join(shapesFolderPath, `${path_1.default.parse(shapePath).name}.shex`), shapeContent);
|
|
36
|
+
}));
|
|
37
|
+
const packageJson = JSON.parse((await fs_extra_1.default.promises.readFile("./package.json")).toString());
|
|
38
|
+
if (!packageJson.scripts) {
|
|
39
|
+
packageJson.scripts = {};
|
|
40
|
+
}
|
|
41
|
+
const ldoFolder = path_1.default.join(parentDirectory, DEFAULT_LDO_FOLDER);
|
|
42
|
+
packageJson.scripts["build:ldo"] = `ldo build --input ${shapesFolderPath} --output ${ldoFolder}`;
|
|
43
|
+
await fs_extra_1.default.promises.writeFile("./package.json", JSON.stringify(packageJson, null, 2));
|
|
44
|
+
await (0, child_process_promise_1.exec)("npm run build:ldo");
|
|
45
|
+
}
|
|
46
|
+
exports.init = init;
|
|
47
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C;AAhED,oBAgEC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ContextDefinition } from "jsonld";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* =============================================================================
|
|
5
|
+
* <%- fileName %>Context: JSONLD Context for <%- fileName %>
|
|
6
|
+
* =============================================================================
|
|
7
|
+
*/
|
|
8
|
+
export const <%- fileName %>Context: ContextDefinition = <%- context %>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# This shape is provided by default as an example
|
|
2
|
+
# You can create your own shape to fit your needs using ShEx (https://shex.io)
|
|
3
|
+
# Also check out https://shaperepo.com for examples of more shapes.
|
|
4
|
+
|
|
5
|
+
PREFIX ex: <https://example.com/>
|
|
6
|
+
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
|
|
7
|
+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
|
|
8
|
+
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
|
|
9
|
+
|
|
10
|
+
ex:FoafProfile EXTRA a {
|
|
11
|
+
a [ foaf:Person ]
|
|
12
|
+
// rdfs:comment "Defines the node as a Person (from foaf)" ;
|
|
13
|
+
foaf:name xsd:string ?
|
|
14
|
+
// rdfs:comment "Define a person's name." ;
|
|
15
|
+
foaf:img xsd:string ?
|
|
16
|
+
// rdfs:comment "Photo link but in string form" ;
|
|
17
|
+
foaf:knows @ex:FoafProfile *
|
|
18
|
+
// rdfs:comment "A list of WebIds for all the people this user knows." ;
|
|
19
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Schema } from "shexj";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* =============================================================================
|
|
5
|
+
* <%- fileName %>Schema: ShexJ Schema for <%- fileName %>
|
|
6
|
+
* =============================================================================
|
|
7
|
+
*/
|
|
8
|
+
export const <%- fileName %>Schema: Schema = <%- schema %>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ShapeType } from "@ldo/ldo";
|
|
2
|
+
import { <%- fileName %>Schema } from "./<%- fileName %>.schema";
|
|
3
|
+
import { <%- fileName %>Context } from "./<%- fileName %>.context";
|
|
4
|
+
import {
|
|
5
|
+
<% typings.forEach((typing) => { -%>
|
|
6
|
+
<%- typing.dts.name %>,
|
|
7
|
+
<% }); -%>} from "./<%- fileName %>.typings";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* =============================================================================
|
|
11
|
+
* LDO ShapeTypes <%- fileName %>
|
|
12
|
+
* =============================================================================
|
|
13
|
+
*/
|
|
14
|
+
<% typings.forEach((typing) => { -%>
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* <%- typing.dts.name %> ShapeType
|
|
18
|
+
*/
|
|
19
|
+
export const <%- typing.dts.name %>ShapeType: ShapeType<<%- typing.dts.name %>> = {
|
|
20
|
+
schema: <%- fileName %>Schema,
|
|
21
|
+
shape: "<%- typing.dts.shapeId %>",
|
|
22
|
+
context: <%- fileName %>Context,
|
|
23
|
+
};
|
|
24
|
+
<% }); -%>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ContextDefinition } from "jsonld";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* =============================================================================
|
|
5
|
+
* Typescript Typings for <%- fileName %>
|
|
6
|
+
* =============================================================================
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
<% typings.forEach((typing) => { -%>
|
|
10
|
+
/**
|
|
11
|
+
* <%- typing.dts.name %> Type
|
|
12
|
+
*/
|
|
13
|
+
export <%- typing.typingString -%>
|
|
14
|
+
<% }); -%>
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ldo/cli",
|
|
3
|
+
"version": "0.0.1-alpha.14",
|
|
4
|
+
"description": "A Command Line Interface for Linked Data Objects",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"ldo": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node dist/index.js build",
|
|
11
|
+
"start:init": "node dist/index.js init",
|
|
12
|
+
"dev": "npm run build && npm run start:init",
|
|
13
|
+
"build": "npm run clean && npm run build:ts && npm run copy-files && npm run update-permission",
|
|
14
|
+
"build:ts": "tsc --project tsconfig.build.json",
|
|
15
|
+
"clean": "rimraf dist/",
|
|
16
|
+
"copy-files": "copyfiles -u 1 \"./src/**/*.ejs\" dist/",
|
|
17
|
+
"update-permission": "chmod +x ../../node_modules/.bin/ldo",
|
|
18
|
+
"watch": "tsc --watch",
|
|
19
|
+
"test": "jest --coverage",
|
|
20
|
+
"test:watch": "jest --watch",
|
|
21
|
+
"prepublishOnly": "npm run test && npm run build",
|
|
22
|
+
"lint": "eslint src/** --fix --no-error-on-unmatched-pattern"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/o-development/ldobjects.git"
|
|
27
|
+
},
|
|
28
|
+
"author": "Jackson Morgan",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/o-development/ldobjects/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/o-development/ldobjects/tree/main/packages/cli#readme",
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/child-process-promise": "^2.2.2",
|
|
36
|
+
"@types/ejs": "^3.1.1",
|
|
37
|
+
"@types/fs-extra": "^9.0.13",
|
|
38
|
+
"@types/jest": "^27.0.3",
|
|
39
|
+
"@types/shexj": "2.1.4",
|
|
40
|
+
"copyfiles": "^2.4.1",
|
|
41
|
+
"jest": "^27.4.2",
|
|
42
|
+
"rimraf": "^3.0.2",
|
|
43
|
+
"ts-jest": "^27.0.7"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@ldo/schema-converter-shex": "^0.0.1-alpha.14",
|
|
47
|
+
"@shexjs/parser": "^1.0.0-alpha.24",
|
|
48
|
+
"child-process-promise": "^2.2.1",
|
|
49
|
+
"commander": "^9.3.0",
|
|
50
|
+
"ejs": "^3.1.8",
|
|
51
|
+
"fs-extra": "^10.1.0",
|
|
52
|
+
"loading-cli": "^1.1.0",
|
|
53
|
+
"prettier": "^3.0.3"
|
|
54
|
+
},
|
|
55
|
+
"files": [
|
|
56
|
+
"dist",
|
|
57
|
+
"src"
|
|
58
|
+
],
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
},
|
|
62
|
+
"gitHead": "5acfea4c43c07c02df9ed4e9166e61a540e26db8"
|
|
63
|
+
}
|
package/src/build.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import type { Schema } from "shexj";
|
|
4
|
+
import parser from "@shexjs/parser";
|
|
5
|
+
import schemaConverterShex from "@ldo/schema-converter-shex";
|
|
6
|
+
import { renderFile } from "ejs";
|
|
7
|
+
import prettier from "prettier";
|
|
8
|
+
import loading from "loading-cli";
|
|
9
|
+
|
|
10
|
+
interface BuildOptions {
|
|
11
|
+
input: string;
|
|
12
|
+
output: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function build(options: BuildOptions) {
|
|
16
|
+
const load = loading("Peparing Environment");
|
|
17
|
+
load.start();
|
|
18
|
+
const shapeDir = await fs.promises.readdir(options.input, {
|
|
19
|
+
withFileTypes: true,
|
|
20
|
+
});
|
|
21
|
+
// Filter out non-shex documents
|
|
22
|
+
const shexFiles = shapeDir.filter(
|
|
23
|
+
(file) => file.isFile() && file.name.endsWith(".shex"),
|
|
24
|
+
);
|
|
25
|
+
// Prepare new folder by clearing/and/or creating it
|
|
26
|
+
if (fs.existsSync(options.output)) {
|
|
27
|
+
await fs.promises.rm(options.output, { recursive: true });
|
|
28
|
+
}
|
|
29
|
+
await fs.promises.mkdir(options.output);
|
|
30
|
+
|
|
31
|
+
load.text = "Generating LDO Documents";
|
|
32
|
+
await Promise.all(
|
|
33
|
+
shexFiles.map(async (file) => {
|
|
34
|
+
const fileName = path.parse(file.name).name;
|
|
35
|
+
// Get the content of each document
|
|
36
|
+
const shexC = await fs.promises.readFile(
|
|
37
|
+
path.join(options.input, file.name),
|
|
38
|
+
"utf8",
|
|
39
|
+
);
|
|
40
|
+
// Convert to ShexJ
|
|
41
|
+
const schema: Schema = parser
|
|
42
|
+
.construct("https://ldo.js.org/")
|
|
43
|
+
.parse(shexC);
|
|
44
|
+
// Convert the content to types
|
|
45
|
+
const [typings, context] = await schemaConverterShex(schema);
|
|
46
|
+
await Promise.all(
|
|
47
|
+
["context", "schema", "shapeTypes", "typings"].map(
|
|
48
|
+
async (templateName) => {
|
|
49
|
+
const finalContent = await renderFile(
|
|
50
|
+
path.join(__dirname, "./templates", `${templateName}.ejs`),
|
|
51
|
+
{
|
|
52
|
+
typings: typings.typings,
|
|
53
|
+
fileName,
|
|
54
|
+
schema: JSON.stringify(schema, null, 2),
|
|
55
|
+
context: JSON.stringify(context, null, 2),
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
// Save conversion to document
|
|
59
|
+
await fs.promises.writeFile(
|
|
60
|
+
path.join(options.output, `${fileName}.${templateName}.ts`),
|
|
61
|
+
await prettier.format(finalContent, { parser: "typescript" }),
|
|
62
|
+
);
|
|
63
|
+
},
|
|
64
|
+
),
|
|
65
|
+
);
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
load.stop();
|
|
69
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { program } from "commander";
|
|
4
|
+
import { build } from "./build";
|
|
5
|
+
import { init } from "./init";
|
|
6
|
+
|
|
7
|
+
program
|
|
8
|
+
.name("LDO-CLI")
|
|
9
|
+
.description("CLI to some JavaScript string utilities")
|
|
10
|
+
.version("3.0.1");
|
|
11
|
+
|
|
12
|
+
program
|
|
13
|
+
.command("build")
|
|
14
|
+
.description("Build contents of a shex folder into Shape Types")
|
|
15
|
+
.option("-i, --input <inputPath>", "Provide the input path", "./shapes")
|
|
16
|
+
.option("-o, --output <outputPath>", "Provide the output path", "./ldo")
|
|
17
|
+
.action(build);
|
|
18
|
+
|
|
19
|
+
program
|
|
20
|
+
.command("init")
|
|
21
|
+
.option("-d --directory>", "A parent directory for ldo files")
|
|
22
|
+
.description("Initializes a project for LDO.")
|
|
23
|
+
.action(init);
|
|
24
|
+
|
|
25
|
+
program.parse();
|
package/src/init.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { exec } from "child-process-promise";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { renderFile } from "ejs";
|
|
5
|
+
|
|
6
|
+
const DEFAULT_SHAPES_FOLDER = "./.shapes";
|
|
7
|
+
const DEFAULT_LDO_FOLDER = "./.ldo";
|
|
8
|
+
const POTENTIAL_PARENT_DIRECTORIES = ["src", "lib", "bin"];
|
|
9
|
+
|
|
10
|
+
export interface InitOptions {
|
|
11
|
+
directory?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function init(initOptions: InitOptions) {
|
|
15
|
+
// Install dependencies
|
|
16
|
+
await exec("npm install @ldo/ldo --save");
|
|
17
|
+
await exec("npm install @ldo/cli @types/shexj @types/jsonld --save-dev");
|
|
18
|
+
|
|
19
|
+
// Find folder to save to
|
|
20
|
+
let parentDirectory = initOptions.directory;
|
|
21
|
+
if (!parentDirectory) {
|
|
22
|
+
parentDirectory = "./";
|
|
23
|
+
const allDirectories = (
|
|
24
|
+
await fs.promises.readdir("./", {
|
|
25
|
+
withFileTypes: true,
|
|
26
|
+
})
|
|
27
|
+
).filter((file) => file.isDirectory());
|
|
28
|
+
for (let i = 0; i < POTENTIAL_PARENT_DIRECTORIES.length; i++) {
|
|
29
|
+
if (
|
|
30
|
+
allDirectories.some(
|
|
31
|
+
(dir) => dir.name === POTENTIAL_PARENT_DIRECTORIES[i],
|
|
32
|
+
)
|
|
33
|
+
) {
|
|
34
|
+
parentDirectory = POTENTIAL_PARENT_DIRECTORIES[i];
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Create "shapes" folder
|
|
41
|
+
const shapesFolderPath = path.join(parentDirectory, DEFAULT_SHAPES_FOLDER);
|
|
42
|
+
await fs.promises.mkdir(shapesFolderPath);
|
|
43
|
+
const defaultShapePaths = await fs.promises.readdir(
|
|
44
|
+
path.join(__dirname, "./templates/defaultShapes"),
|
|
45
|
+
);
|
|
46
|
+
await Promise.all(
|
|
47
|
+
defaultShapePaths.map(async (shapePath) => {
|
|
48
|
+
const shapeContent = await renderFile(
|
|
49
|
+
path.join(__dirname, "./templates/defaultShapes", shapePath),
|
|
50
|
+
{},
|
|
51
|
+
);
|
|
52
|
+
await fs.promises.writeFile(
|
|
53
|
+
path.join(shapesFolderPath, `${path.parse(shapePath).name}.shex`),
|
|
54
|
+
shapeContent,
|
|
55
|
+
);
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
// Add build script
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
|
+
const packageJson: any = JSON.parse(
|
|
62
|
+
(await fs.promises.readFile("./package.json")).toString(),
|
|
63
|
+
);
|
|
64
|
+
if (!packageJson.scripts) {
|
|
65
|
+
packageJson.scripts = {};
|
|
66
|
+
}
|
|
67
|
+
const ldoFolder = path.join(parentDirectory, DEFAULT_LDO_FOLDER);
|
|
68
|
+
packageJson.scripts[
|
|
69
|
+
"build:ldo"
|
|
70
|
+
] = `ldo build --input ${shapesFolderPath} --output ${ldoFolder}`;
|
|
71
|
+
await fs.promises.writeFile(
|
|
72
|
+
"./package.json",
|
|
73
|
+
JSON.stringify(packageJson, null, 2),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// Build LDO
|
|
77
|
+
await exec("npm run build:ldo");
|
|
78
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ContextDefinition } from "jsonld";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* =============================================================================
|
|
5
|
+
* <%- fileName %>Context: JSONLD Context for <%- fileName %>
|
|
6
|
+
* =============================================================================
|
|
7
|
+
*/
|
|
8
|
+
export const <%- fileName %>Context: ContextDefinition = <%- context %>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# This shape is provided by default as an example
|
|
2
|
+
# You can create your own shape to fit your needs using ShEx (https://shex.io)
|
|
3
|
+
# Also check out https://shaperepo.com for examples of more shapes.
|
|
4
|
+
|
|
5
|
+
PREFIX ex: <https://example.com/>
|
|
6
|
+
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
|
|
7
|
+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
|
|
8
|
+
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
|
|
9
|
+
|
|
10
|
+
ex:FoafProfile EXTRA a {
|
|
11
|
+
a [ foaf:Person ]
|
|
12
|
+
// rdfs:comment "Defines the node as a Person (from foaf)" ;
|
|
13
|
+
foaf:name xsd:string ?
|
|
14
|
+
// rdfs:comment "Define a person's name." ;
|
|
15
|
+
foaf:img xsd:string ?
|
|
16
|
+
// rdfs:comment "Photo link but in string form" ;
|
|
17
|
+
foaf:knows @ex:FoafProfile *
|
|
18
|
+
// rdfs:comment "A list of WebIds for all the people this user knows." ;
|
|
19
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Schema } from "shexj";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* =============================================================================
|
|
5
|
+
* <%- fileName %>Schema: ShexJ Schema for <%- fileName %>
|
|
6
|
+
* =============================================================================
|
|
7
|
+
*/
|
|
8
|
+
export const <%- fileName %>Schema: Schema = <%- schema %>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ShapeType } from "@ldo/ldo";
|
|
2
|
+
import { <%- fileName %>Schema } from "./<%- fileName %>.schema";
|
|
3
|
+
import { <%- fileName %>Context } from "./<%- fileName %>.context";
|
|
4
|
+
import {
|
|
5
|
+
<% typings.forEach((typing) => { -%>
|
|
6
|
+
<%- typing.dts.name %>,
|
|
7
|
+
<% }); -%>} from "./<%- fileName %>.typings";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* =============================================================================
|
|
11
|
+
* LDO ShapeTypes <%- fileName %>
|
|
12
|
+
* =============================================================================
|
|
13
|
+
*/
|
|
14
|
+
<% typings.forEach((typing) => { -%>
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* <%- typing.dts.name %> ShapeType
|
|
18
|
+
*/
|
|
19
|
+
export const <%- typing.dts.name %>ShapeType: ShapeType<<%- typing.dts.name %>> = {
|
|
20
|
+
schema: <%- fileName %>Schema,
|
|
21
|
+
shape: "<%- typing.dts.shapeId %>",
|
|
22
|
+
context: <%- fileName %>Context,
|
|
23
|
+
};
|
|
24
|
+
<% }); -%>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ContextDefinition } from "jsonld";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* =============================================================================
|
|
5
|
+
* Typescript Typings for <%- fileName %>
|
|
6
|
+
* =============================================================================
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
<% typings.forEach((typing) => { -%>
|
|
10
|
+
/**
|
|
11
|
+
* <%- typing.dts.name %> Type
|
|
12
|
+
*/
|
|
13
|
+
export <%- typing.typingString -%>
|
|
14
|
+
<% }); -%>
|