diff --git a/bin/gitignore.js b/bin/gitignore.js index 52699c5..04588b6 100644 --- a/bin/gitignore.js +++ b/bin/gitignore.js @@ -1,5 +1,137 @@ #!/usr/bin/env node -var path = require('path'); -var fs = require('fs'); -var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib/main.js'); -require(lib); + +const fs = require("fs"); +const OS = require("os"); +const commandLineArgs = require("command-line-args"); +const commandLineUsage = require("command-line-usage"); +const globalTunnel = require("global-tunnel-ng"); +const GitIgnore = require("../lib/library"); + +const args = [ + { + name: "type", + alias: "t", + type: String, + multiple: true, + defaultOption: true, + typeLabel: "[PROJECT TYPE]" + }, + { + name: "types", + type: Boolean, + defaultValue: false, + description: "Retreive a list of available project types" + }, + { + name: "proxy", + alias: "p", + type: String, + typeLabel: "[URL]", + description: "The URL of a proxy server to use" + }, + { + name: "help", + alias: "h", + type: Boolean, + description: "Display this help message" + } +]; + +const help = args => + console.log( + commandLineUsage([ + { + header: "Usage", + content: + "gitignore [PROJECT TYPE]\n\n" + + "Available project types can be found by running `gitignore --types` or at https://github.com/github/gitignore" + }, + { + header: "Example", + content: "gitignore rails" + }, + { + header: "Options", + optionList: args.map(arg => ({ + name: arg.name, + typeLabel: arg.typeLabel, + description: arg.description + })) + } + ]) + ); + +var options = {}; + +try { + options = commandLineArgs(args); +} catch (err) { + if (err.name == "UNKNOWN_OPTION") { + console.error("\n >>> %s <<<", err.message); + help(args); + } else { + console.error(err); + } + + return; +} + +if (options.proxy) { + process.env.HTTP_PROXY = options.proxy; + process.env.HTTPS_PROXY = options.proxy; + + globalTunnel.initialize(); +} + +if (options.types) { + console.log("Fetching available types..."); + + GitIgnore.getTypes((err, types) => { + if (err) { + if (err.statusCode) { + console.error( + "Could not access file from GitHub. Recieved status code " + + err.statusCode + ); + } else { + console.error("An unexpected error occurred."); + console.error(err); + } + + return; + } + + console.log(types.join(OS.EOL)); + }); +} else if (Array.isArray(options.type)) { + options.type.map(type => { + type = type.charAt(0).toUpperCase() + type.slice(1); + + GitIgnore.writeFile( + { + type: type, + file: fs.createWriteStream(".gitignore", { flags: "a" }) + }, + err => { + if (err) { + if (err.statusCode) { + console.log("There is no gitignore for " + type); + console.log( + "Available project types can be found by running `gitignore -types` or at https://github.com/github/gitignore" + ); + console.error("Recieved status code " + err.statusCode); + } else { + console.error("An unexpected error occurred."); + console.error(err); + } + + return; + } + + console.log("Created .gitignore file for type " + type + " :)"); + } + ); + }); +} else { + help(args); +} diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 33f1681..0000000 --- a/lib/main.js +++ /dev/null @@ -1,55 +0,0 @@ -"use strict"; - -var GitIgnore = require('./library'); -var fs = require('fs'); -var OS = require('os'); - -(function() { - var types = process.argv.slice(2); - - if (!types || types.length === 0) { - console.log("Usage: gitignore [PROJECT TYPE]"); - console.log("Example: gitignore rails"); - console.log("Available project types can be found by running `gitignore -types` or at https://github.com/github/gitignore"); - return; - } - - if (/^((--?)?types|-t)$/i.test(types.join())) { - console.log("Fetching available types..."); - GitIgnore.getTypes(function(err, types){ - if(err){ - if(err.statusCode){ - console.error("Could not access file from GitHub. Recieved status code "+err.statusCode); - } else { - console.error("An unexpected error occurred."); - console.error(err); - } - return; - } - console.log(types.join(OS.EOL)); - }); - } else { - types.forEach(function(type) { - type = type.charAt(0).toUpperCase() + type.slice(1); - var file = fs.createWriteStream(".gitignore", {'flags': 'a'}); - GitIgnore.writeFile({ - type: type, - file: file - }, function(err){ - if(err){ - if(err.statusCode){ - console.log("There is no gitignore for " + type); - console.log("Available project types can be found by running `gitignore -types` or at https://github.com/github/gitignore"); - console.error("Recieved status code "+err.statusCode); - } else { - console.error("An unexpected error occurred."); - console.error(err); - } - return; - } - console.log("Created .gitignore file for type "+type+" :)"); - }); - }); - } - -}).call(this); diff --git a/package.json b/package.json index b9dfe9a..bb06335 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "author": "Michael Feldstein", "name": "gitignore", "description": "Automatically fetch gitignore files for any project type from github into your new project", - "version": "0.5.0", + "version": "1.0.0", + "license": "MIT", "repository": { "url": "https://github.com/msfeldstein/gitignore" }, @@ -10,7 +11,11 @@ "bin": { "gitignore": "./bin/gitignore.js" }, - "dependencies": {}, + "dependencies": { + "command-line-args": "^5.0.2", + "command-line-usage": "^5.0.5", + "global-tunnel-ng": "^2.6.0" + }, "devDependencies": {}, "optionalDependencies": {}, "engines": {