diff --git a/package/src/fileSystem.js b/package/src/fileSystem.js index 3325c1fe..843136a4 100644 --- a/package/src/fileSystem.js +++ b/package/src/fileSystem.js @@ -14,7 +14,7 @@ import { readFile, readdir, } from 'fs'; -import isValid from 'is-valid-path'; +// import isValid from 'is-valid-path'; import { stripSymbols, @@ -24,6 +24,7 @@ import { dirSync, syncStats, getOnlyFiles, + isPathValid, } from './utils'; const _ = require('lodash'); @@ -37,9 +38,7 @@ const _ = require('lodash'); // @TODO cover a test case, when dataStr is not an array. // we can just pass a string there const write = (path, data) => new Promise((resolve, reject) => { - if (!isValid(path)) { - console.log('path is not valid'); - } + isPathValid(path); let dataStr; @@ -67,9 +66,7 @@ const write = (path, data) => new Promise((resolve, reject) => { */ const read = (absolutePath) => new Promise((resolve, reject) => { console.log(absolutePath); - if (!isValid(absolutePath)) { - console.log('path is invalid'); - } + isPathValid(absolutePath); let dataStr; readFile(absolutePath, 'utf8', (err, data) => { if (!err) { @@ -99,9 +96,7 @@ const read = (absolutePath) => new Promise((resolve, reject) => { // @TODO save got 4 attributes and most of them are about directory/files... // there should be another way const save = (folderNamePath, file, fileData, flag) => { - if (!isValid(folderNamePath)) { - console.log('path is not valid'); - } + isPathValid(folderNamePath); const fileDataLength = fileData.length; let success = true; @@ -178,9 +173,7 @@ const save = (folderNamePath, file, fileData, flag) => { * @param {String} file */ const makeFolder = (path, file) => { - if (!isValid(path)) { - console.log('path is not valid'); - } + isPathValid(path); const suffix = '_elements'; const folderName = file.slice(0, -5) + suffix; const folderNamePath = path + folderName; diff --git a/package/src/objects.js b/package/src/objects.js index 4c89aa27..57ec3f30 100644 --- a/package/src/objects.js +++ b/package/src/objects.js @@ -1,7 +1,7 @@ /* eslint-disable no-console */ import { basename, parse, extname } from 'path'; -import { fixPath, updateContent } from './utils'; +import { fixPath, updateContent, isPathValid } from './utils'; import { read, write, save, makeFolder, readAllFiles, @@ -14,6 +14,7 @@ import { */ const combine = (path, keys) => new Promise((resolve) => { const suffix = '_combined.json'; + isPathValid(path); const updatedPath = fixPath(path); // read all json files @@ -48,6 +49,7 @@ function split(fullPath, flag = 1, keys = [], callback) { flag=0 ==> name according to "name" attribute */ const file = basename(fullPath); + isPathValid(fullPath); const path = parse(fullPath).dir; if (extname(file) !== '.json') { diff --git a/package/src/utils.js b/package/src/utils.js index bd318995..a3281ea9 100644 --- a/package/src/utils.js +++ b/package/src/utils.js @@ -7,6 +7,7 @@ import pathExists from 'path-exists'; import _ from 'lodash'; import uuidv1 from 'uuid/v1'; import dayjs from 'dayjs'; +import isValid from 'is-valid-path'; // @TODO what is the purpose of this method? Just separation? // are we using them few times, right? @@ -30,6 +31,11 @@ const syncStats = (filepath) => statSync(filepath); */ const isFolderExists = (folderNamePath) => existsSync(folderNamePath); +const isPathValid = (path) => { + if (!isValid(path)) { + console.log('path is not valid'); + } +}; /** * fixFileName() @@ -110,6 +116,7 @@ const updateContent = (content, keys) => { }; const checkFilePath = async (filePath) => { + isPathValid(filePath); if (await pathExists(filePath)) { console.log(`Filepath ${filePath} exist`); } else { @@ -122,6 +129,7 @@ const checkFilePath = async (filePath) => { * @param {String} path */ const fixPath = (filePath) => { + isPathValid(filePath); let newPath = resolve(__dirname, filePath); if (newPath.charAt(newPath.length - 1) !== '/') { newPath += '/'; @@ -129,7 +137,6 @@ const fixPath = (filePath) => { return newPath; }; - /** * For getOnlyFiles() * @param {String} filePath @@ -138,6 +145,7 @@ const fixPath = (filePath) => { // developer of this code - it looks confusing for me const getOnlyFiles = (filePath) => { const list = []; + isPathValid(filePath); const files = dirSync(filePath); files.forEach((file) => { const fileStat = syncStats(`${filePath}/${file}`).isDirectory(); @@ -148,7 +156,6 @@ const getOnlyFiles = (filePath) => { return list; }; - const generateID = () => uuidv1(); const generateDate = () => dayjs().toDate(); @@ -219,4 +226,5 @@ export { isFolderExists, dirSync, syncStats, + isPathValid, };