1 | #!/usr/bin/env node
|
2 |
|
3 | const fs = require("vinyl-fs");
|
4 | const FTP = require("vinyl-ftp");
|
5 | const fsx = require("fs");
|
6 | const program = require("commander");
|
7 | const chalk = require("chalk");
|
8 | const path = require("path");
|
9 | const os = require("os");
|
10 |
|
11 |
|
12 | const inquirer = require("inquirer");
|
13 | const logger = require("../lib/logger");
|
14 | const exists = require("fs").existsSync;
|
15 |
|
16 | const mDate = new Date();
|
17 |
|
18 | const year = `${mDate.getFullYear()}`;
|
19 | const curMonth = mDate.getMonth();
|
20 | const month = curMonth + 1 > 9 ? curMonth + 1 : `0${curMonth + 1}`;
|
21 |
|
22 | let pwdConf;
|
23 | let ftppass;
|
24 |
|
25 | const 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 |
|
46 |
|
47 |
|
48 | program
|
49 | .usage("<remote-dir> <dir-name>")
|
50 |
|
51 | .option("-f, --force", "重置密码")
|
52 | .option("-s, --source", "上传源文件夹")
|
53 | .option("-r, --release", "发布生产环境代码至CDN");
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | program.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 |
|
76 |
|
77 |
|
78 | function help() {
|
79 | program.parse(process.argv);
|
80 |
|
81 |
|
82 | }
|
83 | help();
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | console.log();
|
91 | process.on("exit", () => {
|
92 | console.log();
|
93 | });
|
94 |
|
95 |
|
96 | const reset = program.force || false;
|
97 |
|
98 | const source = program.source || false;
|
99 |
|
100 | function 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 |
|
108 | };
|
109 | }
|
110 |
|
111 | function 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 |
|
125 | function 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 |
|
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 |
|
166 | function 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 |
|
207 | function 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 |
|
221 | ftpLogin(); |
\ | No newline at end of file |