Skip to content

Commit

Permalink
deps: remove dep on @npmcli/fs
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Oct 18, 2022
1 parent 397ba75 commit 59921bc
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 83 deletions.
3 changes: 0 additions & 3 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ graph LR;
npm-->npmcli-config["@npmcli/config"];
npm-->npmcli-docs["@npmcli/docs"];
npm-->npmcli-eslint-config["@npmcli/eslint-config"];
npm-->npmcli-fs["@npmcli/fs"];
npm-->npmcli-git["@npmcli/git"];
npm-->npmcli-map-workspaces["@npmcli/map-workspaces"];
npm-->npmcli-package-json["@npmcli/package-json"];
Expand Down Expand Up @@ -741,7 +740,6 @@ graph LR;
npm-->npmcli-config["@npmcli/config"];
npm-->npmcli-docs["@npmcli/docs"];
npm-->npmcli-eslint-config["@npmcli/eslint-config"];
npm-->npmcli-fs["@npmcli/fs"];
npm-->npmcli-git["@npmcli/git"];
npm-->npmcli-map-workspaces["@npmcli/map-workspaces"];
npm-->npmcli-package-json["@npmcli/package-json"];
Expand Down Expand Up @@ -854,7 +852,6 @@ graph LR;
npmcli-docs-->npmcli-template-oss["@npmcli/template-oss"];
npmcli-docs-->tap;
npmcli-docs-->yaml;
npmcli-fs-->gar-promisify["@gar/promisify"];
npmcli-fs-->semver;
npmcli-git-->lru-cache;
npmcli-git-->mkdirp;
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// as an array.
//

const fs = require('@npmcli/fs')
const fs = require('fs/promises')
const nopt = require('nopt')

const { definitions, shorthands } = require('../utils/config/index.js')
Expand Down
6 changes: 3 additions & 3 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { resolve, dirname, join } = require('path')
const Config = require('@npmcli/config')
const chalk = require('chalk')
const which = require('which')
const fs = require('@npmcli/fs')
const fs = require('fs/promises')

