From 41e5281c6a47205a242dea712e5e54f075b72963 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 29 Mar 2024 14:18:42 +0200 Subject: [PATCH] chore: lint --- .eslintrc.json | 4 +- .github/workflows/nodejs.yml | 68 +++++++++++-------------- .gitignore | 1 + .madrun.mjs | 1 - README.md | 14 ++++-- lib/readdir.js | 1 - lib/readify.js | 13 +++-- package.json | 1 + test/readdir.js | 13 ++--- test/readify.js | 98 ++++++++++++++++++++++++------------ 10 files changed, 120 insertions(+), 94 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index b2a2404..6c5d119 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -6,11 +6,11 @@ "node/no-unsupported-features/node-builtins": "off" }, "extends": [ - "plugin:node/recommended", + "plugin:n/recommended", "plugin:putout/recommended" ], "plugins": [ "putout", - "node" + "n" ] } diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index b474924..3d474b7 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -1,45 +1,37 @@ name: Node CI - -on: [push, pull_request] - +on: + - push + - pull_request jobs: build: - runs-on: ubuntu-latest - strategy: matrix: - node-version: [14.x, 15.x] - + node-version: + - 18.x + - 20.x + - 21.x steps: - - uses: actions/checkout@v1 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} - - - name: Install Redrun - run: - npm i redrun -g - - - name: Install - run: - npm install - - - name: Bootstrap - run: - redrun bootstrap - - - name: Lint - run: - redrun lint - - - name: Coverage - run: - redrun coverage report - - - name: Coveralls - uses: coverallsapp/github-action@master - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - + - uses: actions/checkout@v4 + - uses: oven-sh/setup-bun@v1 + with: + bun-version: latest + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + - name: Install Redrun + run: bun i redrun -g --no-save + - name: Install + run: bun i --no-save + - name: Bootstrap + run: redrun bootstrap + - name: Lint + run: redrun lint + - name: Coverage + run: redrun coverage report + - name: Coveralls + uses: coverallsapp/github-action@v2 + continue-on-error: true + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 523c5ed..771ef63 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ yarn-error.log .nyc_output *.swp +.idea diff --git a/.madrun.mjs b/.madrun.mjs index 1f6e280..e890025 100644 --- a/.madrun.mjs +++ b/.madrun.mjs @@ -12,4 +12,3 @@ export default { 'watch:test': () => run('watcher', 'npm test'), 'watcher': () => 'nodemon -w test -w lib --exec', }; - diff --git a/README.md b/README.md index 3eba58a..b9d423c 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,10 @@ -# Readify [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL] +# Readify [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL] [NPMIMGURL]: https://img.shields.io/npm/v/readify.svg?style=flat [BuildStatusURL]: https://github.com/coderaiser/readify/actions?query=workflow%3A%22Node+CI%22 "Build Status" [BuildStatusIMGURL]: https://github.com/coderaiser/readify/workflows/Node%20CI/badge.svg -[DependencyStatusIMGURL]: https://img.shields.io/david/coderaiser/readify.svg?style=flat [LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat [NPMURL]: https://npmjs.org/package/readify "npm" -[DependencyStatusURL]: https://david-dm.org/coderaiser/readify "Dependency Status" [LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License" [CoverageURL]: https://coveralls.io/github/coderaiser/readify?branch=master [CoverageIMGURL]: https://coveralls.io/repos/coderaiser/readify/badge.svg?branch=master&service=github @@ -52,7 +50,9 @@ console.log(data); }], }); -readify('/', {type: 'raw'}).then(console.log); +readify('/', { + type: 'raw', +}).then(console.log); // output ({ path: '/', @@ -66,7 +66,11 @@ readify('/', {type: 'raw'}).then(console.log); }], }); -readify('/', {type: 'raw', sort: 'size', order: 'desc'}).then(console.log); +readify('/', { + type: 'raw', + sort: 'size', + order: 'desc', +}).then(console.log); // output ({ path: '/', diff --git a/lib/readdir.js b/lib/readdir.js index a0fbbd3..5785898 100644 --- a/lib/readdir.js +++ b/lib/readdir.js @@ -73,4 +73,3 @@ function getType({name, isDir, isLink}) { return `${type}${link}`; } - diff --git a/lib/readify.js b/lib/readify.js index f1cf072..871a987 100644 --- a/lib/readify.js +++ b/lib/readify.js @@ -1,5 +1,7 @@ 'use strict'; +const process = require('node:process'); +const isUndefined = (a) => typeof a === 'undefined'; const format = require('format-io'); const currify = require('currify'); const tryToCatch = require('try-to-catch'); @@ -9,12 +11,10 @@ const formatify = require('@cloudcmd/formatify'); const WIN = process.platform === 'win32'; -const ifRaw = currify(_ifRaw); -const replaceProperty = currify(_replaceProperty); - -const nicki = !WIN && require('nicki'); - const readdir = require('./readdir'); +const nicki = !WIN && require('nicki'); +const replaceProperty = currify(_replaceProperty); +const ifRaw = currify(_ifRaw); const isString = (a) => typeof a === 'string'; module.exports = readify; @@ -97,7 +97,7 @@ function _replaceProperty(obj, prop, item) { const n = item[prop]; const data = obj[n]; - if (typeof data === 'undefined') + if (isUndefined(data)) return item; return { @@ -105,4 +105,3 @@ function _replaceProperty(obj, prop, item) { [prop]: data, }; } - diff --git a/package.json b/package.json index f891cc9..78e2609 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "readify", "version": "9.0.0", + "type": "commonjs", "author": "coderaiser (https://github.com/coderaiser)", "description": "Read directory content with file attributes: size, date, owner, mode", "homepage": "http://github.com/coderaiser/readify", diff --git a/test/readdir.js b/test/readdir.js index 76f9ea5..649c6af 100644 --- a/test/readdir.js +++ b/test/readdir.js @@ -2,16 +2,16 @@ const fs = require('fs'); -const stub = require('@cloudcmd/stub'); -const test = require('supertape'); +const {test, stub} = require('supertape'); const mockRequire = require('mock-require'); -const {reRequire, stopAll} = mockRequire; -const tryToCatch = require('try-to-catch'); +const tryToCatch = require('try-to-catch'); +const {reRequire, stopAll} = mockRequire; const noop = () => {}; test('readdir: empty dir', async (t) => { const {readdir} = fs.promises; + fs.promises.readdir = async () => []; const _readdir = reRequire('../lib/readdir'); @@ -31,9 +31,7 @@ test('readdir: empty stat', async (t) => { throw Error('some'); }); - fs.promises.readdir = async () => [ - 'hello', - ]; + fs.promises.readdir = async () => ['hello']; const _readdir = reRequire('../lib/readdir'); @@ -245,4 +243,3 @@ test('readdir: result: directory link: no error', async (t) => { t.notOk(e, e?.message || 'should not receive error'); t.end(); }); - diff --git a/test/readify.js b/test/readify.js index 1f45a8b..d868011 100644 --- a/test/readify.js +++ b/test/readify.js @@ -1,13 +1,14 @@ 'use strict'; -const test = require('supertape'); -const stub = require('@cloudcmd/stub'); +const process = require('node:process'); +const {test, stub} = require('supertape'); + const tryToCatch = require('try-to-catch'); const mockRequire = require('mock-require'); const shortdate = require('shortdate'); -const {reRequire} = mockRequire; const readify = require('..'); +const {reRequire, stopAll} = mockRequire; test('readify: path: wrong', async (t) => { const [error] = await tryToCatch(readify, '/wrong/path'); @@ -24,10 +25,14 @@ test('readify: path: correct', async (t) => { t.equal(typeof json.path, 'string'); t.ok(Array.isArray(json.files)); t.end(); +}, { + checkAssertionsCount: false, }); test('readify: type: wrong', async (t) => { - const [e] = await tryToCatch(readify, '.', {type: 1}); + const [e] = await tryToCatch(readify, '.', { + type: 1, + }); t.equal(e.message, 'type should be a string or not to be defined!', 'should throw when type has wrong type'); t.end(); @@ -53,7 +58,8 @@ test('readify: result: should sort by name', async (t) => { const date = new Date('2017-01-12T08:31:58.308Z'); const owner = 0; - const readdir = async () => [{ + + const readdir = stub().resolves([{ name: 'readdir.js', size: 1629, date, @@ -65,7 +71,7 @@ test('readify: result: should sort by name', async (t) => { date, owner, mode: 33_204, - }]; + }]); mockRequire('../lib/readdir', readdir); @@ -73,6 +79,7 @@ test('readify: result: should sort by name', async (t) => { const [, result] = await tryToCatch(readify, '.'); mockRequire.stop('../lib/readdir'); + stopAll(); t.deepEqual(result, expected, 'should get values'); t.end(); @@ -99,7 +106,7 @@ test('readify: result: raw', async (t) => { }], }; - const readdir = async () => [{ + const readdir = stub().resolves([{ name: 'readdir.js', size: 1629, date, @@ -111,14 +118,17 @@ test('readify: result: raw', async (t) => { date, owner, mode: 33_204, - }]; + }]); mockRequire('../lib/readdir', readdir); const readify = reRequire('../lib/readify'); - const result = await readify('.', {type: 'raw'}); + const result = await readify('.', { + type: 'raw', + }); mockRequire.stop('../lib/readdir'); + stopAll(); t.deepEqual(result, expected, 'should get values'); t.end(); @@ -132,14 +142,14 @@ test('readify: result: uid: 0', async (t) => { const owner = 0; const type = 'directory'; - const readdir = async () => [{ + const readdir = stub().resolves([{ name, size, date: mtime, owner, mode, type, - }]; + }]); mockRequire('../lib/readdir', readdir); @@ -163,6 +173,7 @@ test('readify: result: uid: 0', async (t) => { const result = await readify('.'); mockRequire.stop('../lib/readdir'); + stopAll(); t.deepEqual(result, expected, 'should get raw values'); t.end(); @@ -176,14 +187,14 @@ test('readify: result: nicki: no name found', async (t) => { const owner = Math.random(); const type = 'file'; - const readdir = async () => [{ + const readdir = stub().resolves([{ name, size, date: mtime, owner, mode, type, - }]; + }]); mockRequire('../lib/readdir', readdir); @@ -207,6 +218,7 @@ test('readify: result: nicki: no name found', async (t) => { const result = await readify('.'); mockRequire.stop('../lib/readdir'); + stopAll(); t.deepEqual(result, expected, 'should get values'); t.end(); @@ -216,10 +228,9 @@ test('result: files should have fields name, size, date, owner, mode, type', asy const {files} = await readify('.'); const {length} = files; - const fields = files - .filter((file) => Object - .keys(file) - .join(':') === 'name:size:date:owner:mode:type'); + const fields = files.filter((file) => Object + .keys(file) + .join(':') === 'name:size:date:owner:mode:type'); t.equal(fields.length, length, 'files array do not have fields: name, size, date, owner, mode, type'); t.end(); @@ -261,8 +272,9 @@ test('readify: nicki on win', async (t) => { }); mockRequire.stop('nicki'); + stopAll(); - t.notOk(nicki.called, 'nicki should not be called'); + t.notCalled(nicki, 'nicki should not be called'); t.end(); }); @@ -301,7 +313,7 @@ test('readify: result: sort: size (with dir)', async (t) => { }; const date = new Date('2017-01-12T09:01:35.288Z'); - const readdir = async () => [{ + const readdir = stub().resolves([{ name: 'readify.js', size: 3538, date, @@ -329,29 +341,36 @@ test('readify: result: sort: size (with dir)', async (t) => { owner: 0, mode: 33_204, type: 'directory', - }]; + }]); mockRequire('../lib/readdir', readdir); const readify = reRequire('../lib/readify'); const sort = 'size'; - const result = await readify('.', {sort}); + const result = await readify('.', { + sort, + }); mockRequire.stop('../lib/readdir'); + stopAll(); t.deepEqual(result, expected, 'should get values'); t.end(); }); test('readify: options: order: wrong', async (t) => { - const [e] = await tryToCatch(readify, '.', {order: 'wrong'}); + const [e] = await tryToCatch(readify, '.', { + order: 'wrong', + }); t.equal(e.message, 'order can be "asc" or "desc" only!', 'should throw when order is wrong'); t.end(); }); test('readify: options: sort: wrong', async (t) => { - const [e] = await tryToCatch(readify, '.', {sort: 5}); + const [e] = await tryToCatch(readify, '.', { + sort: 5, + }); t.equal(e.message, 'sort should be a string!', 'should throw when sortBy not string'); t.end(); @@ -365,11 +384,12 @@ test('readify: options: sort: name', async (t) => { ]; const sort = 'name'; - const data = await readify('./test/fixture/attr_sort', {sort}); - const sorted = data.files.map((file) => { - return file.name; + const data = await readify('./test/fixture/attr_sort', { + sort, }); + const sorted = data.files.map((file) => file.name); + t.deepEqual(sorted, files, 'should sort by name'); t.end(); }); @@ -386,10 +406,14 @@ test('readify: sort: name: desc', async (t) => { const getName = ({name}) => name; - const data = await readify('./test/fixture/attr_sort', {sort, order}); + const data = await readify('./test/fixture/attr_sort', { + sort, + order, + }); + data.files = data.files.map(getName); - t.deepEqual(data.files, files, 'should equal'); + t.deepEqual(data.files, files); t.end(); }); @@ -400,7 +424,11 @@ test('readify sort: size asc', async (t) => { '2.txt', ]; - const {files} = await readify('./test/fixture/attr_sort', {sort: 'size', order: 'asc'}); + const {files} = await readify('./test/fixture/attr_sort', { + sort: 'size', + order: 'asc', + }); + const sorted = files.map((file) => file.name); t.deepEqual(sorted, expected, 'correct order'); @@ -414,7 +442,11 @@ test('readify sort: size asc raw', async (t) => { '2.txt', ]; - const data = await readify('./test/fixture/attr_sort', {sort: 'size', type: 'raw'}); + const data = await readify('./test/fixture/attr_sort', { + sort: 'size', + type: 'raw', + }); + data.files = data.files.map((file) => file.name); t.deepEqual(data.files, files, 'correct order'); @@ -435,6 +467,7 @@ test('readify: nicki: error ', async (t) => { await readify(__dirname); mockRequire.stop('nicki'); + stopAll(); t.calledWith(fn, [e], 'should call callback when nicki has error'); t.end(); @@ -481,19 +514,20 @@ test('readify: nicki on android', async (t) => { type: 'file', }]; - const readdir = async () => files; + const readdir = stub().resolves(files); mockRequire('../lib/readdir', readdir); const readify = reRequire('../lib/readify'); const result = await readify('.'); + const expected = { path: './', files, }; mockRequire.stopAll(); + stopAll(); t.deepEqual(result, expected); t.end(); }); -