安装依赖包
npm i --save-dev ftp-deploy chalk ora@1.2.0
定义命令
在 package.json 定义命令 publish,指向脚本目录
{
"scripts": {
"publish": "node build/mds-ftp"
},
}
编写mds-ftp 脚本
/**
* mds 开源库- ftp 发布工具
*/
// 依赖包(仅开发阶段依赖 devDependencies)
// npm i --save-dev ftp-deploy chalk ora@1.2.0
const chalk = require('chalk') // 控制台颜色
const ora = require('ora') // 终端指示器 1.2.0 版本直接导入
const path = require('path')
var FtpDeploy = require('ftp-deploy')
var ftpDeploy = new FtpDeploy()
/**
* 配置文件
*/
var config = {
user: '替换你的ftp',
password: '替换你的ftp',
host: '你的ip 地址',
port: 21,
localRoot: path.resolve(__dirname, '../dist'),
remoteRoot: '/dist',
include: ['*', '**/*'], // 这样会上传所有文件
// include: ['*.php', 'dist/*', '.*'],
// e.g. exclude sourcemaps, and ALL files in node_modules (including dot files)
// exclude: ['dist/**/*.map', 'node_modules/**', 'node_modules/**/.*', '.git/**'],
// 如果为true,则在上载之前删除目标上的所有现有文件
deleteRemote: true,
// Passive mode is forced (EPSV command is not sent)
forcePasv: false
}
console.log(chalk.yellow('==== 构建物上传 - 开始 ===='))
const spinner = ora('文件上传中..')
spinner.start()
ftpDeploy
.deploy(config)
.then(res => {
spinner.stop()
console.log(chalk.green('==== 构建物上传 - 完成 ====.\n'))
})
.catch(err => {
spinner.stop()
console.log(chalk.green('==== 构建物上传 - 失败 ====.\n'))
console.log(err)
})
使用
npm run publish
效果
