broken out from sync repo see commits there
commit
626fe1483c
|
@ -0,0 +1,37 @@
|
|||
module.exports = {
|
||||
"ecmaFeatures": {
|
||||
"modules": true,
|
||||
"spread" : true,
|
||||
"restParams" : true
|
||||
},
|
||||
// "plugins": [
|
||||
// "unicorn"
|
||||
// ],
|
||||
"env": {
|
||||
"es6": true,
|
||||
"node": true,
|
||||
"mocha": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2017,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"rules": {
|
||||
"indent": [
|
||||
"error",
|
||||
2
|
||||
],
|
||||
// "unicorn/no-array-instanceof": "error",
|
||||
"no-console": 0,
|
||||
"semi": ["error", "never"],
|
||||
"linebreak-style": [
|
||||
"error",
|
||||
"unix"
|
||||
],
|
||||
"quotes": [
|
||||
"error",
|
||||
"single"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
/node_modules/
|
||||
/coverage/
|
|
@ -0,0 +1,4 @@
|
|||
tests/
|
||||
test/
|
||||
*.test.js
|
||||
testing/
|
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"name": "@uci-utils/read-lines",
|
||||
"version": "0.2.1",
|
||||
"description": "Functions to read and write lines from/to a file into/from an Array (asynchronously)",
|
||||
"main": "src/read-lines.js",
|
||||
"scripts": {
|
||||
"test": "./node_modules/.bin/mocha -r esm --timeout 30000",
|
||||
"testd": "UCI_ENV=dev ./node_modules/.bin/nodemon --exec './node_modules/.bin/mocha -r esm --timeout 30000' || exit 0",
|
||||
"testdd": "UCI_LOG_LEVEL='trace' npm run testd",
|
||||
"testde": "UCI_LOG_LEVEL='warn' npm run testd",
|
||||
"testl": "UCI_ENV=pro UCI_LOG_PATH=./test/test.log 0 npm run test || exit 0"
|
||||
},
|
||||
"author": "David Kebler",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/uCOMmandIt/uci-utils.git"
|
||||
},
|
||||
"keywords": [
|
||||
"node.js",
|
||||
"utilities",
|
||||
"read",
|
||||
"write",
|
||||
"helpers"
|
||||
],
|
||||
"bugs": {
|
||||
"url": "https://github.com/uCOMmandIt/uci-utils/issues"
|
||||
},
|
||||
"homepage": "https://github.com/uCOMmandIt/uci-utils#readme",
|
||||
"dependencies": {
|
||||
"@uci-utils/logger": "0.0.13",
|
||||
"p-settle": "^2.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^4.2.0",
|
||||
"chai-arrays": "^2.0.0",
|
||||
"esm": "^3.2.4",
|
||||
"mocha": "^5.2.0",
|
||||
"nodemon": "^1.18.10"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
### Read Lines to Array
|
||||
#### a uCOMmandIt Utiltiy Function
|
|
@ -0,0 +1,50 @@
|
|||
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 }
|
|
@ -0,0 +1,2 @@
|
|||
/node_modules/
|
||||
/coverage/
|
|
@ -0,0 +1,5 @@
|
|||
tests/
|
||||
test/
|
||||
*.test.js
|
||||
testing/
|
||||
example/
|
|
@ -0,0 +1,7 @@
|
|||
/node_modules/
|
||||
/coverage/
|
||||
tests/
|
||||
test/
|
||||
*.test.js
|
||||
testing/
|
||||
example/
|
|
@ -0,0 +1,54 @@
|
|||
// let ignoreFiles = ['.npmignore','.gitignore']
|
||||
|
||||
import { readLines, writeLines } from '../src/read-lines'
|
||||
import chai from 'chai'
|
||||
import assertArrays from 'chai-arrays'
|
||||
import { it } from 'mocha'
|
||||
|
||||
chai.use(assertArrays)
|
||||
const expect = chai.expect
|
||||
|
||||
describe (
|
||||
'Read a File of Lines to Array and vice versa',
|
||||
function () {
|
||||
readList()
|
||||
writeList()
|
||||
})
|
||||
|
||||
//****************** TESTS **********************
|
||||
function readList() {
|
||||
it('==> can read one or more files (no order) each line as element in an array with common directory', async function () {
|
||||
const shouldbe = [ 'tests/',
|
||||
'test/',
|
||||
'*.test.js',
|
||||
'testing/',
|
||||
'example/',
|
||||
'/node_modules/',
|
||||
'/coverage/' ]
|
||||
let result = await readLines(['.gitignore','.npmignore'],__dirname)
|
||||
expect(result, 'list build failed').to.be.containingAllOf(shouldbe)
|
||||
})
|
||||
|
||||
it('==> can read two files one relative the other absolute', async function () {
|
||||
const shouldbe = [ 'tests/',
|
||||
'test/',
|
||||
'*.test.js',
|
||||
'testing/',
|
||||
'example/',
|
||||
'/node_modules/',
|
||||
'/coverage/' ]
|
||||
let result = await readLines(['./test/.gitignore',__dirname+'/.npmignore'])
|
||||
expect(result, 'list build failed').to.be.containingAllOf(shouldbe)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
function writeList() {
|
||||
|
||||
it('==> can write an array items as lines in a file', async function () {
|
||||
const shouldbe = await readLines(['.gitignore','.npmignore'],__dirname)
|
||||
await writeLines(__dirname+'/combined.list',shouldbe)
|
||||
const result = await readLines(['combined.list'],__dirname)
|
||||
expect(result, 'list build failed').to.be.containingAllOf(shouldbe)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue