remove-ignored-artifacts.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env node
  2. // This script removes the build artifacts of ignored contracts.
  3. const fs = require('fs');
  4. const path = require('path');
  5. const match = require('micromatch');
  6. function readJSON (path) {
  7. return JSON.parse(fs.readFileSync(path));
  8. }
  9. const pkgFiles = readJSON('package.json').files;
  10. // Get only negated patterns.
  11. const ignorePatterns = pkgFiles
  12. .filter(pat => pat.startsWith('!'))
  13. // Remove the negation part. Makes micromatch usage more intuitive.
  14. .map(pat => pat.slice(1));
  15. const ignorePatternsSubtrees = ignorePatterns
  16. // Add **/* to ignore all files contained in the directories.
  17. .concat(ignorePatterns.map(pat => path.join(pat, '**/*')))
  18. .map(p => p.replace(/^\//, ''));
  19. const solcOutput = readJSON('cache/solc-output.json');
  20. const artifactsDir = 'build/contracts';
  21. let n = 0;
  22. for (const sourcePath in solcOutput.contracts) {
  23. const ignore = match.any(sourcePath, ignorePatternsSubtrees);
  24. if (ignore) {
  25. for (const contract in solcOutput.contracts[sourcePath]) {
  26. fs.unlinkSync(path.join(artifactsDir, contract + '.json'));
  27. n += 1;
  28. }
  29. }
  30. }
  31. console.error(`Removed ${n} mock artifacts`);