From 4dc6531d8d76cdb75738c7d5fab43a47a14221d1 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Sat, 3 Dec 2016 00:53:28 +0530 Subject: [PATCH] Initial commit --- default-data.js | 6 +++++ default-template | 22 +++++++++++++++ main.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 28 ++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 default-data.js create mode 100644 default-template create mode 100644 main.js create mode 100644 package.json diff --git a/default-data.js b/default-data.js new file mode 100644 index 0000000..4951d06 --- /dev/null +++ b/default-data.js @@ -0,0 +1,6 @@ + +module.exports = { + user: process.env.USER, + email: process.env.USER + '@' + process.env.HOST, + date: new Date(), +}; diff --git a/default-template b/default-template new file mode 100644 index 0000000..4220738 --- /dev/null +++ b/default-template @@ -0,0 +1,22 @@ +/* + * <%- filename %> + * + * Copyright <%- new Date().getFullYear() %> <%- user %> <<%- email %>> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * + */ diff --git a/main.js b/main.js new file mode 100644 index 0000000..f67fade --- /dev/null +++ b/main.js @@ -0,0 +1,69 @@ +#!/usr/bin/env node + +var path = require('path'); +var os = require('os'); +var fs = require('fs'); +var ejs = require('ejs'); +var configDir = path.join( process.env.HOME, '.file-headers' ); + + +function cp( src, dest ){ + var data = fs.readFileSync(src); + fs.writeFileSync( dest, data); +} + +function initialize(){ + if( !fs.existsSync( configDir )){ + fs.mkdirSync( configDir ); + cp( path.join( __dirname, 'default-template' ), path.join( configDir, 'default' ) ); + cp( path.join( __dirname, 'default-data.js' ), path.join( configDir, 'template_data.js' ) ); + } +} + + +function getTemplate( fname ){ + var ext = path.extname(fname).slice(1) || 'default'; + + if( fs.existsSync( path.join(configDir, ext ) ) ){ + return fs.readFileSync( path.join(configDir, ext ), 'utf-8' ); + } else { + return fs.readFileSync( path.join(configDir, 'default' ), 'utf-8' ); + } +} + + +function getConfig(){ + try{ + return require( path.join(configDir, 'template_data' ) ); + } catch(e){ + return { date: new Date(), user: process.env.USER, email: process.env.USER + '@' + os.hostname() }; + } +} + + +function getHeader( fname ){ + var template = getTemplate( fname ); + var config = getConfig(); + var data = Object.assign({ filename: fname }, config ); + return ejs.render( template, data ); +} + + +function printHeader( fname ){ + var header = getHeader( fname ); + console.log( header ); +} + + +if( require.main === module ){ + var fname = process.argv[2]; + initialize(); + + if( !fname ){ + console.log( 'file-header '); + process.exit(-1); + } + + printHeader( fname ); +} + diff --git a/package.json b/package.json new file mode 100644 index 0000000..2ae4cb3 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "file-header", + "version": "0.0.1", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "bin": { + "file-header": "./main.js" + }, + "author": { + "name": "Harish.K", + "email": "harish2704@gmail.com" + }, + "license": "ISC", + "dependencies": { + "ejs": "^2.5.3" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/harish2704/file-header.git" + }, + "bugs": { + "url": "https://github.com/harish2704/file-header/issues" + }, + "homepage": "https://github.com/harish2704/file-header#readme" +}