@content-collections/cli 0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Sebastian Sdorra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Content Collections
2
+
3
+ <a href="https://content-collections.dev">
4
+ <img align="left" width="96" height="96" src="https://github.com/sdorra/content-collections/blob/main/website/assets/logo_96x96.png?raw=true" alt="logo: A staple of books">
5
+ </a>
6
+
7
+ Transform your content into type-safe data collections. Eliminate the need for manual data fetching and parsing. Simply import your content and begin. Built-in validation ensures the accuracy of your data. Preprocess your data before it enters your app.
8
+
9
+ ## Features
10
+
11
+ - **Beautiful DX**:
12
+ Content Collections is designed to provide a pleasurable user experience. It offers a seamless developer experience without the need to restart the server or refresh the browser. Content collections are automatically updated when you make changes to your content.
13
+
14
+ - **Type-safe**:
15
+ Your content is parsed and validated during the build process, guaranteeing accuracy and currency. Content Collections offers a type-safe API to access your content.
16
+
17
+ - **Simple to use**:
18
+ No need to manually fetch and parse your content anymore. Just import it and start using Content Collections. It provides a simple API, allowing you to concentrate on building your app.
19
+
20
+ - **Tansformation**:
21
+ Content Collections allows you to transform your content before it enters your app. You can use it to modify your content, join two collections or even fetch data from a server.
22
+
23
+ ## Installation
24
+
25
+ 1. Install required packages:
26
+
27
+ ```bash
28
+ pnpm add -D @content-collections/core @content-collections/cli concurrently
29
+ ```
30
+
31
+ 1. Add path alias to your `tsconfig.json`:
32
+
33
+ ```json
34
+ {
35
+ "compilerOptions": {
36
+ // ...
37
+ "paths": {
38
+ "content-collections": ["./.content-collections/generated"]
39
+ }
40
+ }
41
+ }
42
+ ```
43
+
44
+ 1. Update your scripts in `package.json`:
45
+
46
+ ```json
47
+ {
48
+ "scripts": {
49
+ "dev": "concurrently 'content-collections watch' 'build-scripts dev'",
50
+ "build": "content-collections build && build-scripts build"
51
+ }
52
+ }
53
+ ```
54
+
55
+ ## Usage
56
+
57
+ 1. Create a content-collections.ts file at the root of your project:
58
+
59
+ ```ts
60
+ import { defineCollection, defineConfig } from "@content-collections/core";
61
+
62
+ const posts = defineCollection({
63
+ name: "posts",
64
+ directory: "src/posts",
65
+ include: "**/*.md",
66
+ schema: (z) => ({
67
+ title: z.string(),
68
+ summary: z.string(),
69
+ }),
70
+ });
71
+
72
+ export default defineConfig({
73
+ collections: [posts],
74
+ });
75
+ ```
76
+
77
+ 1. Start writing content in `src/posts`:
78
+
79
+ ```md
80
+ ---
81
+ title: Hello World
82
+ summary: This is my first post
83
+ ---
84
+
85
+ # Hello World
86
+
87
+ This is my first post.
88
+ ```
89
+
90
+ 1. Use your content in your app:
91
+
92
+ ```tsx
93
+ import { allPosts } from "content-collections";
94
+
95
+ export function Posts() {
96
+ return (
97
+ <ul>
98
+ {allPosts.map((post) => (
99
+ <li key={post._meta.path}>
100
+ <a href={`/posts/${post._meta.path}`}>
101
+ <h3>{post.title}</h3>
102
+ <p>{post.summary}</p>
103
+ </a>
104
+ </li>
105
+ ))}
106
+ </ul>
107
+ );
108
+ }
109
+ ```
110
+
111
+ For a more detailed guide, please refer to the [documentation](https://content-collections.dev/docs/guides/getting-started).
112
+
113
+ ## License
114
+
115
+ Content Collections is licensed under the [MIT License](./LICENSE).
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@content-collections/cli",
3
+ "description": "Command line interface for content-collections",
4
+ "version": "0.1.0",
5
+ "type": "module",
6
+ "main": "dist/src/index.js",
7
+ "bin": {
8
+ "content-collections": "dist/src/index.js"
9
+ },
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "start": "tsx src/index.ts"
13
+ },
14
+ "devDependencies": {
15
+ "@types/node": "^20.9.0",
16
+ "tsup": "^7.2.0",
17
+ "tsx": "^4.1.1",
18
+ "typescript": "^5.3.2"
19
+ },
20
+ "dependencies": {
21
+ "@content-collections/core": "workspace:*",
22
+ "clerc": "^0.42.2"
23
+ }
24
+ }
@@ -0,0 +1,30 @@
1
+ import { createBuilder } from "@content-collections/core";
2
+ import { isUnknownError, registerErrorListeners } from "../errors.js";
3
+ export default async function build(config) {
4
+ console.log("Building collections...");
5
+ console.time("Finished building collections");
6
+ const builder = await createBuilder(config);
7
+ registerErrorListeners(builder);
8
+ let receivedError = false;
9
+ builder.on("_error", ({ _event, error }) => {
10
+ if (isUnknownError(_event)) {
11
+ console.log();
12
+ console.log("Unknown error:", error.message);
13
+ console.log();
14
+ }
15
+ receivedError = true;
16
+ });
17
+ try {
18
+ await builder.build();
19
+ }
20
+ finally {
21
+ if (receivedError) {
22
+ console.log("Failed to build collections, exiting...");
23
+ process.exit(1);
24
+ }
25
+ else {
26
+ console.timeEnd("Finished building collections");
27
+ }
28
+ }
29
+ }
30
+ //# sourceMappingURL=build.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build.js","sourceRoot":"","sources":["../../../src/commands/build.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAEtE,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,KAAK,CAAC,MAAc;IAChD,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;IAEvC,OAAO,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;IAC9C,MAAM,OAAO,GAAG,MAAM,aAAa,CAAC,MAAM,CAAC,CAAC;IAE5C,sBAAsB,CAAC,OAAO,CAAC,CAAC;IAEhC,IAAI,aAAa,GAAG,KAAK,CAAC;IAC1B,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE;QACzC,IAAI,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC;YAC3B,OAAO,CAAC,GAAG,EAAE,CAAC;YACd,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;YAC7C,OAAO,CAAC,GAAG,EAAE,CAAC;QAChB,CAAC;QACD,aAAa,GAAG,IAAI,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC;QACH,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;YAAS,CAAC;QACT,IAAI,aAAa,EAAE,CAAC;YAClB,OAAO,CAAC,GAAG,CAAC,yCAAyC,CAAC,CAAC;YACvD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,OAAO,CAAC,+BAA+B,CAAC,CAAC;QACnD,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { createBuilder } from "@content-collections/core";
2
+ import path from "node:path";
3
+ import { isUnknownError, registerErrorListeners } from "../errors.js";
4
+ export default async function watch(configPath) {
5
+ const builder = await createBuilder(configPath);
6
+ builder.on("builder:start", () => {
7
+ console.log("build started ...");
8
+ });
9
+ builder.on("builder:end", (event) => {
10
+ console.log("... finished build in", event.endedAt - event.startedAt + "ms");
11
+ });
12
+ builder.on("watcher:file-changed", (event) => {
13
+ const relativePath = path.relative(process.cwd(), event.filePath);
14
+ if (event.modification === "delete") {
15
+ console.log("... file deleted", relativePath);
16
+ }
17
+ else {
18
+ console.log("... file changed", relativePath);
19
+ }
20
+ });
21
+ registerErrorListeners(builder);
22
+ builder.on("_error", ({ _event, error }) => {
23
+ if (isUnknownError(_event)) {
24
+ console.log("... error", error.message);
25
+ }
26
+ });
27
+ await builder.build();
28
+ console.log("start watching ...");
29
+ builder.watch();
30
+ }
31
+ //# sourceMappingURL=watch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watch.js","sourceRoot":"","sources":["../../../src/commands/watch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAEtE,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,KAAK,CAAC,UAAkB;IACpD,MAAM,OAAO,GAAG,MAAM,aAAa,CAAC,UAAU,CAAC,CAAC;IAEhD,OAAO,CAAC,EAAE,CAAC,eAAe,EAAE,GAAG,EAAE;QAC/B,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC;IACnC,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,EAAE,CAAC,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE;QAClC,OAAO,CAAC,GAAG,CACT,uBAAuB,EACvB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,SAAS,GAAG,IAAI,CACvC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,EAAE,CAAC,sBAAsB,EAAE,CAAC,KAAK,EAAE,EAAE;QAC3C,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;QAClE,IAAI,KAAK,CAAC,YAAY,KAAK,QAAQ,EAAE,CAAC;YACpC,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,YAAY,CAAC,CAAC;QAChD,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,YAAY,CAAC,CAAC;QAChD,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,sBAAsB,CAAC,OAAO,CAAC,CAAC;IAEhC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE;QACzC,IAAI,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC;YAC3B,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;QAC1C,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;IAEtB,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;IAClC,OAAO,CAAC,KAAK,EAAE,CAAC;AAClB,CAAC"}
@@ -0,0 +1,20 @@
1
+ import path from "node:path";
2
+ const handledErrors = ["transformer:validation-error", "collector:parse-error"];
3
+ export function isUnknownError(event) {
4
+ return !handledErrors.includes(event);
5
+ }
6
+ export function registerErrorListeners(builder) {
7
+ builder.on("transformer:validation-error", (event) => {
8
+ console.log();
9
+ console.log("Validation failed on", path.join(event.collection.directory, event.file.path) + ":");
10
+ console.log(event.error.message);
11
+ console.log();
12
+ });
13
+ builder.on("collector:parse-error", (event) => {
14
+ console.log();
15
+ console.log("Failed to parse file", path.join(event.filePath) + ":");
16
+ console.log(event.error.message);
17
+ console.log();
18
+ });
19
+ }
20
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,MAAM,aAAa,GAAG,CAAC,8BAA8B,EAAE,uBAAuB,CAAC,CAAC;AAEhF,MAAM,UAAU,cAAc,CAAC,KAAa;IAC1C,OAAO,CAAC,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,OAAgB;IACrD,OAAO,CAAC,EAAE,CAAC,8BAA8B,EAAE,CAAC,KAAK,EAAE,EAAE;QACnD,OAAO,CAAC,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,GAAG,CACT,sBAAsB,EACtB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,CAC7D,CAAC;QACF,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACjC,OAAO,CAAC,GAAG,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,EAAE,CAAC,uBAAuB,EAAE,CAAC,KAAK,EAAE,EAAE;QAC5C,OAAO,CAAC,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,GAAG,CAAC,sBAAsB,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAC,CAAC;QACrE,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACjC,OAAO,CAAC,GAAG,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env node
2
+ import { Clerc, helpPlugin, versionPlugin, completionsPlugin } from "clerc";
3
+ import build from "./commands/build.js";
4
+ import watch from "./commands/watch.js";
5
+ import packageJson from "../package.json" assert { type: "json" };
6
+ const name = "content-collections";
7
+ if (!packageJson.bin[name]) {
8
+ throw new Error(`Missing bin entry for ${name} in package.json`);
9
+ }
10
+ Clerc.create()
11
+ .scriptName(name)
12
+ .description(packageJson.description)
13
+ .version(packageJson.version)
14
+ .command("build", "Build collections", {
15
+ flags: {
16
+ config: {
17
+ type: String,
18
+ description: "Path to config file",
19
+ default: "content-collections.ts",
20
+ },
21
+ },
22
+ })
23
+ .on("build", (context) => {
24
+ return build(context.flags.config);
25
+ })
26
+ .command("watch", "Build collections and watch for changes", {
27
+ flags: {
28
+ config: {
29
+ type: String,
30
+ description: "Path to config file",
31
+ default: "content-collections.ts",
32
+ },
33
+ },
34
+ })
35
+ .on("watch", async (context) => {
36
+ return watch(context.flags.config);
37
+ })
38
+ .use(helpPlugin())
39
+ .use(versionPlugin())
40
+ .use(completionsPlugin())
41
+ .parse();
42
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAC5E,OAAO,KAAK,MAAM,qBAAqB,CAAC;AACxC,OAAO,KAAK,MAAM,qBAAqB,CAAC;AACxC,OAAO,WAAW,MAAM,iBAAiB,CAAC,SAAS,IAAI,EAAE,MAAM,EAAE,CAAC;AAElE,MAAM,IAAI,GAAG,qBAAqB,CAAC;AACnC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;IAC3B,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,kBAAkB,CAAC,CAAC;AACnE,CAAC;AAED,KAAK,CAAC,MAAM,EAAE;KACX,UAAU,CAAC,IAAI,CAAC;KAChB,WAAW,CAAC,WAAW,CAAC,WAAW,CAAC;KACpC,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC;KAC5B,OAAO,CAAC,OAAO,EAAE,mBAAmB,EAAE;IACrC,KAAK,EAAE;QACL,MAAM,EAAE;YACN,IAAI,EAAE,MAAM;YACZ,WAAW,EAAE,qBAAqB;YAClC,OAAO,EAAE,wBAAwB;SAClC;KACF;CACF,CAAC;KACD,EAAE,CAAC,OAAO,EAAE,CAAC,OAAO,EAAE,EAAE;IACvB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC,CAAC;KACD,OAAO,CAAC,OAAO,EAAE,yCAAyC,EAAE;IAC3D,KAAK,EAAE;QACL,MAAM,EAAE;YACN,IAAI,EAAE,MAAM;YACZ,WAAW,EAAE,qBAAqB;YAClC,OAAO,EAAE,wBAAwB;SAClC;KACF;CACF,CAAC;KACD,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;IAC7B,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC,CAAC;KACD,GAAG,CAAC,UAAU,EAAE,CAAC;KACjB,GAAG,CAAC,aAAa,EAAE,CAAC;KACpB,GAAG,CAAC,iBAAiB,EAAE,CAAC;KACxB,KAAK,EAAE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@content-collections/cli",
3
+ "description": "Command line interface for content-collections",
4
+ "version": "0.1.0",
5
+ "type": "module",
6
+ "main": "dist/src/index.js",
7
+ "files": [
8
+ "dist",
9
+ "README.md"
10
+ ],
11
+ "bin": {
12
+ "content-collections": "dist/src/index.js"
13
+ },
14
+ "devDependencies": {
15
+ "@types/node": "^20.9.0",
16
+ "tsup": "^7.2.0",
17
+ "tsx": "^4.1.1",
18
+ "typescript": "^5.3.2"
19
+ },
20
+ "dependencies": {
21
+ "clerc": "^0.42.2",
22
+ "@content-collections/core": "0.1.0"
23
+ },
24
+ "scripts": {
25
+ "build": "tsc",
26
+ "start": "tsx src/index.ts"
27
+ }
28
+ }