├── f.js ├── list.json ├── .travis.yml ├── .gitignore ├── package.json ├── templet └── i.json ├── lib ├── use.js ├── install.js └── list.js ├── bin └── cli.js ├── README.md └── test └── test.js /f.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | require('./bin/cli.js'); 3 | -------------------------------------------------------------------------------- /list.json: -------------------------------------------------------------------------------- 1 | {"i":{"fileName":"init.json","trueName":"i.json","param":""},"test":{"fileName":"package.json","trueName":"test.json","param":""},"package":{"fileName":"package.json","trueName":"package.json","param":""}} -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - stable 5 | - 10 6 | - 8 7 | - 6 8 | - 4 9 | script: 10 | - npm test 11 | after_success: 12 | - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fast-init", 3 | "version": "0.1.0", 4 | "description": "templet init tool", 5 | "main": "fi.js", 6 | "keywords": [ 7 | "template", 8 | "templet", 9 | "init", 10 | "fast-init", 11 | "fast" 12 | ], 13 | "author": "KingNigel", 14 | "preferGlobal": true, 15 | "bin": { 16 | "f": "f.js" 17 | }, 18 | "scripts": { 19 | "test": "nyc ava", 20 | "report": "nyc report --reporter=html" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "https://github.com/KingNigel/fast-init.git" 25 | }, 26 | "bugs": { 27 | "url": "https://github.com/KingNigel/fast-init/issues" 28 | }, 29 | "license": "MIT", 30 | "devDependencies": { 31 | "ava": "^0.17.0", 32 | "commander": "^2.9.0", 33 | "coveralls": "^2.11.9", 34 | "nyc": "^6.6.1" 35 | }, 36 | "dependencies": { 37 | "commander": "^2.9.0" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /templet/i.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fast-init", 3 | "version": "0.0.1", 4 | "description": "templet init tool", 5 | "main": "fi.js", 6 | "keywords": [ 7 | "template", 8 | "templet", 9 | "init", 10 | "fast-init", 11 | "fast" 12 | ], 13 | "author": "KingNigel", 14 | "preferGlobal": "true", 15 | "bin": { 16 | "fi": "fi.js" 17 | }, 18 | "scripts": { 19 | "test": "nyc ava", 20 | "report": "nyc report --reporter=html" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "https://github.com/KingNigel/http-poster.git" 25 | }, 26 | "bugs": { 27 | "url": "https://github.com/KingNigel/http-poster/issues" 28 | }, 29 | "license": "MIT", 30 | "devDependencies": { 31 | "ava": "^0.11.0", 32 | "co-exec": "^1.1.0", 33 | "commander": "^2.9.0", 34 | "coveralls": "^2.11.9", 35 | "nyc": "^6.6.1" 36 | }, 37 | "dependencies": { 38 | "commander": "^2.9.0", 39 | "co-exec": "^1.1.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /lib/use.js: -------------------------------------------------------------------------------- 1 | var listObj = require('../list.json') ? require('../list.json') : {} 2 | var fs = require('fs') 3 | var path = require('path') 4 | 5 | module.exports = function (val) { 6 | var valArray = val.split('@') 7 | var alias = valArray[0] 8 | var createName = valArray[1] 9 | if (listObj[alias]) { 10 | var filePath = '' 11 | var templetPath = path.join(__dirname, '../templet', listObj[alias].trueName) 12 | if (createName) { 13 | if (path.extname(createName)) { 14 | filePath = path.join(process.cwd(), createName) 15 | } else { 16 | createName = createName + path.extname(listObj[alias].fileName) 17 | filePath = path.join(process.cwd(), createName) 18 | } 19 | } else { 20 | filePath = path.join(process.cwd(),listObj[alias].fileName) 21 | } 22 | var rs = fs.createReadStream(templetPath) 23 | var ws = fs.createWriteStream(filePath) 24 | rs.pipe(ws) 25 | } else { 26 | console.log(alias + ' is not found.') 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /lib/install.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs') 2 | var path = require('path') 3 | var list = require('./list') 4 | module.exports = function install(val) { 5 | var valArray = val.split('@') 6 | var filePath = valArray[0] 7 | var alias = valArray[1] 8 | var templetPath = '' 9 | var fileName = path.basename(filePath) 10 | if (alias) { 11 | templetPath = path.join(__dirname, '../templet', alias + path.extname(filePath)) 12 | } else { 13 | templetPath = path.join(__dirname, '../templet', fileName) 14 | alias = path.basename(filePath, path.extname(filePath)) 15 | } 16 | var trueName = alias + path.extname(filePath) 17 | fs.stat(filePath, (err, st) => { 18 | if (err) return console.log(err.message) 19 | if (st.isFile()) { 20 | var rs = fs.createReadStream(filePath) 21 | var ws = fs.createWriteStream(templetPath) 22 | rs.pipe(ws) 23 | list.add(alias, fileName, trueName) 24 | } 25 | else { 26 | console.log(filePath + 'no such file!') 27 | } 28 | }) 29 | } -------------------------------------------------------------------------------- /bin/cli.js: -------------------------------------------------------------------------------- 1 | var program = require('commander') 2 | var install = require('../lib/install') 3 | var list = require('../lib/list') 4 | var use = require('../lib/use') 5 | var installDescription = 'The path must be the true path,[alias] is a key to the templet.' + 6 | '\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' + 7 | 'If [alias] is not set,the filename will be the default key.' 8 | var listDescription = 'List the templet.' 9 | var deleteDescription = 'The alias must be the true alias,you can see it by \'-l\'.' 10 | var useDescription = 'To initialize a file with a templet. is a key to a templet.' + 11 | '\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' + 12 | 'A file well be created,[name] as a filename will be created.' 13 | program 14 | .version('1.0.0') 15 | .usage('[options]') 16 | .option('-i, --install ..[alias]', installDescription, install) 17 | .option('-l, --list ', listDescription, list.showList) 18 | .option('-d, --delete ', deleteDescription, list.del) 19 | .option('-u, --use ..[name]', useDescription, use) 20 | program 21 | .parse(process.argv) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fast-init 2 | [![Build Status](https://travis-ci.org/KingNigel/fast-init.svg?branch=master)](https://travis-ci.org/KingNigel/fast-init) 3 | [![Coverage Status](https://coveralls.io/repos/github/KingNigel/fast-init/badge.svg?branch=master)](https://coveralls.io/github/KingNigel/fast-init?branch=master) 4 | 5 | 快速打造自己的模板,快速创建文件。 6 | v0.1.0版重大修改,将原来的工具使用fi命令修改f命令。 7 | ## 安装 8 | ``` 9 | npm install -g fast-init 10 | ``` 11 | ## 命令 12 | ``` 13 | Usage: f [options] 14 | 15 | Options: 16 | 17 | -h, --help output usage information 18 | -V, --version output the version number 19 | -i, --install @[alias] The path must be the true path,[alias] is a key to the templet . 20 | If [alias] is not set,the filename will be the default key. 21 | -l, --list List the templet. 22 | -d, --delete The alias must be the true alias,you can see it by '-l'. 23 | -u, --use @[name] To initialize a file with a templet. is a key to a templet. 24 | A file well be created,[name] as a filename will be created. 25 | ``` 26 | ###1. f -i @[alias] 加载文件模板 27 | 28 | - path:要装载的模板路径,路径必须正确。 29 | - alias:包的别名,模板装载后的使用可以通过别名简化操作 30 | 31 | ###2. f -l 查看模板列表 32 | - 将已经装载模板的信息输出到控制台 33 | 34 | ###3. f -d 删除模板 35 | - 删除装载的模块,为模板别名,可以通过 fi -l 查看 36 | 37 | ###4. f -u @[name] 使用模板 38 | - 已别名为的模板创建文件,[name]为创建文件的文件名,如果不写,按原文件名创建。 39 | 40 | -------------------------------------------------------------------------------- /lib/list.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | let listObj = require('../list.json') 3 | const fs = require('fs') 4 | const path = require('path') 5 | 6 | exports.showList = function () { 7 | console.log('*'.repeat(60)) 8 | console.log('') 9 | let length = 0 10 | for (let e in listObj) { 11 | if (e.length > length && e.length < 20) { 12 | length = e.length 13 | } else if (e.length >= 20) { 14 | length = 20 15 | } 16 | } 17 | for (let e in listObj) { 18 | let l = length - e.length 19 | let nbsp = ' '.repeat(2) 20 | let s = ' '.repeat(l) 21 | console.log(`alias:${nbsp}${e}${s}${nbsp}|${nbsp}templetName:${nbsp}${listObj[e].fileName}`); 22 | } 23 | console.log('') 24 | console.log('*'.repeat(60)) 25 | } 26 | 27 | exports.add = function (id, fileName, trueName) { 28 | listObj[id] = { 'fileName': fileName, 'trueName': trueName, 'param': '' } 29 | var s = JSON.stringify(listObj) 30 | fs.writeFile(path.join(__dirname, '../list.json'), s, 'utf8', function (err) { 31 | if (err) throw err 32 | console.log('Successfully installed!') 33 | }) 34 | } 35 | 36 | exports.del = function (id) { 37 | if (!listObj[id]) { 38 | console.log(`Not find alias: ${id}`) 39 | return 40 | } 41 | fs.unlink(path.join(__dirname, '../templet/', listObj[id].trueName), (err) => { 42 | if (err) throw err 43 | delete listObj[id] 44 | var s = JSON.stringify(listObj) 45 | fs.writeFile(path.join(__dirname, '../list.json'), s, 'utf8', function (err) { 46 | if (err) throw err 47 | console.log('Successfully deleted!') 48 | }) 49 | }) 50 | } 51 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | const fs = require('fs') 3 | const exec = require('child_process').execSync 4 | 5 | test.serial('install', (t) => { 6 | exec('node f.js -i package.json@test') 7 | exec('node f.js -i package.json') 8 | t.true(fs.accessSync('./templet/test.json') === undefined) 9 | t.true(fs.accessSync('./templet/package.json') === undefined) 10 | let list = JSON.parse(fs.readFileSync('./list.json', 'utf8')) 11 | t.true(list.test.fileName === 'package.json' && list.test.trueName === 'test.json') 12 | t.true(list.package.fileName === 'package.json' && list.package.trueName === 'package.json') 13 | fs.unlinkSync('./templet/test.json') 14 | fs.unlinkSync('./templet/package.json') 15 | }) 16 | 17 | test.serial('delete', (t) => { 18 | exec('node f.js -i package.json@delete') 19 | exec('node f.js -d delete') 20 | fs.access('./templet/delete.json', (err) => { 21 | if (err) t.pass() 22 | else t.fail() 23 | }) 24 | let list = JSON.parse(fs.readFileSync('./list.json', 'utf8')) 25 | t.true(list.delete === undefined) 26 | }) 27 | 28 | test.serial('use', (t) => { 29 | exec('node f.js -u i') 30 | exec('node f.js -u i@initAlias') 31 | exec('node f.js -u i@initAlias.js') 32 | fs.access('./init.json', (err) => { 33 | if (err) t.fail() 34 | else t.pass() 35 | fs.unlinkSync('./init.json') 36 | }) 37 | fs.access('./initAlias.json', (err) => { 38 | if (err) t.fail() 39 | else t.pass() 40 | fs.unlinkSync('./initAlias.json') 41 | }) 42 | fs.access('./initAlias.js', (err) => { 43 | if (err) t.fail() 44 | else t.pass() 45 | fs.unlinkSync('./initAlias.js') 46 | }) 47 | }) 48 | 49 | test.serial('list', (t) => { 50 | exec('node f.js -l') 51 | t.pass() 52 | }) 53 | --------------------------------------------------------------------------------