Skip to content

Commit

Permalink
Merge pull request #361 from Edebo/master
Browse files Browse the repository at this point in the history
updates in methods, related to folder paths #149
  • Loading branch information
atherdon authored Dec 10, 2019
2 parents 34e417f + f6fbe44 commit fe4e478
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
19 changes: 6 additions & 13 deletions package/src/fileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
readFile,
readdir,
} from 'fs';
import isValid from 'is-valid-path';
// import isValid from 'is-valid-path';

import {
stripSymbols,
Expand All @@ -24,6 +24,7 @@ import {
dirSync,
syncStats,
getOnlyFiles,
isPathValid,
} from './utils';

const _ = require('lodash');
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion package/src/objects.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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') {
Expand Down
12 changes: 10 additions & 2 deletions package/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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()
Expand Down Expand Up @@ -110,6 +116,7 @@ const updateContent = (content, keys) => {
};

const checkFilePath = async (filePath) => {
isPathValid(filePath);
if (await pathExists(filePath)) {
console.log(`Filepath ${filePath} exist`);
} else {
Expand All @@ -122,14 +129,14 @@ const checkFilePath = async (filePath) => {
* @param {String} path
*/
const fixPath = (filePath) => {
isPathValid(filePath);
let newPath = resolve(__dirname, filePath);
if (newPath.charAt(newPath.length - 1) !== '/') {
newPath += '/';
}
return newPath;
};


/**
* For getOnlyFiles()
* @param {String} filePath
Expand All @@ -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();
Expand All @@ -148,7 +156,6 @@ const getOnlyFiles = (filePath) => {
return list;
};


const generateID = () => uuidv1();

const generateDate = () => dayjs().toDate();
Expand Down Expand Up @@ -219,4 +226,5 @@ export {
isFolderExists,
dirSync,
syncStats,
isPathValid,
};

0 comments on commit fe4e478

Please sign in to comment.