envHash.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // - scan dirs
  2. // - stat items
  3. // - hash files
  4. // - stat dir items under
  5. // - hash files
  6. // - hash files
  7. const crypto = require('crypto');
  8. const fs = require('graceful-fs');
  9. const path = require('path');
  10. const pkgDir = require('pkg-dir');
  11. const promisify = require('./util/promisify');
  12. const readFile = promisify(fs.readFile);
  13. const readdir = promisify(fs.readdir);
  14. const stat = promisify(fs.stat);
  15. function hashFile(file) {
  16. return readFile(file)
  17. .then(src => [
  18. file,
  19. crypto
  20. .createHash('md5')
  21. .update(src)
  22. .digest('hex'),
  23. ])
  24. .catch(() => {});
  25. }
  26. function hashObject(obj) {
  27. const hash = crypto.createHash('md5');
  28. obj.forEach(item => {
  29. hash.update(item[0]);
  30. hash.update(item[1]);
  31. });
  32. return hash.digest('hex');
  33. }
  34. function hashFiles(root, files) {
  35. return Promise.all(files.map(file => hashFile(path.join(root, file)))).then(
  36. hashes => hashes.filter(Boolean),
  37. );
  38. }
  39. function flatten(items) {
  40. return (items || []).reduce(
  41. (carry, item) => (item ? carry.concat(item) : carry),
  42. [],
  43. );
  44. }
  45. const inputs = async ({ files, directories, root = process.cwd() } = {}) => {
  46. let defaults;
  47. if (!files && !directories) {
  48. const lockFiles = (await Promise.all(
  49. ['package-lock.json', 'yarn.lock'].map(f =>
  50. stat(path.join(root, f)).then(() => f, () => null),
  51. ),
  52. )).filter(Boolean);
  53. if (lockFiles.length) {
  54. return lockFiles;
  55. }
  56. }
  57. if (!files) {
  58. files = ['package.json'];
  59. }
  60. if (!directories) {
  61. directories = ['node_modules'];
  62. }
  63. directories = directories.map(d => `${d}/*`);
  64. return flatten([files, directories]);
  65. };
  66. module.exports = options => {
  67. options = options || {};
  68. const root = options.root || pkgDir.sync(process.cwd());
  69. let files = options.files;
  70. let directories = options.directories;
  71. let hashDefaults = Promise.resolve();
  72. if (!files && !directories) {
  73. hashDefaults = hashFiles(root, ['package-lock.json', 'yarn.lock']);
  74. }
  75. return hashDefaults
  76. .then(_defaults => {
  77. if (_defaults && _defaults.length > 0) {
  78. return [_defaults];
  79. } else {
  80. if (!files) {
  81. files = ['package.json'];
  82. }
  83. if (!directories) {
  84. directories = ['node_modules'];
  85. }
  86. }
  87. return Promise.all([
  88. hashFiles(root, files),
  89. Promise.all(
  90. directories.map(dir =>
  91. readdir(path.join(root, dir))
  92. .then(items =>
  93. Promise.all(
  94. items.map(item =>
  95. stat(path.join(root, dir, item))
  96. .then(stat => {
  97. if (stat.isDirectory()) {
  98. return hashFiles(path.join(root, dir, item), files);
  99. }
  100. if (stat.isFile()) {
  101. return hashFile(path.join(root, dir, item)).then(
  102. hash => (hash ? [hash] : hash),
  103. );
  104. }
  105. })
  106. .catch(function(...args) {
  107. console.error(args);
  108. }),
  109. ),
  110. ).then(hashes => hashes.filter(Boolean)),
  111. )
  112. .catch(() => {})
  113. .then(flatten),
  114. ),
  115. ).then(flatten),
  116. ]);
  117. })
  118. .then(flatten)
  119. .then(items => {
  120. items.forEach(item => {
  121. item[0] = path.relative(root, item[0]);
  122. });
  123. // console.log(items);
  124. items.sort((a, b) => {
  125. if (a[0] < b[0]) {
  126. return -1;
  127. } else if (a[0] > b[0]) {
  128. return 1;
  129. }
  130. return 0;
  131. });
  132. return hashObject(items);
  133. });
  134. };
  135. module.exports.inputs = inputs;