UNPKG

4.8 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require("vinyl-fs");
4const FTP = require("vinyl-ftp");
5const fsx = require("fs");
6const program = require("commander");
7const chalk = require("chalk");
8const path = require("path");
9const os = require("os");
10// const uid = require("uid");
11// const Download = require("download");
12const inquirer = require("inquirer");
13const logger = require("../lib/logger");
14const exists = require("fs").existsSync;
15
16const mDate = new Date();
17
18const year = `${mDate.getFullYear()}`;
19const curMonth = mDate.getMonth();
20const month = curMonth + 1 > 9 ? curMonth + 1 : `0${curMonth + 1}`;
21
22let pwdConf;
23let ftppass;
24
25const globs = [
26 "cdn/**",
27 "dist/**",
28 "!src/**",
29 "!*.sh",
30 "!*.bat",
31 "!.ftppass.json",
32 "!.git",
33 "!*.json",
34 "!*.md",
35 "!*.xml",
36 "!assets",
37 "!bower_components",
38 "!gulpfile.js",
39 "!node_modules",
40 "!node_modules/**",
41];
42
43
44/**
45 * Usage.
46 */
47
48program
49 .usage("<remote-dir> <dir-name>")
50// .usage('[root-dir] <remote-dir> [dir-name]')
51 .option("-f, --force", "重置密码")
52 .option("-s, --source", "上传源文件夹")
53 .option("-r, --release", "发布生产环境代码至CDN");
54
55/**
56 * Help.
57 */
58
59program.on("--help", () => {
60 console.log(" Examples:");
61 console.log();
62 console.log(chalk.gray(" # 发布到zhuanti release目录下的my-project目录"));
63 console.log(" $ cbd deploy release my-project");
64 console.log();
65 console.log(chalk.gray(" # 发布到zhuanti release目录下的默认本地文件夹名目录"));
66 console.log(" $ cbd deploy release");
67 console.log();
68 console.log(chalk.gray(" # 发布到zhuanti release/wx目录下的默认本地文件夹名目录"));
69 console.log(" $ cbd deploy release/wx");
70 console.log();
71});
72
73
74/**
75 * Help.
76 */
77
78function help() {
79 program.parse(process.argv);
80
81 // if (program.args.length < 1) return program.help()
82}
83help();
84
85
86/**
87 * Padding.
88 */
89
90console.log();
91process.on("exit", () => {
92 console.log();
93});
94
95
96const reset = program.force || false;
97
98const source = program.source || false;
99
100function inputPwd(params) {
101 ftppass = {
102 host: "182.254.247.29",
103 user: params.username,
104 password: params.password,
105 parallel: 10,
106 log: logger.log,
107 // debug: console.log.bind(console)
108 };
109}
110
111function upload(config, dest) {
112
113
114 const base = source ? "./" : (exists("./cdn") ? "./cdn" : "./dist");
115 const pipeFiles = fs.src(globs, {
116 base,
117 buffer: false
118 });
119 const conn = new FTP(config);
120 pipeFiles
121 .pipe(conn.newer(dest))
122 .pipe(conn.dest(dest));
123}
124
125function uploader() {
126 let remoteDir;
127 let remoteDirSub;
128 let rootDir;
129 let dest;
130
131 const args = program.args.slice(0);
132 if (args.length > 2) {
133 rootDir = args[0];
134 remoteDir = args[1]; // 远程目录
135 remoteDirSub = args[2] || path.relative("../", process.cwd());
136
137 dest = path.join(rootDir, remoteDir, remoteDirSub);
138 } else if (args.length === 2) {
139 remoteDir = args[0]; // 远程目录
140 remoteDirSub = args[1] || path.relative("../", process.cwd());
141 dest = path.join("/zhuanti/", remoteDir, remoteDirSub);
142 } else {
143 var uploadDir = readUploadDir();
144 if (uploadDir) {
145 dest = uploadDir;
146 } else {
147 dest = path.join("/zhuanti/release/app/", year, month, path.relative("../", process.cwd()));
148 }
149 }
150
151 // console.log('上传至:'+ 'http://zhuanti.chebada.com/'+dest.replace(/\\/g,'/'));
152
153 const prompt = {
154 type: "confirm",
155 name: "confirm",
156 default: false,
157 message: `确定要上传至http://zhuanti.chebada.com/${dest.replace(/\\/g, "/")}`,
158 };
159 inquirer.prompt(prompt).then((answers) => {
160 if (answers.confirm) {
161 upload(ftppass, dest);
162 }
163 });
164}
165
166function ftpLogin() {
167 const prompts = [{
168 name: "username",
169 type: "input",
170 required: true,
171 message: "ftp账号",
172 },
173 {
174 name: "password",
175 type: "password",
176 required: true,
177 message: "ftp密码",
178 },
179 ];
180
181 const tmp = path.join(`${os.tmpdir()}/cbd-key/`);
182
183 const ftpjson = `${tmp}ftppass.json`;
184
185 if (fsx.existsSync(ftpjson) && !reset) {
186 const file = fsx.readFileSync(ftpjson, "utf-8");
187 pwdConf = JSON.parse(file);
188
189 inputPwd(pwdConf);
190 uploader();
191 } else {
192 inquirer.prompt(prompts).then((answers) => {
193 inputPwd(answers);
194 uploader();
195 if (!fsx.existsSync(tmp)) {
196 fsx.mkdirSync(tmp); // 创建目录
197 }
198
199 fsx.writeFile(`${tmp}ftppass.json`, JSON.stringify(answers), "utf8", (err) => {
200 if (err) throw err;
201 console.log("It's saved!");
202 });
203 });
204 }
205}
206
207function readUploadDir() {
208 var data = fsx.readFileSync(path.join(process.cwd(), "./package.json"));
209 if (data) {
210 var pkg = data.toString();
211 pkg = JSON.parse(pkg);
212 if (pkg.uploadDir) {
213 return pkg.uploadDir;
214 }
215 return null;
216
217 }
218}
219
220
221ftpLogin();
\No newline at end of file