From a34cac4aca088e50010ea12465cdf40bb26bd901 Mon Sep 17 00:00:00 2001 From: Trezy Date: Sun, 16 Dec 2018 21:12:04 -0600 Subject: [PATCH] build: Make build versions dynamic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hehehehe, this is really cool. I've removed the hardcoded version number from `nes.scss` and replaced it with a custom function. This function does all sorts of cool stuff. 1. **Injects the framework version from `package.json` This prevents us from having to inject the version number manually. 2. **Adds build info…** The branch that the CSS was built from, when it was built, what version of Node was used… All of the useful information that we could possibly provide. If it’s built on CircleCI, it even includes the build number. 3. **…but not too much build info** If the build is running against the `master` branch, we only inject the framework version. No need to bloat production builds with all the other junk. 😁 --- package.json | 3 ++- scss/functions.js | 28 ++++++++++++++++++++++++++++ scss/nes.scss | 6 +++--- 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 scss/functions.js diff --git a/package.json b/package.json index 65c7598..a062190 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "stylelint": "stylelint scss/**/*.scss", "build:stylelint": "npm run stylelint -- --fix", "build:clean": "rimraf css", - "build:sass": "node-sass --output-style expanded --source-map true scss/nes.scss css/nes.css", + "build:sass": "node-sass --output-style expanded --source-map true --functions scss/functions.js scss/nes.scss css/nes.css", "build:autoprefix": "postcss --use autoprefixer --map false --output css/nes.css css/nes.css", "build:cleancss": "cleancss -o css/nes.min.css css/nes.css", "storybook": "start-storybook -p 6006", @@ -50,6 +50,7 @@ "eslint-config-airbnb-base": "^13.1.0", "eslint-plugin-import": "^2.14.0", "file-loader": "^2.0.0", + "git-rev-sync": "^1.12.0", "husky": "^1.0.0", "lint-staged": "^7.3.0", "node-sass": "^4.9.3", diff --git a/scss/functions.js b/scss/functions.js new file mode 100644 index 0000000..468d277 --- /dev/null +++ b/scss/functions.js @@ -0,0 +1,28 @@ +const git = require('git-rev-sync'); /* eslint-disable-line import/no-extraneous-dependencies */ +const { types } = require('node-sass'); /* eslint-disable-line import/no-extraneous-dependencies */ + +module.exports = { + 'version()': () => { + const packageData = require('../package.json'); /* eslint-disable-line global-require */ + + let buildData = ` + NES.css Framework + Version: ${(git.branch() === 'develop') ? 'development' : `v${packageData.version}`} + `; + + if (git.branch() !== 'master') { + buildData += ` + Build Date: ${(new Date()).toISOString()} + Node Version: ${process.version} + Branch: ${git.branch()} + Commit: ${git.long()}`; + } + + if (process.env.CIRCLECI) { + buildData += ` + Build Number (CircleCI): ${process.env.CIRCLE_BUILD_NUM}`; + } + + return types.String(buildData.replace(/\n/, '').replace(/^ +/gm, ' ')); + }, +}; diff --git a/scss/nes.scss b/scss/nes.scss index 0cb94c0..f1936ab 100644 --- a/scss/nes.scss +++ b/scss/nes.scss @@ -1,8 +1,8 @@ @charset "utf-8"; -/*! -* NES.css v0.0.2(alpha) -*/ +/*****************************************************************************\ +#{version()} +\*****************************************************************************/ @import "base/_index.scss"; @import "utilities/_index.scss";