Skip to content

Commit

Permalink
Feat: fileName replace variable (#2803)
Browse files Browse the repository at this point in the history
* Created `fileName` replace variable
  • Loading branch information
Oceanity authored Sep 23, 2024
1 parent b0f6769 commit d78fe8d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/backend/variables/builtin/utility/file-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ReplaceVariable, Trigger } from "../../../../types/variables";
import { OutputDataType, VariableCategory } from "../../../../shared/variable-constants";
import { basename, extname } from "path";

const model : ReplaceVariable = {
definition: {
handle: "fileName",
usage: 'fileName[path\\to\\file.txt]',
description: "Returns name of file without extension.",
categories: [VariableCategory.ADVANCED],
possibleDataOutput: [OutputDataType.TEXT]
},
evaluator: (
trigger: Trigger,
filePath?: string
) : string => {
if (!filePath) {
return "";
}

try {
return basename(filePath, extname(filePath));
} catch (err) {
// Probably a directory or invalid filename
return "[Invalid File Path]";
}
}
};

export default model;
2 changes: 2 additions & 0 deletions src/backend/variables/builtin/utility/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import evalJS from './eval-js';
import evalVars from './eval-vars';
import fileExists from './file-exists';
import fileLineCount from './file-line-count';
import fileName from './file-name';
import fileRead from './file-read';
import filesInDirectory from './files-in-directory';
import getEffectQueueLength from "./get-effect-queue-length";
Expand All @@ -27,6 +28,7 @@ export default [
evalVars,
fileExists,
fileLineCount,
fileName,
fileRead,
filesInDirectory,
getEffectQueueLength,
Expand Down

0 comments on commit d78fe8d

Please sign in to comment.