forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
370 lines (347 loc) · 9.71 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
'use strict';
// const install = require('./install')
const runCmd = require('./runCmd');
const getBabelCommonConfig = require('./getBabelCommonConfig');
const merge2 = require('merge2');
const { execSync } = require('child_process');
const through2 = require('through2');
const transformLess = require('./transformLess');
const webpack = require('webpack');
const babel = require('gulp-babel');
const argv = require('minimist')(process.argv.slice(2));
const GitHub = require('@octokit/rest');
const packageJson = require(`${process.cwd()}/package.json`);
// const getNpm = require('./getNpm')
// const selfPackage = require('../package.json')
const chalk = require('chalk');
const getNpmArgs = require('./utils/get-npm-args');
const getChangelog = require('./utils/getChangelog');
const path = require('path');
// const watch = require('gulp-watch')
const gulp = require('gulp');
const fs = require('fs');
const rimraf = require('rimraf');
const replaceLib = require('./replaceLib');
const stripCode = require('gulp-strip-code');
const compareVersions = require('compare-versions');
const cwd = process.cwd();
const libDir = path.join(cwd, 'lib');
const esDir = path.join(cwd, 'es');
function dist(done) {
rimraf.sync(path.join(cwd, 'dist'));
process.env.RUN_ENV = 'PRODUCTION';
const webpackConfig = require(path.join(cwd, 'webpack.build.config.js'));
webpack(webpackConfig, (err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return;
}
const info = stats.toJson();
if (stats.hasErrors()) {
console.error(info.errors);
}
if (stats.hasWarnings()) {
console.warn(info.warnings);
}
const buildInfo = stats.toString({
colors: true,
children: true,
chunks: false,
modules: false,
chunkModules: false,
hash: false,
version: false,
});
console.log(buildInfo);
done(0);
});
}
function babelify(js, modules) {
const babelConfig = getBabelCommonConfig(modules);
babelConfig.babelrc = false;
delete babelConfig.cacheDirectory;
if (modules === false) {
babelConfig.plugins.push(replaceLib);
}
let stream = js.pipe(babel(babelConfig)).pipe(
through2.obj(function z(file, encoding, next) {
this.push(file.clone());
if (file.path.match(/\/style\/index\.(js|jsx)$/)) {
const content = file.contents.toString(encoding);
file.contents = Buffer.from(
content.replace(/\/style\/?'/g, "/style/css'").replace(/\.less/g, '.css'),
);
file.path = file.path.replace(/index\.(js|jsx)$/, 'css.js');
this.push(file);
next();
} else {
next();
}
}),
);
if (modules === false) {
stream = stream.pipe(
stripCode({
start_comment: '@remove-on-es-build-begin',
end_comment: '@remove-on-es-build-end',
}),
);
}
return stream.pipe(gulp.dest(modules === false ? esDir : libDir));
}
function compile(modules) {
rimraf.sync(modules !== false ? libDir : esDir);
const less = gulp
.src(['components/**/*.less'])
.pipe(
through2.obj(function(file, encoding, next) {
this.push(file.clone());
if (
file.path.match(/\/style\/index\.less$/) ||
file.path.match(/\/style\/v2-compatible-reset\.less$/)
) {
transformLess(file.path)
.then(css => {
file.contents = Buffer.from(css);
file.path = file.path.replace(/\.less$/, '.css');
this.push(file);
next();
})
.catch(e => {
console.error(e);
});
} else {
next();
}
}),
)
.pipe(gulp.dest(modules === false ? esDir : libDir));
const assets = gulp
.src(['components/**/*.@(png|svg)'])
.pipe(gulp.dest(modules === false ? esDir : libDir));
const source = ['components/**/*.js', 'components/**/*.jsx', '!components/*/__tests__/*'];
const jsFilesStream = babelify(gulp.src(source), modules);
return merge2([less, jsFilesStream, assets]);
}
function tag() {
console.log('tagging');
const { version } = packageJson;
execSync(`git config --global user.email ${process.env.GITHUB_USER_EMAIL}`);
execSync(`git config --global user.name ${process.env.GITHUB_USER_NAME}`);
execSync(`git tag ${version}`);
execSync(
`git push https://${process.env.GITHUB_TOKEN}@github.com/vueComponent/ant-design-vue.git ${version}:${version}`,
);
execSync(
`git push https://${process.env.GITHUB_TOKEN}@github.com/vueComponent/ant-design-vue.git master:master`,
);
console.log('tagged');
}
function githubRelease(done) {
const changlogFiles = [
path.join(cwd, 'CHANGELOG.en-US.md'),
path.join(cwd, 'CHANGELOG.zh-CN.md'),
];
console.log('creating release on GitHub');
if (!process.env.GITHUB_TOKEN) {
console.log('no GitHub token found, skip');
return;
}
if (!changlogFiles.every(file => fs.existsSync(file))) {
console.log('no changelog found, skip');
return;
}
const github = new GitHub();
github.authenticate({
type: 'oauth',
token: process.env.GITHUB_TOKEN,
});
const date = new Date();
const { version } = packageJson;
const enChangelog = getChangelog(changlogFiles[0], version);
const cnChangelog = getChangelog(changlogFiles[1], version);
const changelog = [
`\`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}\``,
enChangelog,
'\n',
'---',
'\n',
cnChangelog,
].join('\n');
const [_, owner, repo] = execSync('git remote get-url origin') // eslint-disable-line
.toString()
.match(/github.com[:/](.+)\/(.+)\.git/);
github.repos
.createRelease({
owner,
repo,
tag_name: version,
name: version,
body: changelog,
})
.then(() => {
done();
});
}
gulp.task(
'tag',
gulp.series(done => {
tag();
githubRelease(done);
}),
);
gulp.task(
'check-git',
gulp.series(done => {
runCmd('git', ['status', '--porcelain'], (code, result) => {
if (/^\?\?/m.test(result)) {
return done(`There are untracked files in the working tree.\n${result}
`);
}
if (/^([ADRM]| [ADRM])/m.test(result)) {
return done(`There are uncommitted changes in the working tree.\n${result}
`);
}
return done();
});
}),
);
function publish(tagString, done) {
let args = ['publish', '--with-antd-tools'];
if (tagString) {
args = args.concat(['--tag', tagString]);
}
const publishNpm = process.env.PUBLISH_NPM_CLI || 'npm';
runCmd(publishNpm, args, code => {
tag();
githubRelease(() => {
done(code);
});
});
}
function pub(done) {
dist(code => {
if (code) {
done(code);
return;
}
const notOk = !packageJson.version.match(/^\d+\.\d+\.\d+$/);
let tagString;
if (argv['npm-tag']) {
tagString = argv['npm-tag'];
}
if (!tagString && notOk) {
tagString = 'next';
}
if (packageJson.scripts['pre-publish']) {
runCmd('npm', ['run', 'pre-publish'], code2 => {
if (code2) {
done(code2);
return;
}
publish(tagString, done);
});
} else {
publish(tagString, done);
}
});
}
gulp.task(
'compile-with-es',
gulp.series(done => {
compile(false).on('finish', function() {
done();
});
}),
);
gulp.task(
'compile',
gulp.series('compile-with-es', done => {
compile().on('finish', function() {
done();
});
}),
);
gulp.task(
'dist',
gulp.series('compile', done => {
dist(done);
}),
);
gulp.task(
'pub',
gulp.series('check-git', 'compile', done => {
if (!process.env.GITHUB_TOKEN) {
console.log('no GitHub token found, skip');
} else {
pub(done);
}
}),
);
gulp.task(
'pub-with-ci',
gulp.series(done => {
if (!process.env.NPM_TOKEN) {
console.log('no NPM token found, skip');
} else {
const github = new GitHub();
github.authenticate({
type: 'oauth',
token: process.env.GITHUB_TOKEN,
});
const [_, owner, repo] = execSync('git remote get-url origin') // eslint-disable-line
.toString()
.match(/github.com[:/](.+)\/(.+)\.git/);
const getLatestRelease = github.repos.getLatestRelease({
owner,
repo,
});
const listCommits = github.repos.listCommits({
owner,
repo,
per_page: 1,
});
Promise.all([getLatestRelease, listCommits]).then(([latestRelease, commits]) => {
const preVersion = latestRelease.data.tag_name;
const { version } = packageJson;
const [_, newVersion] = commits.data[0].commit.message.trim().match(/bump (.+)/) || []; // eslint-disable-line
if (
compareVersions(version, preVersion) === 1 &&
newVersion &&
newVersion.trim() === version
) {
runCmd('npm', ['run', 'pub'], code => {
done();
});
} else {
console.log('donot need publish' + version);
}
});
}
}),
);
gulp.task(
'guard',
gulp.series(done => {
function reportError() {
console.log(chalk.bgRed('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'));
console.log(chalk.bgRed('!! `npm publish` is forbidden for this package. !!'));
console.log(chalk.bgRed('!! Use `npm run pub` instead. !!'));
console.log(chalk.bgRed('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'));
}
const npmArgs = getNpmArgs();
if (npmArgs) {
for (let arg = npmArgs.shift(); arg; arg = npmArgs.shift()) {
if (/^pu(b(l(i(sh?)?)?)?)?$/.test(arg) && npmArgs.indexOf('--with-antd-tools') < 0) {
reportError();
done(1);
return;
}
}
}
done();
}),
);