Skip to content

Commit

Permalink
Add configuration JSON Schema support (#60)
Browse files Browse the repository at this point in the history
* Add configuration JSON Schema support

* Create eleven-glasses-peel.md
  • Loading branch information
yanthomasdev authored Nov 29, 2023
1 parent a7e07b2 commit a1d05f4
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/eleven-glasses-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lunariajs/core": patch
"create-lunaria": patch
---

Add configuration JSON Schema support
1 change: 1 addition & 0 deletions examples/vitepress/lunaria.config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "./node_modules/@lunariajs/core/config.schema.json",
"repository": {
"name": "Yan-Thomas/lunaria",
"rootDir": "examples/vitepress"
Expand Down
1 change: 1 addition & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The example below contains all of the **required** options to generate a dashboa
```json
// lunaria.config.json
{
"$schema": "./node_modules/@lunariajs/core/config.schema.json",
"repository": {
"name": "me/cool-docs"
},
Expand Down
11 changes: 7 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"files": [
"dist",
"config.schema.json",
"CHANGELOG.md"
],
"keywords": [
Expand All @@ -39,13 +40,15 @@
},
"bugs": "https://github.com/Yan-Thomas/lunaria/issues",
"scripts": {
"build": "unbuild",
"lint": "tsc"
"build": "unbuild && pnpm run schema",
"lint": "tsc",
"schema": "jiti ./scripts/schema.ts"
},
"devDependencies": {
"@types/micromatch": "^4.0.3",
"@types/node": "^20.8.9",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"zod-to-json-schema": "^3.22.1"
},
"dependencies": {
"destr": "^2.0.2",
Expand All @@ -58,6 +61,6 @@
"simple-git": "^3.20.0",
"ufo": "^1.3.1",
"ultramatter": "^0.0.4",
"zod": "^3.22.3"
"zod": "^3.22.4"
}
}
18 changes: 18 additions & 0 deletions packages/core/scripts/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { writeFileSync } from 'node:fs';
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import { LunariaConfigSchema } from '../src/schemas/config.js';

const mergedSchema = LunariaConfigSchema.merge(
z
.object({
$schema: z.string().optional(),
})
.describe('A path or URL to a valid Lunaria JSON Schema.')
);

const jsonSchema = JSON.stringify(zodToJsonSchema(mergedSchema, 'LunariaConfigSchema'));

writeFileSync('./dist/config.schema.json', jsonSchema);

console.log('Lunaria JSON schema generated!');
5 changes: 5 additions & 0 deletions packages/create-lunaria/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ import { defineBuildConfig } from 'unbuild';

export default defineBuildConfig({
entries: ['src/create-lunaria'],
rollup: {
esbuild: {
minify: true,
},
},
});
11 changes: 8 additions & 3 deletions packages/create-lunaria/src/create-lunaria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import { join, resolve } from 'node:path';
import { addDependency, detectPackageManager, installDependencies } from 'nypm';
import color from 'picocolors';

// TODO: Add JSON Schema here!
const cleanConfigContent = '{\n \n}';
const cleanConfigContent = JSON.stringify(
{
$schema: './node_modules/@lunariajs/core/config.schema.json',
},
null,
2
);

async function main() {
console.clear();
Expand All @@ -24,7 +29,7 @@ async function main() {
if (value[0] !== '.') return 'Please enter a relative path.';
},
}),
install: ({ results }) =>
install: () =>
p.confirm({
message: 'Install dependencies?',
initialValue: false,
Expand Down
28 changes: 16 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a1d05f4

Please sign in to comment.