From 320c9e8440b90a4ecd93bf5eedb1104a7ffb28c4 Mon Sep 17 00:00:00 2001 From: visioncan Date: Tue, 24 Oct 2017 11:37:29 +0800 Subject: [PATCH 1/3] Add version option --- README.md | 5 +++++ index.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/README.md b/README.md index 915d664..a40f00e 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,11 @@ gulp.task('deploy', function() { Only trigger deployment on the following branch(es). Defaults to `master`. +- `version` + + Tag version from package.json. Defaults to `false`. + + - `verbose` Verbose mode. Will show output from all git commands run. Defaults to `false`. diff --git a/index.js b/index.js index f38ce43..11ac472 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ module.exports = function(options) { repository: '', remoteBranch: 'master', branches: ['master'], + version: false, verbose: false, debug: false }, options); @@ -201,6 +202,52 @@ module.exports = function(options) { return callback(null); }); }, + function version(callback) { + if (!options.version) { + return callback(null); + } + const packageJson = files.reduce(function(prev, file) { + if (file.path.indexOf('package.json') !== -1) { + return file.path + } + }, ''); + var packageContent = fs.readFileSync(packageJson, 'utf8'); + var json = JSON.parse(packageContent.toString()); + var version = json.version; + gutil.log(gutil.colors.yellow('Tag version ' + version)); + var cmdTag = spawn('git', ['tag', '-a', 'v' + version, '-m', 'Version ' + version], {cwd: repoPath}); + cmdTag.stderr.on('data', function(data) { + if (options.verbose || options.debug) gutil.log(gutil.colors.magenta('git tag: ') + data.toString().trim()); + }); + cmdTag.stdout.on('data', function(data) { + if (options.verbose || options.debug) gutil.log(gutil.colors.magenta('git tag: ') + data.toString().trim()); + }); + cmdTag.on('close', function(code) { + if (code !== 0) { + return callback('git tag exited with code ' + code); + } + return callback(null); + }); + }, + function gitPushVersion(callback) { + if (!options.version) { + return callback(null); + } + gutil.log(gutil.colors.yellow('Pushing version to remote deployment repository')); + var cmdPush = spawn('git', ['push', 'origin', '--tags'], {cwd: repoPath}); + cmdPush.stderr.on('data', function(data) { + if (options.verbose || options.debug) gutil.log(gutil.colors.magenta('git push verion: ') + data.toString().trim()); + }); + cmdPush.stdout.on('data', function(data) { + if (options.verbose || options.debug) gutil.log(gutil.colors.magenta('git push verion: ') + data.toString().trim()); + }); + cmdPush.on('close', function(code) { + if (code !== 0) { + return callback('git push verion exited with code ' + code); + } + return callback(null); + }); + }, function gitPush(callback) { gutil.log(gutil.colors.yellow('Pushing to remote deployment repository')); var cmdPush = spawn('git', ['push', 'origin', options.remoteBranch], {cwd: repoPath}); From 1f29a014ecbe34cd9c2b7f890af67a690db3a0ab Mon Sep 17 00:00:00 2001 From: visioncan Date: Tue, 24 Oct 2017 14:14:44 +0800 Subject: [PATCH 2/3] Fixed return the package.josn path and warning --- index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 11ac472..d8e7fe7 100644 --- a/index.js +++ b/index.js @@ -206,11 +206,15 @@ module.exports = function(options) { if (!options.version) { return callback(null); } - const packageJson = files.reduce(function(prev, file) { + const packageJson = files.reduce(function(path, file) { if (file.path.indexOf('package.json') !== -1) { - return file.path + path = file.path } + return path }, ''); + if (packageJson === '') { + return callback('package.json is required if option version is true'); + } var packageContent = fs.readFileSync(packageJson, 'utf8'); var json = JSON.parse(packageContent.toString()); var version = json.version; From c7229cf3e28c8f8389f84f4e021c23ea42082295 Mon Sep 17 00:00:00 2001 From: visioncan Date: Tue, 24 Oct 2017 15:05:19 +0800 Subject: [PATCH 3/3] Rename method --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d8e7fe7..ef980db 100644 --- a/index.js +++ b/index.js @@ -202,7 +202,7 @@ module.exports = function(options) { return callback(null); }); }, - function version(callback) { + function gitTag(callback) { if (!options.version) { return callback(null); } @@ -233,7 +233,7 @@ module.exports = function(options) { return callback(null); }); }, - function gitPushVersion(callback) { + function gitPushTag(callback) { if (!options.version) { return callback(null); }