Skip to content

Commit f7223e2

Browse files
committed
Do our best to guess the language in git init
1 parent c75ed34 commit f7223e2

File tree

4 files changed

+59
-50
lines changed

4 files changed

+59
-50
lines changed

api.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ exports.api = function(args, opts) {
8787
};
8888

8989
exports.load = function(action) {
90-
//try {
91-
return require(path.join(__dirname, 'lib', `${action}.js`));
92-
//} catch(e) {
93-
//console.log('Action not found');
94-
//}
90+
var file = path.join(__dirname, 'lib', `${action}.js`);
91+
if(utils.fileExists(file)) {
92+
return require(file);
93+
}
94+
95+
console.log('Action not found.'.red);
96+
console.log('Type ' + 'api help'.yellow + ' to see all commands');
9597
};
9698

lib/init.js

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ exports.run = function(config, info) {
7474
choices: types,
7575
validate: function (value) {
7676
var pass = /.(json|yaml|yml)$/.test(value);
77-
var doesntExist = !fileExists(value);
77+
var doesntExist = !utils.fileExists(value);
7878

7979
if (pass && doesntExist) {
8080
return true;
@@ -87,37 +87,7 @@ exports.run = function(config, info) {
8787
return 'This file already exists';
8888
}
8989
}
90-
},
91-
{
92-
type: 'list',
93-
name: 'language',
94-
message: 'API primary language',
95-
choices: [
96-
'JavaScript',
97-
'Ruby',
98-
'Python',
99-
'CoffeeScript',
100-
'PHP',
101-
'Java',
102-
'Go',
103-
'Other',
104-
],
105-
validate: function (value) {
106-
var pass = /.(json|yaml|yml)$/.test(value);
107-
var doesntExist = !fileExists(value);
108-
109-
if (pass && doesntExist) {
110-
return true;
111-
}
112-
113-
if(!pass) {
114-
return 'Your file must end with .json or .yaml';
115-
}
116-
if(!doesntExist) {
117-
return 'This file already exists';
118-
}
119-
}
120-
},
90+
}
12191
];
12292

12393
inquirer.prompt(questions).then(function (answers) {
@@ -164,7 +134,7 @@ exports.run = function(config, info) {
164134
console.log("following syntax in a comment above the code:");
165135
console.log("");
166136

167-
console.log(utils.swaggerInlineExample(answers.language));
137+
console.log(utils.swaggerInlineExample(utils.guessLanguage()));
168138

169139
console.log('');
170140
console.log('For more information on this syntax, see https://github.com/readmeio/swagger-inline');
@@ -187,7 +157,7 @@ function writeFile(output, swagger) {
187157

188158
function getDefaultSwagger() {
189159
var i = 0;
190-
while(file = fileExists(_file(i))) {
160+
while(file = utils.fileExists(_file(i))) {
191161
i++;
192162
}
193163
return _file(i);
@@ -197,10 +167,3 @@ function getDefaultSwagger() {
197167
}
198168
}
199169

200-
function fileExists(file) {
201-
try {
202-
return fs.statSync(file).isFile();
203-
} catch (err) {
204-
return false;
205-
}
206-
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"base-store": "^0.4.4",
2424
"colors": "^1.1.2",
2525
"git-utils": "^4.1.2",
26+
"glob": "^7.1.0",
2627
"inquirer": "^1.2.1",
2728
"json2yaml": "^1.1.0",
2829
"jsonfile": "^2.3.1",

utils.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var fs = require('fs');
22
var os = require('os');
33
var path = require('path');
4+
var glob = require("glob")
45

56
var _ = require('lodash');
67
var git = require('git-utils');
@@ -85,6 +86,48 @@ exports.addId = function(file, id) {
8586
return true;
8687
};
8788

89+
exports.fileExists = function(file) {
90+
try {
91+
return fs.statSync(file).isFile();
92+
} catch (err) {
93+
return false;
94+
}
95+
};
96+
97+
exports.guessLanguage = function(cb) {
98+
// Really simple way at guessing the language.
99+
// If we're wrong, it's not a big deal... and
100+
// way better than asking them what language
101+
// they're writing (since the UI was confusing).
102+
103+
var language = 'js';
104+
var languages = {
105+
rb: 0,
106+
coffee: 0,
107+
py: 0,
108+
js: 0,
109+
java: 0,
110+
php: 0,
111+
go: 0
112+
};
113+
114+
var files = glob.sync("*");
115+
_.each(files, function(f) {
116+
var ext = f.split('.').slice(-1)[0];
117+
if(typeof languages[ext] !== 'undefined') {
118+
languages[ext]++;
119+
}
120+
});
121+
122+
_.each(languages, function(i, l) {
123+
if(i > languages[language]) {
124+
language = l;
125+
}
126+
});
127+
128+
return language;
129+
};
130+
88131
exports.swaggerInlineExample = function(_lang) {
89132
var prefix = ' ';
90133

@@ -99,12 +142,12 @@ exports.swaggerInlineExample = function(_lang) {
99142
];
100143

101144
var languages = {
102-
'javascript': ['/*', ' * ', '*/', 'route.get("/pet/:petId", pet.show);'],
145+
'js': ['/*', ' * ', '*/', 'route.get("/pet/:petId", pet.show);'],
103146
'java': ['/*', ' * ', '*/', 'public String getPet(id) {'],
104147
'php': ['/*', ' * ', '*/', 'function showPet($id) {'],
105-
'coffeescript': ['###', '', '###', "route.get '/pet/:petId', pet.show"],
106-
'ruby': ['=begin', '', '=end', "get '/pet/:petId' do"],
107-
'python': ['"""', '', '"""', "def getPet(id):"],
148+
'coffee': ['###', '', '###', "route.get '/pet/:petId', pet.show"],
149+
'rb': ['=begin', '', '=end', "get '/pet/:petId' do"],
150+
'py': ['"""', '', '"""', "def getPet(id):"],
108151
'go': ['/*', ' * ', '*/', 'func getPet(id) {'],
109152
};
110153

0 commit comments

Comments
 (0)