-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
107 lines (93 loc) · 3.6 KB
/
build.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
const path = require('path');
const fs = require('fs');
const { exec, execSync } = require('child_process');
const { MarkdownBuild } = require('./tools/markdown-build');
const regArgs = /^([^\=]+)\=([^\=]+)$/;
const getArgs = () => {
const _args = process.argv.slice(2);
const ret = {};
_args.forEach((str) => {
const result = str.match(regArgs);
if (result && typeof result.length === 'number' && result.length === 3) {
ret[result[1]] = result[2];
}
});
return ret;
};
const GenerateArticleIndex = async ()=> {
let articleRoot = path.resolve(__dirname, './src/assets/articles');
let articleMarkdownBuild = new MarkdownBuild(articleRoot);
if(!fs.existsSync(articleRoot)) {
fs.mkdirSync(articleRoot, {
recursive: true
});
}
let newsFolder = 'news';
let newsData = await articleMarkdownBuild.getIndexJson(newsFolder);
fs.writeFileSync(path.join(articleRoot, `${newsFolder}.json`), JSON.stringify(newsData, null ,'\t'));
};
class MyBuilder {
args = getArgs(); // { repo, cname }
//build
constructor() {
this.configSettings();
}
lowerCaseConfigProp(prop) {
if(this.config && typeof this.config[prop] === 'string') {
this.config[prop] = this.config[prop].toLowerCase();
}
}
configSettings() {
//read settings
const args = getArgs();
console.log(args);
const distPath = path.resolve(__dirname, './dist');
this.config = Object.assign({
dist: './dist',
distPath: distPath,
baseHref: `file://${distPath}/`,
genereateCNAME: true,
}, args);
this.lowerCaseConfigProp('cname');
this.lowerCaseConfigProp('repo');
//set baseurl
if(this.config.cname) {
this.config.baseHref = `https://${this.config.cname}/`;
} else if(this.config.repo) {
const _repo = this.config.repo.split('/');
if(_repo.length>1 && _repo[0].toLowerCase() !== 'momentum-design') {
this.config.baseHref = `https://${_repo[0]}.github.io/${_repo[1]}/`;
} else {
this.config.baseHref = `https://webex.design/`;
}
}
}
async ngBuild() {
return new Promise((resolve, reject)=>{
console.log(`ng build momentum --output-path '${this.config.dist}' --configuration=production --base-href '${this.config.baseHref}'`);
exec(`ng build momentum --output-path '${this.config.dist}' --configuration=production --base-href '${this.config.baseHref}'`, (err, stdout, stderr) => {
//genereate 404
const fileIndex = path.join(this.config.distPath,'index.html');
const file404 = path.join(this.config.distPath,'404.html');
if(fs.existsSync(fileIndex)){
fs.copyFileSync(fileIndex, file404);
}
//generate cname
if(this.config.baseHref === `https://webex.design/`) {
fs.writeFileSync(path.join(this.config.distPath, 'CNAME'), 'webex.design');
}
fs.writeFileSync(path.join(this.config.distPath, '_config.yml'), `include:
- assets`);
fs.writeFileSync(path.join(this.config.distPath, '.nojekyll'), ``);
resolve(1);
});
});
}
async build() {
console.log('start to build articles');
await GenerateArticleIndex();
console.log('start to build website');
await this.ngBuild();
}
}
new MyBuilder().build();