|
| 1 | +/** |
| 2 | + * @fileoverview Build file |
| 3 | + * @author nzakas |
| 4 | + */ |
| 5 | +/*global cat, cp, echo, exec, exit, find, mkdir, mv, rm, target, test*/ |
| 6 | + |
| 7 | +"use strict"; |
| 8 | + |
| 9 | +/* eslint no-console: 0*/ |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | +// Requirements |
| 12 | +//------------------------------------------------------------------------------ |
| 13 | + |
| 14 | +require("shelljs/make"); |
| 15 | + |
| 16 | +var checker = require("npm-license"), |
| 17 | + dateformat = require("dateformat"), |
| 18 | + nodeCLI = require("shelljs-nodecli"), |
| 19 | + semver = require("semver"); |
| 20 | + |
| 21 | +//------------------------------------------------------------------------------ |
| 22 | +// Settings |
| 23 | +//------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +var OPEN_SOURCE_LICENSES = [ |
| 26 | + /MIT/, /BSD/, /Apache/, /ISC/, /WTF/, /Public Domain/ |
| 27 | +]; |
| 28 | + |
| 29 | +//------------------------------------------------------------------------------ |
| 30 | +// Data |
| 31 | +//------------------------------------------------------------------------------ |
| 32 | + |
| 33 | +var NODE_MODULES = "./node_modules/", |
| 34 | + TEMP_DIR = "./tmp/", |
| 35 | + BUILD_DIR = "./build/", |
| 36 | + |
| 37 | + // Utilities - intentional extra space at the end of each string |
| 38 | + MOCHA = NODE_MODULES + "mocha/bin/_mocha ", |
| 39 | + |
| 40 | + // Files |
| 41 | + MAKEFILE = "./Makefile.js", |
| 42 | + /*eslint-disable no-use-before-define */ |
| 43 | + JS_FILES = find("lib/").filter(fileType("js")).join(" ") + " espree.js", |
| 44 | + TEST_FILES = find("tests/lib/").filter(fileType("js")).join(" "); |
| 45 | + /*eslint-enable no-use-before-define */ |
| 46 | + |
| 47 | +//------------------------------------------------------------------------------ |
| 48 | +// Helpers |
| 49 | +//------------------------------------------------------------------------------ |
| 50 | + |
| 51 | +/** |
| 52 | + * Generates a function that matches files with a particular extension. |
| 53 | + * @param {string} extension The file extension (i.e. "js") |
| 54 | + * @returns {Function} The function to pass into a filter method. |
| 55 | + * @private |
| 56 | + */ |
| 57 | +function fileType(extension) { |
| 58 | + return function(filename) { |
| 59 | + return filename.substring(filename.lastIndexOf(".") + 1) === extension; |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Executes a command and returns the output instead of printing it to stdout. |
| 65 | + * @param {string} cmd The command string to execute. |
| 66 | + * @returns {string} The result of the executed command. |
| 67 | + */ |
| 68 | +function execSilent(cmd) { |
| 69 | + return exec(cmd, { silent: true }).output; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Creates a release version tag and pushes to origin. |
| 74 | + * @param {string} type The type of release to do (patch, minor, major) |
| 75 | + * @returns {void} |
| 76 | + */ |
| 77 | +function release(type) { |
| 78 | + var newVersion; |
| 79 | + |
| 80 | + target.test(); |
| 81 | + newVersion = execSilent("npm version " + type).trim(); |
| 82 | + target.changelog(); |
| 83 | + |
| 84 | + // add changelog to commit |
| 85 | + exec("git add CHANGELOG.md"); |
| 86 | + exec("git commit --amend --no-edit"); |
| 87 | + |
| 88 | + // replace existing tag |
| 89 | + exec("git tag -f " + newVersion); |
| 90 | + |
| 91 | + // push all the things |
| 92 | + exec("git push origin master --tags"); |
| 93 | + exec("npm publish"); |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +/** |
| 98 | + * Splits a command result to separate lines. |
| 99 | + * @param {string} result The command result string. |
| 100 | + * @returns {array} The separated lines. |
| 101 | + */ |
| 102 | +function splitCommandResultToLines(result) { |
| 103 | + return result.trim().split("\n"); |
| 104 | +} |
| 105 | + |
| 106 | +function getVersionTags() { |
| 107 | + var tags = splitCommandResultToLines(exec("git tag", { silent: true }).output); |
| 108 | + |
| 109 | + return tags.reduce(function(list, tag) { |
| 110 | + if (semver.valid(tag)) { |
| 111 | + list.push(tag); |
| 112 | + } |
| 113 | + return list; |
| 114 | + }, []).sort(semver.compare); |
| 115 | +} |
| 116 | + |
| 117 | +//------------------------------------------------------------------------------ |
| 118 | +// Tasks |
| 119 | +//------------------------------------------------------------------------------ |
| 120 | + |
| 121 | +target.all = function() { |
| 122 | + target.test(); |
| 123 | +}; |
| 124 | + |
| 125 | +target.lint = function() { |
| 126 | + var errors = 0, |
| 127 | + lastReturn; |
| 128 | + |
| 129 | + echo("Validating Makefile.js"); |
| 130 | + lastReturn = nodeCLI.exec("eslint", MAKEFILE); |
| 131 | + if (lastReturn.code !== 0) { |
| 132 | + errors++; |
| 133 | + } |
| 134 | + |
| 135 | + echo("Validating JavaScript files"); |
| 136 | + lastReturn = nodeCLI.exec("eslint", JS_FILES); |
| 137 | + if (lastReturn.code !== 0) { |
| 138 | + errors++; |
| 139 | + } |
| 140 | + |
| 141 | + echo("Validating JavaScript test files"); |
| 142 | + lastReturn = nodeCLI.exec("eslint", TEST_FILES); |
| 143 | + if (lastReturn.code !== 0) { |
| 144 | + errors++; |
| 145 | + } |
| 146 | + |
| 147 | + if (errors) { |
| 148 | + exit(1); |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | +target.test = function() { |
| 153 | + // target.lint(); |
| 154 | + |
| 155 | + var errors = 0, |
| 156 | + lastReturn; |
| 157 | + |
| 158 | + lastReturn = nodeCLI.exec("istanbul", "cover", MOCHA, "-- -c", TEST_FILES); |
| 159 | + |
| 160 | + if (lastReturn.code !== 0) { |
| 161 | + errors++; |
| 162 | + } |
| 163 | + |
| 164 | + if (errors) { |
| 165 | + exit(1); |
| 166 | + } |
| 167 | + |
| 168 | + // target.checkLicenses(); |
| 169 | +}; |
| 170 | + |
| 171 | +target.docs = function() { |
| 172 | + echo("Generating documentation"); |
| 173 | + nodeCLI.exec("jsdoc", "-d jsdoc lib"); |
| 174 | + echo("Documentation has been output to /jsdoc"); |
| 175 | +}; |
| 176 | + |
| 177 | +target.browserify = function() { |
| 178 | + |
| 179 | + // 1. create temp and build directory |
| 180 | + if (!test("-d", TEMP_DIR)) { |
| 181 | + mkdir(TEMP_DIR); |
| 182 | + mkdir(TEMP_DIR + "/lib"); |
| 183 | + } |
| 184 | + |
| 185 | + if (!test("-d", BUILD_DIR)) { |
| 186 | + mkdir(BUILD_DIR); |
| 187 | + } |
| 188 | + |
| 189 | + // 2. copy files into temp directory |
| 190 | + cp("-r", "lib/*", TEMP_DIR + "/lib"); |
| 191 | + cp("espree.js", TEMP_DIR); |
| 192 | + cp("package.json", TEMP_DIR); |
| 193 | + |
| 194 | + |
| 195 | + // 3. browserify the temp directory |
| 196 | + nodeCLI.exec("browserify", TEMP_DIR + "espree.js", "-o", BUILD_DIR + "espree.js", "-s espree"); |
| 197 | + |
| 198 | + // 4. remove temp directory |
| 199 | + rm("-r", TEMP_DIR); |
| 200 | +}; |
| 201 | + |
| 202 | +target.changelog = function() { |
| 203 | + |
| 204 | + // get most recent two tags |
| 205 | + var tags = getVersionTags(), |
| 206 | + rangeTags = tags.slice(tags.length - 2), |
| 207 | + now = new Date(), |
| 208 | + timestamp = dateformat(now, "mmmm d, yyyy"); |
| 209 | + |
| 210 | + // output header |
| 211 | + (rangeTags[1] + " - " + timestamp + "\n").to("CHANGELOG.tmp"); |
| 212 | + |
| 213 | + // get log statements |
| 214 | + var logs = exec("git log --pretty=format:\"* %s (%an)\" " + rangeTags.join(".."), {silent: true}).output.split(/\n/g); |
| 215 | + logs = logs.filter(function(line) { |
| 216 | + return line.indexOf("Merge pull request") === -1 && line.indexOf("Merge branch") === -1; |
| 217 | + }); |
| 218 | + logs.push(""); // to create empty lines |
| 219 | + logs.unshift(""); |
| 220 | + |
| 221 | + // output log statements |
| 222 | + logs.join("\n").toEnd("CHANGELOG.tmp"); |
| 223 | + |
| 224 | + // switch-o change-o |
| 225 | + cat("CHANGELOG.tmp", "CHANGELOG.md").to("CHANGELOG.md.tmp"); |
| 226 | + rm("CHANGELOG.tmp"); |
| 227 | + rm("CHANGELOG.md"); |
| 228 | + mv("CHANGELOG.md.tmp", "CHANGELOG.md"); |
| 229 | +}; |
| 230 | + |
| 231 | +target.checkLicenses = function() { |
| 232 | + |
| 233 | + function isPermissible(dependency) { |
| 234 | + var licenses = dependency.licenses; |
| 235 | + |
| 236 | + if (Array.isArray(licenses)) { |
| 237 | + return licenses.some(function(license) { |
| 238 | + return isPermissible({ |
| 239 | + name: dependency.name, |
| 240 | + licenses: license |
| 241 | + }); |
| 242 | + }); |
| 243 | + } |
| 244 | + |
| 245 | + return OPEN_SOURCE_LICENSES.some(function(license) { |
| 246 | + return license.test(licenses); |
| 247 | + }); |
| 248 | + } |
| 249 | + |
| 250 | + echo("Validating licenses"); |
| 251 | + |
| 252 | + checker.init({ |
| 253 | + start: __dirname |
| 254 | + }, function(deps) { |
| 255 | + var impermissible = Object.keys(deps).map(function(dependency) { |
| 256 | + return { |
| 257 | + name: dependency, |
| 258 | + licenses: deps[dependency].licenses |
| 259 | + }; |
| 260 | + }).filter(function(dependency) { |
| 261 | + return !isPermissible(dependency); |
| 262 | + }); |
| 263 | + |
| 264 | + if (impermissible.length) { |
| 265 | + impermissible.forEach(function (dependency) { |
| 266 | + console.error("%s license for %s is impermissible.", |
| 267 | + dependency.licenses, |
| 268 | + dependency.name |
| 269 | + ); |
| 270 | + }); |
| 271 | + exit(1); |
| 272 | + } |
| 273 | + }); |
| 274 | +}; |
| 275 | + |
| 276 | +target.patch = function() { |
| 277 | + release("patch"); |
| 278 | +}; |
| 279 | + |
| 280 | +target.minor = function() { |
| 281 | + release("minor"); |
| 282 | +}; |
| 283 | + |
| 284 | +target.major = function() { |
| 285 | + release("major"); |
| 286 | +}; |
0 commit comments