ant-design-vue/antd-tools/gulpfile.js

433 lines
11 KiB
JavaScript
Raw Normal View History

2020-03-29 09:41:43 +00:00
/* eslint-disable no-console */
2020-10-19 08:43:10 +00:00
const { getProjectPath, injectRequire } = require('./utils/projectHelper');
injectRequire();
2018-03-18 11:59:38 +00:00
// const install = require('./install')
2019-01-12 03:33:27 +00:00
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));
2020-04-09 15:11:01 +00:00
const { Octokit } = require('@octokit/rest');
2018-03-18 11:59:38 +00:00
// const getNpm = require('./getNpm')
// const selfPackage = require('../package.json')
2019-01-12 03:33:27 +00:00
const chalk = require('chalk');
const getNpmArgs = require('./utils/get-npm-args');
const getChangelog = require('./utils/getChangelog');
const path = require('path');
2018-03-18 11:59:38 +00:00
// const watch = require('gulp-watch')
2020-10-19 08:43:10 +00:00
const ts = require('gulp-typescript');
2019-01-12 03:33:27 +00:00
const gulp = require('gulp');
const fs = require('fs');
const rimraf = require('rimraf');
2020-10-19 08:43:10 +00:00
const tsConfig = require('./getTSCommonConfig')();
2019-01-12 03:33:27 +00:00
const replaceLib = require('./replaceLib');
const stripCode = require('gulp-strip-code');
const compareVersions = require('compare-versions');
2018-03-18 11:59:38 +00:00
2020-10-19 08:43:10 +00:00
const packageJson = require(getProjectPath('package.json'));
const tsDefaultReporter = ts.reporter.defaultReporter();
2019-01-12 03:33:27 +00:00
const cwd = process.cwd();
2020-10-19 08:43:10 +00:00
const libDir = getProjectPath('lib');
const esDir = getProjectPath('es');
2018-03-18 11:59:38 +00:00
2019-01-12 03:33:27 +00:00
function dist(done) {
rimraf.sync(path.join(cwd, 'dist'));
process.env.RUN_ENV = 'PRODUCTION';
2020-10-19 08:43:10 +00:00
const webpackConfig = require(getProjectPath('webpack.build.conf.js'));
2018-03-18 11:59:38 +00:00
webpack(webpackConfig, (err, stats) => {
if (err) {
2019-01-12 03:33:27 +00:00
console.error(err.stack || err);
2018-03-18 11:59:38 +00:00
if (err.details) {
2019-01-12 03:33:27 +00:00
console.error(err.details);
2018-03-18 11:59:38 +00:00
}
2019-01-12 03:33:27 +00:00
return;
2018-03-18 11:59:38 +00:00
}
2019-01-12 03:33:27 +00:00
const info = stats.toJson();
2018-03-18 11:59:38 +00:00
if (stats.hasErrors()) {
2019-01-12 03:33:27 +00:00
console.error(info.errors);
2018-03-18 11:59:38 +00:00
}
if (stats.hasWarnings()) {
2019-01-12 03:33:27 +00:00
console.warn(info.warnings);
2018-03-18 11:59:38 +00:00
}
const buildInfo = stats.toString({
colors: true,
children: true,
chunks: false,
modules: false,
chunkModules: false,
hash: false,
version: false,
2019-01-12 03:33:27 +00:00
});
console.log(buildInfo);
done(0);
});
2018-03-18 11:59:38 +00:00
}
2020-10-19 08:43:10 +00:00
const tsFiles = ['**/*.ts', '**/*.tsx', '!node_modules/**/*.*', 'typings/**/*.d.ts'];
function compileTs(stream) {
return stream
.pipe(ts(tsConfig))
.js.pipe(
through2.obj(function(file, encoding, next) {
// console.log(file.path, file.base);
file.path = file.path.replace(/\.[jt]sx$/, '.js');
this.push(file);
next();
}),
)
.pipe(gulp.dest(process.cwd()));
}
gulp.task('tsc', () =>
compileTs(
gulp.src(tsFiles, {
base: cwd,
}),
),
);
2019-01-12 03:33:27 +00:00
function babelify(js, modules) {
const babelConfig = getBabelCommonConfig(modules);
babelConfig.babelrc = false;
delete babelConfig.cacheDirectory;
2018-03-19 09:51:47 +00:00
if (modules === false) {
2019-01-12 03:33:27 +00:00
babelConfig.plugins.push(replaceLib);
2018-03-19 09:51:47 +00:00
}
2019-01-12 03:33:27 +00:00
let stream = js.pipe(babel(babelConfig)).pipe(
through2.obj(function z(file, encoding, next) {
this.push(file.clone());
2020-10-31 06:09:09 +00:00
if (file.path.match(/\/style\/index\.(js|jsx|ts|tsx)$/)) {
2019-01-12 03:33:27 +00:00
const content = file.contents.toString(encoding);
file.contents = Buffer.from(
content
.replace(/\/style\/?'/g, "/style/css'")
.replace(/\/style\/?"/g, '/style/css"')
.replace(/\.less/g, '.css'),
2019-01-12 03:33:27 +00:00
);
2020-10-31 06:09:09 +00:00
file.path = file.path.replace(/index\.(js|jsx|ts|tsx)$/, 'css.js');
2019-01-12 03:33:27 +00:00
this.push(file);
next();
2018-03-19 09:51:47 +00:00
} else {
2019-01-12 03:33:27 +00:00
next();
2018-03-19 09:51:47 +00:00
}
2019-01-12 03:33:27 +00:00
}),
);
2018-03-19 09:51:47 +00:00
if (modules === false) {
2019-01-12 03:33:27 +00:00
stream = stream.pipe(
stripCode({
start_comment: '@remove-on-es-build-begin',
end_comment: '@remove-on-es-build-end',
}),
);
2018-03-19 09:51:47 +00:00
}
2019-01-12 03:33:27 +00:00
return stream.pipe(gulp.dest(modules === false ? esDir : libDir));
2018-03-19 09:51:47 +00:00
}
2019-01-12 03:33:27 +00:00
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));
2020-10-19 08:43:10 +00:00
let error = 0;
const source = [
'components/**/*.js',
'components/**/*.jsx',
'components/**/*.tsx',
'components/**/*.ts',
'typings/**/*.d.ts',
'!components/*/__tests__/*',
];
const tsResult = gulp.src(source).pipe(
ts(tsConfig, {
error(e) {
tsDefaultReporter.error(e);
error = 1;
},
finish: tsDefaultReporter.finish,
}),
);
function check() {
if (error && !argv['ignore-error']) {
process.exit(1);
}
}
2018-03-19 09:51:47 +00:00
2020-10-19 08:43:10 +00:00
tsResult.on('finish', check);
tsResult.on('end', check);
const tsFilesStream = babelify(tsResult.js, modules);
const tsd = tsResult.dts.pipe(gulp.dest(modules === false ? esDir : libDir));
return merge2([less, tsFilesStream, tsd, assets]);
2018-03-19 09:51:47 +00:00
}
2019-01-12 03:33:27 +00:00
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(
2019-08-12 12:38:09 +00:00
`git push https://${process.env.GITHUB_TOKEN}@github.com/vueComponent/ant-design-vue.git ${version}:${version}`,
2019-01-12 03:33:27 +00:00
);
execSync(
2019-08-12 12:38:09 +00:00
`git push https://${process.env.GITHUB_TOKEN}@github.com/vueComponent/ant-design-vue.git master:master`,
2019-01-12 03:33:27 +00:00
);
console.log('tagged');
2018-04-21 10:41:13 +00:00
}
2019-01-12 03:33:27 +00:00
function githubRelease(done) {
2018-04-21 10:41:13 +00:00
const changlogFiles = [
path.join(cwd, 'CHANGELOG.en-US.md'),
path.join(cwd, 'CHANGELOG.zh-CN.md'),
2019-01-12 03:33:27 +00:00
];
console.log('creating release on GitHub');
2018-04-21 10:41:13 +00:00
if (!process.env.GITHUB_TOKEN) {
2019-01-12 03:33:27 +00:00
console.log('no GitHub token found, skip');
return;
2018-04-21 10:41:13 +00:00
}
if (!changlogFiles.every(file => fs.existsSync(file))) {
2019-01-12 03:33:27 +00:00
console.log('no changelog found, skip');
return;
2018-04-21 10:41:13 +00:00
}
2020-04-09 15:11:01 +00:00
const github = new Octokit({
auth: process.env.GITHUB_TOKEN,
2019-01-12 03:33:27 +00:00
});
const date = new Date();
const { version } = packageJson;
const enChangelog = getChangelog(changlogFiles[0], version);
const cnChangelog = getChangelog(changlogFiles[1], version);
2018-04-21 10:41:13 +00:00
const changelog = [
`\`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}\``,
enChangelog,
'\n',
'---',
'\n',
cnChangelog,
2019-01-12 03:33:27 +00:00
].join('\n');
2018-04-21 10:41:13 +00:00
const [_, owner, repo] = execSync('git remote get-url origin') // eslint-disable-line
.toString()
2019-01-12 03:33:27 +00:00
.match(/github.com[:/](.+)\/(.+)\.git/);
github.repos
.createRelease({
owner,
repo,
tag_name: version,
name: version,
body: changelog,
})
.then(() => {
done();
2020-07-05 05:52:53 +00:00
})
.catch(err => {
console.log(err);
2019-01-12 03:33:27 +00:00
});
2018-04-21 10:41:13 +00:00
}
2018-07-27 13:42:45 +00:00
2019-07-04 10:28:27 +00:00
gulp.task(
'tag',
gulp.series(done => {
tag();
githubRelease(done);
}),
);
2018-07-27 13:42:45 +00:00
2019-07-04 10:28:27 +00:00
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}
2019-01-12 03:33:27 +00:00
`);
2019-07-04 10:28:27 +00:00
}
if (/^([ADRM]| [ADRM])/m.test(result)) {
return done(`There are uncommitted changes in the working tree.\n${result}
2019-01-12 03:33:27 +00:00
`);
2019-07-04 10:28:27 +00:00
}
return done();
});
}),
);
2018-04-21 10:41:13 +00:00
2019-01-12 03:33:27 +00:00
function publish(tagString, done) {
let args = ['publish', '--with-antd-tools'];
2021-04-25 07:16:11 +00:00
args = args.concat(['--tag', 'next']);
2018-04-21 10:41:13 +00:00
if (tagString) {
2019-01-12 03:33:27 +00:00
args = args.concat(['--tag', tagString]);
2018-04-21 10:41:13 +00:00
}
2019-01-12 03:33:27 +00:00
const publishNpm = process.env.PUBLISH_NPM_CLI || 'npm';
runCmd(publishNpm, args, code => {
tag();
2018-07-21 08:13:54 +00:00
githubRelease(() => {
2019-01-12 03:33:27 +00:00
done(code);
});
});
2018-04-21 10:41:13 +00:00
}
2019-01-12 03:33:27 +00:00
function pub(done) {
dist(code => {
2018-04-21 10:41:13 +00:00
if (code) {
2019-01-12 03:33:27 +00:00
done(code);
return;
2018-04-21 10:41:13 +00:00
}
2019-01-12 03:33:27 +00:00
const notOk = !packageJson.version.match(/^\d+\.\d+\.\d+$/);
let tagString;
2018-04-21 10:41:13 +00:00
if (argv['npm-tag']) {
2019-01-12 03:33:27 +00:00
tagString = argv['npm-tag'];
2018-04-21 10:41:13 +00:00
}
if (!tagString && notOk) {
2019-01-12 03:33:27 +00:00
tagString = 'next';
2018-04-21 10:41:13 +00:00
}
if (packageJson.scripts['pre-publish']) {
2019-01-12 03:33:27 +00:00
runCmd('npm', ['run', 'pre-publish'], code2 => {
2018-04-21 10:41:13 +00:00
if (code2) {
2019-01-12 03:33:27 +00:00
done(code2);
return;
2018-04-21 10:41:13 +00:00
}
2019-01-12 03:33:27 +00:00
publish(tagString, done);
});
2018-04-21 10:41:13 +00:00
} else {
2019-01-12 03:33:27 +00:00
publish(tagString, done);
2018-04-21 10:41:13 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-04-21 10:41:13 +00:00
}
2019-07-04 10:28:27 +00:00
gulp.task(
'compile-with-es',
gulp.series(done => {
compile(false).on('finish', function() {
done();
2019-01-12 03:33:27 +00:00
});
2019-07-04 10:28:27 +00:00
}),
);
gulp.task(
'compile',
gulp.series('compile-with-es', done => {
compile().on('finish', function() {
done();
2019-01-12 03:33:27 +00:00
});
2019-07-04 10:28:27 +00:00
}),
);
2018-07-21 04:36:49 +00:00
2019-07-04 10:28:27 +00:00
gulp.task(
'dist',
gulp.series('compile', done => {
dist(done);
}),
);
gulp.task(
'pub',
gulp.series('check-git', 'compile', done => {
Feat 1.5.0 (#1853) * feat: add Result component * fix: update md template tag html>tpl - fix `result` typo - update jest `result` snapshots * refactor: svg file to functional component icon - update jest snapshot * feat: add result * Feat descriptions (#1251) * feat: add descriptions * fix: add descriptions types and fix docs * fix: lint change code * fix: demo warning * fix: update demo, snapshot and remove classnames * test: add descriptions test * fix: descriptions demo (#1498) * feat: add page header (#1250) * feat: add page-header component * update site: page-header * ts definition update: page-header * get page-header props with getComponentFromProp func * optimize page-header * doc: add page-header actions.md responsive.md * breadcrumb itemRender add pure function support * style: format code * feat: update style to 3.23.6 from 2.13.6 * feat: update style to 3.26.8 from 3.23.6 * chore: update util * chore: update util * feat: update affix * feat: update alert * feat: update anchor * feat: update auto-complete * feat: update avatar * feat: update back-top * feat: update badge * feat: update button * feat: update breadcrumb * feat: update ts * docs: update doc * feat: update calendat * feat: update card * feat: update carousel * feat: update carousel * feat: update checkbox * feat: update comment * feat: update config-provider * docs: update doc * feat: update collapse * feat: update locale * feat: update date-picker * feat: update divider * feat: update drawer * feat: update dropdown * feat: update rc-trigger * feat: update dropdown * feat: update empty * test: add empty test * feat: update form * feat: update form * feat: update spin * feat: update grid * docs: update grid doc * feat: update icon * feat: update slider * feat: update textarea * feat: update input-number * feat: update layout * feat: update list * feat: update menu * feat: meaage add key for update content * feat: modal add closeIcon support * feat: update notification * feat: add pagination disabled support * feat: popconfirm add disabled support * test: update popover * feat: progress support custom line-gradiend * feat: update radio * test: update radio test * docs: update rate demo * feat: skeleton add avatar support number type * test: add switch test * test: update statistic test * fix: input clear icon event * feat: steps add type、 v-model、subTitle * feat: delete typography component * feat: delete Typography style * perf: update select * feat: add download transformFile previewFile actio * docs: update upload * feat: update tree-select * docs: update tree-select * feat: tree add blockNode selectable * docs: add tree demo * test: update snap * docs: updatedoc * feat: update tag * docs: update ad doc * feat: update tooltip * feat: update timeline * feat: time-picker add clearIcon * docs: update tabs * feat: transfer support custom children * test: update transfer test * feat: update table * test: update table test * test: update test * feat: calendar update locale * test: update test snap * feat: add mentions (#1790) * feat: mentions style * feat: theme default * feat: add mentions component * feat: mentions API * feat: add unit test for mentions * feat: update mentions demo * perf: model and inheritAttrs for mentions * perf: use getComponentFromProp instead of this.$props * perf: mentions rm defaultProps * feat: rm rows in mentionsProps * fix: mentions keyDown didn't work * docs: update mentions api * perf: mentions code * feat: update mentions * bump 1.5.0-alpha.1 * feat: pageheader add ghost prop * docs: update descriptions demo * chore: page-header add ghost type * fix: color error * feat: update to 3.26.12 * fix: some prop default value * fix(typo): form, carousel, upload. duplicate identifier (#1848) * Add Mentions Type (#1845) * feat: add mentions type * feat: add mentions in ant-design-vue.d.ts * docs: update doc * docs: add changelog * fix: mentions getPopupCotainer value (#1850) * docs: update doc * docs: uptate demo * docs: update demo * docs: delete demo * docs: delete doc * test: update snapshots * style: format code * chore: update travis * docs: update demo Co-authored-by: Sendya <18x@loacg.com> Co-authored-by: zkwolf <chenhao5866@gmail.com> Co-authored-by: drafish <xwlyy1991@163.com> Co-authored-by: Amour1688 <31695475+Amour1688@users.noreply.github.com>
2020-03-07 11:45:13 +00:00
// if (!process.env.GITHUB_TOKEN) {
// console.log('no GitHub token found, skip');
// } else {
// pub(done);
// }
pub(done);
2019-07-04 10:28:27 +00:00
}),
);
gulp.task(
'pub-with-ci',
gulp.series(done => {
if (!process.env.NPM_TOKEN) {
console.log('no NPM token found, skip');
} else {
2020-04-09 15:11:01 +00:00
const github = new Octokit({
auth: process.env.GITHUB_TOKEN,
2019-07-04 10:28:27 +00:00
});
const [_, owner, repo] = execSync('git remote get-url origin') // eslint-disable-line
.toString()
.match(/github.com[:/](.+)\/(.+)\.git/);
const getLatestRelease = github.repos.getLatestRelease({
owner,
repo,
});
2019-07-24 11:24:16 +00:00
const listCommits = github.repos.listCommits({
2019-07-04 10:28:27 +00:00
owner,
repo,
per_page: 1,
});
2019-07-24 11:24:16 +00:00
Promise.all([getLatestRelease, listCommits]).then(([latestRelease, commits]) => {
2019-07-04 10:28:27 +00:00
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
) {
2020-03-29 09:41:43 +00:00
// eslint-disable-next-line no-unused-vars
2019-08-13 06:47:57 +00:00
runCmd('npm', ['run', 'pub'], code => {
2019-07-04 10:28:27 +00:00
done();
});
} else {
console.log('donot need publish' + version);
}
});
}
}),
);
2018-04-21 10:41:13 +00:00
2019-07-04 10:28:27 +00:00
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;
}
2018-04-21 10:41:13 +00:00
}
}
2019-07-04 10:28:27 +00:00
done();
}),
);