Skip to content

Commit

Permalink
Fixed Bug where JSON was still Outputted, even though Help was requested
Browse files Browse the repository at this point in the history
  • Loading branch information
Chickenbreadlp committed Jul 23, 2019
1 parent 9be1bc9 commit e6b9493
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
`-?`
Expand Down
174 changes: 88 additions & 86 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit e6b9493

Please sign in to comment.