export-m3u8s.js 2.2 KB

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