Skip to content

Commit

Permalink
Log all errors together, use chalk
Browse files Browse the repository at this point in the history
  • Loading branch information
HaveAGitGat committed May 21, 2022
1 parent fc2b3f9 commit ca8eedf
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 61 deletions.
67 changes: 56 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "tdarr_plugins",
"version": "1.0.0",
"description": "There are two types of plugin:",
"main": "Tdarr_Plugin_aaaa_Pre_Proc_Example.js",
"dependencies": {},
"description": "Tdar Plugins Repo",
"main": "",
"dependencies": {
"chalk": "^4.1.2"
},
"devDependencies": {
"chai": "^4.3.6",
"eslint": "^7.14.0",
Expand Down
105 changes: 58 additions & 47 deletions tests/checkPlugins.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/* eslint no-console: 0 */ // --> OFF
/* eslint max-len: 0 */

const fs = require('fs');
const chalk = require('chalk');

const folders = [
'./Community',
'./examples',
];

let errorEncountered = false;

folders.forEach((folder) => {
const files = fs.readdirSync(folder).filter((row) => row.includes('.js'));

Expand All @@ -28,16 +32,16 @@ folders.forEach((folder) => {

const importLib = 'const lib = require(\'../methods/lib\')();';
if (!read.includes(importLib)) {
console.log(`Plugin error: '${folder}/${files[i]}' does not contain ${importLib}`);
console.log(chalk.red(`Plugin error: '${folder}/${files[i]}' does not contain ${importLib}`));
read = `${importLib}\n${read}`;
// fs.writeFileSync(`${folder}/${files[i]}`, read)
process.exit(1);
errorEncountered = true;
}

const detailsText = 'const details = () =>';
if (!read.includes(detailsText)) {
console.log(`Plugin error: '${folder}/${files[i]}' does not contain ${detailsText}`);
process.exit(1);
console.log(chalk.red(`Plugin error: '${folder}/${files[i]}' does not contain ${detailsText}`));
errorEncountered = true;
}

const syncText = 'const plugin = (file, librarySettings, inputs, otherArguments) => {';
Expand All @@ -46,27 +50,27 @@ folders.forEach((folder) => {
if (!read.includes(syncText)
&& !read.includes(asyncText)
) {
console.log(`Plugin error: '${folder}/${files[i]}' does not contain ${syncText} or ${asyncText}`);
process.exit(1);
console.log(chalk.red(`Plugin error: '${folder}/${files[i]}' does not contain ${syncText} or ${asyncText}`));
errorEncountered = true;
}

const inputsText = 'inputs = lib.loadDefaultValues(inputs, details);';
if (!read.includes(inputsText)
) {
console.log(`Plugin error: '${folder}/${files[i]}' does not contain ${inputsText}`);
process.exit(1);
console.log(chalk.red(`Plugin error: '${folder}/${files[i]}' does not contain ${inputsText}`));
errorEncountered = true;
}

const exportText = `module.exports.details = details;
module.exports.plugin = plugin;`;

if (!read.includes(exportText)) {
console.log(`Plugin error: '${folder}/${files[i]}' does not contain ${exportText}`);
console.log(chalk.red(`Plugin error: '${folder}/${files[i]}' does not contain ${exportText}`));
read = read.replace('module.exports.details = details;', '');
read = read.replace('module.exports.plugin = plugin;', '');
read += `\n${exportText}`;
// fs.writeFileSync(`${folder}/${files[i]}`, read)
process.exit(1);
errorEncountered = true;
}

// check deps are within functions
Expand All @@ -80,8 +84,8 @@ module.exports.plugin = plugin;`;
const countOpen = allBefore.join(keyWord).split('{').length - 1;
const countClose = allBefore.join(keyWord).split('}').length - 1;
if (countOpen === countClose) {
console.log(`Plugin has requires outside of function '${folder}/${files[i]}'`);
process.exit(1);
console.log(chalk.red(`Plugin has requires outside of function '${folder}/${files[i]}'`));
errorEncountered = true;
}
}
}
Expand All @@ -91,64 +95,66 @@ module.exports.plugin = plugin;`;
// eslint-disable-next-line import/no-dynamic-require,global-require
pluginDetails = require(`.${folder}/${files[i]}`).details();
} catch (err) {
console.log(err.message);
process.exit(1);
console.log(chalk.red(err.message));
errorEncountered = true;
}

const detailsKeys = Object.keys(pluginDetails);

// eslint-disable-next-line no-loop-func
detailsOrder.forEach((detail) => {
if (detailsKeys.indexOf(detail) === -1) {
console.log(`Plugin details is missing '${folder}/${files[i]}' : ${detail}`);
process.exit(1);
console.log(chalk.red(`Plugin details is missing '${folder}/${files[i]}' : ${detail}`));
errorEncountered = true;
}
});

// eslint-disable-next-line no-loop-func
detailsKeys.forEach((detail, index) => {
if (detailsOrder[index] !== detail) {
console.log(`Plugin details keys are not in the correct order: '${folder}/${files[i]}' ${detail}`);
process.exit(1);
console.log(chalk.red(`Plugin details keys are not in the correct order: '${folder}/${files[i]}' ${detail}`));
errorEncountered = true;
}
});

if (detailsKeys.length < detailsOrder.length) {
console.log(`Plugin details are too few '${folder}/${files[i]}'`);
process.exit(1);
console.log(chalk.red(`Plugin details are too few '${folder}/${files[i]}'`));
errorEncountered = true;
}

if (!['Pre-processing', 'Post-processing'].includes(pluginDetails.Stage)) {
console.log(`Plugin does not have a valid Type'${folder}/${files[i]}'`);
process.exit(1);
console.log(chalk.red(`Plugin does not have a valid Type'${folder}/${files[i]}'`));
errorEncountered = true;
}

if (!['Video', 'Audio', 'Subtitle', 'Any'].includes(pluginDetails.Type)) {
console.log(`Plugin does not have a valid Type'${folder}/${files[i]}'`);
process.exit(1);
console.log(chalk.red(`Plugin does not have a valid Type'${folder}/${files[i]}'`));
errorEncountered = true;
}

if (files[i].split('.js').join('') !== pluginDetails.id) {
console.log(`Plugin file name does not match details id'${folder}/${files[i]}'`);
process.exit(1);
console.log(chalk.red(`Plugin file name does not match details id'${folder}/${files[i]}'`));
errorEncountered = true;
}

if (!['Transcode', 'Filter'].includes(pluginDetails.Operation)) {
console.log(`Plugin does not have a valid Operation '${folder}/${files[i]}'`);
process.exit(1);
console.log(chalk.red(`Plugin does not have a valid Operation '${folder}/${files[i]}'`));
errorEncountered = true;
} else if (detailsKeys.length > detailsOrder.length) {
console.log(`Plugin details are too many '${folder}/${files[i]}'`);
process.exit(1);
console.log(chalk.red(`Plugin details are too many '${folder}/${files[i]}'`));
errorEncountered = true;
} else if (pluginDetails.Inputs && !Array.isArray(pluginDetails.Inputs)) {
// Check default values are set;
console.log(`Plugin Inputs is not an array: ${files[i]}`);
process.exit(1);
console.log(chalk.red(`Plugin Inputs is not an array: ${files[i]}`));
errorEncountered = true;
} else if (pluginDetails.Inputs && Array.isArray(pluginDetails.Inputs)) {
const inputs = pluginDetails.Inputs;
const savedInputs = {};
for (let j = 0; j < inputs.length; j += 1) {
// Prevent duplicate plugin inputs
if (savedInputs[inputs[j].name] === true) {
console.log(`Plugin Input already exists: '${folder}/${files[i]}' : ${inputs[j].name}`);
process.exit(1);
console.log(chalk.red(`Plugin Input already exists: '${folder}/${files[i]}' : ${inputs[j].name}`));
errorEncountered = true;
} else {
savedInputs[inputs[j].name] = true;
}
Expand All @@ -161,31 +167,31 @@ module.exports.plugin = plugin;`;
|| inputKeys[3] !== 'inputUI'
|| inputKeys[4] !== 'tooltip'
) {
console.log(`Plugin Input keys are not in correct order: '${folder}/${files[i]}' : ${inputs[j].name}`);
process.exit(1);
console.log(chalk.red(`Plugin Input keys are not in correct order: '${folder}/${files[i]}' : ${inputs[j].name}`));
errorEncountered = true;
} else if (inputs[j].type === undefined || !pluginInputTypes.includes(inputs[j].type)) {
console.log(`Plugin Input does not have a type: '${folder}/${files[i]}' : ${inputs[j].name}`);
process.exit(1);
console.log(chalk.red(`Plugin Input does not have a type: '${folder}/${files[i]}' : ${inputs[j].name}`));
errorEncountered = true;
} else if (
(inputs[j].type === 'string' && typeof inputs[j].defaultValue !== 'string')
|| (inputs[j].type === 'number' && typeof inputs[j].defaultValue !== 'number')
|| (inputs[j].type === 'boolean' && typeof inputs[j].defaultValue !== 'boolean')
) {
console.log(`Plugin Input type does not match defaultValue type:
'${folder}/${files[i]}' : ${inputs[j].name}`);
process.exit(1);
console.log(chalk.red(`Plugin Input type does not match defaultValue type:
'${folder}/${files[i]}' : ${inputs[j].name}`));
errorEncountered = true;
} else if (!['text', 'dropdown'].includes(inputs[j].inputUI.type)) {
console.log(`Plugin Input inputUI is invalid: '${folder}/${files[i]}' : ${inputs[j].name}`);
process.exit(1);
console.log(chalk.red(`Plugin Input inputUI is invalid: '${folder}/${files[i]}' : ${inputs[j].name}`));
errorEncountered = true;
} else if (inputs[j].defaultValue === undefined) {
console.log(`Plugin Input does not have a default value: '${folder}/${files[i]}' : ${inputs[j].name}`);
process.exit(1);
console.log(chalk.red(`Plugin Input does not have a default value: '${folder}/${files[i]}' : ${inputs[j].name}`));
errorEncountered = true;
}

const count = read.split(inputs[j].name).length - 1;
if (count === 1) {
console.log(`Plugin Input is not used: '${folder}/${files[i]}' : ${inputs[j].name}`);
process.exit(1);
console.log(chalk.red(`Plugin Input is not used: '${folder}/${files[i]}' : ${inputs[j].name}`));
errorEncountered = true;
}
}
}
Expand All @@ -195,3 +201,8 @@ module.exports.plugin = plugin;`;
});

console.log('Done!');

if (errorEncountered) {
console.log('Errors encountered');
process.exit(1);
}

0 comments on commit ca8eedf

Please sign in to comment.