forked from npm/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: issue npm#7892 - fix for npm install creating directories and em…
…pty package.json file
- Loading branch information
1 parent
6995303
commit 4fccaa7
Showing
6 changed files
with
242 additions
and
11 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
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,26 @@ | ||
const fs = require('node:fs') | ||
const path = require('node:path') | ||
|
||
// Validates that a package.json exists in the target directory | ||
function validateProjectStructure (prefix) { | ||
const projectPath = prefix || process.cwd() | ||
const packageJsonPath = path.join(projectPath, 'package.json') | ||
|
||
// Check if directory exists when --prefix is used | ||
if (prefix && !fs.existsSync(projectPath)) { | ||
const err = new Error(`Dir "${projectPath}" does not exist. Run "npm init" first.`) | ||
err.code = 'ENOPROJECT' | ||
throw err | ||
} | ||
|
||
// Check for package.json | ||
if (!fs.existsSync(packageJsonPath)) { | ||
const err = new Error('No package.json found. Run "npm init" to create a new package.') | ||
err.code = 'ENOPROJECT' | ||
throw err | ||
} | ||
|
||
return true | ||
} | ||
|
||
module.exports = validateProjectStructure |
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
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,47 @@ | ||
const t = require('tap') | ||
|
||
// Mock fs.existsSync to control file existence checks | ||
const mockFs = { | ||
existsSync: () => true, | ||
} | ||
|
||
const validate = t.mock('../../../lib/utils/validate-project.js', { | ||
'node:fs': mockFs, | ||
}) | ||
|
||
t.test('validate project structure', async t => { | ||
t.test('returns true when package.json exists', async t => { | ||
mockFs.existsSync = () => true | ||
t.equal(validate('/some/path'), true, 'should validate successfully') | ||
}) | ||
|
||
t.test('uses cwd() when no prefix provided', async t => { | ||
mockFs.existsSync = () => true | ||
t.equal(validate(), true, 'should validate successfully with default path') | ||
}) | ||
|
||
t.test('throws ENOPROJECT when directory does not exist', async t => { | ||
mockFs.existsSync = () => false | ||
t.throws( | ||
() => validate('/non-existent-dir'), | ||
{ | ||
code: 'ENOPROJECT', | ||
message: 'Dir "/non-existent-dir" does not exist. Run "npm init" to begin.', | ||
}, | ||
'should throw correct error for missing directory' | ||
) | ||
}) | ||
|
||
t.test('throws ENOPROJECT when package.json is missing', async t => { | ||
// Directory exists but package.json doesn't | ||
mockFs.existsSync = (p) => !p.endsWith('package.json') | ||
t.throws( | ||
() => validate('/some/path'), | ||
{ | ||
code: 'ENOPROJECT', | ||
message: 'No package.json found. Run "npm init" to create a new package.', | ||
}, | ||
'should throw correct error for missing package.json' | ||
) | ||
}) | ||
}) |