Skip to content
This repository was archived by the owner on Jan 6, 2022. It is now read-only.

Commit 8e18565

Browse files
committed
read-package-files
- refactor lib/read-package-files.js - added tests
1 parent 54c213b commit 8e18565

File tree

4 files changed

+76
-25
lines changed

4 files changed

+76
-25
lines changed

index.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
1-
const fs = require('fs')
2-
const { promisify } = require('util')
3-
41
const Arborist = require('@npmcli/arborist')
52
const pacote = require('pacote')
6-
const packlist = require('npm-packlist')
73
const rpj = require('read-package-json-fast')
84

95
const formatDiff = require('./lib/format-diff.js')
6+
const readPackageFiles = require('./lib/read-package-files.js')
107
const untar = require('./lib/untar.js')
118

12-
const readPackageFiles = async ({ files, path, prefix, refs }) => {
13-
const readFile = promisify(fs.readFile)
14-
const stat = promisify(fs.stat)
15-
const filenames = await packlist({ path })
16-
const read = await Promise.all(
17-
filenames.map(filename => Promise.all([
18-
filename,
19-
readFile(filename, { encoding: 'utf8' }),
20-
stat(filename)
21-
]))
22-
)
23-
24-
for (const [filename, content, stat] of read) {
25-
files.add(filename)
26-
refs.set(`${prefix}${filename}`, {
27-
content,
28-
mode: stat.mode.toString(8)
29-
})
30-
}
31-
}
32-
339
const diffSelf = async (opts = {}) => {
3410
const { prefix: path } = opts
3511

lib/read-package-files.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require('fs')
2+
const { promisify } = require('util')
3+
const readFile = promisify(fs.readFile)
4+
const stat = promisify(fs.stat)
5+
6+
const packlist = require('npm-packlist')
7+
8+
const readPackageFiles = async ({ files, path, prefix, refs }) => {
9+
const filenames = await packlist({ path })
10+
const read = await Promise.all(
11+
filenames.map(filename => Promise.all([
12+
filename,
13+
readFile(filename, { encoding: 'utf8' }),
14+
stat(filename)
15+
]))
16+
)
17+
18+
for (const [filename, content, stat] of read) {
19+
files.add(filename)
20+
refs.set(`${prefix}${filename}`, {
21+
content,
22+
mode: stat.mode.toString(8)
23+
})
24+
}
25+
}
26+
27+
module.exports = readPackageFiles
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/read-package-files.js TAP read files from a package cwd > should return list of filenames 1`] = `
9+
index.js
10+
test.js
11+
package.json
12+
`
13+
14+
exports[`test/read-package-files.js TAP read files from a package cwd > should return map of filenames to its contents 1`] = `
15+
a/index.js: true
16+
a/test.js: true
17+
a/package.json: true
18+
`

test/read-package-files.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const t = require('tap')
2+
3+
const readPackageFiles = require('../lib/read-package-files.js')
4+
5+
t.test('read files from a package cwd', async t => {
6+
const files = new Set()
7+
const refs = new Map()
8+
9+
const path = t.testdir({
10+
'package.json': JSON.stringify({
11+
name: 'test-pkg',
12+
version: '1.0.0'
13+
}),
14+
'index.js': 'module.exports = () => "foo"',
15+
'test.js': '// TODO'
16+
})
17+
18+
await readPackageFiles({
19+
files,
20+
path,
21+
prefix: 'a/',
22+
refs
23+
})
24+
25+
t.matchSnapshot([...files].join('\n'), 'should return list of filenames')
26+
t.matchSnapshot(
27+
[...refs.entries()].map(([k, v]) => `${k}: ${!!v}`).join('\n'),
28+
'should return map of filenames to its contents'
29+
)
30+
})

0 commit comments

Comments
 (0)