vite-plugin-deploy-ftp 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +131 -0
- package/dist/index.mjs +100 -0
- package/package.json +40 -0
- package/vite-plugin-deploy-ftp-0.0.0.tgz +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# vite-plugin-deploy-ftp
|
|
2
|
+
|
|
3
|
+
将 dist 目录上传到 FTP 服务器
|
|
4
|
+
|
|
5
|
+
## 介绍
|
|
6
|
+
|
|
7
|
+
`vite-plugin-deploy-ftp` 是一个 Vite 插件,用于将打包后的文件上传到 FTP 服务器。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add vite-plugin-deploy-ftp -D
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 使用
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// vite.config.ts
|
|
19
|
+
import vitePluginDeployFtp from 'vite-plugin-deploy-ftp'
|
|
20
|
+
|
|
21
|
+
// ...existing code...
|
|
22
|
+
export default {
|
|
23
|
+
// ...existing code...
|
|
24
|
+
plugins: [
|
|
25
|
+
// 在最后一个插件中使用
|
|
26
|
+
vitePluginDeployFtp({
|
|
27
|
+
open: true,
|
|
28
|
+
host: process.env.zH5FtpHost as string,
|
|
29
|
+
port: +(process.env.zH5FtpPort || 21),
|
|
30
|
+
user: process.env.zH5FtpUser as string,
|
|
31
|
+
password: process.env.zH5FtpPassword as string,
|
|
32
|
+
uploadPath: `${env.VITE_FTP_DIRNAME}`,
|
|
33
|
+
alias: `https://h5.eventnet.cn/`,
|
|
34
|
+
}),
|
|
35
|
+
],
|
|
36
|
+
}
|
|
37
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
type vitePluginDeployFtpOption = {
|
|
4
|
+
host: string;
|
|
5
|
+
port?: number;
|
|
6
|
+
user: string;
|
|
7
|
+
password: string;
|
|
8
|
+
uploadPath: string;
|
|
9
|
+
alias?: string;
|
|
10
|
+
open?: boolean;
|
|
11
|
+
};
|
|
12
|
+
declare function vitePluginDeployFtp(option: vitePluginDeployFtpOption): Plugin;
|
|
13
|
+
|
|
14
|
+
export { vitePluginDeployFtp as default, type vitePluginDeployFtpOption };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
type vitePluginDeployFtpOption = {
|
|
4
|
+
host: string;
|
|
5
|
+
port?: number;
|
|
6
|
+
user: string;
|
|
7
|
+
password: string;
|
|
8
|
+
uploadPath: string;
|
|
9
|
+
alias?: string;
|
|
10
|
+
open?: boolean;
|
|
11
|
+
};
|
|
12
|
+
declare function vitePluginDeployFtp(option: vitePluginDeployFtpOption): Plugin;
|
|
13
|
+
|
|
14
|
+
export { vitePluginDeployFtp as default, type vitePluginDeployFtpOption };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
default: () => vitePluginDeployFtp
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
var import_prompts = require("@inquirer/prompts");
|
|
37
|
+
var import_archiver = __toESM(require("archiver"));
|
|
38
|
+
var import_basic_ftp = require("basic-ftp");
|
|
39
|
+
var import_chalk = __toESM(require("chalk"));
|
|
40
|
+
var import_dayjs = __toESM(require("dayjs"));
|
|
41
|
+
var import_node_fs = __toESM(require("fs"));
|
|
42
|
+
var import_node_path = __toESM(require("path"));
|
|
43
|
+
var import_ora = __toESM(require("ora"));
|
|
44
|
+
var import_vite = require("vite");
|
|
45
|
+
function vitePluginDeployFtp(option) {
|
|
46
|
+
const { open = true, host, port = 21, user, password, uploadPath, alias = "" } = option || {};
|
|
47
|
+
let outDir = "dist";
|
|
48
|
+
return {
|
|
49
|
+
name: "vite-plugin-deploy-ftp",
|
|
50
|
+
apply: "build",
|
|
51
|
+
enforce: "post",
|
|
52
|
+
configResolved(config) {
|
|
53
|
+
outDir = config.build?.outDir || "dist";
|
|
54
|
+
},
|
|
55
|
+
closeBundle: {
|
|
56
|
+
sequential: true,
|
|
57
|
+
order: "post",
|
|
58
|
+
async handler() {
|
|
59
|
+
if (!host || !port || !user || !password || !uploadPath || !open) {
|
|
60
|
+
console.log(import_chalk.default.yellow("\u8BF7\u914D\u7F6E\u6B63\u786E\u7684FTP\u4FE1\u606F"));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const ftpUploadChoice = await (0, import_prompts.select)({
|
|
64
|
+
message: "\u662F\u5426\u4E0A\u4F20FTP",
|
|
65
|
+
choices: ["\u662F", "\u5426"],
|
|
66
|
+
default: "\u662F"
|
|
67
|
+
});
|
|
68
|
+
if (ftpUploadChoice === "\u5426") return;
|
|
69
|
+
const uploadSpinner = (0, import_ora.default)("\u51C6\u5907\u81EA\u52A8\u4E0A\u4F20\uFF0C\u521B\u5EFA\u8FDE\u63A5\u4E2D...").start();
|
|
70
|
+
const client = new import_basic_ftp.Client();
|
|
71
|
+
client.ftp.verbose = false;
|
|
72
|
+
await client.access({
|
|
73
|
+
host,
|
|
74
|
+
port,
|
|
75
|
+
user,
|
|
76
|
+
password,
|
|
77
|
+
secure: true,
|
|
78
|
+
secureOptions: { rejectUnauthorized: false, timeout: 6e4 }
|
|
79
|
+
});
|
|
80
|
+
uploadSpinner.color = "blue";
|
|
81
|
+
uploadSpinner.text = "\u8FDE\u63A5\u6210\u529F";
|
|
82
|
+
const fileList = await client.list(uploadPath);
|
|
83
|
+
uploadSpinner.succeed(`\u5DF2\u8FDE\u63A5 ${import_chalk.default.green(`\u76EE\u5F55: ==> ${alias}${uploadPath}`)}`);
|
|
84
|
+
if (fileList.length) {
|
|
85
|
+
await createBackupFile(client, uploadPath, alias);
|
|
86
|
+
}
|
|
87
|
+
const uploadFileSpinner = (0, import_ora.default)("\u4E0A\u4F20\u4E2D...").start();
|
|
88
|
+
await client.uploadFromDir(outDir, uploadPath);
|
|
89
|
+
uploadFileSpinner.succeed("\u4E0A\u4F20\u6210\u529F url:" + import_chalk.default.green(`${alias}${uploadPath}/`));
|
|
90
|
+
client.close();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
async function createBackupFile(client, dir, alias) {
|
|
96
|
+
const backupSpinner = (0, import_ora.default)(`\u521B\u5EFA\u5907\u4EFD\u6587\u4EF6\u4E2D ${import_chalk.default.yellow(`\u76EE\u5F55: ==> ${alias}${dir}`)}`).start();
|
|
97
|
+
const fileName = `backup_${(0, import_dayjs.default)().format("YYYYMMDD_HHmmss")}.zip`;
|
|
98
|
+
const localDir = `./__temp/zip`;
|
|
99
|
+
const zipFilePath = `./__temp/${fileName}`;
|
|
100
|
+
if (!import_node_fs.default.existsSync(localDir)) {
|
|
101
|
+
import_node_fs.default.mkdirSync(localDir, { recursive: true });
|
|
102
|
+
}
|
|
103
|
+
await client.downloadToDir(localDir, dir);
|
|
104
|
+
backupSpinner.text = `\u4E0B\u8F7D\u8FDC\u7A0B\u6587\u4EF6\u6210\u529F ${import_chalk.default.yellow(`\u76EE\u5F55: ==> ${alias}${dir}`)}`;
|
|
105
|
+
import_node_fs.default.readdirSync(localDir).forEach((i) => {
|
|
106
|
+
if (i.startsWith("backup_") && i.endsWith(".zip")) {
|
|
107
|
+
import_node_fs.default.rmSync(import_node_path.default.join(localDir, i));
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
const output = import_node_fs.default.createWriteStream(zipFilePath);
|
|
111
|
+
const archive = (0, import_archiver.default)("zip", {
|
|
112
|
+
zlib: { level: 9 }
|
|
113
|
+
});
|
|
114
|
+
output.on("close", function() {
|
|
115
|
+
});
|
|
116
|
+
archive.on("error", function(err) {
|
|
117
|
+
backupSpinner.fail("\u538B\u7F29\u5931\u8D25");
|
|
118
|
+
throw err;
|
|
119
|
+
});
|
|
120
|
+
archive.pipe(output);
|
|
121
|
+
archive.directory(localDir, false);
|
|
122
|
+
await archive.finalize();
|
|
123
|
+
backupSpinner.text = `\u538B\u7F29\u5B8C\u6210, \u51C6\u5907\u4E0A\u4F20 ${import_chalk.default.yellow(
|
|
124
|
+
`\u76EE\u5F55: ==> ${(0, import_vite.normalizePath)(`${alias}${dir}/${fileName}`)}`
|
|
125
|
+
)}`;
|
|
126
|
+
await client.uploadFrom(zipFilePath, (0, import_vite.normalizePath)(`${dir}/${fileName}`));
|
|
127
|
+
backupSpinner.succeed(
|
|
128
|
+
`\u5907\u4EFD\u6210\u529F ${import_chalk.default.green(`\u76EE\u5F55: ==> ${(0, import_vite.normalizePath)(`${alias}${dir}/${fileName}`)}`)}`
|
|
129
|
+
);
|
|
130
|
+
import_node_fs.default.rmSync(`./__temp`, { recursive: true });
|
|
131
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { select } from "@inquirer/prompts";
|
|
3
|
+
import archiver from "archiver";
|
|
4
|
+
import { Client } from "basic-ftp";
|
|
5
|
+
import chalk from "chalk";
|
|
6
|
+
import dayjs from "dayjs";
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import ora from "ora";
|
|
10
|
+
import { normalizePath } from "vite";
|
|
11
|
+
function vitePluginDeployFtp(option) {
|
|
12
|
+
const { open = true, host, port = 21, user, password, uploadPath, alias = "" } = option || {};
|
|
13
|
+
let outDir = "dist";
|
|
14
|
+
return {
|
|
15
|
+
name: "vite-plugin-deploy-ftp",
|
|
16
|
+
apply: "build",
|
|
17
|
+
enforce: "post",
|
|
18
|
+
configResolved(config) {
|
|
19
|
+
outDir = config.build?.outDir || "dist";
|
|
20
|
+
},
|
|
21
|
+
closeBundle: {
|
|
22
|
+
sequential: true,
|
|
23
|
+
order: "post",
|
|
24
|
+
async handler() {
|
|
25
|
+
if (!host || !port || !user || !password || !uploadPath || !open) {
|
|
26
|
+
console.log(chalk.yellow("\u8BF7\u914D\u7F6E\u6B63\u786E\u7684FTP\u4FE1\u606F"));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const ftpUploadChoice = await select({
|
|
30
|
+
message: "\u662F\u5426\u4E0A\u4F20FTP",
|
|
31
|
+
choices: ["\u662F", "\u5426"],
|
|
32
|
+
default: "\u662F"
|
|
33
|
+
});
|
|
34
|
+
if (ftpUploadChoice === "\u5426") return;
|
|
35
|
+
const uploadSpinner = ora("\u51C6\u5907\u81EA\u52A8\u4E0A\u4F20\uFF0C\u521B\u5EFA\u8FDE\u63A5\u4E2D...").start();
|
|
36
|
+
const client = new Client();
|
|
37
|
+
client.ftp.verbose = false;
|
|
38
|
+
await client.access({
|
|
39
|
+
host,
|
|
40
|
+
port,
|
|
41
|
+
user,
|
|
42
|
+
password,
|
|
43
|
+
secure: true,
|
|
44
|
+
secureOptions: { rejectUnauthorized: false, timeout: 6e4 }
|
|
45
|
+
});
|
|
46
|
+
uploadSpinner.color = "blue";
|
|
47
|
+
uploadSpinner.text = "\u8FDE\u63A5\u6210\u529F";
|
|
48
|
+
const fileList = await client.list(uploadPath);
|
|
49
|
+
uploadSpinner.succeed(`\u5DF2\u8FDE\u63A5 ${chalk.green(`\u76EE\u5F55: ==> ${alias}${uploadPath}`)}`);
|
|
50
|
+
if (fileList.length) {
|
|
51
|
+
await createBackupFile(client, uploadPath, alias);
|
|
52
|
+
}
|
|
53
|
+
const uploadFileSpinner = ora("\u4E0A\u4F20\u4E2D...").start();
|
|
54
|
+
await client.uploadFromDir(outDir, uploadPath);
|
|
55
|
+
uploadFileSpinner.succeed("\u4E0A\u4F20\u6210\u529F url:" + chalk.green(`${alias}${uploadPath}/`));
|
|
56
|
+
client.close();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
async function createBackupFile(client, dir, alias) {
|
|
62
|
+
const backupSpinner = ora(`\u521B\u5EFA\u5907\u4EFD\u6587\u4EF6\u4E2D ${chalk.yellow(`\u76EE\u5F55: ==> ${alias}${dir}`)}`).start();
|
|
63
|
+
const fileName = `backup_${dayjs().format("YYYYMMDD_HHmmss")}.zip`;
|
|
64
|
+
const localDir = `./__temp/zip`;
|
|
65
|
+
const zipFilePath = `./__temp/${fileName}`;
|
|
66
|
+
if (!fs.existsSync(localDir)) {
|
|
67
|
+
fs.mkdirSync(localDir, { recursive: true });
|
|
68
|
+
}
|
|
69
|
+
await client.downloadToDir(localDir, dir);
|
|
70
|
+
backupSpinner.text = `\u4E0B\u8F7D\u8FDC\u7A0B\u6587\u4EF6\u6210\u529F ${chalk.yellow(`\u76EE\u5F55: ==> ${alias}${dir}`)}`;
|
|
71
|
+
fs.readdirSync(localDir).forEach((i) => {
|
|
72
|
+
if (i.startsWith("backup_") && i.endsWith(".zip")) {
|
|
73
|
+
fs.rmSync(path.join(localDir, i));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
const output = fs.createWriteStream(zipFilePath);
|
|
77
|
+
const archive = archiver("zip", {
|
|
78
|
+
zlib: { level: 9 }
|
|
79
|
+
});
|
|
80
|
+
output.on("close", function() {
|
|
81
|
+
});
|
|
82
|
+
archive.on("error", function(err) {
|
|
83
|
+
backupSpinner.fail("\u538B\u7F29\u5931\u8D25");
|
|
84
|
+
throw err;
|
|
85
|
+
});
|
|
86
|
+
archive.pipe(output);
|
|
87
|
+
archive.directory(localDir, false);
|
|
88
|
+
await archive.finalize();
|
|
89
|
+
backupSpinner.text = `\u538B\u7F29\u5B8C\u6210, \u51C6\u5907\u4E0A\u4F20 ${chalk.yellow(
|
|
90
|
+
`\u76EE\u5F55: ==> ${normalizePath(`${alias}${dir}/${fileName}`)}`
|
|
91
|
+
)}`;
|
|
92
|
+
await client.uploadFrom(zipFilePath, normalizePath(`${dir}/${fileName}`));
|
|
93
|
+
backupSpinner.succeed(
|
|
94
|
+
`\u5907\u4EFD\u6210\u529F ${chalk.green(`\u76EE\u5F55: ==> ${normalizePath(`${alias}${dir}/${fileName}`)}`)}`
|
|
95
|
+
);
|
|
96
|
+
fs.rmSync(`./__temp`, { recursive: true });
|
|
97
|
+
}
|
|
98
|
+
export {
|
|
99
|
+
vitePluginDeployFtp as default
|
|
100
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-deploy-ftp",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.mjs",
|
|
10
|
+
"require": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"vite-plugin"
|
|
15
|
+
],
|
|
16
|
+
"author": "yulinzhao",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"description": "将dist目录下的文件上传到阿里云oss",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^22.10.1",
|
|
21
|
+
"tsup": "^8.3.5",
|
|
22
|
+
"typescript": "^5.7.2"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"vite": "^6.0.3"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@inquirer/prompts": "^7.2.0",
|
|
29
|
+
"@types/archiver": "^6.0.3",
|
|
30
|
+
"archiver": "^7.0.1",
|
|
31
|
+
"basic-ftp": "^5.0.5",
|
|
32
|
+
"chalk": "^5.3.0",
|
|
33
|
+
"dayjs": "^1.11.13",
|
|
34
|
+
"ora": "^8.1.1"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup",
|
|
38
|
+
"pack": "pnpm run build && pnpm pack"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
Binary file
|