-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring of the automation process (gulp)
- Loading branch information
Showing
8 changed files
with
359 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
var gulp = require('gulp'); | ||
var header = require('gulp-header'); | ||
var clean = require('gulp-clean'); | ||
var gulpWebpack = require('webpack-stream'); | ||
var webpack = require('webpack'); | ||
var babel = require("gulp-babel"); | ||
var runSequence = require('run-sequence'); | ||
var fs = require('fs'); | ||
|
||
var settings = require('./settings.json'); | ||
var shared = require('./shared.js'); | ||
|
||
gulp.task("clean", function(){ | ||
return gulp.src(["bin/", "dist/"], {read: false}) | ||
.pipe(clean()); | ||
}); | ||
|
||
|
||
gulp.task("babel", function () { | ||
return babelFunc(); | ||
}); | ||
|
||
|
||
function babelFunc(){ | ||
return gulp.src("src/**/*") | ||
.pipe(babel({ | ||
presets: ['es2015'], | ||
plugins: [["transform-es2015-classes", {loose: true}]] | ||
})) | ||
.pipe(gulp.dest("bin/")); | ||
} | ||
|
||
|
||
gulp.task("webpack", ["babel"], function () { | ||
return webpackFunc(); | ||
}); | ||
|
||
|
||
function webpackFunc(){ | ||
return gulp.src('bin/JsBarcode.js') | ||
.pipe(gulpWebpack( | ||
{ | ||
output: { | ||
filename: 'JsBarcode.all.js' | ||
} | ||
} | ||
, webpack)) | ||
.pipe(gulp.dest("dist/")); | ||
} | ||
|
||
|
||
gulp.task("webpack-min", ["babel"], function () { | ||
return webpackMin('all'); | ||
}); | ||
|
||
|
||
function webpackMin(name, dest){ | ||
dest = dest || './'; | ||
return gulp.src('bin/JsBarcode.js') | ||
.pipe(gulpWebpack( | ||
{ | ||
output: { | ||
filename: shared.minifiedFilename(name) | ||
}, | ||
plugins: [new webpack.optimize.UglifyJsPlugin()] | ||
} | ||
, webpack)) | ||
.pipe(header(settings.banner, require(settings.baseDir + 'package.json') )) | ||
.pipe(gulp.dest("dist/" + dest)); | ||
} | ||
|
||
|
||
gulp.task("webpack-all", function (cb) { | ||
var barcodes = require('./barcode-building.json'); | ||
|
||
// Move the real barcodes/index.js out of the way while compiling the individual barcodes | ||
fs.renameSync("src/barcodes/index.js", "src/barcodes/index.tmp.js"); | ||
|
||
// Take a barcode from the barcodes array, call the functions to compile that | ||
// format and have a callback when it has finished. | ||
function loopBarcode(i){ | ||
if(i < barcodes.length){ | ||
createBarcodeInclude(barcodes[i], function(){ | ||
loopBarcode(i + 1); | ||
}); | ||
} | ||
else{ | ||
fs.renameSync("src/barcodes/index.tmp.js", "src/barcodes/index.js"); | ||
cb(); // Done | ||
} | ||
} | ||
|
||
loopBarcode(0); | ||
}); | ||
|
||
|
||
// Takes information about a barcode formatSize | ||
// Modifies the barcodes/index.js file to only import the specified format | ||
// and then does a recompilation and minifies everything with webpack | ||
function createBarcodeInclude(barcode, callback){ | ||
var toFile = ""; | ||
toFile += "import {" + barcode.names + "} from '" + barcode.barcodeFile + "'"; | ||
toFile += "\n"; | ||
toFile += "export default {" + barcode.names + "}"; | ||
|
||
// Write a new barcodes/index file that only includes the specified barcode | ||
fs.writeFile("src/barcodes/index.js", toFile, function(){ | ||
// Remove the compiled barcodes/index file (if it exist) | ||
if(fs.existsSync("bin/barcodes/index.js")){ | ||
fs.unlinkSync("bin/barcodes/index.js"); | ||
} | ||
// Re-compile with babel and webpack everything | ||
babelFunc().on('end', function(){ | ||
webpackMin(barcode.name, 'barcodes/').on('end', callback); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
gulp.task('compress', function(cb) { | ||
runSequence( | ||
"clean", | ||
"webpack-all", | ||
"webpack", | ||
"webpack-min", | ||
cb | ||
); | ||
}); | ||
|
||
gulp.task('compile', ['babel']); | ||
|
||
gulp.task('compile-web', ['webpack']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var gulp = require('gulp'); | ||
var eslint = require('gulp-eslint'); | ||
|
||
gulp.task("lint", function () { | ||
return gulp.src(['src/**/*.js']) | ||
.pipe(eslint()) | ||
.pipe(eslint.format()) | ||
.pipe(eslint.failAfterError()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*eslint no-console: 0 */ | ||
|
||
var gulp = require('gulp'); | ||
var request = require('request'); | ||
var fs = require('fs'); | ||
|
||
gulp.task('jsdelivr', function(callback){ | ||
console.log("Making request..."); | ||
request({ | ||
url: "https://api.jsdelivr.com/v1/jsdelivr/libraries?name=jsbarcode", | ||
json: true | ||
}, function (error, response, body) { | ||
if (!error && response.statusCode === 200) { | ||
var readme = fs.readFileSync('README.md', "utf-8"); | ||
var version = body[0].lastversion; | ||
|
||
readme = readme.replace(/https:\/\/cdn\.jsdelivr\.net\/jsbarcode\/[0-9]+\.[0-9]+\.[0-9]+\//g, | ||
"https://cdn.jsdelivr.net/jsbarcode/" + version + "/"); | ||
|
||
fs.writeFileSync('README.md', readme, 'utf8'); | ||
|
||
console.log("New version: " + version); | ||
callback(); | ||
} | ||
else{ | ||
console.error("Failed to make jsdelivr api request"); | ||
callback(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/*eslint | ||
no-console: 0 | ||
*/ | ||
|
||
var gulp = require('gulp'); | ||
var bump = require('gulp-bump'); | ||
var git = require('gulp-git'); | ||
var publishRelease = require('publish-release'); | ||
var gzipSize = require('gzip-size'); | ||
var runSequence = require('run-sequence'); | ||
var fs = require('fs'); | ||
|
||
var settings = require('./settings.json'); | ||
var shared = require('./shared.js'); | ||
|
||
|
||
gulp.task('git-release', ['compress'], function(cb){ | ||
var pkg = require(settings.baseDir + 'package.json'); | ||
var v = 'v' + pkg.version; | ||
var message = 'Release ' + v; | ||
|
||
updateReadmeFileSizes(); | ||
|
||
gulp.src(['./package.json', './bower.json', './README.md', './bin/', './dist']) | ||
.pipe(git.add({args: '--all --force'})) | ||
.pipe(git.commit(message)); | ||
|
||
git.push('origin', 'master', function(){ | ||
git.tag(v, message, function(){ | ||
git.push('origin', 'master', {args: '--tags'}, cb); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
// Bump (increase) the version number | ||
gulp.task('bump-patch', function(){ | ||
return gulp.src(['./package.json', './bower.json']) | ||
.pipe(bump({type:'patch'})) | ||
.pipe(gulp.dest('./')); | ||
}); | ||
|
||
|
||
gulp.task('bump-minor', function(){ | ||
return gulp.src(['./package.json', './bower.json']) | ||
.pipe(bump({type:'minor'})) | ||
.pipe(gulp.dest('./')); | ||
}); | ||
|
||
|
||
gulp.task('bump-major', function(){ | ||
return gulp.src(['./package.json', './bower.json']) | ||
.pipe(bump({type:'major'})) | ||
.pipe(gulp.dest('./')); | ||
}); | ||
|
||
|
||
gulp.task('npm', function (done) { | ||
require('child_process').spawn('npm', ['publish'], { stdio: 'inherit' }) | ||
.on('close', done); | ||
}); | ||
|
||
|
||
gulp.task('github-release', function(done) { | ||
var pkg = require(settings.baseDir + './package.json'); | ||
var v = 'v' + pkg.version; | ||
var name = "JsBarcode " + v; | ||
|
||
publishRelease({ | ||
token: process.env.GITHUB_TOKEN, | ||
owner: "lindell", | ||
repo: "JsBarcode", | ||
tag: v, | ||
name: name, | ||
assets: [__dirname + settings.baseDir + "/dist/JsBarcode.all.min.js", __dirname + settings.baseDir + "/dist/JsBarcode.all.js"] | ||
}, done); | ||
}); | ||
|
||
|
||
|
||
gulp.task('release', ['lint'], function(callback){ | ||
runSequence( | ||
'git-release', | ||
'github-release', | ||
'npm', | ||
callback | ||
); | ||
}); | ||
|
||
|
||
gulp.task('patch', function(){ | ||
runSequence( | ||
'bump-patch', | ||
'release', | ||
releaseDone | ||
); | ||
}); | ||
|
||
|
||
gulp.task('minor', function(){ | ||
runSequence( | ||
'bump-minor', | ||
'release', | ||
releaseDone | ||
); | ||
}); | ||
|
||
|
||
gulp.task('major', function(){ | ||
runSequence( | ||
'bump-major', | ||
'release', | ||
releaseDone | ||
); | ||
}); | ||
|
||
function releaseDone (error) { | ||
if (error) { | ||
console.log(error.message); | ||
} | ||
else { | ||
console.log('Successful!'); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
function updateReadmeFileSizes(){ | ||
var files = require('./barcode-building.json'); | ||
var readme = fs.readFileSync('README.md', "utf-8"); | ||
|
||
// Update .all files | ||
var allData = fs.readFileSync('dist/JsBarcode.all.min.js'); | ||
var allFilesize = gzipSize.sync(allData); | ||
|
||
var allRegexp = new RegExp('\\|[^\\|]*\\|([ \\t\\*]*\\[JsBarcode\\.all\\.min\\.js\\])'); | ||
readme = readme.replace(allRegexp, "| *" + formatSize(allFilesize) + "* |$1"); | ||
|
||
// Update all barcodes files | ||
for(var i in files){ | ||
var filename = shared.minifiedFilename(files[i].name); | ||
|
||
var fileData = fs.readFileSync('dist/barcodes/' + filename); | ||
var fileFilesize = gzipSize.sync(fileData); | ||
|
||
var fileRegexp = new RegExp('\\|[^\\|]*\\|([ \\t]*\\[' + RegExp.escape(filename) + '\\])'); | ||
|
||
readme = readme.replace(fileRegexp, "| " + formatSize(fileFilesize) + " |$1"); | ||
} | ||
|
||
fs.writeFileSync('README.md', readme, 'utf8'); | ||
} | ||
|
||
function formatSize(bytes){ | ||
var kilobytes = Math.round(bytes / 1024 * 10) / 10; | ||
|
||
return kilobytes + " kB"; | ||
} | ||
|
||
|
||
// Util functions | ||
RegExp.escape = function(s) { | ||
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"baseDir": "../", | ||
"distDir": "./dist/", | ||
"binDir": "./bin/", | ||
"banner": "/*! JsBarcode v<%= version %> | (c) <%= author %> | <%= license %> license */\n" | ||
} |
Oops, something went wrong.