Skip to content

Commit

Permalink
chore: init
Browse files Browse the repository at this point in the history
  • Loading branch information
davidenke committed Oct 20, 2024
0 parents commit 159cc99
Show file tree
Hide file tree
Showing 39 changed files with 14,630 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
.DS_Store
dist
tmp

.vscode/*
!.vscode/extensions.json
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.18.0
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arrowParens": "avoid",
"endOfLine": "auto",
"jsxSingleQuote": false,
"printWidth": 100,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 David Enke

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Astro Decap Collection

Derive Astro collection schemata from Decap configs.

## Local development

Run a local tsx compiler in watch mode

```bash
npx tsx watch ./src/cli.ts -c ../website/public/admin/config.yml -t ../website/src/content
```

## Validation only (runtime)

This wont get you types, but you can still validate content against the schema.

```typescript
import {
getCollection,
loadDecapConfig,
prepareSchema,
transformCollection,
} from 'astro-decap-collection';
import { defineCollection, z as zod } from 'astro:content';
import { fileURLToPath } from 'node:url';

// load Decap config and transform it at runtime
const configURL = fileURLToPath(new URL('../../public/admin/config.yml', import.meta.url));
const config = await loadDecapConfig(configURL);
const collection = getCollection(config, 'blog')!;
const schema = await transformCollection(collection, { zod });

export const collections = {
blog: defineCollection(prepareSchema(schema.runtime)),
};
```

## Types and validation (preferred)

Transform the Decap config at build time and use the generated Zod schema.

```bash
# astrodecap-collection, adc - Binary name
# --config, -c - Decap YML config file path to read from
# --target, -t - Astro content directory path to write to
adc -c ./public/admin/config.yml -t ./src/content
```

```typescript
import { defineCollection } from 'astro:content';
import { prepareSchema } from 'astro-decap-collection'; // wraps schema and adds content type

import { schema as blogSchema } from './config.blog.ts'; // <-- generated from Decap before
import { schema as pageSchema } from './config.pages.ts'; // <-- generated from Decap before

export const collections = {
blog: defineCollection(prepareSchema(blogSchema)),
pages: defineCollection(prepareSchema(pageSchema)),
};
```
15 changes: 15 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createJsWithTsEsmPreset, type JestConfigWithTsJest } from 'ts-jest';

const esmPreset = createJsWithTsEsmPreset({ tsconfig: 'tsconfig.build.json' });
const jestConfig: JestConfigWithTsJest = {
...esmPreset,
extensionsToTreatAsEsm: ['.ts'],
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
moduleNameMapper: { '(.+)\\.js': '$1' },
setupFiles: ['<rootDir>/jest.setup.ts'],
transform: {
'\\.[jt]s?$': ['ts-jest', { useESM: true }],
},
};

export default jestConfig;
21 changes: 21 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { transpileModule, type TranspileOptions } from 'typescript';
import z from 'zod';

import tsConfig from './tsconfig.json';

declare global {
function serializeShape(type: z.ZodType<any, any, any>): string;
function transpileFrom(source: string): string;
}

globalThis.serializeShape = global.serializeShape = function (type) {
const wrapped = z.strictObject({ type });
return JSON.stringify(wrapped.shape);
};

globalThis.transpileFrom = global.transpileFrom = function (source) {
const { outputText } = transpileModule(source, tsConfig as unknown as TranspileOptions);
return outputText;
};

export {};
27 changes: 27 additions & 0 deletions mocks/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
locale: "de"

backend:
name: git-gateway
branch: main

publish_mode: editorial_workflow

media_folder: "/public/media"
public_folder: "/public/media"

collections:
- name: "blog" # Used in routes, e.g., /admin/collections/blog
label: "Blog" # Used in the UI
folder: "packages/website/src/content/blog" # The path to the folder where the documents are stored
create: true # Allow users to create new documents in this collection
fields: # The fields for each document, usually in frontmatter
- { label: "Layout", name: "layout", widget: "hidden", default: "blog" }
- { label: "Title", name: "title", widget: "string" }
- { label: "Published", name: "published", widget: "boolean" }
- { label: "Color", name: "color", widget: "color", enableAlpha: true }
- { label: "Place", name: "place", widget: "map", hint: "Set a location" }
- { label: "Publish Date", name: "date", widget: "datetime" }
- { label: "Featured Image(s)", name: "thumbnail", widget: "image" }
- { label: "Rating", name: "rating", widget: "number", min: 1, max: 5 }
- { label: "Code", name: "code", widget: "code", output_code_only: true }
- { label: "Body", name: "body", widget: "markdown" }
Loading

0 comments on commit 159cc99

Please sign in to comment.