From 6de9475334dea3f9944cc3f542747e94cd7c54ce Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 2 Jan 2021 20:29:05 +0100 Subject: [PATCH] Added support for custom output folders closes #3 --- .gitignore | 2 +- bin/exporter.js | 56 +++++++++++++++++++++++++++++++++++-------------- package.json | 21 +++++++++++++++---- 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index a2ffac2..241ca7c 100644 --- a/.gitignore +++ b/.gitignore @@ -126,4 +126,4 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* - +*lock* \ No newline at end of file diff --git a/bin/exporter.js b/bin/exporter.js index 20ddd9b..e30a236 100644 --- a/bin/exporter.js +++ b/bin/exporter.js @@ -2,15 +2,39 @@ 'use strict'; const fs = require('fs'); +const yargs = require('yargs'); -const args = process.argv.slice(2); -if (args.includes('--help')) { - console.log(`Arguments: - --help: View this help page - --recursive: Include all dependencies' subdependencies - --json: Exports the license information into ./licenses.json as json - --md: Exports the license information into ./licenses.md as markdown`); -} +const args = yargs + .option('json', { + alias: 'j', + description: 'Exports the license information into ./licenses.json as json.', + type: 'boolean', + }) + .option('pretty', { + alias: 'p', + description: 'Prettify the json output.', + type: 'boolean', + }) + .option('markdown', { + alias: 'm', + description: 'Exports the license information into ./licenses.md as markdown.', + type: 'boolean', + }) + .option('recursive', { + alias: 'r', + description: 'Include all of the dependencies\' subdependencies.', + type: 'boolean', + }) + .option('output', { + alias : 'o', + describe: 'Output folder for the exports. (Default: Current folder)', + type: 'string', + default: '.' + }) + .help() + .alias('help', 'h') + .alias('v', 'version') + .argv; function parsePackageInfo(path) { const packagecontents = JSON.parse(fs.readFileSync(path, { encoding: 'utf-8' })); @@ -54,20 +78,20 @@ function getDependencyLicenseInfo(all_dependencies, recursive) { return all; } const packageInfo = parsePackageInfo(`./package.json`); -const all = getDependencyLicenseInfo(mergeDependencies(packageInfo), args.includes('--recursive')); +const all = getDependencyLicenseInfo(mergeDependencies(packageInfo), args.recursive); -if (args.includes('--json')) { - if (args.includes('--pretty')) { - fs.writeFileSync('./licenses.json', JSON.stringify(all, null, 4)); +if (args.json) { + if (args.pretty) { + fs.writeFileSync((args.output+'/licenses.json'), JSON.stringify(all, null, 4)); } else { - fs.writeFileSync('./licenses.json', JSON.stringify(all)); + fs.writeFileSync((args.output+'/licenses.json'), JSON.stringify(all)); } } -if (args.includes('--md')) { - fs.writeFileSync('./licenses.md', ''); +if (args.markdown) { + fs.writeFileSync((args.output+'/licenses.md'), ''); all.forEach((p) => { fs.appendFileSync( - './licenses.md', + (args.output+'/licenses.md'), `# ${p.name}\n**Author**: ${p.author}\n**Repo**: ${p.repo}\n**License**: ${p.license}\n**Description**: ${p.description}\n## License Text\n${p.licensetext} \n\n` ); }); diff --git a/package.json b/package.json index 69f6526..74ef8b0 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,12 @@ { "name": "@odit/license-exporter", - "version": "0.0.6", + "version": "0.0.7", "description": "A simple license crawler", - "keywords": [ "license", "ODIT.Services", "cli" ], + "keywords": [ + "license", + "ODIT.Services", + "cli" + ], "repository": { "type": "git", "url": "https://git.odit.services/odit/license-exporter" @@ -26,7 +30,16 @@ ], "main": "exporter.js", "bin": { - "license-exporter": "./bin/exporter.js" + "license-exporter": "bin/exporter.js" }, - "files": [ "bin", "exporter.js", "README.md", "package.json", "LICENSE" ] + "files": [ + "bin", + "exporter.js", + "README.md", + "package.json", + "LICENSE" + ], + "dependencies": { + "yargs": "^16.2.0" + } }