You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.2 KiB

  1. var fs = require('fs');
  2. var path = require('path');
  3. var basePath = path.resolve(__dirname, '..');
  4. var testDataDir = path.join(basePath,'test');
  5. var manifestDir = path.join(basePath, 'utils', 'manifest');
  6. var manifestFilepath = path.join(testDataDir, 'test-manifests.js');
  7. var expectedFilepath = path.join(testDataDir, 'test-expected.js');
  8. var build = function() {
  9. var manifests = 'export default {\n';
  10. var expected = 'export default {\n';
  11. var files = fs.readdirSync(manifestDir);
  12. while (files.length > 0) {
  13. var file = path.resolve(manifestDir, files.shift());
  14. var extname = path.extname(file);
  15. if (extname === '.m3u8') {
  16. // translate this manifest
  17. manifests += ' \'' + path.basename(file, '.m3u8') + '\': ';
  18. manifests += fs.readFileSync(file, 'utf8')
  19. .split(/\r\n|\n/)
  20. // quote and concatenate
  21. .map(function(line) {
  22. return ' \'' + line + '\\n\' +\n';
  23. }).join('')
  24. // strip leading spaces and the trailing '+'
  25. .slice(4, -3);
  26. manifests += ',\n';
  27. } else if (extname === '.js') {
  28. // append the expected parse
  29. expected += ' "' + path.basename(file, '.js') + '": ';
  30. expected += fs.readFileSync(file, 'utf8');
  31. expected += ',\n';
  32. } else {
  33. console.log('Unknown file ' + file + ' found in manifest dir ' + manifestDir);
  34. }
  35. }
  36. // clean up and close the objects
  37. manifests = manifests.slice(0, -2);
  38. manifests += '\n};\n';
  39. expected = expected.slice(0, -2);
  40. expected += '\n};\n';
  41. fs.writeFileSync(manifestFilepath, manifests);
  42. fs.writeFileSync(expectedFilepath, expected);
  43. console.log('Wrote test data file ' + manifestFilepath);
  44. console.log('Wrote test data file ' + expectedFilepath);
  45. };
  46. var watch = function() {
  47. build();
  48. fs.watch(manifestDir, function(event, filename) {
  49. console.log('files in manifest dir were changed rebuilding manifest data');
  50. build();
  51. });
  52. };
  53. var clean = function() {
  54. try {
  55. fs.unlinkSync(manifestFilepath);
  56. } catch(e) {
  57. console.log(e);
  58. }
  59. try {
  60. fs.unlinkSync(expectedFilepath);
  61. } catch(e) {
  62. console.log(e);
  63. }
  64. }
  65. module.exports = {
  66. build: build,
  67. watch: watch,
  68. clean: clean
  69. };