简述
- 本文使用node模块: commander && inquirer,从0到1快速搭建脚手架,用以自动创建定制化项目。
- 利用sftp文件上传工具,来上传脚手架
- 发布到npm,并在其他项目中使用
一、了解用户与命令行交互的工具 commander.js && inquirer.js
关于commander.js , inquirer.js在这就不具体介绍了,可自行先补。
- 新建项目
npm init
npm install commander inquirer --save
- 新建index.js
#!/usr/bin/env node
const Program = require('commander')
const inquirer = require('inquirer')
Program
.version("0.1.0")
.description("ftp自动化上传")
.option("-i, --init", "ftp上传工具初始化")
const promptList = [{
type: 'input',
message: '设置一个用户名:',
name: 'name',
default: "我是默认用户名" // 默认值
},{
type: "confirm",
message: "是否使用监听?",
name: "watch",
prefix: "前缀"
},{
type: "confirm",
message: "是否进行文件过滤?",
name: "filter",
suffix: "后缀",
when: function(answers) { // 当watch为true的时候才会提问当前问题
return answers.watch
}
},{
type: 'list',
message: '请选择一种水果:',
name: 'fruit',
choices: [
"Apple",
"Pear",
"Banana"
],
filter: function (val) { // 使用filter将回答变为小写
return val.toLowerCase();
}
},{
type: "password", // 密码为密文输入
message: "请输入密码:",
name: "密码"
}]
Program
.command("test")
.alias("t")
.description("测试命令")
.action(async () => {
let resultBranch = await inquirer.prompt(promptList)
console.log(resultBranch)
})
Program.parse(process.argv);
- 测试执行命令:
node index.js
node index.js test
此时完成了,命令行交互的测试。下面接下来开始上传文件。
二、利用sftp,上传文件到服务器
- 1、新建一个项目
npm init
- 2、在package.json中增加:
"name": "hbirthdaycli",
"version": "1.0.2",
"main": "index.js",
"bin": {
"hbirthday": "./index.js"
},
name是定义发布到npm的包名,在别的项目中安装就用这个npm install -g hbirthdaycli。
每次发布的时候,更改一下版本号
“main”: “index.js”, 入口文件
。 在别的项目执行打包时,就用hbirthday publish或者hbirthday pub
- 3、程序的解释程序写在最上边,定义好命令的名称以及执行该命令时,所执行脚本的路径,目的是使用env来找到node,
#!/usr/bin/env node
- 4、新建dist文件夹:里面新建一些测试的文本,待会将其传到服务器上。
- 5、新建config.js:
module.exports = {
host: '****************',
dirPath: 'dist',
remotePath: '/cli',
port: 10000,
username: 'root',
password: '********',
test: '我是测试config'
}
- 6、新建index.js:
npm install commander inquirer path gulp gulp-sftp-up5 --save
#!/usr/bin/env node
//上边这行,必须有
const Program = require('commander')
const inquirer = require('inquirer')
const path = require('path')
const gulp = require("gulp")
const sftp = require("gulp-sftp-up5")
//引入本地的配置文件config.js, process.cwd()是当前Node.js进程执行时的工作目录
const configFile = require(path.join(process.cwd() + '/', 'config.js'))
//定义配置文件
const promptList = [{
type: 'input',
message: '请输入你本地要上传的目录:',
name: 'dirPath',
},{
type: 'input',
message: '请输入远程服务器的目录:',
name: 'remotePath',
},{
type: 'input',
message: '请输入远程服务器地址:',
name: 'host',
},{
type: 'input',
message: '请输入端口号:',
name: 'port',
default: "22" // 默认值
},{
type: 'input',
message: '请输入用户名:',
name: 'username',
},{
type: "password", // 密码为密文输入
message: "请输入密码:",
name: "password",
}]
Program
.command("publish")
.alias("pub")
.description("上传文件")
.action(async () => {
//有本地的config,js
if (!!configFile['remotePath'] && !!configFile['host'] && !!configFile['username'] && !!configFile['password'] && !!configFile['port']) {
var data = configFile;
var sftpConfig = {
remotePath: configFile['remotePath'].startsWith('/') ? configFile['remotePath'] :'/'+configFile['remotePath'], // 部署到服务器的路径
host: configFile['host'], // 服务器地址
user: configFile['username'], // 帐号
pass: configFile['password'], // 密码
port: configFile['port'], // 端口
removeCurrentFolderFiles: true, // 该属性可删除 remotePath 下的所有文件/文件夹
}
}
else {
//没有有本地的config,js,就加载promptList数组
let resultBranch = await inquirer.prompt(promptList);
var data = resultBranch;
var sftpConfig = {
remotePath: resultBranch['remotePath'].startsWith('/') ? resultBranch['remotePath'] :'/'+resultBranch['remotePath'], // 部署到服务器的路径
host: resultBranch['host'], // 服务器地址
user: resultBranch['username'], // 帐号
pass: resultBranch['password'], // 密码
port: resultBranch['port'], // 端口
removeCurrentFolderFiles: true, // 该属性可删除 remotePath 下的所有文件/文件夹
}
}
// 采用管道流的方式将 outputDir 中的文件上传到远端
let dirPath_1 = path.join(process.cwd() + data['dirPath']) + "/**";
let dirPath_2 = path.join(process.cwd()+'/'+data['dirPath']) + "/**"
gulp.src(data['dirPath'].startsWith('/') ? dirPath_1 : dirPath_2).pipe(sftp(sftpConfig))
})
Program.parse(process.argv);
action其中的判断是,当本地有config.js时就加载本地的config.js中配置的。若没有就用上边定义的promptList数组
- 7、执行
node index.js pub
或者执行node index.js publish
,pub在index.js中.alias(“pub”) 自定义的简写
此时已经可以将dist目录下的本地文件,传到服务器上了。接下来该脚手架将其发布到npm,以供别的项目引用。
三、发布到npm
在项目目录下,执行以下命令,将该脚手架传到npm上。
npm config set registry https://registry.npmjs.org/
npm login
npm publish
四、引用npm上的脚手架
- 1、新建一个文件夹:安装hbirthdaycli脚手架
npm install -g hbirthdaycli
- 2、新建静态文件,测试将其传到服务器上。
- 3、新建config.js
module.exports = {
host: '***************',
dirPath: 'dist',
remotePath: '/cli',
port: 10000,
username: 'root',
password: '*********',
test: '我是测试config'
}
- 4、上传到服务器上:
hbirthday pub 或者 hbirthday publish
hbirthday 是在hbirthday文件夹的package.json中bin中定义的。pub
是在hbirthday文件夹的index.js中.alias(“pub”) 自命名的alias
此时已经完成啦~~~