This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- refactor lib/read-package-files.js - added tests
- Loading branch information
Showing
4 changed files
with
76 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const fs = require('fs') | ||
const { promisify } = require('util') | ||
const readFile = promisify(fs.readFile) | ||
const stat = promisify(fs.stat) | ||
|
||
const packlist = require('npm-packlist') | ||
|
||
const readPackageFiles = async ({ files, path, prefix, refs }) => { | ||
const filenames = await packlist({ path }) | ||
const read = await Promise.all( | ||
filenames.map(filename => Promise.all([ | ||
filename, | ||
readFile(filename, { encoding: 'utf8' }), | ||
stat(filename) | ||
])) | ||
) | ||
|
||
for (const [filename, content, stat] of read) { | ||
files.add(filename) | ||
refs.set(`${prefix}${filename}`, { | ||
content, | ||
mode: stat.mode.toString(8) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = readPackageFiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* IMPORTANT | ||
* This snapshot file is auto-generated, but designed for humans. | ||
* It should be checked into source control and tracked carefully. | ||
* Re-generate by setting TAP_SNAPSHOT=1 and running tests. | ||
* Make sure to inspect the output below. Do not ignore changes! | ||
*/ | ||
'use strict' | ||
exports[`test/read-package-files.js TAP read files from a package cwd > should return list of filenames 1`] = ` | ||
index.js | ||
test.js | ||
package.json | ||
` | ||
|
||
exports[`test/read-package-files.js TAP read files from a package cwd > should return map of filenames to its contents 1`] = ` | ||
a/index.js: true | ||
a/test.js: true | ||
a/package.json: true | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const t = require('tap') | ||
|
||
const readPackageFiles = require('../lib/read-package-files.js') | ||
|
||
t.test('read files from a package cwd', async t => { | ||
const files = new Set() | ||
const refs = new Map() | ||
|
||
const path = t.testdir({ | ||
'package.json': JSON.stringify({ | ||
name: 'test-pkg', | ||
version: '1.0.0' | ||
}), | ||
'index.js': 'module.exports = () => "foo"', | ||
'test.js': '// TODO' | ||
}) | ||
|
||
await readPackageFiles({ | ||
files, | ||
path, | ||
prefix: 'a/', | ||
refs | ||
}) | ||
|
||
t.matchSnapshot([...files].join('\n'), 'should return list of filenames') | ||
t.matchSnapshot( | ||
[...refs.entries()].map(([k, v]) => `${k}: ${!!v}`).join('\n'), | ||
'should return map of filenames to its contents' | ||
) | ||
}) |