run.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const format = require('./format-lines');
  4. function getVersion (path) {
  5. try {
  6. return fs
  7. .readFileSync(path, 'utf8')
  8. .match(/\/\/ OpenZeppelin Contracts \(last updated v[^)]+\)/)[0];
  9. } catch (err) {
  10. return null;
  11. }
  12. }
  13. for (const [ file, template ] of Object.entries({
  14. // SafeCast
  15. 'utils/math/SafeCast.sol': './templates/SafeCast',
  16. 'mocks/SafeCastMock.sol': './templates/SafeCastMock',
  17. // EnumerableSet
  18. 'utils/structs/EnumerableSet.sol': './templates/EnumerableSet',
  19. 'mocks/EnumerableSetMock.sol': './templates/EnumerableSetMock',
  20. // EnumerableMap
  21. 'utils/structs/EnumerableMap.sol': './templates/EnumerableMap',
  22. 'mocks/EnumerableMapMock.sol': './templates/EnumerableMapMock',
  23. })) {
  24. const path = `./contracts/${file}`;
  25. const version = getVersion(path);
  26. const content = format(
  27. '// SPDX-License-Identifier: MIT',
  28. (version ? version + ` (${file})\n` : ''),
  29. require(template).trimEnd(),
  30. );
  31. fs.writeFileSync(path, content);
  32. }