This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
93 lines (74 loc) · 2.25 KB
/
gulpfile.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
const fs = require("fs");
const gulp = require('gulp');
const header = require('gulp-header');
/**
* Format datetime
* https://stackoverflow.com/a/34015511/10636614
* @param string
* @returns {string}
*/
function formatDate(string = new Date()){
const datetime = new Date(string);
const day = datetime.toLocaleString("vi-VN", {day: 'numeric'});
const month = datetime.toLocaleString("vi-VN", {month: 'numeric'});
const year = datetime.toLocaleString("vi-VN", {year: 'numeric'});
const hour = datetime.toLocaleTimeString("vi-VN", {hour: '2-digit'});
const minute = datetime.toLocaleTimeString("vi-VN", {minute: '2-digit'});
return `${year}/${month}/${day} - ${hour}:${minute}`;
}
/**
* Capitalize the first letter in string
* @param string
* @returns {string}
*/
function capitalizeFirstLetter(string){
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Format title
* @param string
* @returns {string}
*/
const formatTitle = string => capitalizeFirstLetter(string.split('-').join(' ').replace('.md', ''));
/**
* Generate content
*/
gulp.task('content', function(callback){
let string = '';
const path = 'tasks/';
const files = fs.readdirSync(path);
// read src
for(const file of files){
const content = fs.readFileSync(`${path}${file}`, {encoding: 'utf8', flag: 'rs'});
// title
const name = file.replace('.md', '');
string += `### [${formatTitle(file)}](https://phucbm.github.io/gulp-cheatsheet/tasks/${name})\n\n`;
// content
string += `${content}\n\n`;
}
// write
fs.writeFile('./README.md', string, callback);
callback();
});
/**
* Prepend header
*/
gulp.task('header', function(){
let headerContent = fs.readFileSync(`./header.md`, {encoding: 'utf8', flag: 'rs'});
headerContent = headerContent.replace('{{date}}', formatDate(new Date()));
return gulp.src('./README.md')
.pipe(header(`${headerContent}\n\n`))
.pipe(gulp.dest(function(file){
return file.base;
}, {overwrite: true}));
});
/**
* Export
*/
gulp.task('export', gulp.series('content', 'header'));
/**
* Watch
*/
gulp.task('serve', function(){
gulp.watch(['*.md', 'tasks/*.md', '!README.md'], gulp.series('export'));
});