deno.land / x / lume@v2.1.4 / cli / create.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { stringify } from "../deps/yaml.ts";import { dirname, join, toFileUrl } from "../deps/path.ts";import { ensureDir } from "../deps/fs.ts";import { isGenerator } from "../core/utils/generator.ts";import { isUrl } from "../core/utils/path.ts";import { log } from "../core/utils/log.ts";import { createSite } from "./run.ts";
import type Site from "../core/site.ts";
/** Run an archetype */export async function create( config: string | undefined, name: string, args: string[],) { // deno-lint-ignore no-explicit-any let fn: any; const site = await createSite(config);
try { const mod = name.startsWith(".") ? await import(toFileUrl(join(Deno.cwd(), name)).href) : isUrl(name) ? await import(name) : await Promise.any([ import(toFileUrl(site.src(`_archetypes/${name}.ts`)).href), import(toFileUrl(site.src(`_archetypes/${name}.js`)).href), ]);
if (mod?.default) { fn = mod.default; } } catch (cause) { throw new Error(`Archetype "${name}" not found or is errored.`, { cause, }); }
if (typeof fn !== "function") { throw new Error(`Archetype "${name}" is not a function.`); }
if (isGenerator(fn)) { for await (const archetype of fn(...args) as Generator<Archetype>) { await saveArchetype(site, archetype); } } else { const archetype = fn(...args) as Archetype; await saveArchetype(site, archetype); }}
async function saveArchetype(site: Site, archetype: Archetype) { const { path, content } = archetype;
if (!path) { throw new Error("Archetype path is required."); } if (!content) { throw new Error("Archetype content is required."); }
if (typeof content === "string" || content instanceof Uint8Array) { return await saveFile(site.src(path), content); }
if (path.endsWith(".json")) { return await saveFile( site.src(path), JSON.stringify(content, null, 2) + "\n", ); }
if (path.endsWith(".yaml") || path.endsWith(".yml")) { return await saveFile( site.src(path), stringify(content) + "\n", ); }
const { content: body, ...frontmatter } = content;
return await saveFile( site.src(path), `---\n${stringify(frontmatter)}---\n${body || ""}\n`, );}
async function saveFile(path: string, content: string | Uint8Array) { await ensureDir(dirname(path));
try { content instanceof Uint8Array ? await Deno.writeFile(path, content, { createNew: true }) : await Deno.writeTextFile(path, content, { createNew: true });
log.info(`✔️ Created file: <gray>${path}</gray>`); } catch (error) { if (error instanceof Deno.errors.AlreadyExists) { log.warn(`⚠️ File already exists: <gray>${path}</gray>`); } else { throw error; } }}
/** Definition used to create a new Page */export interface Archetype { path: string; content: string | Record<string, unknown> | Uint8Array;}
lume

Version Info

Tagged at
7 months ago