create-test-data.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const fs = require('fs');
  2. const path = require('path');
  3. const baseDir = path.join(__dirname, '..');
  4. const formatDir = path.join(baseDir, 'test', 'fixtures', 'formats');
  5. const parsingDir = path.join(baseDir, 'test', 'fixtures', 'parsing');
  6. const getFiles = (dir) => (fs.readdirSync(dir) || []).reduce((acc, d) => {
  7. d = path.resolve(dir, d);
  8. const stat = fs.statSync(d);
  9. if (!stat.isDirectory()) {
  10. return acc;
  11. }
  12. const subfiles = fs.readdirSync(d).map((f) => path.resolve(d, f));
  13. return acc.concat(subfiles);
  14. }, []);
  15. const buildDataString = function(files, id) {
  16. const data = {};
  17. files.forEach((file) => {
  18. // read the file directly as a buffer before converting to base64
  19. const base64 = fs.readFileSync(file).toString('base64');
  20. data[path.basename(file)] = base64;
  21. });
  22. const dataExportStrings = Object.keys(data).reduce((acc, key) => {
  23. // use a function since the segment may be cleared out on usage
  24. acc.push(`${id}Files['${key}'] = () => {
  25. cache['${key}'] = cache['${key}'] || base64ToUint8Array('${data[key]}');
  26. const dest = new Uint8Array(cache['${key}'].byteLength);
  27. dest.set(cache['${key}']);
  28. return dest;
  29. };`);
  30. return acc;
  31. }, []);
  32. const file =
  33. '/* istanbul ignore file */\n' +
  34. '\n' +
  35. `import base64ToUint8Array from "${path.resolve(baseDir, 'src/decode-b64-to-uint8-array.js')}";\n` +
  36. 'const cache = {};\n' +
  37. `const ${id}Files = {};\n` +
  38. dataExportStrings.join('\n') +
  39. `export default ${id}Files`;
  40. return file;
  41. };
  42. /* we refer to them as .js, so that babel and other plugins can work on them */
  43. const formatsKey = 'create-test-data!formats.js';
  44. const parsingKey = 'create-test-data!parsing.js';
  45. module.exports = function() {
  46. return {
  47. name: 'createTestData',
  48. buildStart() {
  49. this.addWatchFile(formatDir);
  50. this.addWatchFile(parsingDir);
  51. getFiles(formatDir).forEach((file) => this.addWatchFile(file));
  52. getFiles(parsingDir).forEach((file) => this.addWatchFile(file));
  53. },
  54. resolveId(importee, importer) {
  55. // if this is not an id we can resolve return
  56. if (importee.indexOf('create-test-data!') !== 0) {
  57. return;
  58. }
  59. const name = importee.split('!')[1];
  60. if (name.indexOf('formats') !== -1) {
  61. return formatsKey;
  62. }
  63. if (name.indexOf('parsing') !== -1) {
  64. return parsingKey;
  65. }
  66. return null;
  67. },
  68. load(id) {
  69. if (id === formatsKey) {
  70. return buildDataString.call(this, getFiles(formatDir), 'format');
  71. }
  72. if (id === parsingKey) {
  73. return buildDataString.call(this, getFiles(parsingDir), 'parsing');
  74. }
  75. }
  76. };
  77. };