-
Notifications
You must be signed in to change notification settings - Fork 99
/
init.ts
125 lines (100 loc) · 3.16 KB
/
init.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
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2022 the Deno authors. All rights reserved. MIT license.
import { join, resolve } from "https://deno.land/[email protected]/path/mod.ts";
const HELP = `deno_blog
Initialize a new blog project. This will create all the necessary files for
a new blog.
To generate a blog in the './my_blog' subdirectory:
deno run ${import.meta.url} ./my_blog
To generate a blog in the current directory:
deno run ${import.meta.url} .
Print this message:
deno run ${import.meta.url} --help
`;
const CURRENT_DATE = new Date();
const CURRENT_DATE_STRING = CURRENT_DATE.toISOString().slice(0, 10);
const FIRST_POST_CONTENTS = `---
title: Hello world!
publish_date: ${CURRENT_DATE_STRING}
---
This is my first blog post!
`;
const MAIN_NAME = "main.tsx";
const MAIN_CONTENTS = `/** @jsx h */
import blog, { ga, redirects, h } from "blog";
blog({
title: "My Blog",
description: "This is my new blog.",
// header: <header>Your custom header</header>,
// section: (post: Post) => <section>Your custom section with access to Post props.</section>,
// footer: <footer>Your custom footer</footer>,
avatar: "https://deno-avatar.deno.dev/avatar/blog.svg",
avatarClass: "rounded-full",
author: "An author",
// middlewares: [
// If you want to set up Google Analytics, paste your GA key here.
// ga("UA-XXXXXXXX-X"),
// If you want to provide some redirections, you can specify them here,
// pathname specified in a key will redirect to pathname in the value.
// redirects({
// "/hello_world.html": "/hello_world",
// }),
// ]
});
`;
const DENO_JSONC_NAME = "deno.jsonc";
const DENO_JSONC_CONTENTS = `{
"tasks": {
"dev": "deno run --allow-net --allow-read --allow-env --watch main.tsx --dev",
"serve": "deno run --allow-net --allow-read --allow-env --no-check main.tsx"
},
"imports": {
"blog": "https://deno.land/x/[email protected]/blog.tsx"
}
}
`;
async function init(directory: string) {
directory = resolve(directory);
console.log(`Initializing blog in ${directory}...`);
try {
const dir = [...Deno.readDirSync(directory)];
if (dir.length > 0) {
const confirmed = confirm(
"You are trying to initialize blog in an non-empty directory, do you want to continue?",
);
if (!confirmed) {
throw new Error("Directory is not empty, aborting.");
}
}
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
await Deno.mkdir(join(directory, "posts"), { recursive: true });
await Deno.writeTextFile(
join(directory, "posts/hello_world.md"),
FIRST_POST_CONTENTS,
);
await Deno.writeTextFile(join(directory, MAIN_NAME), MAIN_CONTENTS);
await Deno.writeTextFile(
join(directory, DENO_JSONC_NAME),
DENO_JSONC_CONTENTS,
);
console.log("Blog initialized, run `deno task dev` to get started.");
}
function printHelp() {
console.log(HELP);
Deno.exit(0);
}
if (import.meta.main) {
if (Deno.args.includes("-h") || Deno.args.includes("--help")) {
printHelp();
}
const directory = Deno.args[0];
if (directory == null) {
printHelp();
}
await init(directory);
} else {
throw new Error("This module is meant to be executed as a CLI.");
}