prepare.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 cp = require('child_process');
  6. const match = require('micromatch');
  7. function readJSON (path) {
  8. return JSON.parse(fs.readFileSync(path));
  9. }
  10. cp.spawnSync('npm', ['run', 'compile'], { stdio: 'inherit' });
  11. const pkgFiles = readJSON('package.json').files;
  12. // Get only negated patterns.
  13. const ignorePatterns = pkgFiles
  14. .filter(pat => pat.startsWith('!'))
  15. // Remove the negation part. Makes micromatch usage more intuitive.
  16. .map(pat => pat.slice(1));
  17. const ignorePatternsSubtrees = ignorePatterns
  18. // Add **/* to ignore all files contained in the directories.
  19. .concat(ignorePatterns.map(pat => path.join(pat, '**/*')));
  20. const artifactsDir = 'build/contracts';
  21. let n = 0;
  22. for (const artifact of fs.readdirSync(artifactsDir)) {
  23. const fullArtifactPath = path.join(artifactsDir, artifact);
  24. const { sourcePath: fullSourcePath } = readJSON(fullArtifactPath);
  25. const sourcePath = path.relative('.', fullSourcePath);
  26. const ignore = match.any(sourcePath, ignorePatternsSubtrees);
  27. if (ignore) {
  28. fs.unlinkSync(fullArtifactPath);
  29. n += 1;
  30. }
  31. }
  32. console.error(`Removed ${n} mock artifacts`);