prepare.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. .map(p => p.replace(/^\//, ''));
  21. const artifactsDir = 'build/contracts';
  22. let n = 0;
  23. for (const artifact of fs.readdirSync(artifactsDir)) {
  24. const fullArtifactPath = path.join(artifactsDir, artifact);
  25. const { sourcePath: fullSourcePath } = readJSON(fullArtifactPath);
  26. const sourcePath = path.relative('.', fullSourcePath);
  27. const ignore = match.any(sourcePath, ignorePatternsSubtrees);
  28. if (ignore) {
  29. fs.unlinkSync(fullArtifactPath);
  30. n += 1;
  31. }
  32. }
  33. console.error(`Removed ${n} mock artifacts`);