iptv/scripts/helpers/file.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-08-01 19:42:04 +02:00
const markdownInclude = require('markdown-include')
2021-08-01 18:45:26 +02:00
const path = require('path')
2021-09-08 19:46:19 +02:00
const glob = require('glob')
2021-08-01 19:42:04 +02:00
const fs = require('fs')
2021-08-01 18:45:26 +02:00
const rootPath = path.resolve(__dirname) + '/../../'
const file = {}
2021-09-08 19:46:19 +02:00
file.list = function (include = [], exclude = []) {
return new Promise(resolve => {
2021-09-08 23:43:01 +02:00
glob('channels/**/*.m3u', function (err, files) {
2021-09-08 19:46:19 +02:00
if (include.length) {
include = include.map(filename => `channels/${filename}.m3u`)
files = files.filter(filename => include.includes(filename))
}
if (exclude.length) {
exclude = exclude.map(filename => `channels/${filename}.m3u`)
files = files.filter(filename => !exclude.includes(filename))
}
resolve(files)
})
})
}
2021-08-01 18:45:26 +02:00
file.getFilename = function (filename) {
return path.parse(filename).name
}
file.createDir = function (dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
}
file.read = function (filename) {
return fs.readFileSync(rootPath + filename, { encoding: 'utf8' })
}
file.append = function (filename, data) {
fs.appendFileSync(rootPath + filename, data)
}
file.create = function (filename, data = '') {
fs.writeFileSync(rootPath + filename, data)
}
2021-08-01 19:42:04 +02:00
file.compileMarkdown = function (filename) {
markdownInclude.compileFiles(rootPath + filename)
2021-08-01 18:45:26 +02:00
}
module.exports = file