From 626fe1483c4c4a69059b37f79a76c826970c5779 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sat, 16 Feb 2019 12:37:48 -0800 Subject: [PATCH] broken out from sync repo see commits there --- .eslintrc.js | 37 ++++++++++++++++++++++++++++ .gitignore | 2 ++ .npmignore | 4 +++ package.json | 41 +++++++++++++++++++++++++++++++ readme.md | 2 ++ src/read-lines.js | 50 ++++++++++++++++++++++++++++++++++++++ test/.gitignore | 2 ++ test/.npmignore | 5 ++++ test/combined.list | 7 ++++++ test/read-lines.test.js | 54 +++++++++++++++++++++++++++++++++++++++++ 10 files changed, 204 insertions(+) create mode 100644 .eslintrc.js create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 package.json create mode 100644 readme.md create mode 100644 src/read-lines.js create mode 100644 test/.gitignore create mode 100644 test/.npmignore create mode 100644 test/combined.list create mode 100644 test/read-lines.test.js diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..49bac18 --- /dev/null +++ b/.eslintrc.js @@ -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" + ] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e61051f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +/coverage/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..f16fc41 --- /dev/null +++ b/.npmignore @@ -0,0 +1,4 @@ +tests/ +test/ +*.test.js +testing/ diff --git a/package.json b/package.json new file mode 100644 index 0000000..40fe8f8 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..33679be --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +### Read Lines to Array +#### a uCOMmandIt Utiltiy Function diff --git a/src/read-lines.js b/src/read-lines.js new file mode 100644 index 0000000..a71063b --- /dev/null +++ b/src/read-lines.js @@ -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 } diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..e61051f --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +/coverage/ diff --git a/test/.npmignore b/test/.npmignore new file mode 100644 index 0000000..02078d5 --- /dev/null +++ b/test/.npmignore @@ -0,0 +1,5 @@ +tests/ +test/ +*.test.js +testing/ +example/ diff --git a/test/combined.list b/test/combined.list new file mode 100644 index 0000000..6115502 --- /dev/null +++ b/test/combined.list @@ -0,0 +1,7 @@ +/node_modules/ +/coverage/ +tests/ +test/ +*.test.js +testing/ +example/ \ No newline at end of file diff --git a/test/read-lines.test.js b/test/read-lines.test.js new file mode 100644 index 0000000..0c24618 --- /dev/null +++ b/test/read-lines.test.js @@ -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) + }) +}