UNPKG

hem

Version:

stitches CommonJS, and ties up other lose ends of web-app development.

112 lines (90 loc) 3.18 kB
// Generated by CoffeeScript 1.12.7 (function() { var BuildVersion, NpmPackageVersion, fs, log, path, types, updateVersionInAppFiles, updateVersionInData, utils; fs = require('fs'); path = require('path'); utils = require('./utils'); log = require('./log'); types = {}; updateVersionInAppFiles = function(files, packages, value) { var data, file, i, key, len, pkg, results; results = []; for (i = 0, len = files.length; i < len; i++) { file = files[i]; log("- updating file <yellow>" + file + "</yellow> with version: <b>" + value + "</b>"); data = fs.readFileSync(file, 'utf8'); for (key in packages) { pkg = packages[key]; data = updateVersionInData(data, value, pkg); } results.push(fs.writeFileSync(file, data)); } return results; }; updateVersionInData = function(data, value, pkg) { var ext, match, name, replace; ext = path.extname(pkg.target); name = path.basename(pkg.target, ext); match = new RegExp("=(\"|')(.*/?)" + name + "[^\"']?" + ext + "(\"|')"); replace = "=$1$2" + name + "." + value + ext + "$3"; if (data.match(match)) { log("> found target: " + pkg.target); return data.replace(match, replace); } else { return data; } }; types["package"] = NpmPackageVersion = (function() { function NpmPackageVersion(app, options) { if (options == null) { options = {}; } this.app = app; this.files = utils.toArray(options.files).map((function(_this) { return function(file) { return _this.app.applyRootDir(file)[0]; }; })(this)); } NpmPackageVersion.prototype.getVersion = function() { return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version; }; NpmPackageVersion.prototype.update = function() { return updateVersionInAppFiles(this.files, this.app.packages, this.getVersion()); }; NpmPackageVersion.prototype.trim = function(url) { return url.replace(/^([^.]+).*(\.css|\.js)$/i, "$1$2"); }; return NpmPackageVersion; })(); types.build = BuildVersion = (function() { function BuildVersion(app, options) { if (options == null) { options = {}; } this.app = app; this.files = utils.toArray(options.files).map((function(_this) { return function(file) { return _this.app.applyRootDir(file)[0]; }; })(this)); this.envVariable = options.envVariable || "BUILD_NUMBER"; } BuildVersion.prototype.getVersion = function() { if (process.env[this.envVariable]) { return process.env[this.envVariable]; } else { log("ERROR: " + this.envVariable + " not set correctly as an environment variable."); return process.exit(1); } }; BuildVersion.prototype.update = function() { return updateVersionInAppFiles(this.files, this.app.packages, this.getVersion()); }; BuildVersion.prototype.trim = function(url) { return url.replace(/^([^.]+).*(\.css|\.js)$/i, "$1$2"); }; return BuildVersion; })(); module.exports = types; }).call(this);