diff --git a/bin/gitignore.js b/bin/gitignore.js index 52699c5..9240fa7 100644 --- a/bin/gitignore.js +++ b/bin/gitignore.js @@ -1,5 +1,5 @@ #!/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); +import path form 'path'; +import fs form 'fs'; +let lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib/main.js'); +require(lib) \ No newline at end of file diff --git a/lib/library.js b/lib/library.js index 3183d26..509b90b 100644 --- a/lib/library.js +++ b/lib/library.js @@ -1,22 +1,19 @@ "use strict"; -var https = require('https'); +import https from 'https'; -var GitIgnore = {}; - -GitIgnore.getTypes = function(callback){ +let getTypes = (callback) => { var types = []; - https.get({host: "api.github.com", path: "/repos/github/gitignore/contents", headers: {"User-Agent": "gitignore node app"}}, function(res) { + https.get({host: "api.github.com", path: "/repos/github/gitignore/contents", headers: {"User-Agent": "gitignore node app"}}, (res) => { if (res.statusCode !== 200) { var err = new Error("somethingWentWrong"); err.statusCode = res.statusCode; return callback(err); } var body = ""; - res.on("data", function(chunk) { - body += chunk; - }); - res.on("end", function() { + res.on("data", chunk => { body += chunk }); + + res.on("end", () => { var json = JSON.parse(body); for(var i = 0; i < json.length; ++i) { var name = json[i].name; @@ -30,14 +27,14 @@ GitIgnore.getTypes = function(callback){ }).on("error", callback); }; -GitIgnore.writeFile = function(options, callback){ +let writeFile = (options, callback) => { if(!options.type){ return callback(new Error("noTypeProvided")); } if(!options.file && !options.writable){ return callback(new Error("noWritableProvided")); } - https.get("https://raw.githubusercontent.com/github/gitignore/master/" + options.type + ".gitignore", function(res) { + https.get(`https://raw.githubusercontent.com/github/gitignore/master/${options.type}.gitignore`, (res) => { if (res.statusCode !== 200) { var err = new Error("typeDoesNotExist"); err.statusCode = res.statusCode; @@ -48,4 +45,7 @@ GitIgnore.writeFile = function(options, callback){ }).on("error", callback); }; -module.exports = GitIgnore; +export default { + getTypes, + writeFile, +}; \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index 33f1681..825336f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -4,7 +4,7 @@ var GitIgnore = require('./library'); var fs = require('fs'); var OS = require('os'); -(function() { +(() => { var types = process.argv.slice(2); if (!types || types.length === 0) { @@ -16,10 +16,10 @@ var OS = require('os'); if (/^((--?)?types|-t)$/i.test(types.join())) { console.log("Fetching available types..."); - GitIgnore.getTypes(function(err, types){ + GitIgnore.getTypes((err, types) => { if(err){ if(err.statusCode){ - console.error("Could not access file from GitHub. Recieved status code "+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); @@ -29,25 +29,25 @@ var OS = require('os'); console.log(types.join(OS.EOL)); }); } else { - types.forEach(function(type) { + types.forEach((type) => { type = type.charAt(0).toUpperCase() + type.slice(1); var file = fs.createWriteStream(".gitignore", {'flags': 'a'}); GitIgnore.writeFile({ - type: type, - file: file + type, + file }, function(err){ if(err){ if(err.statusCode){ - console.log("There is no gitignore for " + type); + 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); + 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+" :)"); + console.log(`Created .gitignore file for type ${type} :)`); }); }); }