0

I have a function checkIsDir and I have imported it to the main file cli.js but I am unable to access the value which is been returned, in this case, it is true.

checkIsDir.js

const fs = require('fs')
const listr = require('listr')

function checkIsDir(path) {
  const tasks = new listr([
    {
      title: 'Checking if path is a directory',
      task: () => {
        try {
          fs.lstatSync(path).isDirectory()
          // return true if the path is a directory
          return true
        }
        catch (err) {
          throw new Error(`Path ${path} is not a directory`)
        }
      }
    }
  ])
  tasks.run().catch(err => {
    throw new Error(err)
  })
}

module.exports = checkIsDir

cli.js

#! /usr/bin/env node

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

const questions = require('./data/questions');
const checkIsDir = require('./lib/checkIsDir');
const spiltPath = require('./lib/splitPath');

const path = process.cwd();

inquirer.prompt(questions).then(async (answers) => {
  console.log(checkIsDir(answers.path))
  if (checkIsDir(answers.path) === true) {
    spiltPath(answers.path);
  }
  else {
    console.log('Oh no')
  }
});

terminal output

splitPath.js

function spiltPath(path) {
  var spiltArray = path.split('/');
  var folderName = spiltArray[spiltArray.length - 1];
  console.log(folderName);
}

module.exports = spiltPath
0xMukesh
  • 447
  • 1
  • 7
  • 10
  • 1
    `checkIsDir` does not return anything. The only `return` is in the function assigned to `task` but nothing in the main function itself. – VLAZ Mar 20 '22 at 09:00
  • @Kira - As @​VLAZ said, your function doesn't return anything, and since it's dealing with asynchronous processes, it *can't* return the result directly (just a promise of the result, just like `tasks.run` does). – T.J. Crowder Mar 20 '22 at 09:03
  • Is their any way to return the value ? – 0xMukesh Mar 20 '22 at 09:36

0 Answers0