forked from wepyjs/wepy-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
69 lines (57 loc) · 2.11 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
const fs = require('fs');
const path = require("path");
const gulp = require("gulp");
const zip = require("gulp-zip");
const gutil = require("gulp-util");
const getTemplates = () => {
let dirData = fs.readdirSync('./templates');
return dirData;
};
const updateTable = (md, flag, table) => {
let newDoc = '';
let start = md.indexOf(flag) + flag.length, l = md.length, endding = '';
newDoc = md.substr(0, start) + '\n\n';
let i = start - 1;
// Get the reset part
while (++i < l) {
if (endding) {
endding += md[i];
} else if (!endding && md[i] === '#' && md[i + 1] === '#') {
endding += md[i];
}
start++;
}
newDoc += table + '\n' + endding;
return newDoc;
};
/**
* Write readme based on templates.json
*/
const buildReadMe = () => {
json = fs.readFileSync('./meta.json');
json = JSON.parse(json);
let md = fs.readFileSync('./README.md', 'utf-8');
let table = '| Name | Description |\n| --- | --- |\n';
json.official.forEach(item => {
table += `| [${item.name}](https://github.com/wepyjs/wepy_templates/tree/master/templates/${item.name}) | ${item.description} |\n`;
});
let githubTable = '| Repository | Stars | Description | Last Updated |\n| --- | --- | --- | --- |\n';
json.github.forEach(item => {
githubTable += `| [${item.repo}](https://github.com/${item.repo}) | ${item.star || '-'} | ${item.description || '-'} | ${item.last_update || '-'} |\n`;
});
let newDoc = updateTable(md, '## Template List', table);
newDoc = updateTable(newDoc, '## Github Project', githubTable);
fs.writeFileSync('./README.md', newDoc);
};
gulp.task("default", ["build"]);
gulp.task('build', function () {
let templates = getTemplates();
gutil.log(`${templates.length} templates found`);
templates.forEach(template => {
gutil.log(`compress template [${template}]`);
gulp.src([`templates/${template}/**`, `!templates/${template}/{node_modules,node_modules/**,dist,dist/**}`], { dot: true })
.pipe(zip(`${template}.zip`))
.pipe(gulp.dest('zips'));
});
buildReadMe();
});