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

Commit

Permalink
read-package-files
Browse files Browse the repository at this point in the history
- refactor lib/read-package-files.js
- added tests
  • Loading branch information
ruyadorno committed Jan 14, 2021
1 parent 54c213b commit 8e18565
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 25 deletions.
26 changes: 1 addition & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
const fs = require('fs')
const { promisify } = require('util')

const Arborist = require('@npmcli/arborist')
const pacote = require('pacote')
const packlist = require('npm-packlist')
const rpj = require('read-package-json-fast')

const formatDiff = require('./lib/format-diff.js')
const readPackageFiles = require('./lib/read-package-files.js')
const untar = require('./lib/untar.js')

const readPackageFiles = async ({ files, path, prefix, refs }) => {
const readFile = promisify(fs.readFile)
const stat = promisify(fs.stat)
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)
})
}
}

const diffSelf = async (opts = {}) => {
const { prefix: path } = opts

Expand Down
27 changes: 27 additions & 0 deletions lib/read-package-files.js
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
18 changes: 18 additions & 0 deletions tap-snapshots/test-read-package-files.js-TAP.test.js
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
`
30 changes: 30 additions & 0 deletions test/read-package-files.js
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'
)
})

0 comments on commit 8e18565

Please sign in to comment.