11'use strict'
22
33// const install = require('./install')
4- // const runCmd = require('./runCmd')
4+ const runCmd = require ( './runCmd' )
55const getBabelCommonConfig = require ( './getBabelCommonConfig' )
66const merge2 = require ( 'merge2' )
7- // const { execSync } = require('child_process')
7+ const { execSync } = require ( 'child_process' )
88const through2 = require ( 'through2' )
99const transformLess = require ( './transformLess' )
1010const webpack = require ( 'webpack' )
1111const babel = require ( 'gulp-babel' )
12- // const argv = require('minimist')(process.argv.slice(2))
13- // const GitHub = require('github ')
12+ const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) )
13+ const GitHub = require ( '@octokit/rest ' )
1414
15- // const packageJson = require(`${process.cwd()}/package.json`)
15+ const packageJson = require ( `${ process . cwd ( ) } /package.json` )
1616// const getNpm = require('./getNpm')
1717// const selfPackage = require('../package.json')
18- // const chalk = require('chalk')
19- // const getNpmArgs = require('./utils/get-npm-args')
20-
18+ const chalk = require ( 'chalk' )
19+ const getNpmArgs = require ( './utils/get-npm-args' )
20+ const getChangelog = require ( './utils/getChangelog' )
2121const path = require ( 'path' )
2222// const watch = require('gulp-watch')
2323const gulp = require ( 'gulp' )
24- // const fs = require('fs')
24+ const fs = require ( 'fs' )
2525const rimraf = require ( 'rimraf' )
2626const replaceLib = require ( './replaceLib' )
2727const stripCode = require ( 'gulp-strip-code' )
@@ -123,12 +123,118 @@ function compile (modules) {
123123 const source = [
124124 'components/**/*.js' ,
125125 'components/**/*.jsx' ,
126- // '!components/vc-slider/**/*', // exclude vc-slider
127126 ]
128127 const jsFilesStream = babelify ( gulp . src ( source ) , modules )
129128 return merge2 ( [ less , jsFilesStream , assets ] )
130129}
131130
131+ function tag ( ) {
132+ console . log ( 'tagging' )
133+ const { version } = packageJson
134+ execSync ( `git tag ${ version } ` )
135+ execSync ( `git push origin ${ version } :${ version } ` )
136+ execSync ( 'git push origin master:master' )
137+ console . log ( 'tagged' )
138+ }
139+
140+ function githubRelease ( ) {
141+ const changlogFiles = [
142+ path . join ( cwd , 'CHANGELOG.en-US.md' ) ,
143+ path . join ( cwd , 'CHANGELOG.zh-CN.md' ) ,
144+ ]
145+ console . log ( 'creating release on GitHub' )
146+ if ( ! process . env . GITHUB_TOKEN ) {
147+ console . log ( 'no GitHub token found, skip' )
148+ return
149+ }
150+ if ( ! changlogFiles . every ( file => fs . existsSync ( file ) ) ) {
151+ console . log ( 'no changelog found, skip' )
152+ return
153+ }
154+ const github = new GitHub ( )
155+ github . authenticate ( {
156+ type : 'oauth' ,
157+ token : process . env . GITHUB_TOKEN ,
158+ } )
159+ const date = new Date ( )
160+ const { version } = packageJson
161+ const enChangelog = getChangelog ( changlogFiles [ 0 ] , version )
162+ const cnChangelog = getChangelog ( changlogFiles [ 1 ] , version )
163+ const changelog = [
164+ `\`${ date . getFullYear ( ) } -${ date . getMonth ( ) + 1 } -${ date . getDate ( ) } \`` ,
165+ enChangelog ,
166+ '\n' ,
167+ '---' ,
168+ '\n' ,
169+ cnChangelog ,
170+ ] . join ( '\n' )
171+ const [ _ , owner , repo ] = execSync ( 'git remote get-url origin' ) // eslint-disable-line
172+ . toString ( )
173+ . match ( / g i t h u b .c o m [: / ] ( .+ ) \/ ( .+ ) \. g i t / )
174+
175+ github . repos . createRelease ( {
176+ owner,
177+ repo,
178+ tag_name : version ,
179+ name : version ,
180+ body : changelog ,
181+ } )
182+ }
183+ gulp . task ( 'check-git' , ( done ) => {
184+ runCmd ( 'git' , [ 'status' , '--porcelain' ] , ( code , result ) => {
185+ if ( / ^ \? \? / m. test ( result ) ) {
186+ return done ( `There are untracked files in the working tree.\n${ result }
187+ ` )
188+ }
189+ if ( / ^ ( [ A D R M ] | [ A D R M ] ) / m. test ( result ) ) {
190+ return done ( `There are uncommitted changes in the working tree.\n${ result }
191+ ` )
192+ }
193+ return done ( )
194+ } )
195+ } )
196+
197+ function publish ( tagString , done ) {
198+ let args = [ 'publish' , '--with-antd-tools' ]
199+ if ( tagString ) {
200+ args = args . concat ( [ '--tag' , tagString ] )
201+ }
202+ const publishNpm = process . env . PUBLISH_NPM_CLI || 'npm'
203+ runCmd ( publishNpm , args , ( code ) => {
204+ tag ( )
205+ githubRelease ( )
206+ done ( code )
207+ } )
208+ }
209+
210+ function pub ( done ) {
211+ dist ( ( code ) => {
212+ if ( code ) {
213+ done ( code )
214+ return
215+ }
216+ const notOk = ! packageJson . version . match ( / ^ \d + \. \d + \. \d + $ / )
217+ let tagString
218+ if ( argv [ 'npm-tag' ] ) {
219+ tagString = argv [ 'npm-tag' ]
220+ }
221+ if ( ! tagString && notOk ) {
222+ tagString = 'next'
223+ }
224+ if ( packageJson . scripts [ 'pre-publish' ] ) {
225+ runCmd ( 'npm' , [ 'run' , 'pre-publish' ] , ( code2 ) => {
226+ if ( code2 ) {
227+ done ( code2 )
228+ return
229+ }
230+ publish ( tagString , done )
231+ } )
232+ } else {
233+ publish ( tagString , done )
234+ }
235+ } )
236+ }
237+
132238gulp . task ( 'dist' , [ 'compile' ] , ( done ) => {
133239 dist ( done )
134240} )
@@ -138,3 +244,29 @@ gulp.task('compile', ['compile-with-es'], () => {
138244gulp . task ( 'compile-with-es' , ( ) => {
139245 compile ( false )
140246} )
247+
248+ gulp . task ( 'pub' , [ 'check-git' , 'compile' ] , ( done ) => {
249+ pub ( done )
250+ } )
251+
252+ function reportError ( ) {
253+ console . log ( chalk . bgRed ( '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' ) )
254+ console . log ( chalk . bgRed ( '!! `npm publish` is forbidden for this package. !!' ) )
255+ console . log ( chalk . bgRed ( '!! Use `npm run pub` instead. !!' ) )
256+ console . log ( chalk . bgRed ( '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' ) )
257+ }
258+
259+ gulp . task ( 'guard' , ( done ) => {
260+ const npmArgs = getNpmArgs ( )
261+ if ( npmArgs ) {
262+ for ( let arg = npmArgs . shift ( ) ; arg ; arg = npmArgs . shift ( ) ) {
263+ if ( / ^ p u ( b ( l ( i ( s h ? ) ? ) ? ) ? ) ? $ / . test ( arg ) && npmArgs . indexOf ( '--with-antd-tools' ) < 0 ) {
264+ reportError ( )
265+ done ( 1 )
266+ return
267+ }
268+ }
269+ }
270+ done ( )
271+ } )
272+
0 commit comments