diff --git a/package.json b/package.json index a4de28e5..a387fd89 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ "husky": "0.14.3", "image-webpack-loader": "3.4.2", "jest": "22.1.1", + "klaw-sync": "3.0.2", "less": "2.7.3", "less-loader": "4.0.5", "lint-staged": "6.0.0", @@ -130,6 +131,7 @@ "db": "nodemon ./db", "build": "rimraf dist && webpack --config webpack_config/webpack.prod.js", "prebuild": "check-node-version --package", + "postbuild": "node ./utils/postBuild.js", "build:downloadable": "BUILD_DOWNLOADABLE=true rimraf dist && webpack --config webpack_config/webpack.prod.js", "prebuild:demo": "check-node-version --package", "test:coverage": "jest --config=jest_config/jest.config.json --coverage", diff --git a/utils/postBuild.js b/utils/postBuild.js new file mode 100644 index 00000000..d79d5a6b --- /dev/null +++ b/utils/postBuild.js @@ -0,0 +1,46 @@ + +/** + * (1) Parses the '.cache' file in the 'dist/icons' folder + * (2) Sorts the 'cache.result.files' property + * (3) Rewrites the file to ensure a deterministic build + */ + + +const fs = require('fs') +const path = require('path') +const klawSync = require('klaw-sync') + +const DIST_PATH = path.resolve('./dist/') +const CACHE_FILE_REGEX = /.*icons-[a-z0-9]*\/\.cache$/ + +const findCacheFile = item => CACHE_FILE_REGEX.test(item.path) + +console.log('postBuild start') + +try { + const cacheFilePaths = klawSync(DIST_PATH, { filter: findCacheFile }) + + if (!cacheFilePaths.length) { + throw new Error('Could not find .cache file') + } + + if (cacheFilePaths.length > 1) { + throw new Error('More than one possible .cache file detected') + } + + const cacheFilePath = cacheFilePaths[0].path + const rawCacheFile = fs.readFileSync(cacheFilePath, 'utf8') + const cache = JSON.parse(rawCacheFile) + + cache.result.files = cache.result.files.sort() + + fs.writeFileSync(cacheFilePath, JSON.stringify(cache), 'utf8') + +} catch(err) { + console.log('postBuild fail', err) + process.exit(1) +} + +console.log('postBuild finish') + +