generate-error-constants.js 793 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env node
  2. var path = require('path');
  3. var script = path.basename(__filename);
  4. var errorFile = process.argv[2];
  5. if (!errorFile) {
  6. console.error('Usage: ./' + script + ' path/to/errmsg-utf8.txt');
  7. process.exit(1);
  8. }
  9. var fs = require('fs');
  10. var errors = fs.readFileSync(errorFile, 'utf-8');
  11. var number = Number(errors.match(/start-error-number (\d+)/)[1]);
  12. var codes = errors.match(/^([A-Z_]+)/mg)
  13. var source =
  14. '// Generated by ' + script + ', do not modify by hand\n' +
  15. codes
  16. .map(function(code) {
  17. return 'exports[' + (number++) + '] = \'' + code + '\';';
  18. })
  19. .join('\n');
  20. var targetFile = path.join(__dirname, '../lib/protocol/constants/errors.js');
  21. fs.writeFileSync(targetFile, source, 'utf-8');
  22. console.log('Wrote constants to ' + targetFile);