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

333 lines
9.2 KiB
JavaScript
Raw Normal View History

2018-03-18 11:59:38 +00:00
'use strict'
// const install = require('./install')
2018-04-21 10:41:13 +00:00
const runCmd = require('./runCmd')
2018-03-19 09:51:47 +00:00
const getBabelCommonConfig = require('./getBabelCommonConfig')
const merge2 = require('merge2')
2018-04-21 10:41:13 +00:00
const { execSync } = require('child_process')
2018-03-19 09:51:47 +00:00
const through2 = require('through2')
const transformLess = require('./transformLess')
2018-03-18 11:59:38 +00:00
const webpack = require('webpack')
2018-03-19 09:51:47 +00:00
const babel = require('gulp-babel')
2018-04-21 10:41:13 +00:00
const argv = require('minimist')(process.argv.slice(2))
const GitHub = require('@octokit/rest')
2018-03-18 11:59:38 +00:00
2018-04-21 10:41:13 +00:00
const packageJson = require(`${process.cwd()}/package.json`)
2018-03-18 11:59:38 +00:00
// const getNpm = require('./getNpm')
// const selfPackage = require('../package.json')
2018-04-21 10:41:13 +00:00
const chalk = require('chalk')
const getNpmArgs = require('./utils/get-npm-args')
const getChangelog = require('./utils/getChangelog')
2018-03-18 11:59:38 +00:00
const path = require('path')
// const watch = require('gulp-watch')
const gulp = require('gulp')
2018-04-21 10:41:13 +00:00
const fs = require('fs')
2018-03-18 11:59:38 +00:00
const rimraf = require('rimraf')
2018-03-19 09:51:47 +00:00
const replaceLib = require('./replaceLib')
const stripCode = require('gulp-strip-code')
2018-07-21 04:36:49 +00:00
const compareVersions = require('compare-versions')
2018-03-18 11:59:38 +00:00
const cwd = process.cwd()
2018-03-19 09:51:47 +00:00
const libDir = path.join(cwd, 'lib')
const esDir = path.join(cwd, 'es')
2018-03-18 11:59:38 +00:00
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)
})
}
2018-03-19 09:51:47 +00:00
function babelify (js, modules) {
2018-05-13 15:19:12 +00:00
const babelConfig = getBabelCommonConfig(modules)
babelConfig.babelrc = false
2018-03-19 09:51:47 +00:00
delete babelConfig.cacheDirectory
if (modules === false) {
babelConfig.plugins.push(replaceLib)
} else {
babelConfig.plugins.push(require.resolve('babel-plugin-add-module-exports'))
}
let stream = js.pipe(babel(babelConfig))
.pipe(through2.obj(function z (file, encoding, next) {
this.push(file.clone())
2018-05-02 02:13:46 +00:00
if (file.path.match(/\/style\/index\.(js|jsx)$/)) {
2018-03-19 09:51:47 +00:00
const content = file.contents.toString(encoding)
file.contents = Buffer.from(content
.replace(/\/style\/?'/g, '/style/css\'')
.replace(/\.less/g, '.css'))
2018-05-02 02:13:46 +00:00
file.path = file.path.replace(/index\.(js|jsx)$/, 'css.js')
2018-03-19 09:51:47 +00:00
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',
2018-05-13 15:11:51 +00:00
'!components/*/__tests__/*',
2018-03-19 09:51:47 +00:00
]
2018-03-19 11:09:30 +00:00
const jsFilesStream = babelify(gulp.src(source), modules)
return merge2([less, jsFilesStream, assets])
2018-03-19 09:51:47 +00:00
}
2018-04-21 10:41:13 +00:00
function tag () {
console.log('tagging')
const { version } = packageJson
2018-07-21 08:13:54 +00:00
execSync(`git config --global user.email ${process.env.GITHUB_USER_EMAIL}`)
execSync(`git config --global user.name ${process.env.GITHUB_USER_NAME}`)
2018-04-21 10:41:13 +00:00
execSync(`git tag ${version}`)
2018-07-27 13:42:45 +00:00
execSync(`git push https://${process.env.GITHUB_TOKEN}@github.com/vueComponent/ant-design.git ${version}:${version}`)
execSync(`git push https://${process.env.GITHUB_TOKEN}@github.com/vueComponent/ant-design.git master:master`)
2018-04-21 10:41:13 +00:00
console.log('tagged')
}
2018-07-21 08:13:54 +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'),
]
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,
2018-07-21 08:13:54 +00:00
}).then(() => {
done()
2018-04-21 10:41:13 +00:00
})
}
2018-07-27 13:42:45 +00:00
gulp.task('tag', (done) => {
tag()
githubRelease(done)
})
2018-04-21 10:41:13 +00:00
gulp.task('check-git', (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()
2018-07-21 08:13:54 +00:00
githubRelease(() => {
done(code)
})
2018-04-21 10:41:13 +00:00
})
}
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)
}
})
}
2018-04-10 04:08:41 +00:00
gulp.task('dist', ['compile'], (done) => {
2018-03-18 11:59:38 +00:00
dist(done)
})
2018-05-02 05:31:58 +00:00
gulp.task('compile', ['compile-with-es'], (done) => {
2018-03-19 09:51:47 +00:00
compile()
2018-05-02 05:31:58 +00:00
.on('finish', function () {
done()
})
2018-03-19 09:51:47 +00:00
})
2018-05-02 05:31:58 +00:00
gulp.task('compile-with-es', (done) => {
2018-03-19 09:51:47 +00:00
compile(false)
2018-05-02 05:31:58 +00:00
.on('finish', function () {
done()
})
2018-03-19 09:51:47 +00:00
})
2018-04-21 10:41:13 +00:00
gulp.task('pub', ['check-git', 'compile'], (done) => {
2018-05-08 03:49:56 +00:00
if (!process.env.GITHUB_TOKEN) {
console.log('no GitHub token found, skip')
} else {
pub(done)
}
2018-04-21 10:41:13 +00:00
})
2018-07-21 04:36:49 +00:00
gulp.task('pub-with-ci', (done) => {
2018-07-21 05:52:21 +00:00
if (!process.env.NPM_TOKEN) {
console.log('no NPM token found, skip')
2018-07-21 04:36:49 +00:00
} 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 getCommits = github.repos.getCommits({
owner,
repo,
per_page: 1,
})
Promise.all([getLatestRelease, getCommits]).then(([latestRelease, commits]) => {
const preVersion = latestRelease.data.tag_name
const { version } = packageJson
const [_, newVersion] = commits.data[0].commit.message.trim().match(/bump (.+)/) || []
if (compareVersions(version, preVersion) === 1 && newVersion && newVersion.trim() === version) {
gulp.run('pub', (err) => {
err && console.log('err', err)
done()
})
} else {
console.log('donot need publish' + version)
}
})
}
})
2018-04-21 10:41:13 +00:00
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('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'))
}
gulp.task('guard', (done) => {
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()
})