-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
42 lines (38 loc) · 1.12 KB
/
init.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
import chalk from 'chalk';
import { exec } from 'child_process';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import dateFormat, { masks } from "dateformat";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const pathPosts = path.join(__dirname, "posts");
const n = (process.argv.slice(2)[0] || "").trim();
const noDir = process.argv.slice(3)[0] === '-n'
if (n) {
try {
const now = new Date();
!noDir && fs.mkdirSync(`${pathPosts}/${n}`)
fs.writeFileSync(`${pathPosts}/${n}.md`, (`
---
title: '${n}'
date: ${dateFormat(now, "yyyy-mm-dd HH:MM:ss")}
categories:
tags: []
---
`)
.replace(/^\n/, '').replace(new RegExp(
`^${' '.repeat(6)}`,
'gm'
), '')
.replace(/(\x20*)$/gm, ''));
console.info(`${chalk.green("Create Success:")} ${chalk.underline(`${pathPosts}/${n}.md`)}`)
exec(`code ${pathPosts}/${n}.md`)
} catch (e) {
if (e.errno === -17) {
console.info(chalk.yellow("File already exists, create failed."))
} else {
console.error(e);
}
}
}