Blame view

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