-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (107 loc) · 3.13 KB
/
index.js
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
#!/usr/bin/env node
const { execSync } = require("child_process");
const fs = require("fs");
function generateError(errorMessage) {
console.error(errorMessage);
return process.exit(1);
}
const packageJSONContent = (appName = "") => `{
"name": "${appName.replace(/\s/g, "_").toLowerCase()}",
"version": "1.0.0",
"description": "A simple tool to build out a static website using nothing but Markdown.",
"main": "",
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1",
"dev": "stratify dev",
"build": "stratify build",
"start": "stratify start"
},
"dependencies": {
"stratify-web": "^1.3.0"
},
"keywords": [],
"author": "",
"license": "UNLICENSED"
}`;
const filesToGenerate = [
{
name: ".gitignore",
content: "node_modules\n.env\nbuild\n.live\n",
},
{
name: "package.json",
content: ({ appName }) => packageJSONContent(appName),
},
{
name: "pages/index.md",
requiresFolder: "pages",
content: ({ appName }) => `# This is a Sample Page
<!--
Title: ${appName}
-->
Populate this with content, run \`npm run dev\` to see this page change live.
To change the title of the page, just use the comment block in the source for this file.
Once you're done with working on the files, run \`npm run build\` to compile the entire directory to static HTML Files.
Run \`npm run start\` to serve the built output.`,
},
{
name: "public/staticasset.md",
requiresFolder: "public",
content: `Keep your static assets like images, robots.txt, videos etc in this folder.`,
},
{
name: "templates/index.html",
requiresFolder: "templates",
content: `<html lang="en">
<head>
<title>{{ title }}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- This serves as a template for your file,
add stylesheets,
scripts or any other properties that you need common in your pages.
-->
{{ content }}
</body>
</html>`,
},
];
function generateStratifyProject() {
let directoryName = process.argv[2];
const appName = process.argv[3] || "stratify-boilerplate";
if (!directoryName)
return generateError(
`Error: Please provide a directory name as the first command line argument.\n` +
`Example: npx create-stratify-project directory-name\n`
);
directoryName = directoryName.replace("./", "");
// Create directory for project
const directory = `./${directoryName}`;
if (fs.existsSync(directory))
return generateError(
"A directory with the specified name already exists. Please remove it first."
);
fs.mkdirSync(directory);
// Write pre-requisite files to the directory.
for (let file of filesToGenerate) {
if (file.requiresFolder)
fs.mkdirSync(`${directory}/${file.requiresFolder}`);
fs.writeFileSync(
`${directory}/${file.name}`,
typeof file.content === "function"
? file.content({ appName, directory })
: file.content
);
}
execSync(`cd ${directory}`);
// Install dependencies
execSync("npm install");
// Initialize it as a git repositry
execSync("git init");
// Come back to the main folder
execSync("cd ../");
console.log("Successfully generated stratify project.");
}
generateStratifyProject();