1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
12 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14 | function verb(n) { return function (v) { return step([n, v]); }; }
|
15 | function step(op) {
|
16 | if (f) throw new TypeError("Generator is already executing.");
|
17 | while (_) try {
|
18 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19 | if (y = 0, t) op = [op[0] & 2, t.value];
|
20 | switch (op[0]) {
|
21 | case 0: case 1: t = op; break;
|
22 | case 4: _.label++; return { value: op[1], done: false };
|
23 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
24 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25 | default:
|
26 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30 | if (t[2]) _.ops.pop();
|
31 | _.trys.pop(); continue;
|
32 | }
|
33 | op = body.call(thisArg, _);
|
34 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36 | }
|
37 | };
|
38 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
39 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
40 | };
|
41 | Object.defineProperty(exports, "__esModule", { value: true });
|
42 | exports.download = exports.clearGitCertByPath = exports.clearGitCert = exports.transformUrl = exports.setServerUrl = void 0;
|
43 | var cross_spawn_1 = __importDefault(require("cross-spawn"));
|
44 | var request_promise_1 = __importDefault(require("request-promise"));
|
45 | var url_1 = require("./url");
|
46 | var gitAccount;
|
47 | var serverUrl;
|
48 | function setServerUrl(url) {
|
49 | serverUrl = url;
|
50 | }
|
51 | exports.setServerUrl = setServerUrl;
|
52 | function getHostname(url) {
|
53 | if (/https?/.test(url)) {
|
54 | var match = url.match(/^http(s)?:\/\/(.*?)\//);
|
55 | return match[2].split('@').pop();
|
56 | }
|
57 | else {
|
58 | var match = url.match(/@(.*):/);
|
59 | return match[1];
|
60 | }
|
61 | }
|
62 | function prepareAccount() {
|
63 | return __awaiter(this, void 0, void 0, function () {
|
64 | var url, options;
|
65 | return __generator(this, function (_a) {
|
66 | if (gitAccount) {
|
67 | return [2 ];
|
68 | }
|
69 | url = url_1.getURL(serverUrl, 'apply/getlist?name=0');
|
70 | if (!url) {
|
71 | return [2 ];
|
72 | }
|
73 | options = {
|
74 | url: url,
|
75 | method: 'GET'
|
76 | };
|
77 | return [2 , request_promise_1.default(options)
|
78 | .then(function (response) {
|
79 | var data = JSON.parse(response);
|
80 | if (data.account) {
|
81 | gitAccount = data.account;
|
82 | }
|
83 | })
|
84 | .catch(function (err) { })];
|
85 | });
|
86 | });
|
87 | }
|
88 | function transformUrl(url, account) {
|
89 | return __awaiter(this, void 0, void 0, function () {
|
90 | var hostname, transformedUrl, username, password;
|
91 | return __generator(this, function (_a) {
|
92 | switch (_a.label) {
|
93 | case 0:
|
94 | if (!account) return [3 , 1];
|
95 | gitAccount = account;
|
96 | return [3 , 3];
|
97 | case 1: return [4 , prepareAccount()];
|
98 | case 2:
|
99 | _a.sent();
|
100 | _a.label = 3;
|
101 | case 3:
|
102 | hostname = getHostname(url);
|
103 | if (/https?/.test(url)) {
|
104 | transformedUrl = url;
|
105 | }
|
106 | else {
|
107 | transformedUrl = url.replace("git@" + hostname + ":", "http://" + hostname + "/");
|
108 | }
|
109 | if (gitAccount) {
|
110 | username = gitAccount.username, password = gitAccount.password;
|
111 | return [2 , transformedUrl.replace(/https?:\/\/(.*?(:.*?)?@)?/, "http://" + username + ":" + password + "@")];
|
112 | }
|
113 | return [2 , transformedUrl];
|
114 | }
|
115 | });
|
116 | });
|
117 | }
|
118 | exports.transformUrl = transformUrl;
|
119 | function clearGitCert(url) {
|
120 | return __awaiter(this, void 0, void 0, function () {
|
121 | var username;
|
122 | return __generator(this, function (_a) {
|
123 | switch (_a.label) {
|
124 | case 0:
|
125 | username = gitAccount.username;
|
126 | if (!username) {
|
127 | return [2 ];
|
128 | }
|
129 | if (!!/https?:\/\/(.*?(:.*?)?@)/.test(url)) return [3 , 2];
|
130 | return [4 , transformUrl(url)];
|
131 | case 1:
|
132 | url = _a.sent();
|
133 | _a.label = 2;
|
134 | case 2: return [2 , new Promise(function (resolve) {
|
135 | var _a, _b;
|
136 | var child = cross_spawn_1.default('git', ['credential', 'reject'], {
|
137 | windowsHide: true,
|
138 | timeout: 60 * 1000 * 1
|
139 | });
|
140 | (_a = child.stdin) === null || _a === void 0 ? void 0 : _a.write("url=" + url);
|
141 | (_b = child.stdin) === null || _b === void 0 ? void 0 : _b.end();
|
142 | child.on('close', function (code) {
|
143 | resolve(code);
|
144 | });
|
145 | })];
|
146 | }
|
147 | });
|
148 | });
|
149 | }
|
150 | exports.clearGitCert = clearGitCert;
|
151 | function clearGitCertByPath(repoPath) {
|
152 | var _a;
|
153 | return __awaiter(this, void 0, void 0, function () {
|
154 | var ret, url;
|
155 | return __generator(this, function (_b) {
|
156 | ret = cross_spawn_1.default.sync('git', ['config', '--get', 'remote.origin.url'], {
|
157 | windowsHide: true,
|
158 | cwd: repoPath
|
159 | });
|
160 | url = (_a = ret === null || ret === void 0 ? void 0 : ret.stdout) === null || _a === void 0 ? void 0 : _a.toString().trim();
|
161 | return [2 , clearGitCert(url)];
|
162 | });
|
163 | });
|
164 | }
|
165 | exports.clearGitCertByPath = clearGitCertByPath;
|
166 | function download(url, tag, filepath) {
|
167 | return __awaiter(this, void 0, void 0, function () {
|
168 | var cloneUrl;
|
169 | return __generator(this, function (_a) {
|
170 | switch (_a.label) {
|
171 | case 0: return [4 , transformUrl(url)];
|
172 | case 1:
|
173 | cloneUrl = _a.sent();
|
174 | console.log('clone from', url);
|
175 | return [2 , new Promise(function (resolve, reject) {
|
176 | var _a, _b;
|
177 | var child = cross_spawn_1.default('git', ['clone', '-b', tag, '--progress', '--depth', '1', cloneUrl, filepath], {
|
178 | stdio: 'pipe',
|
179 | windowsHide: true
|
180 | });
|
181 | var doneFlag = false;
|
182 | (_a = child.stderr) === null || _a === void 0 ? void 0 : _a.on('data', function (d) {
|
183 | if (doneFlag) {
|
184 | return;
|
185 | }
|
186 | if (d.toString().startsWith('Note:')) {
|
187 | doneFlag = true;
|
188 | return;
|
189 | }
|
190 | process.stderr.write(d);
|
191 | });
|
192 | (_b = child.stdout) === null || _b === void 0 ? void 0 : _b.on('data', function (d) {
|
193 | if (doneFlag) {
|
194 | return;
|
195 | }
|
196 | process.stdout.write(d);
|
197 | });
|
198 | child.on('close', function (code) {
|
199 | if (code === 0) {
|
200 | resolve(0);
|
201 | }
|
202 | else {
|
203 | reject(code);
|
204 | }
|
205 | });
|
206 | child.on('error', function (err) {
|
207 | reject(err);
|
208 | });
|
209 | }).finally(function () { return clearGitCert(cloneUrl); })];
|
210 | }
|
211 | });
|
212 | });
|
213 | }
|
214 | exports.download = download;
|
215 |
|
\ | No newline at end of file |