Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert files to ES6 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bin/gitignore.js
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 12 additions & 12 deletions lib/library.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -48,4 +45,7 @@ GitIgnore.writeFile = function(options, callback){
}).on("error", callback);
};

module.exports = GitIgnore;
export default {
getTypes,
writeFile,
};
18 changes: 9 additions & 9 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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} :)`);
});
});
}
Expand Down