kodilist
Version:
CLI tool for generating Kodi playlists based on filename-embedded tags.
170 lines (169 loc) • 7.84 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const recursive_readdir_1 = __importDefault(require("recursive-readdir"));
const fs_1 = __importDefault(require("fs"));
const _1 = require(".");
/**
* Produces an OperationSet object that can be used to easily match the command
* line input to the desired function. The first term passed to kodilist selects
* a top-level key in the OperationSet. If the first term matched a function,
* all further terms become its arguments; if the first term matched an object,
* the second term selects a function and terms 3+ become its arguments.
*
* For example, `$ kodilist add source bbc.co.uk` matches
* `['add']['source']['bbc.co.uk']()`.
*
* Arguments do not need to be directly passed to the matched function; the
* full string is passed in when `Operations()` is invoked and each function
* selects the portion it needs.
*/
function Operations(config, paramSet) {
const param = paramSet
.slice(2)
.join(" ")
.toLowerCase();
return {
generate: function () {
return __awaiter(this, void 0, void 0, function* () {
const files = [];
for (let dirName of config.directories) {
const currentDirFiles = yield recursive_readdir_1.default(dirName, config.ignoreList);
currentDirFiles.forEach(filename => files.push({
filename,
simpleName: _1.simplifyName(filename),
mtime: fs_1.default.statSync(filename).mtimeMs
}));
}
files.sort((a, b) => (a.simpleName < b.simpleName ? -1 : 1));
_1.writePlaylistsSearchTerms(config, files);
_1.writePlaylistsNew(config, files);
_1.writePlaylistsAlphabet(config, files);
});
},
list: {
people: () => config.searchTerms.people.forEach(s => console.log(s)),
directories: () => config.directories.forEach(s => console.log(s)),
tags: () => config.searchTerms.tags.forEach(s => console.log(s)),
sources: () => config.searchTerms.sources.forEach(s => console.log(s)),
ignores: () => config.ignoreList.forEach(s => console.log(s))
},
delete: {
person: function () {
const index = config.searchTerms.people.indexOf(param);
if (index === -1) {
console.log(`${chalk_1.default.red(param)} wasn't in list of people`);
return;
}
config.searchTerms.people.splice(index, 1);
config.save();
console.log(`Stopped tracking ${chalk_1.default.green(param)}`);
},
directory: function () {
const index = config.directories.indexOf(param);
if (index === -1) {
console.log(`${chalk_1.default.red(param)} wasn't in list of directories`);
return;
}
config.directories.splice(index, 1);
config.save();
console.log(`Stopped tracking ${chalk_1.default.green(param)}`);
},
tag: function () {
const index = config.searchTerms.tags.indexOf(param);
if (index === -1) {
console.log(`${chalk_1.default.red(param)} wasn't in list of tags`);
return;
}
config.searchTerms.tags.splice(index, 1);
config.save();
console.log(`Stopped tracking ${chalk_1.default.green(param)}`);
},
source: function () {
const index = config.searchTerms.sources.indexOf(param);
if (index === -1) {
console.log(`${chalk_1.default.red(param)} wasn't in list of sources`);
return;
}
config.searchTerms.sources.splice(index, 1);
config.save();
console.log(`Stopped tracking ${chalk_1.default.green(param)}`);
},
ignore: function () {
const index = config.ignoreList.indexOf(param);
if (index === -1) {
console.log(`${chalk_1.default.red(param)} wasn't on ignore list`);
return;
}
config.ignoreList.splice(index, 1);
config.save();
console.log(`Stopped ignoring ${chalk_1.default.green(param)}`);
}
},
ignore: function () {
const toIgnore = paramSet.slice(1).join(" ");
if (config.ignoreList.includes(toIgnore)) {
console.log(`Already ignoring ${chalk_1.default.blue(toIgnore)}`);
}
else {
config.ignoreList.push(toIgnore);
console.log(`Now ignoring ${chalk_1.default.green(toIgnore)}`);
}
config.save();
},
add: {
person: function () {
if (config.searchTerms.people.includes(param)) {
console.log("Already tracking", chalk_1.default.blue(param));
}
else {
config.searchTerms.people.push(param);
config.save();
console.log("Now tracking", chalk_1.default.blue(param));
}
},
tag: function () {
if (config.searchTerms.tags.includes(param)) {
console.log("Already tracking", chalk_1.default.blue(param));
}
else {
config.searchTerms.tags.push(param);
config.save();
console.log("Now tracking", chalk_1.default.blue(param));
}
},
source: function () {
if (config.searchTerms.sources.includes(param)) {
console.log("Already tracking", chalk_1.default.blue(param));
}
else {
config.searchTerms.sources.push(param);
config.save();
console.log("Now tracking", chalk_1.default.blue(param));
}
},
directory: function () {
if (config.directories.includes(paramSet[2])) {
console.log("Already tracking", chalk_1.default.blue(paramSet[2]));
}
else {
config.directories.push(paramSet[2]);
config.save();
console.log("Now tracking", chalk_1.default.blue(paramSet[2]));
}
}
}
};
}
exports.default = Operations;