-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·67 lines (61 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/env node
var fs = require('fs');
var path = require('path');
var prompt = require('prompt');
path.basename = process.cwd();
console.log('*********************************************');
var readmePath = path.join(process.cwd(), 'README.md');
var templatePath = path.join(__dirname, 'template.txt');
var userArgs = process.argv[2];
if(userArgs === undefined){
return errorMessage(new Error('Parameter --service-name or -sn is required :('));
}
var args = userArgs.split('=');
if ( args[0] !== '--service-name' && args[0] !== '-sn'){
return errorMessage(new Error('Parameter --service-name or -sn is required :('));
}
if ( args[1] == null || args[1] === ''){ // comprueba al mismo tiempo null y undefined con ==
return errorMessage(new Error('Parameter '+ args[0] +' must have a value :('));
}
function errorMessage(err){
console.log('Something bad happened :(');
console.log(err);
console.log('*********************************************');
}
function writeFile(){
fs.readFile(templatePath, 'UTF-8', function (err, data) {
if(err){
return errorMessage(err);
}
data = data.replace(/__SERVICE_NAME__/g, function (dato) {
return args[1].toUpperCase();
});
data = data.replace(/__service_name__/g, function (dato) {
return args[1].toLowerCase();
});
fs.writeFile(readmePath, data,function (err) {
if(err){
return errorMessage(err);
}
console.log('Everything is alright :D');
console.log('*********************************************');
});
});
}
fs.stat(readmePath, (err, stats) => {
if(err && err.code !== 'ENOENT'){
return errorMessage(err);
}
if(stats === undefined){
writeFile();
}else{
console.log('The file README.md already exists, continue??? (yes/no)')
prompt.get(['continue'], function (err, result) {
if(result.continue === 'yes'){
writeFile();
}else{
console.log('Process canceled.. byeeee');
}
});
}
})