Comparing version 1.0.4 to 1.0.5
#! /usr/bin/env node | ||
(function () { | ||
@@ -7,2 +6,3 @@ | ||
// Load dependencies | ||
var fs = require('fs'), | ||
@@ -13,14 +13,19 @@ path = require('path'), | ||
// Paths | ||
var tplDirName = '.templates'; | ||
var tplDir = path.join(getUserHome(), tplDirName); | ||
var BR = "\r\n"; | ||
var complete = omelette("mkhere <action>"); | ||
complete.on("action", function () { | ||
return this.reply(["list", "help"]); | ||
var tpls = list(tplDir, false).toString(); | ||
var completions = tpls.split(','); | ||
return this.reply(completions); | ||
}); | ||
complete.init(); | ||
var tplDir = path.join(getUserHome(), '.templates'); | ||
var BR = "\r\n"; | ||
// Work | ||
if (process.argv[2] === 'list') { | ||
return list(tplDir); | ||
return list(tplDir, true); | ||
} else if (process.argv[2] === 'help' || !process.argv[2]) { | ||
@@ -31,3 +36,6 @@ return usage(); | ||
} | ||
/** | ||
* getUserHome | ||
* @returns {String} | ||
*/ | ||
function getUserHome() { | ||
@@ -37,9 +45,63 @@ return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; | ||
function list(dir) { | ||
/** | ||
* list | ||
* @param dir | ||
* @param print | ||
* @returns {Array} or {String} | ||
*/ | ||
function list(dir, print) { | ||
var tpls = fs.readdirSync(dir); | ||
var files = []; | ||
tpls.forEach(function (file) { | ||
console.log(file); | ||
// match temp files | ||
if (isHiddenFile(file)) { | ||
files.push(file); | ||
} | ||
}); | ||
if (tpls && print) { | ||
console.log(files.join("\n")); | ||
} else { | ||
return files; | ||
} | ||
} | ||
/** | ||
* mk | ||
* @param tplDir | ||
*/ | ||
function mk(tplDir) { | ||
var tplFile = process.argv[2]; | ||
var tplName = path.join(tplDir, tplFile); | ||
var newFileExtArray = tplFile.split(/\s*\.\s*/); | ||
var newFileExt = ''; | ||
if (newFileExtArray.length > 1) { | ||
newFileExtArray.shift(); | ||
newFileExt = '.' + newFileExtArray.join('.'); | ||
} | ||
var newFile = process.argv[3] + newFileExt; | ||
fs.readFile(tplName, 'utf8', function (err, data) { | ||
if (err) { | ||
return console.log( | ||
'Error! No such template file '.red + tplFile.red.bold + ' in '.red + tplDir.red + BR + | ||
"For usage see: " + "mkhere help".bold | ||
); | ||
} | ||
fs.writeFile(newFile, data); | ||
}); | ||
} | ||
/** | ||
* isHiddenFile | ||
* @param file | ||
* @returns {boolean} | ||
*/ | ||
function isHiddenFile(file) { | ||
if (file.charAt(file.length - 1) !== '~' && file !== '.DS_Store') { | ||
return true | ||
} | ||
} | ||
/** | ||
* usage | ||
*/ | ||
function usage() { | ||
@@ -53,5 +115,7 @@ console.log( | ||
"Run: ".white + "mkhere tplname.html newname ".green.bold + BR + | ||
"\t This will create new file " + "newname.html".italic + " based on " + path.join(tplDir, "tplname.html").italic + " in current directory" + BR + BR + | ||
"\t This will create new file " + "newname.html".italic + " based on " + path.join(tplDir, "tplname.html").italic + " in current directory" + BR + | ||
"Run: ".white + "mkhere html.html ~/Desktop/sa ".green.bold + BR + | ||
"\t This will create new file " + "sa.html".italic + " based on " + path.join(tplDir, "html.html").italic + " on the desktop" + BR + | ||
"------------------Auto-completion-----------------" + BR + BR + | ||
"Run: " + "mkhere --completion >> ~/.mkhere.completion.sh && echo 'source ~/.mkhere.completion.sh' >> .bash_profile".green.bold + BR + | ||
"Run: " + "mkhere --completion >> ~/.mkhere.completion.sh && echo 'source ~/.mkhere.completion.sh' >> ~/.bashrc".green.bold + BR + | ||
"\tIn order to enable auto-completion in your `BASH` terminal" + BR + | ||
@@ -63,18 +127,2 @@ "Or: " + "echo '. <(./githubber --completion)' >> .zshrc".green.bold + BR + | ||
function mk(tplDir) { | ||
var CWD = process.cwd(); | ||
var tplFile = process.argv[2]; | ||
var tplName = path.join(tplDir, tplFile); | ||
var newFileExt = tplFile.slice(tplFile.indexOf('.')); | ||
var newFile = process.argv[3] + newFileExt; | ||
fs.readFile(tplName, 'utf8', function (err, data) { | ||
if (err) { | ||
return console.log( | ||
'Error! No such template file '.red + tplFile.red.bold + ' in '.red + tplDir.red + BR + | ||
"For usage see: " + "mkhere help".bold | ||
); | ||
} | ||
fs.writeFile(path.join(CWD, newFile), data); | ||
}); | ||
} | ||
}).call(this); |
{ | ||
"name": "mkhere", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Make files based on template", | ||
@@ -5,0 +5,0 @@ "main": "lib/mkhere.js", |
@@ -17,2 +17,2 @@ # mkhere | ||
Run: `mkhere --completion >> ~/.mkhere.completion.sh && echo 'source ~/.mkhere.completion.sh' >> .bash_profile` In order to enable auto-completion in your __bash__ terminal, or `echo '. <(./githubber --completion)' >> .zshrc` if you're using __zsh__ | ||
Run: `mkhere --completion >> ~/.mkhere.completion.sh && echo 'source ~/.mkhere.completion.sh' >> .bashrc` In order to enable auto-completion in your __bash__ terminal, or `echo '. <(./githubber --completion)' >> .zshrc` if you're using __zsh__ |
7851
110