refactor and improve both functions
uses promise.all now
This commit is contained in:
parent
a05927ef7e
commit
d522ac6883
7 changed files with 99 additions and 46 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
/node_modules/
|
/node_modules/
|
||||||
/coverage/
|
/coverage/
|
||||||
|
test/combined2.list
|
||||||
|
|
37
api.md
Normal file
37
api.md
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
## Functions
|
||||||
|
|
||||||
|
<dl>
|
||||||
|
<dt><a href="#readLines">readLines(files, dir)</a> ⇒ <code>Promise.<array></code></dt>
|
||||||
|
<dd><p>read lines from one or more files and return an array of those all those lines</p>
|
||||||
|
</dd>
|
||||||
|
<dt><a href="#writeLines">writeLines(filePath, list)</a> ⇒ <code>Promise.<null></code></dt>
|
||||||
|
<dd><p>Writes files with a line for each element in a array or item in comma delimited list</p>
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<a name="readLines"></a>
|
||||||
|
|
||||||
|
## readLines(files, dir) ⇒ <code>Promise.<array></code>
|
||||||
|
read lines from one or more files and return an array of those all those lines
|
||||||
|
|
||||||
|
**Kind**: global function
|
||||||
|
**Returns**: <code>Promise.<array></code> - - resolves to an single array of all lines from all files (no guarenteed order)
|
||||||
|
|
||||||
|
| Param | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| files | <code>array</code> \| <code>string</code> | array or comma delimited string of file paths. Can be absolute or relative (see dir) |
|
||||||
|
| dir | <code>string</code> | the directory to join to any relative paths |
|
||||||
|
|
||||||
|
<a name="writeLines"></a>
|
||||||
|
|
||||||
|
## writeLines(filePath, list) ⇒ <code>Promise.<null></code>
|
||||||
|
Writes files with a line for each element in a array or item in comma delimited list
|
||||||
|
|
||||||
|
**Kind**: global function
|
||||||
|
**Returns**: <code>Promise.<null></code> - can catch errors but otherwise nothing resolves
|
||||||
|
|
||||||
|
| Param | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| filePath | <code>string</code> | filepath to which file is written |
|
||||||
|
| list | <code>array</code> \| <code>string</code> | each element becomes line in file | each comma delimited item becomes a line |
|
||||||
|
|
|
@ -28,9 +28,7 @@
|
||||||
"url": "https://github.com/uCOMmandIt/uci-utils/issues"
|
"url": "https://github.com/uCOMmandIt/uci-utils/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/uCOMmandIt/uci-utils#readme",
|
"homepage": "https://github.com/uCOMmandIt/uci-utils#readme",
|
||||||
"dependencies": {
|
"dependencies": {},
|
||||||
"p-settle": "^5.1.1"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"chai": "^5.1.2",
|
"chai": "^5.1.2",
|
||||||
"chai-arrays": "^2.2.0",
|
"chai-arrays": "^2.2.0",
|
||||||
|
@ -38,4 +36,4 @@
|
||||||
"mocha": "^11.0.1",
|
"mocha": "^11.0.1",
|
||||||
"nodemon": "^3.1.9"
|
"nodemon": "^3.1.9"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,2 +1,7 @@
|
||||||
### Read Lines to Array
|
### Read Lines to Array
|
||||||
#### a uCOMmandIt Utiltiy Function
|
#### a uCOMmandIt Utiltiy Function
|
||||||
|
|
||||||
|
|
||||||
|
exports two functions, readlines, writelines
|
||||||
|
|
||||||
|
[API](./api.md)
|
|
@ -1,50 +1,49 @@
|
||||||
import { promisify } from 'util'
|
import path from 'node:path'
|
||||||
import path from 'path'
|
import { readFile as read, writeFile as write, stat } from 'node:fs/promises'
|
||||||
import { readFile, writeFile } from 'fs'
|
|
||||||
const read = promisify(readFile)
|
|
||||||
const write = promisify(writeFile)
|
|
||||||
import settle from 'p-settle'
|
|
||||||
// import logger from '@uci-utils/logger'
|
|
||||||
// let log = logger({ package:'@uci-utils/read-lines', file:'src/read-lines.js'})
|
|
||||||
|
|
||||||
// read lines from one or more files
|
|
||||||
|
/**
|
||||||
|
* read lines from one or more files and return an array of those all those lines
|
||||||
|
* @param {array|string} files - array or comma delimited string of file paths. Can be absolute or relative (see dir)
|
||||||
|
* @param {string} dir - the directory to join to any relative paths
|
||||||
|
* @returns {Promise<array>} - resolves to an single array of all lines from all files (no guarenteed order)
|
||||||
|
*/
|
||||||
function readLines (files=[],dir) {
|
function readLines (files=[],dir) {
|
||||||
// log.debug({files:files,dir:dir,msg:'additional files'})
|
|
||||||
let list = []
|
let list = []
|
||||||
if (!Array.isArray(files)) files=[files]
|
// if not an array of file names it must be a string
|
||||||
// each set in an the array is new line delimited set of ignore patterns
|
if (!Array.isArray(files)) files=files.split(',')
|
||||||
// settle returns array of error,value pairs
|
return Promise.all(files.map(async file => {
|
||||||
return settle(files.map(file => {
|
if (! path.isAbsolute(file) ) {
|
||||||
// console.log('directory',path.dirname(file))
|
if ( typeof dir === 'string' ) {
|
||||||
if (path.dirname(file)==='.') file = dir+'/'+file
|
file = path.join(dir,file)
|
||||||
// log.debug({function:'readLines',file:file,msg:'reading a file of lines into array'})
|
}}
|
||||||
return read(file)
|
return await read(file)
|
||||||
|
.then(lines => {
|
||||||
|
list.push(...lines.toString().match(/.+/g))
|
||||||
|
return Promise.resolve(lines)
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
return Promise.reject(e)
|
||||||
|
});
|
||||||
}))
|
}))
|
||||||
.then((sets) => {
|
.then((sets) => {
|
||||||
sets.forEach( set => {
|
|
||||||
if (set.isFulfilled) list.push(...set.value.toString().match(/.+/g))
|
|
||||||
else
|
|
||||||
console.log("unable to read file")
|
|
||||||
// log.warn({function:'readLines', error:set.reason, msg:' was unable to read file'})
|
|
||||||
})
|
|
||||||
return Promise.resolve(list)
|
return Promise.resolve(list)
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
// only returned when something horrible is wrong
|
|
||||||
return Promise.reject(err)
|
return Promise.reject(err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// an ignore list should not be huge so can serailize at once
|
|
||||||
function writeLines (filePath,list) {
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes files with a line for each element in a array or item in comma delimited list
|
||||||
|
* @param {string} filePath - filepath to which file is written
|
||||||
|
* @param {array|string} list - each element becomes line in file | each comma delimited item becomes a line
|
||||||
|
* @returns {Promise<null>} can catch errors but otherwise nothing resolves
|
||||||
|
*/
|
||||||
|
function writeLines (filePath,list) {
|
||||||
|
if (typeof list === "string") list = list.split(',')
|
||||||
return write(filePath,list.join('\n'))
|
return write(filePath,list.join('\n'))
|
||||||
.then(() => {
|
|
||||||
// log.info({function:'writeLines', file:filePath, msg:'wrote array to file of lines'})
|
|
||||||
})
|
|
||||||
.catch( err => {
|
|
||||||
// log.fatal({function:'writeLines', error:err, msg:'unable to write array to file of lines'})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default readLines
|
export default readLines
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
/node_modules/
|
|
||||||
/coverage/
|
|
||||||
tests/
|
|
||||||
test/
|
|
||||||
*.test.js
|
|
||||||
testing/
|
|
||||||
example/
|
|
|
@ -1,5 +1,3 @@
|
||||||
// let ignoreFiles = ['.npmignore','.gitignore']
|
|
||||||
|
|
||||||
import { readLines, writeLines } from '../src/read-lines.js'
|
import { readLines, writeLines } from '../src/read-lines.js'
|
||||||
import { it } from 'mocha'
|
import { it } from 'mocha'
|
||||||
import { dirname } from 'dirname-filename-esm';
|
import { dirname } from 'dirname-filename-esm';
|
||||||
|
@ -41,6 +39,20 @@ function readList() {
|
||||||
let result = await readLines(['./test/.gitignore',__dirname+'/.npmignore'])
|
let result = await readLines(['./test/.gitignore',__dirname+'/.npmignore'])
|
||||||
expect(result, 'list build failed').to.be.containingAllOf(shouldbe)
|
expect(result, 'list build failed').to.be.containingAllOf(shouldbe)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('==> can read files passed as a comma delimited string', async function () {
|
||||||
|
const shouldbe = [ 'tests/',
|
||||||
|
'test/',
|
||||||
|
'*.test.js',
|
||||||
|
'testing/',
|
||||||
|
'example/',
|
||||||
|
'/node_modules/',
|
||||||
|
'/coverage/' ]
|
||||||
|
let result = await readLines('test/.gitignore,test/.npmignore')
|
||||||
|
expect(result, 'list build failed').to.be.containingAllOf(shouldbe)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,4 +64,12 @@ function writeList() {
|
||||||
const result = await readLines(['combined.list'],__dirname)
|
const result = await readLines(['combined.list'],__dirname)
|
||||||
expect(result, 'list build failed').to.be.containingAllOf(shouldbe)
|
expect(result, 'list build failed').to.be.containingAllOf(shouldbe)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('==> can write an comma delimited string of items as lines in a file', async function () {
|
||||||
|
const shouldbe = await readLines(['.gitignore','.npmignore'],__dirname)
|
||||||
|
await writeLines(__dirname+'/combined2.list',shouldbe.join(','))
|
||||||
|
const result = await readLines(['combined2.list'],__dirname)
|
||||||
|
expect(result, 'list build failed').to.be.containingAllOf(shouldbe)
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue