NodeJS: Custom Module if File Exists (Custom Module for files outside root)

For my custom synchronization module I created several custom modules to be able to upload files from our local Windows Server to Ionos S3.

Everything works quite well. I have recreated the custom “File/Folder Statistic Module” and an “Upload Module to S3”. What I still need is the “if file exists module” for files outside website root.

I know that all I need is to delete the “const { toSystemPath } = require(’…/…/…/lib/core/path’);” and change the path settings in the function.

The recreation of a custom “if file exists” Module is more complicated. Any hints how to accomplish this? I need the then else steps :slight_smile:

This is what I have:

hjson:

{
  type: 'ifExtFileExists',
  module : 'ifExtFileExists',
  action : 'ifExtFileExists',
  groupTitle : 'My Modules',
  groupIcon : 'fas fa-lg fa-project-diagram comp-images',
  title : 'If ext. file exists',
  icon : 'fas fa-lg fa-question comp-images',
  dataPickObject: true,
  properties : [
    {
      group: 'If ext. file exists',
      variables: [
        { name: 'name', optionName: 'name', title: 'Name', type: 'text', required: true, defaultValue: 'fileExists'},
        { name: 'path', optionName: 'path', title: 'File Path', type: 'file', required: true, defaultValue: '', serverDataBindings: true,
          help: 'Please enter your File Path'},
        { name: 'output', optionName: 'output', title: 'Output', type: 'boolean', defaultValue: false }
      ]
    }
  ]
}

js:

const fs = require('fs-extra');

exports.ifExtFileExists = async function (options) {
    let path = this.parseRequired(options.path, 'string', 'fs.exists: path is required.');

    if (fs.existsSync(path)) {
        if (options.then) this.exec(options.then, true);
        return true;
    } else {
        if (options.else) this.exec(options.else, true);
        return false;
    }
}

This is what I get:

image

This is what I need:

image

1 Like

@George & @patrick
what do I have to add in the hjson file to show the else then steps?

Would this be possible in Custom Modules?

I think we already have a file exist step in the File System Connector group? :slight_smile:

I know, but not for files outside of the website root folder.

I need it for a UNC Path like \\Server\Path\file.pdf

My Problem is that the S3 Upload works great with my custom module, but I get an error if the file does not exist. (Manually deleted from the server but not deleted in the db)

1 Like

If the variable in the hjson is of type: “steps”
You will get nested steps :slight_smile:

Thanks a lot!

Does this looks correct (I’m not a definition expert :wink: )?

{
  type: 'ifExtFileExists',
  module : 'ifExtFileExists',
  action : 'ifExtFileExists',
  groupTitle : 'My Modules',
  groupIcon : 'fas fa-lg fa-project-diagram comp-images',
  title : 'If ext. file exists',
  icon : 'fas fa-lg fa-question comp-images',
  dataPickObject: true,
  properties : [
    {
      group: 'If ext. file exists',
      variables: [
        { name: 'then', optionName: 'then', title: 'then', type: 'steps'},
        { name: 'else', optionName: 'else', title: 'else', type: 'steps'},
        { name: 'name', optionName: 'name', title: 'Name', type: 'text', required: true, defaultValue: 'fileExists'},
        { name: 'path', optionName: 'path', title: 'File Path', type: 'file', required: true, defaultValue: '', serverDataBindings: true,
          help: 'Please enter your File Path'},
        { name: 'output', optionName: 'output', title: 'Output', type: 'boolean', defaultValue: false }
      ]
    }
  ]
}

Do I need the title?

looks all fine! You might only add required: true at the then step as it is required.

This is not javascript btw - it is just a definition :slight_smile:

1 Like

Final Code of the hjson:

{
  type: 'ifExtFileExists',
  module : 'ifExtFileExists',
  action : 'ifExtFileExists',
  groupTitle : 'My Modules',
  groupIcon : 'fas fa-lg fa-project-diagram comp-images',
  title : 'If ext. file exists',
  icon : 'fas fa-lg fa-question comp-images',
  dataPickObject: true,
  properties : [
    {
      group: 'If ext. file exists',
      variables: [
        { name: 'then', optionName: 'then', title: 'then', type: 'steps', required: true},
        { name: 'else', optionName: 'else', title: 'else', type: 'steps'},
        { name: 'name', optionName: 'name', title: 'Name', type: 'text', required: true, defaultValue: 'fileExists'},
        { name: 'path', optionName: 'path', title: 'File Path', type: 'file', required: true, defaultValue: '', serverDataBindings: true,
          help: 'Please enter your File Path'},
        { name: 'output', optionName: 'output', title: 'Output', type: 'boolean', defaultValue: false }
      ]
    }
  ]
}