From e6b94935e73474612ce655cd777b2dcad5e0f324 Mon Sep 17 00:00:00 2001 From: Chickenbread Date: Tue, 23 Jul 2019 09:04:02 +0200 Subject: [PATCH] Fixed Bug where JSON was still Outputted, even though Help was requested --- README.md | 2 +- index.js | 174 ++++++++++++++++++++++++++------------------------- package.json | 2 +- 3 files changed, 90 insertions(+), 88 deletions(-) diff --git a/README.md b/README.md index e9eda17..b1f60fe 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Outputs all npm Packages as well as Dependencies within a Node.JS Project as JSON. # How to use -Run `npx npmLister` on the project. +Run `npx npmlister` on the project. ## Arguments `-?` diff --git a/index.js b/index.js index 7473648..984ed0a 100644 --- a/index.js +++ b/index.js @@ -24,110 +24,112 @@ if (process.argv.indexOf('-?') >= 0) { '\t-o [PATH]\tSpecifies a file-output' ); } -if (process.argv.indexOf('-obj') >= 0) { - array = false; -} -if (process.argv.indexOf('-p') >= 0 && process.argv.indexOf('-p') + 1 < process.argv.length) { - runPath = process.argv[process.argv.indexOf('-p') + 1] -} - -var allPackages; - -if (array) { - allPackages = [] -} else { - allPackages = {} -} + if (process.argv.indexOf('-obj') >= 0) { + array = false; + } + if (process.argv.indexOf('-p') >= 0 && process.argv.indexOf('-p') + 1 < process.argv.length) { + runPath = process.argv[process.argv.indexOf('-p') + 1] + } -// Returns the contents of the package.json -function readPackageData(modulePath) { - var rawdata = fs.readFileSync(path.resolve(runPath, 'node_modules', modulePath, 'package.json')); - var package = JSON.parse(rawdata); + var allPackages; - return package; -} -// Returns the contents of the license file -function readLicenseFile(modulePath) { - if (fs.existsSync(path.resolve(runPath, 'node_modules', modulePath, 'LICENSE'))) { - var licenseText = fs.readFileSync(path.resolve(runPath, 'node_modules', modulePath, 'LICENSE'), { encoding: 'utf8'}); - return licenseText + if (array) { + allPackages = [] } else { - return '' + allPackages = {} } -} -// Adds a package with it's license to the allPackages object -function addPackage(modulePath) { - // Gets the package.json and the license - var package = readPackageData(modulePath); - var license = readLicenseFile(modulePath); + // Returns the contents of the package.json + function readPackageData(modulePath) { + var rawdata = fs.readFileSync(path.resolve(runPath, 'node_modules', modulePath, 'package.json')); + var package = JSON.parse(rawdata); - if (typeof package.author !== 'object') { - package.author = { - name: '', - email: '' + return package; + } + // Returns the contents of the license file + function readLicenseFile(modulePath) { + if (fs.existsSync(path.resolve(runPath, 'node_modules', modulePath, 'LICENSE'))) { + var licenseText = fs.readFileSync(path.resolve(runPath, 'node_modules', modulePath, 'LICENSE'), { encoding: 'utf8'}); + return licenseText + } + else { + return '' } } - // Adds it all to the AllPackages Object - if (array) { - allPackages.push({ - name: package.name, - author: package.author, - description: package.description, - license: package.license, - licenseText: license, - homepage: package.homepage, - version: package.version - }); - } - else { - allPackages[package.name] = { - author: package.author, - description: package.description, - license: package.license, - licenseText: license, - homepage: package.homepage, - version: package.version - }; + // Adds a package with it's license to the allPackages object + function addPackage(modulePath) { + // Gets the package.json and the license + var package = readPackageData(modulePath); + var license = readLicenseFile(modulePath); + + if (typeof package.author !== 'object') { + package.author = { + name: '', + email: '' + } + } + + // Adds it all to the AllPackages Object + if (array) { + allPackages.push({ + name: package.name, + author: package.author, + description: package.description, + license: package.license, + licenseText: license, + homepage: package.homepage, + version: package.version + }); + } + else { + allPackages[package.name] = { + author: package.author, + description: package.description, + license: package.license, + licenseText: license, + homepage: package.homepage, + version: package.version + }; + } } -} -if (fs.existsSync(path.resolve(runPath, 'node_modules'))) { - // Gets an Array of all node_modules - var allDirs = getDirectories(path.resolve(runPath, 'node_modules')); + if (fs.existsSync(path.resolve(runPath, 'node_modules'))) { + // Gets an Array of all node_modules + var allDirs = getDirectories(path.resolve(runPath, 'node_modules')); - for (var i = 0; i < allDirs.length; i++) { - // Only if the folder isn't a "hidden" one, check if it's a module collection or not - if (allDirs[i].indexOf('.') !== 0) { - if (allDirs[i].indexOf('@') === 0) { - // go through all the modules within a module collection - var tmpDirs = getDirectories(path.resolve(runPath, 'node_modules', allDirs[i])) - for (var j = 0; j < tmpDirs.length; j++) { - if (tmpDirs[j].indexOf('.') !== 0 && tmpDirs[j].indexOf('@') !== 0) { - addPackage(allDirs[i] + '/' + tmpDirs[j]); + for (var i = 0; i < allDirs.length; i++) { + // Only if the folder isn't a "hidden" one, check if it's a module collection or not + if (allDirs[i].indexOf('.') !== 0) { + if (allDirs[i].indexOf('@') === 0) { + // go through all the modules within a module collection + var tmpDirs = getDirectories(path.resolve(runPath, 'node_modules', allDirs[i])) + for (var j = 0; j < tmpDirs.length; j++) { + if (tmpDirs[j].indexOf('.') !== 0 && tmpDirs[j].indexOf('@') !== 0) { + addPackage(allDirs[i] + '/' + tmpDirs[j]); + } } } - } - else { - // add the module - addPackage(allDirs[i]); + else { + // add the module + addPackage(allDirs[i]); + } } } - } - // output the allPackages Object - // Checks wether the OutputFile Arg is given - if (process.argv.indexOf('-o') >= 0 && process.argv.indexOf('-o') + 1 < process.argv.length) { - // Writes Packagedata to specified file - var outputFile = process.argv[process.argv.indexOf('-o') + 1]; - fs.writeFileSync(outputFile, JSON.stringify(allPackages), { encoding: 'utf8' }); - console.log('File written: ' + path.resolve(outputFile)); - } - else { - // Outputs Packagedata to Console (Useful for pipes) - console.log(JSON.stringify(allPackages)); + // output the allPackages Object + // Checks wether the OutputFile Arg is given + if (process.argv.indexOf('-o') >= 0 && process.argv.indexOf('-o') + 1 < process.argv.length) { + // Writes Packagedata to specified file + var outputFile = process.argv[process.argv.indexOf('-o') + 1]; + fs.writeFileSync(outputFile, JSON.stringify(allPackages), { encoding: 'utf8' }); + console.log('File written: ' + path.resolve(outputFile)); + } + else { + // Outputs Packagedata to Console (Useful for pipes) + console.log(JSON.stringify(allPackages)); + } } } diff --git a/package.json b/package.json index b165d2c..296b515 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "npmlister", - "version": "1.1.1", + "version": "1.1.2", "description": "Creates a JSON file with all Descriptions and Licenses of your installed Node Modules", "main": "index.js", "scripts": {