Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test for getFileInfo and path fix in writeFile.js and src/utils.js #562

Merged
merged 24 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 105 additions & 80 deletions generator/writeFile.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,117 @@
// const filePath = require('../files')

const fs = require('fs')
const PATH = require('path')
const { promisify } = require('util') // ?? it's utils of not
// const { promisify } = require('util')

// const _ = require('lodash')
// const { promisify } = require('util')
// const _ = require('lodash')

const writeFilePromisify = promisify(fs.writeFile)

// @TODO change the name of this method. it's not selfexplanatory
async function main (path, data) {
await writeFilePromisify(
path, data
)

console.info('file generated successfully!')
async function writing(path, data) {
await writeFilePromisify(
path, data
)
console.info(path + ' file generated successfully!')
}

/**
* Write in file
* @param {String} path
* @param {Object} data
*/
function writeFile (path, data) { // @TODO looks like a duplicate, am i right?
// console.log(typeof users);
// console.log(typeof usersStr);
if (typeof data === 'undefined') {
console.error('Error variable is undefined')
return
}

if (typeof data === 'object') {
var dataStr = JSON.stringify(data)
if (typeof dataStr !== 'string') {
console.error('Error occured after stringify or variabe has another type not string')
return
function writeFile(path, data) {
// console.log(typeof users);
// console.log(typeof usersStr);
if (typeof data === 'undefined') {
console.error('Error variable is undefined')
return
}

main(path, dataStr).catch(
error => console.error(error)
)
}
if (typeof data === 'object') {
var dataStr = JSON.stringify(data)
writing(path, dataStr).catch(
error => console.error(error)
)
}
}

function test () {
// console.log(filePath["groceryFilePath"]);
// console.log(JSON.parse("src/data/Grocery/grocery.json"));
// console.log(grocery);
// writeFiles()
// console.log(typeof require(filePath.groceryFilePath));
function test() {
// console.log(filePath["groceryFilePath"]);
// console.log(JSON.parse("src/data/Grocery/grocery.json"));
// console.log(grocery);
// writeFiles()
// console.log(typeof require(filePath.groceryFilePath));

console.log('ok')
console.log('ok')
}

// execute function
// writeFiles()

/**
* For fixPath()
* @param {String} path
*/
function fixPath(path) {
path = PATH.resolve(__dirname, path) //absolute path
if (path[-1] !== '/') { path = path + '/' } //path correction
return path
}

/**
* readData()
* @param {string} path
* @param {string} file
* */
function readData(path, file) {
let data = fs.readFileSync(path + file)
let fileData = JSON.parse(data)
return fileData
}

/**
* @param {String} folderNamePath
* @param {String} file
* @param {Object} fileData
* @param {var} flag
* */
function saveFile(folderNamePath, file, fileData, flag) {
var fileDataLength = fileData.length
for (var i = 0; i < fileDataLength; i++) {
var fileName = getFileName(file, fileData[i], flag, i)
var elementPath = folderNamePath + '/' + fileName
writeFile(elementPath, fileData[i])
}
}

/**
* @param {String} path
* @param {String} file
*/
function makeFolder(path, file) {
var folderName = file.slice(0, -5) + '_elements'
var folderNamePath = path + folderName
if (isDirectory(folderNamePath)) {
fs.mkdirSync(folderNamePath)
}
return folderNamePath
}
/**
* For splitObject
* @param {String} path
* @param {String} file
* @param {var} flag
*/
function splitObject (path, file, flag) { // @TODO do we have only one this method or i'm mistaken?
/*
flag=1 ==> name according to index
flag=0 ==> name according to "name" attribute
*/
var temp = path.charAt(path.length - 1) // path correction
if (temp !== '/') { path = path + '/' }

// Reading data...
let data = fs.readFileSync(path + file)
let fileData = JSON.parse(data)

var folderName = file.slice(0, -5) + '_elements'
var folderNamePath = path + folderName

if (isDirectory(folderNamePath)) {
fs.mkdirSync(folderNamePath)
}

for (var i = 0; i < fileData.length; i++) {
var fileName = getFileName(file, fileData[i], flag, i)
var elementPath = path + folderName + '/' + fileName
writeFile(elementPath, fileData[i])
}
function splitObject(path, file, flag = 1) { // split large files into single elements
/*
flag=1 ==> name according to index
flag=0 ==> name according to "name" attribute
*/
path = fixPath(path)
let fileData = readData(path, file) // Reading data...
var folderNamePath = makeFolder(path, file) //new folder to save splitted files
saveFile(folderNamePath, file, fileData, flag) //saving files

}
// execute function
// splitObject()
Expand All @@ -94,21 +120,21 @@ function splitObject (path, file, flag) { // @TODO do we have only one this meth
* fixFileName()
* @param {string} fileName
*/
function fixFileName (fileName) {
fileName = fileName.replace(/ /g, '_') // Replace space with underscore
fileName = fileName.toLowerCase() // Maintain Uniformity
return fileName
function fixFileName(fileName) {
fileName = fileName.replace(/ /g, '_') // Replace space with underscore
fileName = fileName.toLowerCase() // Maintain Uniformity
return fileName
}

/** isDirectory()
/**
* isDirectory()
* @param {string} folderNamePath
* */
function isDirectory (folderNamePath) {
if (fs.existsSync(folderNamePath)) {
return false
} else {
function isDirectory(folderNamePath) {
if (fs.existsSync(folderNamePath)) {
return false
}
return true
}
}

/**
Expand All @@ -117,18 +143,17 @@ function isDirectory (folderNamePath) {
* @param {Object} fileData
* @param {var} flag
* @param {var} index
*/
function getFileName (file, fileData, flag, index) {
var fileName
if (flag === 1) fileName = index + '-' + file// for example: 23-someJsonFile.json
else fileName = fileData.name + '.json' // for example: someValueOfName.json

fileName = fixFileName(fileName)
return fileName
*/
function getFileName(file, fileData, flag, index) {
var fileName
if (flag === 1) fileName = index + '-' + file // for example: 23-someJsonFile.json
else fileName = fileData.name + '.json' // for example: someValueOfName.json
fileName = fixFileName(fileName)
return fileName
}

module.exports = {
writeFile,
test,
splitObject
writeFile,
test,
splitObject
}
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const uuidv1 = require('uuid/v1')
const dayjs = require('dayjs')
const fs = require('fs')
const PATH = require('path')

/**
* For readAllFiles()
Expand Down Expand Up @@ -42,6 +43,7 @@ function getListContent (path, fileName = 'undefined') {
* @param {String} path
*/
function fixPath (path) {
path = PATH.resolve(__dirname,path)
if (path.charAt(path.length - 1) !== '/') path = path + '/'
return path
}
Expand Down
26 changes: 26 additions & 0 deletions tests/getFileInfo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* global describe, it, expect */
'use strict'
const { getFileInfo } = require('../src/utils')
const { matchers } = require('jest-json-schema')
const path = require('path')
expect.extend(matchers)

var location = './sampleFile'
location = path.resolve(__dirname,location)
console.log(location)

describe('getFileInfo with only 1 argument', () => {
it('should return the list of files in the folder', () => {
var result = getFileInfo(location)
const expected = ['basic-grocery-list.json']
expect(result.values).toEqual(expected.values)
})
})

describe('getFileInfo with all 3 arguments', () => {
it('should return the content of given file', () => {
var result = getFileInfo(location, 1, 'basic-grocery-list.json')
const expected = { "departments": ["staples", "tinnedFood", "vegetables"], "name": "basic-grocery-list", "img": false, "desc": false, "slug": false }
expect(result.values).toEqual(expected.values)
})
})
1 change: 1 addition & 0 deletions tests/sampleFile/basic-grocery-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "departments": ["staples", "tinnedFood", "vegetables"], "name": "basic-grocery-list", "img": false, "desc": false, "slug": false }