Skip to content

Commit

Permalink
Core configuration rework (#49)
Browse files Browse the repository at this point in the history
* Remove package minification

* Rework core configuration system

* Move Vitepress example into new configuration system

* Add new configuration system support to `create-lunaria` package

* Update README to reflect new configuration system

* Create stale-gorillas-cross.md

* Update README.md
  • Loading branch information
yanthomasdev authored Nov 20, 2023
1 parent b7003ec commit 8fea07a
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 319 deletions.
6 changes: 6 additions & 0 deletions .changeset/stale-gorillas-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lunariajs/core": patch
"create-lunaria": patch
---

Reworked core configuration system
1 change: 0 additions & 1 deletion build.preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default definePreset({
rollup: {
esbuild: {
target: 'es2022',
minify: true,
},
},
});
51 changes: 51 additions & 0 deletions examples/vitepress/lunaria.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"repository": "https://github.com/Yan-Thomas/lunaria",
"rootDir": "./examples/vitepress",
"dashboard": {
"url": "https://localhost:3000/"
},
"defaultLocale": {
"label": "English",
"lang": "en",
"content": {
"location": "**/*.md",
"ignore": ["pt/*.md", "es/*.md"]
},
"dictionaries": {
"location": "ui/en/*.{js,cjs,mjs,ts,yml,json}",
"optionalKeys": {
"ui/nav.json": ["today"],
"ui/ui.cjs": ["type"],
"ui/ui.js": ["type"],
"ui/ui.mjs": ["type"],
"ui/ui.mts": ["type"],
"ui/ui.ts": ["type"],
"ui/ui.yml": ["here"]
}
}
},
"locales": [
{
"label": "Português",
"lang": "pt",
"content": {
"location": "pt/**/*.md"
},
"dictionaries": {
"location": "ui/pt/*.{js,cjs,mjs,ts,yml,json}"
}
},
{
"label": "Spanish",
"lang": "es",
"content": {
"location": "es/**/*.md"
},
"dictionaries": {
"location": "ui/es/*.{js,cjs,mjs,ts,yml,json}"
}
}
],
"translatableProperty": "i18nReady",
"renderer": "./renderer.config.js"
}
66 changes: 0 additions & 66 deletions examples/vitepress/lunaria.config.ts

This file was deleted.

7 changes: 7 additions & 0 deletions examples/vitepress/renderer.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineRendererConfig, html } from '@lunariajs/core';

export default defineRendererConfig({
slots: {
afterTitle: () => html`<p>This is an example slotted component!</p>`,
},
});
63 changes: 26 additions & 37 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,40 @@ yarn add @lunariajs/core

## Basic Usage

Start using `@lunariajs/core` by setting up your own `lunaria.config` file and adding a script to your `package.json` file.
Start using `@lunariajs/core` by setting up your own `lunaria.config.json` file and adding a script to your `package.json` file.

The example below contains all of the **required** options to generate a dashboard tracking the status of both the Portuguese and Spanish translations of a site:

```js
// lunaria.config.ts
import { defineConfig } from '@lunariajs/core';

export default defineConfig({
// Current repository of your content
repository: 'https://github.com/me/cool-docs',
dashboard: {
// Generated dashboard URL used in meta tags
url: 'https://tracker.cool-docs.com',
```json
// lunaria.config.json
{
"repository": "https://github.com/me/cool-docs",
"dashboard": {
"url": "https://tracker.cool-docs.com"
},
// Information about the source locale of your content
defaultLocale: {
// User-friendly label/name of the language
label: 'English',
// BCP-47 tag of the language
lang: 'en',
content: {
// Glob pattern of where your content is
location: 'content/en/**/*.md',
},
"defaultLocale": {
"label": "English",
"lang": "en",
"content": {
"location": "content/en/**/*.md"
}
},
// Array of objects of your translated locales.
locales: [
"locales": [
{
label: 'Português',
lang: 'pt',
content: {
location: 'content/pt/**/*.md',
},
"label": "Português",
"lang": "pt",
"content": {
"location": "content/pt/**/*.md"
}
},
{
label: 'Spanish',
lang: 'es',
content: {
location: 'content/es/**/*.md',
},
},
],
// Property to find in valid frontmatter files marking if a page should be translated or not
translatableProperty: 'i18nReady',
"label": "Spanish",
"lang": "es",
"content": {
"location": "content/es/**/*.md"
}
}
]
});
```

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"c12": "^1.5.1",
"destr": "^2.0.2",
"fast-glob": "^3.3.1",
"jiti": "^1.21.0",
"lit-html": "^3.0.0",
Expand Down
48 changes: 37 additions & 11 deletions packages/core/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/usr/bin/env node
import { loadConfig } from 'c12';
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { destr } from 'destr';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { LunariaConfigSchema } from './schemas/config.js';
import { LunariaConfigSchema, LunariaRendererConfigSchema } from './schemas/config.js';
import { generateDashboardHtml, getContentIndex, getTranslationStatus } from './tracker.js';
import { handleShallowRepo } from './utils/git.js';
import { loadFile } from './utils/misc.js';

const { config } = await loadConfig({
name: 'lunaria',
});
const configPath = './lunaria.config.json';

if (config === null) {
console.error(new Error('Could not find a `lunaria.config.*` file, does it exist?'));
process.exit(1);
if (!existsSync(configPath)) {
console.error(
new Error(`Could not find a \`lunaria.config.json\` file in ${process.cwd()}, does it exist?`)
);
}

