parent
8c50776013
commit
22c476d3ef
@ -0,0 +1 @@ |
|||||||
|
test/.ldo |
@ -0,0 +1,36 @@ |
|||||||
|
import { exec } from "child-process-promise"; |
||||||
|
import { init } from "./init"; |
||||||
|
import { modifyPackageJson } from "./util/modifyPackageJson"; |
||||||
|
import { generateReadme } from "./generateReadme"; |
||||||
|
import path from "path"; |
||||||
|
|
||||||
|
interface CreateOptions { |
||||||
|
directory: string; |
||||||
|
name: string; |
||||||
|
} |
||||||
|
|
||||||
|
export async function create(options: CreateOptions) { |
||||||
|
// Init the NPM Package
|
||||||
|
await exec(`npm init ${options.directory}`); |
||||||
|
|
||||||
|
// Init LDO
|
||||||
|
await init({ directory: options.directory }); |
||||||
|
|
||||||
|
// Add prepublish script
|
||||||
|
await modifyPackageJson(async (packageJson) => { |
||||||
|
if (!packageJson.scripts) packageJson.scripts = {}; |
||||||
|
packageJson.scripts.prepublish = |
||||||
|
"npm run build:ldo & npm run generate-readme"; |
||||||
|
packageJson.scripts[ |
||||||
|
"genenerate-readme" |
||||||
|
] = `ldo generate-readme --readme ./README.md --shapes ./.shapes --ldo ./ldo`; |
||||||
|
return packageJson; |
||||||
|
}); |
||||||
|
|
||||||
|
// Generate ReadMe
|
||||||
|
await generateReadme({ |
||||||
|
readmePath: path.join(options.directory, "README.md"), |
||||||
|
shapesPath: path.join(options.directory, ".shapes"), |
||||||
|
ldoPath: path.join(options.directory, ".ldo"), |
||||||
|
}); |
||||||
|
} |
@ -0,0 +1,50 @@ |
|||||||
|
import { getPackageJson } from "./util/modifyPackageJson"; |
||||||
|
import { forAllShapes } from "./util/forAllShapes"; |
||||||
|
import { promises as fs } from "fs"; |
||||||
|
import path from "path"; |
||||||
|
|
||||||
|
interface GenerateReadmeOptions { |
||||||
|
projectFolder: string; |
||||||
|
shapesPath: string; |
||||||
|
ldoPath: string; |
||||||
|
} |
||||||
|
|
||||||
|
interface ReadmeEjsOptions { |
||||||
|
projectName: string; |
||||||
|
projectDescription: string; |
||||||
|
shapes: { |
||||||
|
name: string; |
||||||
|
types: { |
||||||
|
typeName: string; |
||||||
|
shapeTypeName: string; |
||||||
|
}[]; |
||||||
|
shex: string; |
||||||
|
typescript: string; |
||||||
|
}[]; |
||||||
|
} |
||||||
|
|
||||||
|
export async function generateReadme(options: GenerateReadmeOptions) { |
||||||
|
const packageJson = await getPackageJson(options.projectFolder); |
||||||
|
const projectName = packageJson.name; |
||||||
|
const projectDescription = packageJson.description; |
||||||
|
const shapes: ReadmeEjsOptions["shapes"] = []; |
||||||
|
|
||||||
|
await forAllShapes(options.shapesPath, async (fileName, shexC) => { |
||||||
|
const typesRaw = await fs.readFile( |
||||||
|
path.join(options.shapesPath, `${fileName}.typings.ts`), |
||||||
|
"utf8", |
||||||
|
); |
||||||
|
const shapeTypesRaw = await fs.readFile( |
||||||
|
path.join(options.shapesPath, `${fileName}.shapeTypes.ts`), |
||||||
|
"utf8", |
||||||
|
); |
||||||
|
|
||||||
|
console.log(typesRaw); |
||||||
|
console.log(shapeTypesRaw); |
||||||
|
|
||||||
|
// shapes.push({
|
||||||
|
// name: fileName,
|
||||||
|
// shex: shexC,
|
||||||
|
// });
|
||||||
|
}); |
||||||
|
} |
@ -0,0 +1,15 @@ |
|||||||
|
# <%= projectname %> |
||||||
|
|
||||||
|
<%= projectDescrition %> |
||||||
|
|
||||||
|
This project includes shapes and generated files for [LDO](https://ldo.js.org). |
||||||
|
|
||||||
|
## Installation |
||||||
|
|
||||||
|
```bash |
||||||
|
npm i <%= projectname %> |
||||||
|
``` |
||||||
|
|
||||||
|
<% shapes.forEach(function(shape) { %> |
||||||
|
<%- include('shape', { shape: shape, projectname: projectname }) %> |
||||||
|
<% }); %> |
@ -0,0 +1,27 @@ |
|||||||
|
## <%= shape.name %> |
||||||
|
|
||||||
|
### Usage with LDO |
||||||
|
|
||||||
|
```typescript |
||||||
|
import { createLdoDataset } from "@ldo/ldo"; |
||||||
|
import { <%= shape.types.map((type) => type.shapeTypeName).join(", ") %> } from "<%= projectName =>"; |
||||||
|
import type { <%= shape.types.map((type) => type.typeName).join(", ") %> } from "<%= projectName =>"; |
||||||
|
const ldoDataset = createLdoDataset(); |
||||||
|
<% shape.types.forEach(function(type, index) { %> |
||||||
|
const example<%= index %>: <%= type.typeName %> = ldoDataset |
||||||
|
.usingType(<%= type.shapeTypeName %>) |
||||||
|
.fromSubject("http://example.com/example<%= index %>"); |
||||||
|
<%> }); <%> |
||||||
|
``` |
||||||
|
|
||||||
|
### ShEx Typings |
||||||
|
|
||||||
|
```shex |
||||||
|
<%= shape.shex %> |
||||||
|
``` |
||||||
|
|
||||||
|
### TypeScript Typings |
||||||
|
|
||||||
|
```typescript |
||||||
|
<%= shape.typescript %> |
||||||
|
``` |
@ -0,0 +1,26 @@ |
|||||||
|
import fs from "fs"; |
||||||
|
import path from "path"; |
||||||
|
|
||||||
|
export async function forAllShapes( |
||||||
|
shapePath: string, |
||||||
|
callback: (filename: string, shape: string) => Promise<void>, |
||||||
|
): Promise<void> { |
||||||
|
const shapeDir = await fs.promises.readdir(shapePath, { |
||||||
|
withFileTypes: true, |
||||||
|
}); |
||||||
|
// Filter out non-shex documents
|
||||||
|
const shexFiles = shapeDir.filter( |
||||||
|
(file) => file.isFile() && file.name.endsWith(".shex"), |
||||||
|
); |
||||||
|
await Promise.all( |
||||||
|
shexFiles.map(async (file) => { |
||||||
|
const fileName = path.parse(file.name).name; |
||||||
|
// Get the content of each document
|
||||||
|
const shexC = await fs.promises.readFile( |
||||||
|
path.join(shapePath, file.name), |
||||||
|
"utf8", |
||||||
|
); |
||||||
|
await callback(fileName, shexC); |
||||||
|
}), |
||||||
|
); |
||||||
|
} |
@ -0,0 +1,25 @@ |
|||||||
|
import type { PackageJson } from "type-fest"; |
||||||
|
import fs from "fs-extra"; |
||||||
|
import path from "path"; |
||||||
|
|
||||||
|
export async function getPackageJson( |
||||||
|
projectFolder: string, |
||||||
|
): Promise<PackageJson> { |
||||||
|
return JSON.parse( |
||||||
|
( |
||||||
|
await fs.promises.readFile(path.join(projectFolder, "./package.json")) |
||||||
|
).toString(), |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
export async function modifyPackageJson( |
||||||
|
projectFolder: string, |
||||||
|
modifyCallback: (packageJson: PackageJson) => Promise<PackageJson>, |
||||||
|
): Promise<void> { |
||||||
|
const packageJson: PackageJson = await getPackageJson(projectFolder); |
||||||
|
const newPackageJson = await modifyCallback(packageJson); |
||||||
|
await fs.promises.writeFile( |
||||||
|
path.join(projectFolder, "./package.json"), |
||||||
|
JSON.stringify(newPackageJson, null, 2), |
||||||
|
); |
||||||
|
} |
@ -0,0 +1,4 @@ |
|||||||
|
{ |
||||||
|
"name": "foaf-profile", |
||||||
|
"description": "A profile using FOAF." |
||||||
|
} |
Loading…
Reference in new issue