@ldo/cli 0.0.1-1.0.0-alpha.1.0
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/LICENSE.txt +21 -0
- package/README.md +83 -0
- package/dist/build.d.ts +6 -0
- package/dist/build.js +51 -0
- package/dist/build.js.map +1 -0
- package/dist/create.d.ts +1 -0
- package/dist/create.js +98 -0
- package/dist/create.js.map +1 -0
- package/dist/generateReadme.d.ts +11 -0
- package/dist/generateReadme.js +59 -0
- package/dist/generateReadme.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +1 -0
- package/dist/init.js +49 -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/readme/main.ejs +15 -0
- package/dist/templates/readme/projectIndex.ejs +2 -0
- package/dist/templates/readme/shape.ejs +27 -0
- package/dist/templates/schema.ejs +8 -0
- package/dist/templates/shapeTypes.ejs +24 -0
- package/dist/templates/typings.ejs +14 -0
- package/dist/util/forAllShapes.d.ts +1 -0
- package/dist/util/forAllShapes.js +21 -0
- package/dist/util/forAllShapes.js.map +1 -0
- package/dist/util/modifyPackageJson.d.ts +4 -0
- package/dist/util/modifyPackageJson.js +24 -0
- package/dist/util/modifyPackageJson.js.map +1 -0
- package/package.json +67 -0
- package/src/build.ts +66 -0
- package/src/create.ts +111 -0
- package/src/generateReadme.ts +101 -0
- package/src/index.ts +53 -0
- package/src/init.ts +73 -0
- package/src/templates/context.ejs +8 -0
- package/src/templates/defaultShapes/foafProfile.ejs +19 -0
- package/src/templates/readme/main.ejs +15 -0
- package/src/templates/readme/projectIndex.ejs +2 -0
- package/src/templates/readme/shape.ejs +27 -0
- package/src/templates/schema.ejs +8 -0
- package/src/templates/shapeTypes.ejs +24 -0
- package/src/templates/typings.ejs +14 -0
- package/src/util/forAllShapes.ts +26 -0
- package/src/util/modifyPackageJson.ts +33 -0
|
@@ -0,0 +1,24 @@
|
|
|
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.modifyPackageJson = exports.savePackageJson = exports.getPackageJson = void 0;
|
|
7
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
async function getPackageJson(projectFolder) {
|
|
10
|
+
return JSON.parse((await fs_extra_1.default.promises.readFile(path_1.default.join(projectFolder, "./package.json"))).toString());
|
|
11
|
+
}
|
|
12
|
+
exports.getPackageJson = getPackageJson;
|
|
13
|
+
async function savePackageJson(projectFolder, packageJson) {
|
|
14
|
+
await fs_extra_1.default.promises.mkdir(projectFolder, { recursive: true });
|
|
15
|
+
await fs_extra_1.default.promises.writeFile(path_1.default.join(projectFolder, "./package.json"), JSON.stringify(packageJson, null, 2));
|
|
16
|
+
}
|
|
17
|
+
exports.savePackageJson = savePackageJson;
|
|
18
|
+
async function modifyPackageJson(projectFolder, modifyCallback) {
|
|
19
|
+
const packageJson = await getPackageJson(projectFolder);
|
|
20
|
+
const newPackageJson = await modifyCallback(packageJson);
|
|
21
|
+
await savePackageJson(projectFolder, newPackageJson);
|
|
22
|
+
}
|
|
23
|
+
exports.modifyPackageJson = modifyPackageJson;
|
|
24
|
+
//# sourceMappingURL=modifyPackageJson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modifyPackageJson.js","sourceRoot":"","sources":["../../src/util/modifyPackageJson.ts"],"names":[],"mappings":";;;;;;AACA,wDAA0B;AAC1B,gDAAwB;AAEjB,KAAK,UAAU,cAAc,CAClC,aAAqB;IAErB,OAAO,IAAI,CAAC,KAAK,CACf,CACE,MAAM,kBAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,cAAI,CAAC,IAAI,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC,CACvE,CAAC,QAAQ,EAAE,CACb,CAAC;AACJ,CAAC;AARD,wCAQC;AAEM,KAAK,UAAU,eAAe,CACnC,aAAqB,EACrB,WAAwB;IAExB,MAAM,kBAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,aAAa,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAC5D,MAAM,kBAAE,CAAC,QAAQ,CAAC,SAAS,CACzB,cAAI,CAAC,IAAI,CAAC,aAAa,EAAE,gBAAgB,CAAC,EAC1C,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC,CACrC,CAAC;AACJ,CAAC;AATD,0CASC;AAEM,KAAK,UAAU,iBAAiB,CACrC,aAAqB,EACrB,cAAkE;IAElE,MAAM,WAAW,GAAgB,MAAM,cAAc,CAAC,aAAa,CAAC,CAAC;IACrE,MAAM,cAAc,GAAG,MAAM,cAAc,CAAC,WAAW,CAAC,CAAC;IACzD,MAAM,eAAe,CAAC,aAAa,EAAE,cAAc,CAAC,CAAC;AACvD,CAAC;AAPD,8CAOC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ldo/cli",
|
|
3
|
+
"version": "0.0.1-1.0.0-alpha.1.0",
|
|
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
|
+
"build": "npm run clean && npm run build:ts && npm run copy-files && npm run update-permission",
|
|
11
|
+
"build:ts": "tsc --project tsconfig.build.json",
|
|
12
|
+
"clean": "rimraf dist/",
|
|
13
|
+
"copy-files": "copyfiles -u 1 \"./src/**/*.ejs\" dist/",
|
|
14
|
+
"update-permission": "chmod +x ./dist/index.js",
|
|
15
|
+
"test": "jest --coverage",
|
|
16
|
+
"prepublishOnly": "npm run test && npm run build",
|
|
17
|
+
"lint": "eslint src/** --fix --no-error-on-unmatched-pattern",
|
|
18
|
+
"test:init": "rm -rf ./example-init && cp -R ./example-init-placeholder ./example-init && cd ./example-init && ../dist/index.js init",
|
|
19
|
+
"test:create": "rm -rf ./example-create && ./dist/index.js create ./example-create"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/o-development/ldobjects.git"
|
|
24
|
+
},
|
|
25
|
+
"author": "Jackson Morgan",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/o-development/ldobjects/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/o-development/ldobjects/tree/main/packages/cli#readme",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@ldo/cli": "^0.0.1-alpha.28",
|
|
33
|
+
"@types/child-process-promise": "^2.2.2",
|
|
34
|
+
"@types/ejs": "^3.1.1",
|
|
35
|
+
"@types/fs-extra": "^9.0.13",
|
|
36
|
+
"@types/jest": "^27.0.3",
|
|
37
|
+
"@types/jsonld": "^1.5.15",
|
|
38
|
+
"@types/prompts": "^2.4.9",
|
|
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/ldo": "^0.0.1-1.0.0-alpha.1.0",
|
|
47
|
+
"@ldo/schema-converter-shex": "^0.0.1-1.0.0-alpha.1.0",
|
|
48
|
+
"@shexjs/parser": "^1.0.0-alpha.24",
|
|
49
|
+
"child-process-promise": "^2.2.1",
|
|
50
|
+
"commander": "^9.3.0",
|
|
51
|
+
"ejs": "^3.1.8",
|
|
52
|
+
"fs-extra": "^10.1.0",
|
|
53
|
+
"loading-cli": "^1.1.0",
|
|
54
|
+
"prettier": "^3.0.3",
|
|
55
|
+
"prompts": "^2.4.2",
|
|
56
|
+
"ts-morph": "^24.0.0",
|
|
57
|
+
"type-fest": "^2.19.0"
|
|
58
|
+
},
|
|
59
|
+
"files": [
|
|
60
|
+
"dist",
|
|
61
|
+
"src"
|
|
62
|
+
],
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
},
|
|
66
|
+
"gitHead": "d7b2ebdb3c49c15615dd6c1f6bd170c867191bd3"
|
|
67
|
+
}
|
package/src/build.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
import { forAllShapes } from "./util/forAllShapes";
|
|
10
|
+
|
|
11
|
+
interface BuildOptions {
|
|
12
|
+
input: string;
|
|
13
|
+
output: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function build(options: BuildOptions) {
|
|
17
|
+
const load = loading("Peparing Environment");
|
|
18
|
+
load.start();
|
|
19
|
+
// Prepare new folder by clearing/and/or creating it
|
|
20
|
+
if (fs.existsSync(options.output)) {
|
|
21
|
+
await fs.promises.rm(options.output, { recursive: true });
|
|
22
|
+
}
|
|
23
|
+
await fs.promises.mkdir(options.output);
|
|
24
|
+
|
|
25
|
+
load.text = "Generating LDO Documents";
|
|
26
|
+
await forAllShapes(options.input, async (fileName, shexC) => {
|
|
27
|
+
// Convert to ShexJ
|
|
28
|
+
let schema: Schema;
|
|
29
|
+
try {
|
|
30
|
+
schema = parser.construct("https://ldo.js.org/").parse(shexC);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
const errMessage =
|
|
33
|
+
err instanceof Error
|
|
34
|
+
? err.message
|
|
35
|
+
: typeof err === "string"
|
|
36
|
+
? err
|
|
37
|
+
: "Unknown Error";
|
|
38
|
+
console.error(`Error processing ${fileName}: ${errMessage}`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// Convert the content to types
|
|
42
|
+
const [typings, context] = await schemaConverterShex(schema);
|
|
43
|
+
await Promise.all(
|
|
44
|
+
["context", "schema", "shapeTypes", "typings"].map(
|
|
45
|
+
async (templateName) => {
|
|
46
|
+
const finalContent = await renderFile(
|
|
47
|
+
path.join(__dirname, "./templates", `${templateName}.ejs`),
|
|
48
|
+
{
|
|
49
|
+
typings: typings.typings,
|
|
50
|
+
fileName,
|
|
51
|
+
schema: JSON.stringify(schema, null, 2),
|
|
52
|
+
context: JSON.stringify(context, null, 2),
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
// Save conversion to document
|
|
56
|
+
await fs.promises.writeFile(
|
|
57
|
+
path.join(options.output, `${fileName}.${templateName}.ts`),
|
|
58
|
+
await prettier.format(finalContent, { parser: "typescript" }),
|
|
59
|
+
);
|
|
60
|
+
},
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
load.stop();
|
|
66
|
+
}
|
package/src/create.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { init } from "./init";
|
|
2
|
+
import { modifyPackageJson, savePackageJson } from "./util/modifyPackageJson";
|
|
3
|
+
import { generateReadme } from "./generateReadme";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import prompts from "prompts";
|
|
6
|
+
import type { PackageJson } from "type-fest";
|
|
7
|
+
import loading from "loading-cli";
|
|
8
|
+
import { promises as fs } from "fs";
|
|
9
|
+
import { renderFile } from "ejs";
|
|
10
|
+
|
|
11
|
+
export async function create(directory: string) {
|
|
12
|
+
// Init the NPM Package
|
|
13
|
+
const responses = await prompts([
|
|
14
|
+
{
|
|
15
|
+
type: "text",
|
|
16
|
+
name: "name",
|
|
17
|
+
message: "Package name:",
|
|
18
|
+
initial: path.basename(directory),
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
type: "text",
|
|
22
|
+
name: "version",
|
|
23
|
+
message: "Version:",
|
|
24
|
+
initial: "1.0.0",
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
type: "text",
|
|
28
|
+
name: "description",
|
|
29
|
+
message: "Description:",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
type: "list",
|
|
33
|
+
name: "keywords",
|
|
34
|
+
message: "Keywords (comma separated):",
|
|
35
|
+
separator: ",",
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
type: "text",
|
|
39
|
+
name: "author",
|
|
40
|
+
message: "Author:",
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
type: "text",
|
|
44
|
+
name: "license",
|
|
45
|
+
message: "License:",
|
|
46
|
+
initial: "MIT",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
type: "text",
|
|
50
|
+
name: "repository",
|
|
51
|
+
message: "Git repository (optional):",
|
|
52
|
+
},
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
const load = loading("Generating package.json");
|
|
56
|
+
|
|
57
|
+
const packageJson: PackageJson = {
|
|
58
|
+
name: responses.name,
|
|
59
|
+
version: responses.version,
|
|
60
|
+
description: responses.description,
|
|
61
|
+
keywords: responses.keywords,
|
|
62
|
+
author: responses.author,
|
|
63
|
+
license: responses.license,
|
|
64
|
+
main: "./index.js",
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
if (responses.repository) {
|
|
68
|
+
packageJson.repository = {
|
|
69
|
+
type: "git",
|
|
70
|
+
url: responses.repository,
|
|
71
|
+
};
|
|
72
|
+
packageJson.bugs = {
|
|
73
|
+
url: `${responses.repository.replace(/\.git$/, "")}/issues`,
|
|
74
|
+
};
|
|
75
|
+
packageJson.homepage = `${responses.repository.replace(
|
|
76
|
+
/\.git$/,
|
|
77
|
+
"",
|
|
78
|
+
)}#readme`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
await savePackageJson(directory, packageJson);
|
|
82
|
+
|
|
83
|
+
// Init LDO
|
|
84
|
+
load.text = "Initializing LDO";
|
|
85
|
+
await init(directory);
|
|
86
|
+
|
|
87
|
+
// Add prepublish script
|
|
88
|
+
await modifyPackageJson(directory, async (packageJson) => {
|
|
89
|
+
if (!packageJson.scripts) packageJson.scripts = {};
|
|
90
|
+
packageJson.scripts.prepublish =
|
|
91
|
+
"npm run build:ldo && npm run generate-readme";
|
|
92
|
+
packageJson.scripts[
|
|
93
|
+
"generate-readme"
|
|
94
|
+
] = `ldo generate-readme --project ./ --shapes ./.shapes --ldo ./.ldo`;
|
|
95
|
+
return packageJson;
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Generate ReadMe
|
|
99
|
+
load.text = "Generating README";
|
|
100
|
+
await generateReadme({
|
|
101
|
+
project: directory,
|
|
102
|
+
shapes: path.join(directory, ".shapes"),
|
|
103
|
+
ldo: path.join(directory, ".ldo"),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Create .gitignore
|
|
107
|
+
load.text = "Create .gitignore";
|
|
108
|
+
await fs.writeFile(path.join(directory, ".gitignore"), "node_modules");
|
|
109
|
+
|
|
110
|
+
load.stop();
|
|
111
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { getPackageJson } from "./util/modifyPackageJson";
|
|
2
|
+
import { forAllShapes } from "./util/forAllShapes";
|
|
3
|
+
import { promises as fs } from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { Project } from "ts-morph";
|
|
6
|
+
import { renderFile } from "ejs";
|
|
7
|
+
|
|
8
|
+
interface GenerateReadmeOptions {
|
|
9
|
+
project: string;
|
|
10
|
+
shapes: string;
|
|
11
|
+
ldo: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ReadmeEjsOptions {
|
|
15
|
+
projectName: string;
|
|
16
|
+
projectDescription: string;
|
|
17
|
+
shapes: {
|
|
18
|
+
name: string;
|
|
19
|
+
types: {
|
|
20
|
+
typeName: string;
|
|
21
|
+
shapeTypeName: string;
|
|
22
|
+
}[];
|
|
23
|
+
shex: string;
|
|
24
|
+
typescript: string;
|
|
25
|
+
}[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function generateReadme(options: GenerateReadmeOptions) {
|
|
29
|
+
const packageJson = await getPackageJson(options.project);
|
|
30
|
+
const projectName = packageJson.name!;
|
|
31
|
+
const projectDescription = packageJson.description!;
|
|
32
|
+
const shapes: ReadmeEjsOptions["shapes"] = [];
|
|
33
|
+
|
|
34
|
+
await forAllShapes(options.shapes, async (fileName, shexC) => {
|
|
35
|
+
const typeFilePath = path.join(options.ldo, `${fileName}.typings.ts`);
|
|
36
|
+
|
|
37
|
+
const typesRaw = await fs.readFile(typeFilePath, "utf8");
|
|
38
|
+
|
|
39
|
+
const shape: ReadmeEjsOptions["shapes"][0] = {
|
|
40
|
+
name: fileName,
|
|
41
|
+
shex: shexC,
|
|
42
|
+
typescript: typesRaw,
|
|
43
|
+
types: [],
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
listInterfaces(typeFilePath).forEach((interfaceName) => {
|
|
47
|
+
shape.types.push({
|
|
48
|
+
typeName: interfaceName,
|
|
49
|
+
shapeTypeName: `${interfaceName}ShapeType`,
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
shapes.push(shape);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const readmeEjsOptions: ReadmeEjsOptions = {
|
|
57
|
+
projectName,
|
|
58
|
+
projectDescription,
|
|
59
|
+
shapes,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// Save Readme
|
|
63
|
+
const finalContent = await renderFile(
|
|
64
|
+
path.join(__dirname, "./templates/readme/", "main.ejs"),
|
|
65
|
+
readmeEjsOptions,
|
|
66
|
+
);
|
|
67
|
+
// Save readme to document
|
|
68
|
+
await fs.writeFile(path.join(options.project, "README.md"), finalContent);
|
|
69
|
+
|
|
70
|
+
await generateIndex({ project: options.project });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Helper Function that lists all the interfaces in a typescript file
|
|
75
|
+
*/
|
|
76
|
+
function listInterfaces(filePath: string): string[] {
|
|
77
|
+
const project = new Project();
|
|
78
|
+
const sourceFile = project.addSourceFileAtPath(filePath);
|
|
79
|
+
|
|
80
|
+
// Get all interfaces in the file
|
|
81
|
+
const interfaces = sourceFile.getInterfaces().map((iface) => iface.getName());
|
|
82
|
+
return interfaces;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Generate Index
|
|
87
|
+
*/
|
|
88
|
+
interface GenerateIndexOptions {
|
|
89
|
+
project: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function generateIndex(options: GenerateIndexOptions) {
|
|
93
|
+
const ldoDir = await fs.readdir(path.join(options.project, "./.ldo"), {
|
|
94
|
+
withFileTypes: true,
|
|
95
|
+
});
|
|
96
|
+
const indexText = await renderFile(
|
|
97
|
+
path.join(__dirname, "./templates/readme/projectIndex.ejs"),
|
|
98
|
+
{ fileNames: ldoDir.map((file) => file.name) },
|
|
99
|
+
);
|
|
100
|
+
await fs.writeFile(path.join(options.project, "index.js"), indexText);
|
|
101
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { program } from "commander";
|
|
4
|
+
import { build } from "./build";
|
|
5
|
+
import { init } from "./init";
|
|
6
|
+
import { create } from "./create";
|
|
7
|
+
import { generateReadme } from "./generateReadme";
|
|
8
|
+
|
|
9
|
+
program
|
|
10
|
+
.name("LDO-CLI")
|
|
11
|
+
.description("CLI to some JavaScript string utilities")
|
|
12
|
+
.version("3.0.1");
|
|
13
|
+
|
|
14
|
+
program
|
|
15
|
+
.command("build")
|
|
16
|
+
.description("Build contents of a shex folder into Shape Types")
|
|
17
|
+
.option("-i, --input <inputPath>", "Provide the input path", "./.shapes")
|
|
18
|
+
.option("-o, --output <outputPath>", "Provide the output path", "./.ldo")
|
|
19
|
+
.action(build);
|
|
20
|
+
|
|
21
|
+
program
|
|
22
|
+
.command("init")
|
|
23
|
+
.argument("[directory]", "A parent directory for ldo files")
|
|
24
|
+
.description("Initializes a project for LDO.")
|
|
25
|
+
.action(init);
|
|
26
|
+
|
|
27
|
+
program
|
|
28
|
+
.command("create")
|
|
29
|
+
.argument("<directory>", "The package's directory")
|
|
30
|
+
.description("Creates a standalone package for shapes to publish to NPM.")
|
|
31
|
+
.action(create);
|
|
32
|
+
|
|
33
|
+
program
|
|
34
|
+
.command("generate-readme")
|
|
35
|
+
.description("Create a ReadMe from the shapes and generated code.")
|
|
36
|
+
.requiredOption(
|
|
37
|
+
"-p, --project <projectPath>",
|
|
38
|
+
"Provide the path to the root project",
|
|
39
|
+
"./",
|
|
40
|
+
)
|
|
41
|
+
.requiredOption(
|
|
42
|
+
"-s, --shapes <shapesPath>",
|
|
43
|
+
"Provide the path to the shapes folder",
|
|
44
|
+
"./.shapes",
|
|
45
|
+
)
|
|
46
|
+
.requiredOption(
|
|
47
|
+
"-s, --ldo <ldoPath>",
|
|
48
|
+
"Provide the path to the ldo folder",
|
|
49
|
+
"./.ldo",
|
|
50
|
+
)
|
|
51
|
+
.action(generateReadme);
|
|
52
|
+
|
|
53
|
+
program.parse();
|
package/src/init.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { exec } from "child-process-promise";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { renderFile } from "ejs";
|
|
5
|
+
import { modifyPackageJson } from "./util/modifyPackageJson";
|
|
6
|
+
|
|
7
|
+
const DEFAULT_SHAPES_FOLDER = "./.shapes";
|
|
8
|
+
const DEFAULT_LDO_FOLDER = "./.ldo";
|
|
9
|
+
const POTENTIAL_PARENT_DIRECTORIES = ["src", "lib", "bin"];
|
|
10
|
+
|
|
11
|
+
export async function init(directory?: string) {
|
|
12
|
+
// Find folder to save to
|
|
13
|
+
let parentDirectory = directory!;
|
|
14
|
+
if (!parentDirectory) {
|
|
15
|
+
parentDirectory = "./";
|
|
16
|
+
const allDirectories = (
|
|
17
|
+
await fs.promises.readdir("./", {
|
|
18
|
+
withFileTypes: true,
|
|
19
|
+
})
|
|
20
|
+
).filter((file) => file.isDirectory());
|
|
21
|
+
for (let i = 0; i < POTENTIAL_PARENT_DIRECTORIES.length; i++) {
|
|
22
|
+
if (
|
|
23
|
+
allDirectories.some(
|
|
24
|
+
(dir) => dir.name === POTENTIAL_PARENT_DIRECTORIES[i],
|
|
25
|
+
)
|
|
26
|
+
) {
|
|
27
|
+
parentDirectory = POTENTIAL_PARENT_DIRECTORIES[i];
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Install dependencies
|
|
34
|
+
await exec(`cd ${parentDirectory} && npm install @ldo/ldo --save`);
|
|
35
|
+
await exec(
|
|
36
|
+
`cd ${parentDirectory} && npm install @ldo/cli @types/shexj @types/jsonld --save-dev`,
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
// Create "shapes" folder
|
|
40
|
+
const shapesFolderPath = path.join(parentDirectory, DEFAULT_SHAPES_FOLDER);
|
|
41
|
+
await fs.promises.mkdir(shapesFolderPath);
|
|
42
|
+
const defaultShapePaths = await fs.promises.readdir(
|
|
43
|
+
path.join(__dirname, "./templates/defaultShapes"),
|
|
44
|
+
);
|
|
45
|
+
await Promise.all(
|
|
46
|
+
defaultShapePaths.map(async (shapePath) => {
|
|
47
|
+
const shapeContent = await renderFile(
|
|
48
|
+
path.join(__dirname, "./templates/defaultShapes", shapePath),
|
|
49
|
+
{},
|
|
50
|
+
);
|
|
51
|
+
await fs.promises.writeFile(
|
|
52
|
+
path.join(shapesFolderPath, `${path.parse(shapePath).name}.shex`),
|
|
53
|
+
shapeContent,
|
|
54
|
+
);
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
// Add build script
|
|
59
|
+
await modifyPackageJson(parentDirectory, async (packageJson) => {
|
|
60
|
+
if (!packageJson.scripts) {
|
|
61
|
+
packageJson.scripts = {};
|
|
62
|
+
}
|
|
63
|
+
const ldoFolder = path.join(parentDirectory, DEFAULT_LDO_FOLDER);
|
|
64
|
+
packageJson.scripts["build:ldo"] = `ldo build --input ${path.relative(
|
|
65
|
+
parentDirectory,
|
|
66
|
+
shapesFolderPath,
|
|
67
|
+
)} --output ${path.relative(parentDirectory, ldoFolder)}`;
|
|
68
|
+
return packageJson;
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Build LDO
|
|
72
|
+
await exec(`cd ${parentDirectory} && npm run build:ldo`);
|
|
73
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { LdoJsonldContext } from "@ldo/ldo";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* =============================================================================
|
|
5
|
+
* <%- fileName %>Context: JSONLD Context for <%- fileName %>
|
|
6
|
+
* =============================================================================
|
|
7
|
+
*/
|
|
8
|
+
export const <%- fileName %>Context: LdoJsonldContext = <%- 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,15 @@
|
|
|
1
|
+
# <%= projectName %>
|
|
2
|
+
|
|
3
|
+
<%- projectDescription %>
|
|
4
|
+
|
|
5
|
+
This project includes shapes and generated files for [LDO](https://ldo.js.org).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i <%= projectName %>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
<% shapes.forEach(function(shape) { %>
|
|
14
|
+
<%- include('shape', { shape: shape, projectName: projectName }) %>
|
|
15
|
+
<% }); %>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
## <%= shape.name %>
|
|
2
|
+
|
|
3
|
+
### Usage with LDO
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { createLdoDataset } from "@ldo/ldo";
|
|
7
|
+
import { <%= shape.types.map((type) => type.shapeTypeName).join(", ") %> } from "<%= projectName %>";
|
|
8
|
+
import type { <%= shape.types.map((type) => type.typeName).join(", ") %> } from "<%= projectName %>";
|
|
9
|
+
const ldoDataset = createLdoDataset();
|
|
10
|
+
<% shape.types.forEach(function(type, index) { %>
|
|
11
|
+
const example<%= index %>: <%= type.typeName %> = ldoDataset
|
|
12
|
+
.usingType(<%= type.shapeTypeName %>)
|
|
13
|
+
.fromSubject("http://example.com/example<%= index %>");
|
|
14
|
+
<% }); %>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### ShEx Typings
|
|
18
|
+
|
|
19
|
+
```shex
|
|
20
|
+
<%- shape.shex %>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### TypeScript Typings
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
<%- shape.typescript %>
|
|
27
|
+
```
|
|
@@ -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 { LdoJsonldContext, LdSet } from "@ldo/ldo";
|
|
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
|
+
<% }); -%>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
export async function forAllShapes(
|
|
5
|
+
shapePath: string,
|
|
6
|
+
callback: (filename: string, shape: string) => Promise<void>,
|
|
7
|
+
): Promise<void> {
|
|
8
|
+
const shapeDir = await fs.promises.readdir(shapePath, {
|
|
9
|
+
withFileTypes: true,
|
|
10
|
+
});
|
|
11
|
+
// Filter out non-shex documents
|
|
12
|
+
const shexFiles = shapeDir.filter(
|
|
13
|
+
(file) => file.isFile() && file.name.endsWith(".shex"),
|
|
14
|
+
);
|
|
15
|
+
await Promise.all(
|
|
16
|
+
shexFiles.map(async (file) => {
|
|
17
|
+
const fileName = path.parse(file.name).name;
|
|
18
|
+
// Get the content of each document
|
|
19
|
+
const shexC = await fs.promises.readFile(
|
|
20
|
+
path.join(shapePath, file.name),
|
|
21
|
+
"utf8",
|
|
22
|
+
);
|
|
23
|
+
await callback(fileName, shexC);
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
}
|