prepack.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. 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, ignorePatterns);
  28. if (ignore) {
  29. fs.unlinkSync(fullArtifactPath);
  30. }
  31. }