-
Notifications
You must be signed in to change notification settings - Fork 25
/
gulpfile.js
218 lines (184 loc) · 6.48 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use strict';
const fs = require('fs-extra');
const { join, resolve, dirname } = require('path');
const { spawn, execSync, exec } = require('child_process');
const del = require('del');
const gulp = require('gulp');
const es = require('event-stream');
const program = require('commander');
const Fs = require('fs');
const globby = require('globby');
program
.option('-o, --only <items>', 'Only build these files, specrated by ","', x => x.split(','))
.option('--dest <path>', 'Copy generated document to specified path.')
.option('--engine <path to engine>')
.option('--lang <language>')
.parse(process.argv);
gulp.task('publish', function (done) {
var dest = program.dest;
if (!dest) {
return done('dest not supplied');
}
var delPattern = join(dest, '*');
console.log('deleting ' + delPattern);
del.sync(delPattern, { force: true });
console.log('copying _book/**/* to ' + dest);
gulp.src('_book/**/*', {
base: '_book'
})
.pipe(gulp.dest(dest))
.on('end', done);
});
gulp.task('cp-apisrc', function () {
var engine = program.engine;
del.sync('lib/temp-src/engine/*');
fs.copySync(join(engine, 'docs'), 'lib/temp-src/engine/docs');
const DefaultModuleHeader =
'/**\n' +
' * @module cc\n' +
' */\n';
const LinkTagRE = /({\s*@link\s+)\/*(.+})/g;
var apisrc = fs.readJsonSync(join(engine, 'docs', 'apisrc.json'));
return gulp.src(apisrc, {
cwd: engine,
base: engine,
nodir: true
})
.pipe(es.through(function (file) {
var content = file.contents.toString();
// rebase example link to temp-src/engine/docs
content = content.replace(LinkTagRE, (m, p1, p2) => p1 + 'temp-src/engine/docs/' + p2);
// set defualt module as cc
content = DefaultModuleHeader + content;
file.contents = new Buffer(content);
this.emit('data', file);
}))
.pipe(gulp.dest('lib/temp-src/engine'));
});
gulp.task('build-md', gulp.series('cp-apisrc', function (cb) {
// del old files
var dest = resolve(program.lang);
del.sync(join(dest, 'assets'), { force: true });
del.sync(join(dest, 'classes'), { force: true });
del.sync(join(dest, 'enums'), { force: true });
del.sync(join(dest, 'modules'), { force: true });
// generate md
var commitId = execSync('git log --format="%H" -n 1', {
cwd: program.engine
}).toString().trim().replace(/"/g, '');
var args = [
'../node_modules/firedoc/bin/firedoc.js',
'build',
resolve('lib/temp-src'),
'-M',
'--theme',
resolve('lib/firedoc-theme/markdown'),
'-L',
program.lang,
'-D',
dest,
'-C',
commitId
];
// console.log('running node with args', args);
var child = spawn('node', args, {
stdio: 'inherit',
cwd: 'lib'
});
child.on('exit', function (err) {
if (err) {
return cb(err);
}
// attach editor api
var indexPath = join(dest, 'index.md');
var editorIndexPath = 'editor-index.md';
var content = fs.readFileSync(indexPath, 'utf8') + '\n' + fs.readFileSync(editorIndexPath, 'utf8');
fs.writeFileSync(indexPath, content, 'utf8');
cb();
});
}));
gulp.task('build-tsd', gulp.series('cp-apisrc', function (cb) {
program
.option('--engine <path to engine>')
.option('--jsbAdapter <path to jsb-adapter>')
.option('--dest <tsd path>')
.parse(process.argv);
let { engine, jsbAdapter, dest } = program;
let tsdGen = require('./lib/tsd-generator');
let cwd = process.cwd();
process.chdir('lib');
tsdGen.generateTsd('temp-src/engine', function (err, output) {
process.chdir(cwd);
if (err) {
console.log(err.stack || err);
}
else {
// search docs from engine and jsb adapter directory
let tsDefines = globby.sync([
join(engine, '**/*.d.ts'),
join(jsbAdapter, '**/*.d.ts'),
'!' + join(engine, 'node_modules/**/*.d.ts'),
'!' + join(jsbAdapter, 'node_modules/**/*.d.ts')
]);
tsDefines.forEach(d => {
output += fs.readFileSync(d) + '\n';
})
fs.ensureDirSync(dirname(dest));
fs.writeFileSync(dest, output, 'utf8');
console.log('Generate tsd file complete, dest path: ' + dest);
}
cb();
});
}));
const FORBID_IGNORE_ARRAY = ['index.md', 'SUMMARY.md'];
const allPagesPattern = ['zh/**/*.md', 'en/**/*.md', '!zh/*.md', '!en/*.md'];
const START_TAG_IGNORE = '\n\nCC_IGNORE_START';
const END_TAG_IGNORE = '\nCC_IGNORE_END';
gulp.task('restore-ignore', function () {
restoreIgnore('.bookignore');
});
gulp.task('preview', gulp.series('restore-ignore', function (done) {
var includeFiles = program.only;
if (includeFiles) {
quickPreview(includeFiles, (error) => {
if (error) {
return done(error);
}
});
}
openServer(done);
}));
function openServer (done) {
var server = exec('gitbook serve --no-watch --open');
server.stderr.on('error', error => {
if(error) {
return done(error);
process.exit(1);
}
});
server.stdout.on('data', data => {
console.log(data.toString());
});
}
//only build the target file
function quickPreview (includeFiles, done) {
const Path = require('path');
const Globby = require('globby');
includeFiles = includeFiles.concat(FORBID_IGNORE_ARRAY);
var excludePattern = includeFiles.map(x => '!**/' + Path.basename(x, '.md') + '.md');
var allIgnorePages = Globby.sync(allPagesPattern.concat(excludePattern), { absolute: true });
allIgnorePages = allIgnorePages.map(x => '/' + Path.relative(__dirname,x).replace(/\\/g, '/'));
Fs.readFile('.bookignore', 'utf8', function (error, content) {
if (error) {
return done(error);
}
const fileContent = `${content}\n${START_TAG_IGNORE}\n${allIgnorePages.join('\n')}${END_TAG_IGNORE}`;
Fs.writeFile('.bookignore', fileContent, 'utf8', done);
});
}
function restoreIgnore (path) {
var re = new RegExp(START_TAG_IGNORE + '(?:\\n|.)*' + END_TAG_IGNORE);
var content = Fs.readFileSync(path, 'utf8');
content = content.replace(re, '');
Fs.writeFileSync(path, content, 'utf8');
}