// Patch the global fs module here at the app level
require('graceful-fs').gracefulify(require('fs'))
Expand Down Expand Up @@ -246,13 +246,13 @@ class Npm extends EventEmitter {
// a cache dir, but we don't want to fail immediately since
// the command might not need a cache dir (like `npm --version`)
await this.time('npm:load:mkdirpcache', () =>
fs.mkdir(this.cache, { recursive: true, owner: 'inherit' })
fs.mkdir(this.cache, { recursive: true })
.catch((e) => log.verbose('cache', `could not create cache: ${e}`)))

// its ok if this fails. user might have specified an invalid dir
// which we will tell them about at the end
await this.time('npm:load:mkdirplogs', () =>
fs.mkdir(this.logsDir, { recursive: true, owner: 'inherit' })
fs.mkdir(this.logsDir, { recursive: true })
.catch((e) => log.verbose('logfile', `could not create logs-dir: ${e}`)))

// note: this MUST be shorter than the actual argv length, because it
Expand Down
8 changes: 2 additions & 6 deletions lib/utils/exit-handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const os = require('os')
const fs = require('@npmcli/fs')
const fs = require('fs')

const log = require('./log-shim.js')
const errorMessage = require('./error-message.js')
Expand Down Expand Up @@ -186,11 +186,7 @@ const exitHandler = err => {
file = `${npm.logPath}${file}`
content = `'Log files:\n${npm.logFiles.join('\n')}\n\n${content.trim()}\n`
try {
fs.withOwnerSync(
file,
() => fs.writeFileSync(file, content),
{ owner: 'inherit' }
)
fs.writeFileSync(file, content)
detail.push(['', `\n\nFor a full report see:\n${file}`])
} catch (err) {
log.warn('', `Could not write error message to ${file} due to ${err}`)
Expand Down
7 changes: 1 addition & 6 deletions lib/utils/log-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const rimraf = promisify(require('rimraf'))
const glob = promisify(require('glob'))
const MiniPass = require('minipass')
const fsMiniPass = require('fs-minipass')
const fs = require('@npmcli/fs')
const log = require('./log-shim')

const padZero = (n, length) => n.toString().padStart(length.toString().length, '0')
Expand Down Expand Up @@ -174,11 +173,7 @@ class LogFiles {
// during process.on('exit') which has to be synchronous. So in order
// to never drop log messages, it is easiest to make it sync all the time
// and this was measured to be about 1.5% slower for 40k lines of output
const logStream = fs.withOwnerSync(
f,
() => new fsMiniPass.WriteStreamSync(f, { flags: 'a' }),
{ owner: 'inherit' }
)
const logStream = new fsMiniPass.WriteStreamSync(f, { flags: 'a' })
if (count > 0) {
// Reset file log count if we are opening
// after our first file
Expand Down
8 changes: 2 additions & 6 deletions lib/utils/timers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const EE = require('events')
const fs = require('@npmcli/fs')
const fs = require('fs')
const log = require('./log-shim')

// This is an event emiiter but on/off
Expand Down Expand Up @@ -90,11 +90,7 @@ class Timers extends EE {
return acc
}, {}),
}
fs.withOwnerSync(
this.file,
() => fs.writeFileSync(this.file, JSON.stringify(content) + '\n'),
{ owner: 'inherit' }
)
fs.writeFileSync(this.file, JSON.stringify(content) + '\n')
} catch (e) {
this.file = null
log.warn('timing', `could not write timing file: ${e}`)
Expand Down
2 changes: 0 additions & 2 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@npmcli/arborist",
"@npmcli/ci-detect",
"@npmcli/config",
"@npmcli/fs",
"@npmcli/map-workspaces",
"@npmcli/package-json",
"@npmcli/run-script",
Expand Down Expand Up @@ -93,7 +92,6 @@
"@npmcli/arborist": "^6.0.0-pre.4",
"@npmcli/ci-detect": "^3.0.0",
"@npmcli/config": "^6.0.1",
"@npmcli/fs": "^2.1.0",
"@npmcli/map-workspaces": "^3.0.0",
"@npmcli/package-json": "^3.0.0",
"@npmcli/run-script": "^5.0.0",
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"@npmcli/arborist": "^6.0.0-pre.4",
"@npmcli/ci-detect": "^3.0.0",
"@npmcli/config": "^6.0.1",
"@npmcli/fs": "^2.1.0",
"@npmcli/map-workspaces": "^3.0.0",
"@npmcli/package-json": "^3.0.0",
"@npmcli/run-script": "^5.0.0",
Expand Down Expand Up @@ -134,7 +133,6 @@
"@npmcli/arborist",
"@npmcli/ci-detect",
"@npmcli/config",
"@npmcli/fs",
"@npmcli/map-workspaces",
"@npmcli/package-json",
"@npmcli/run-script",
Expand Down
2 changes: 1 addition & 1 deletion scripts/bundle-and-gitignore-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const packlist = require('npm-packlist')
const git = require('@npmcli/git')
const { resolve, join, relative } = require('path')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const fs = require('@npmcli/fs')
const fs = require('fs/promises')
const PackageJson = require('@npmcli/package-json')

const RM_FLAG = '--remove-ignored-files'
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const MockRegistry = require('../../fixtures/mock-registry.js')
const mockGlobals = require('../../fixtures/mock-globals')

const cacache = require('cacache')
const fs = require('@npmcli/fs')
const fs = require('fs')
const path = require('path')

const pkg = 'test-package'
Expand Down
30 changes: 9 additions & 21 deletions test/lib/commands/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { load: _loadMockNpm } = require('../../fixtures/mock-npm')
const MockRegistry = require('../../fixtures/mock-registry.js')

const path = require('path')
const fs = require('@npmcli/fs')
const fs = require('fs')

// t.cleanSnapshot = str => str.replace(/ in [0-9ms]+/g, ' in {TIME}')

Expand Down Expand Up @@ -73,16 +73,10 @@ t.test('reifies, audits, removes node_modules', async t => {
registry.nock.post('/-/npm/v1/security/advisories/bulk').reply(200, {})
await npm.exec('ci', [])
t.match(joinedOutput(), 'added 1 package, and audited 2 packages in')
await t.resolveMatch(
fs.exists(path.join(npm.prefix, 'node_modules', 'test')),
false,
'existing node_modules is removed'
)
await t.resolveMatch(
fs.exists(path.join(npm.prefix, 'node_modules', 'abbrev')),
true,
'installs abbrev'
)
const nmTest = path.join(npm.prefix, 'node_modules', 'test')
t.equal(fs.existsSync(nmTest), false, 'existing node_modules is removed')
const nmAbbrev = path.join(npm.prefix, 'node_modules', 'abbrev')
t.equal(fs.existsSync(nmAbbrev), true, 'installs abbrev')
})

t.test('--no-audit and --ignore-scripts', async t => {
Expand Down Expand Up @@ -159,11 +153,8 @@ t.test('should throw if package-lock.json or npm-shrinkwrap missing', async t =>
},
})
await t.rejects(npm.exec('ci', []), { code: 'EUSAGE', message: /package-lock.json/ })
await t.resolveMatch(
fs.exists(path.join(npm.prefix, 'node_modules', 'test-file')),
true,
'does not remove node_modules'
)
const nmTestFile = path.join(npm.prefix, 'node_modules', 'test-file')
t.equal(fs.existsSync(nmTestFile), true, 'does not remove node_modules')
})

t.test('should throw ECIGLOBAL', async t => {
Expand Down Expand Up @@ -193,9 +184,6 @@ t.test('should throw error when ideal inventory mismatches virtual', async t =>
npm.exec('ci', []),
{ code: 'EUSAGE', message: /in sync/ }
)
await t.resolveMatch(
fs.exists(path.join(npm.prefix, 'node_modules', 'test-file')),
true,
'does not remove node_modules'
)
const nmTestFile = path.join(npm.prefix, 'node_modules', 'test-file')
t.equal(fs.existsSync(nmTestFile), true, 'does not remove node_modules')
})
2 changes: 1 addition & 1 deletion test/lib/commands/exec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const t = require('tap')
const fs = require('@npmcli/fs')
const fs = require('fs/promises')
const path = require('path')
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
Expand Down
32 changes: 14 additions & 18 deletions test/lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const MockRegistry = require('../../fixtures/mock-registry.js')
const pacote = require('pacote')
const Arborist = require('@npmcli/arborist')
const path = require('path')
const fs = require('@npmcli/fs')
const fs = require('fs')
const npa = require('npm-package-arg')

const pkg = 'test-package'
Expand Down Expand Up @@ -80,14 +80,10 @@ t.test('respects publishConfig.registry, runs appropriate scripts', async t => {
}).reply(200, {})
await npm.exec('publish', [])
t.matchSnapshot(joinedOutput(), 'new package version')
t.resolveMatch(fs.exists(path.join(prefix, 'scripts-prepublishonly')), true, 'ran prepublishOnly')
t.resolveMatch(
fs.exists(path.join(prefix, 'scripts-prepublish')),
false,
'did not run prepublish'
)
t.resolveMatch(fs.exists(path.join(prefix, 'scripts-publish')), true, 'ran publish')
t.resolveMatch(fs.exists(path.join(prefix, 'scripts-postpublish')), true, 'ran postpublish')
t.equal(fs.existsSync(path.join(prefix, 'scripts-prepublishonly')), true, 'ran prepublishOnly')
t.equal(fs.existsSync(path.join(prefix, 'scripts-prepublish')), false, 'did not run prepublish')
t.equal(fs.existsSync(path.join(prefix, 'scripts-publish')), true, 'ran publish')
t.equal(fs.existsSync(path.join(prefix, 'scripts-postpublish')), true, 'ran postpublish')
})

