You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Tdarr_Plugins/FlowPluginsTs/CommunityFlowPlugins/file/replaceOriginalFile/1.0.0/index.ts

78 lines
1.9 KiB

import {
IpluginDetails,
IpluginInputArgs,
IpluginOutputArgs,
} from '../../../../FlowHelpers/1.0.0/interfaces/interfaces';
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
const details = ():IpluginDetails => ({
name: 'Replace Original File',
description: 'Replace the original file',
style: {
borderColor: 'green',
},
tags: '',
isStartPlugin: false,
sidebarPosition: -1,
icon: 'faArrowRight',
inputs: [],
outputs: [
{
number: 1,
tooltip: 'Continue to next plugin',
},
],
});
const getNewPath = (originalPath:string, tempPath:string) => {
const tempPathParts = tempPath.split('.');
const container = tempPathParts[tempPathParts.length - 1];
const originalPathParts = originalPath.split('.');
originalPathParts[originalPathParts.length - 1] = container;
return originalPathParts.join('.');
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const plugin = async (args:IpluginInputArgs):Promise<IpluginOutputArgs> => {
const fs = require('fs');
const lib = require('../../../../../methods/lib')();
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
args.inputs = lib.loadDefaultValues(args.inputs, details);
const currentPath = args.inputFileObj._id;
const newPath = getNewPath(args.originalLibraryFile._id, currentPath);
const newPathTmp = `${newPath}.tmp`;
args.jobLog(JSON.stringify({
currentPath,
newPath,
newPathTmp,
}));
await new Promise((resolve) => setTimeout(resolve, 2000));
fs.renameSync(currentPath, newPathTmp);
if (fs.existsSync(newPath)) {
fs.unlinkSync(newPath);
}
await new Promise((resolve) => setTimeout(resolve, 2000));
fs.renameSync(newPathTmp, newPath);
return {
outputFileObj: {
_id: newPath,
},
outputNumber: 1,
variables: args.variables,
};
};
export {
details,
plugin,
};