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')
}
});
splitPath.js
function spiltPath(path) {
var spiltArray = path.split('/');
var folderName = spiltArray[spiltArray.length - 1];
console.log(folderName);
}
module.exports = spiltPath