prepare.js 1.3 KB

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