remove-ignored-artifacts.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 buildinfo = 'artifacts/build-info';
  20. const filenames = fs.readdirSync(buildinfo);
  21. if (filenames.length !== 1) {
  22. throw new Error(`There should only be one file in ${buildinfo}`);
  23. }
  24. const solcOutput = readJSON(path.join(buildinfo, filenames[0])).output;
  25. const artifactsDir = 'build/contracts';
  26. let n = 0;
  27. for (const sourcePath in solcOutput.contracts) {
  28. const ignore = match.any(sourcePath, ignorePatternsSubtrees);
  29. if (ignore) {
  30. for (const contract in solcOutput.contracts[sourcePath]) {
  31. fs.unlinkSync(path.join(artifactsDir, contract + '.json'));
  32. n += 1;
  33. }
  34. }
  35. }
  36. console.error(`Removed ${n} mock artifacts`);