prepack.js 1.2 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 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. // Add **/* to ignore all files contained in the directories.
  19. .flatMap(pat => [pat, path.join(pat, '**/*')])
  20. // Remove the negation part. Makes micromatch usage more intuitive.
  21. .map(pat => pat.slice(1));
  22. const artifactsDir = 'build/contracts';
  23. let n = 0;
  24. for (const artifact of fs.readdirSync(artifactsDir)) {
  25. const fullArtifactPath = path.join(artifactsDir, artifact);
  26. const { sourcePath: fullSourcePath } = readJSON(fullArtifactPath);
  27. const sourcePath = path.relative('.', fullSourcePath);
  28. const ignore = match.any(sourcePath, ignorePatterns);
  29. if (ignore) {
  30. fs.unlinkSync(fullArtifactPath);
  31. n += 1;
  32. }
  33. }
  34. console.error(`Removed ${n} mock artifacts`);