const parsedConfig = LunariaConfigSchema.safeParse(config);
const configContents = destr(readFileSync(configPath, 'utf-8'));
const parsedConfig = LunariaConfigSchema.safeParse(configContents);

if (!parsedConfig.success) {
console.error(
Expand All @@ -28,14 +29,39 @@ if (!parsedConfig.success) {
}

const userConfig = parsedConfig.data;

if (userConfig.renderer && !existsSync(userConfig.renderer)) {
console.error(
new Error(
`Could not find your specified renderer file at \`${userConfig.renderer}\`, does it exist?`
)
);
process.exit(1);
}

const rendererConfigContents = userConfig.renderer ? loadFile(userConfig.renderer) : {};
const parsedRendererConfig = LunariaRendererConfigSchema.safeParse(rendererConfigContents);

if (!parsedRendererConfig.success) {
console.error(
new Error(
'Invalid renderer configuration options passed to `@lunariajs/core`\n' +
parsedRendererConfig.error.issues.map((i) => i).join('\n')
)
);
process.exit(1);
}

const userRendererConfig = parsedRendererConfig.data;

const isShallowRepo = await handleShallowRepo(userConfig);

console.time('⌛ Building translation dashboard');
console.log(`➡️ Dashboard output path: ${resolve(userConfig.outDir)}`);

const contentIndex = await getContentIndex(userConfig, isShallowRepo);
const translationStatus = await getTranslationStatus(userConfig, contentIndex);
const html = await generateDashboardHtml(userConfig, translationStatus);
const html = await generateDashboardHtml(userConfig, userRendererConfig, translationStatus);

const outputDir = dirname(userConfig.outDir);

Expand Down
33 changes: 23 additions & 10 deletions packages/core/src/dashboard/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,34 @@ import type {
FileTranslationStatus,
Locale,
LunariaConfig,
LunariaRendererConfig,
TranslationStatus,
} from '../types.js';
import { getTextFromFormat } from '../utils/misc.js';
import { Styles } from './styles.js';

export const Page = (opts: LunariaConfig, translationStatus: FileTranslationStatus[]) => {
export const Page = (
opts: LunariaConfig,
rendererOpts: LunariaRendererConfig,
translationStatus: FileTranslationStatus[]
) => {
const { dashboard } = opts;
const { slots, overrides } = rendererOpts;

return html`
<!doctype html>
<html dir="${dashboard.ui.dir}" lang="${dashboard.ui.lang}">
<head>
<!-- Built-in/custom meta tags -->
${dashboard.overrides.meta?.(opts) ?? Meta(dashboard)}
${overrides.meta?.(opts) ?? Meta(dashboard)}
<!-- Additional head tags -->
${dashboard.slots.head?.(opts) ?? ''}
${slots.head?.(opts) ?? ''}
<!-- Built-in/custom styles -->
${dashboard.overrides.styles?.(opts) ?? Styles}
${overrides.styles?.(opts) ?? Styles}
</head>
<body>
<!-- Built-in/custom body content -->
${dashboard.overrides.body?.(opts, translationStatus) ?? Body(opts, translationStatus)}
${overrides.body?.(opts, translationStatus) ?? Body(opts, rendererOpts, translationStatus)}
</body>
</html>
`;
Expand All @@ -42,18 +49,24 @@ export const Meta = (dashboard: Dashboard) => html`
<meta property="og:description" content="${dashboard.description}" />
`;

export const Body = (opts: LunariaConfig, translationStatus: FileTranslationStatus[]) => {
export const Body = (
opts: LunariaConfig,
rendererOpts: LunariaRendererConfig,
translationStatus: FileTranslationStatus[]
) => {
const { dashboard } = opts;
const { slots, overrides } = rendererOpts;

return html`
<main>
<div class="limit-to-viewport">
${dashboard.slots.beforeTitle?.(opts) ?? ''}
${slots.beforeTitle?.(opts) ?? ''}
<h1>${dashboard.title}</h1>
${dashboard.slots.afterTitle?.(opts) ?? ''}
${dashboard.overrides.statusByLocale?.(opts, translationStatus) ??
${slots.afterTitle?.(opts) ?? ''}
${overrides.statusByLocale?.(opts, translationStatus) ??
StatusByLocale(opts, translationStatus)}
</div>
${dashboard.overrides.statusByContent?.(opts, translationStatus) ??
${overrides.statusByContent?.(opts, translationStatus) ??
StatusByContent(opts, translationStatus)}
</main>
`;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { LunariaUserConfig } from './types.js';
import type { LunariaUserRendererConfig } from './types.js';

export { html } from 'lit-html';
export type * from './types.js';

export function defineConfig(opts: LunariaUserConfig) {
export function defineRendererConfig(opts: LunariaUserRendererConfig) {
return opts;
}
Loading

0 comments on commit 8fea07a

Please sign in to comment.