t.test('re-loads publishConfig.registry if added during script process', async t => {
Expand Down Expand Up @@ -230,7 +226,7 @@ t.test('tarball', async t => {
})
const tarball = await pacote.tarball(home, { Arborist })
const tarFilename = path.join(home, 'tarball.tgz')
await fs.writeFile(tarFilename, tarball)
fs.writeFileSync(tarFilename, tarball)
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
Expand Down Expand Up @@ -612,23 +608,23 @@ t.test('ignore-scripts', async t => {
registry.nock.put(`/${pkg}`).reply(200, {})
await npm.exec('publish', [])
t.matchSnapshot(joinedOutput(), 'new package version')
t.resolveMatch(
fs.exists(path.join(prefix, 'scripts-prepublishonly')),
t.equal(
fs.existsSync(path.join(prefix, 'scripts-prepublishonly')),
false,
'did not run prepublishOnly'
)
t.resolveMatch(
fs.exists(path.join(prefix, 'scripts-prepublish')),
t.equal(
fs.existsSync(path.join(prefix, 'scripts-prepublish')),
false,
'did not run prepublish'
)
t.resolveMatch(
fs.exists(path.join(prefix, 'scripts-publish')),
t.equal(
fs.existsSync(path.join(prefix, 'scripts-publish')),
false,
'did not run publish'
)
t.resolveMatch(
fs.exists(path.join(prefix, 'scripts-postpublish')),
t.equal(
fs.existsSync(path.join(prefix, 'scripts-postpublish')),
false,
'did not run postpublish'
)
Expand Down
2 changes: 1 addition & 1 deletion test/lib/docs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { join, resolve, basename, extname, dirname } = require('path')
const fs = require('@npmcli/fs')
const fs = require('fs/promises')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const docs = require('@npmcli/docs')

Expand Down
4 changes: 2 additions & 2 deletions test/lib/npm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { resolve, dirname, join } = require('path')
const fs = require('@npmcli/fs')
const fs = require('fs')

const { load: loadMockNpm } = require('../fixtures/mock-npm.js')
const mockGlobals = require('../fixtures/mock-globals')
Expand Down Expand Up @@ -448,7 +448,7 @@ t.test('debug log', async t => {
const logsDir = join(testdir, 'my_logs_dir')

// make logs dir a file before load so it files
await fs.writeFile(logsDir, 'A_TEXT_FILE')
fs.writeFileSync(logsDir, 'A_TEXT_FILE')
await t.resolves(npm.load(), 'loads with invalid logs dir')

t.equal(npm.logFiles.length, 0, 'no log files array')
Expand Down
Loading

0 comments on commit 59921bc

Please sign in to comment.