File tree Expand file tree Collapse file tree 2 files changed +15
-5
lines changed
Expand file tree Collapse file tree 2 files changed +15
-5
lines changed Original file line number Diff line number Diff line change 11const nodeExternals = require ( '../index.js' ) ;
2+ const path = require ( 'path' ) ;
23const utils = require ( '../utils.js' ) ;
34const testUtils = require ( './test-utils.js' ) ;
45const mockNodeModules = testUtils . mockNodeModules ;
@@ -326,3 +327,11 @@ describe('validate options', function () {
326327 expect ( results [ 0 ] . correctTerm ) . to . be . equal ( 'modulesDir' ) ;
327328 } ) ;
328329} ) ;
330+
331+ describe ( 'file path' , function ( ) {
332+ it ( 'should return the correct file path' , function ( ) {
333+ expect ( utils . getFilePath ( { fileName : 'file.json' } ) ) . to . be . equal ( path . resolve ( process . cwd ( ) , 'file.json' ) ) ;
334+ expect ( utils . getFilePath ( { } ) ) . to . be . equal ( path . resolve ( process . cwd ( ) , 'package.json' ) ) ;
335+ expect ( utils . getFilePath ( { fileName : 'c:\\test\\test.js' } ) ) . to . be . equal ( 'c:\\test\\test.js' ) ;
336+ } )
337+ } )
Original file line number Diff line number Diff line change @@ -38,6 +38,11 @@ exports.readDir = function readDir(dirName) {
3838 }
3939} ;
4040
41+ function getFilePath ( options ) {
42+ return path . resolve ( process . cwd ( ) , options . fileName || 'package.json' ) ;
43+ }
44+ exports . getFilePath = getFilePath ;
45+
4146exports . readFromPackageJson = function readFromPackageJson ( options ) {
4247 if ( typeof options !== 'object' ) {
4348 options = { } ;
@@ -48,11 +53,7 @@ exports.readFromPackageJson = function readFromPackageJson(options) {
4853 // read the file
4954 let packageJson ;
5055 try {
51- const fileName = options . fileName || 'package.json' ;
52- const packageJsonString = fs . readFileSync (
53- path . resolve ( process . cwd ( ) , fileName ) ,
54- 'utf8'
55- ) ;
56+ const packageJsonString = fs . readFileSync ( getFilePath ( options ) , 'utf8' ) ;
5657 packageJson = JSON . parse ( packageJsonString ) ;
5758 } catch ( e ) {
5859 return [ ] ;
You can’t perform that action at this time.
0 commit comments