forked from denoland/deno_blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.ts
116 lines (92 loc) · 2.85 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
// Copyright 2022 the Deno authors. All rights reserved. MIT license.
import { join, resolve } from "https://deno.land/std@0.140.0/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 MAIN_FILE_URL = new URL(
"./blog.tsx",
import.meta.url,
);
const FIRST_POST_CONTENTS = `---
title: Hello world!
publish_date: ${CURRENT_DATE_STRING}
---
This is my first blog post!
`;
const MAIN_CONTENTS = `import blog, { ga, redirects } from "${MAIN_FILE_URL}";
blog({
title: "My Blog",
description: "This is my new blog.",
picture: "https://deno-avatar.deno.dev/avatar/blog.svg",
author: "An author",
background: "#f9f9f9",
// 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_CONTENTS = `{
"tasks": {
"dev": "deno run --allow-net --allow-read --watch main.ts --dev",
"serve": "deno run --allow-net --allow-read --no-check main.ts",
}
}
`;
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.ts"), MAIN_CONTENTS);
await Deno.writeTextFile(join(directory, "deno.jsonc"), 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.");
}