51 lines
1.7 KiB
JavaScript
51 lines
1.7 KiB
JavaScript
|
import { promisify } from 'util'
|
||
|
import path from 'path'
|
||
|
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:'@cui/sync', file:'src/read-lines.js'})
|
||
|
|
||
|
// A promise helper function to return a list of paths to ignore from .npmignore, .gitignore, .rcignore
|
||
|
function readLines (files=[],dir) {
|
||
|
// console.log('additional files', files)
|
||
|
let list = []
|
||
|
if (!Array.isArray(files)) files=[files]
|
||
|
|
||
|
// each set in an the array is new line delimited set of ignore patterns
|
||
|
// settle returns array of error,value pairs
|
||
|
return settle(files.map(file => {
|
||
|
// console.log('directory',path.dirname(file))
|
||
|
if (path.dirname(file)==='.') file = dir+'/'+file
|
||
|
log.debug({function:'readLines',file:file,msg:'reading a file of lines into array'})
|
||
|
return read(file)
|
||
|
}))
|
||
|
.then((sets) => {
|
||
|
sets.forEach( set => {
|
||
|
if (set.isFulfilled) list.push(...set.value.toString().match(/.+/g))
|
||
|
else log.warn({function:'readLines', error:set.reason, msg:' was unable to read file'})
|
||
|
})
|
||
|
return Promise.resolve(list)
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
// only returned when something horrible is wrong
|
||
|
return Promise.reject(err)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// an ignore list should not be huge so can serailize at once
|
||
|
function writeLines (filePath,list) {
|
||
|
|
||
|
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 { readLines, writeLines }
|