forked from lisong/code-push-server
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathcommon.ts
More file actions
192 lines (177 loc) · 6.45 KB
/
common.ts
File metadata and controls
192 lines (177 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* eslint-disable no-cond-assign */
import fs from 'fs';
import { pipeline } from 'stream';
import util from 'util';
import extract from 'extract-zip';
import fsextra from 'fs-extra';
import { Logger } from 'kv-logger';
import _ from 'lodash';
import fetch from 'node-fetch';
import validator from 'validator';
import { AppError } from '../app-error';
import { config } from '../config';
const streamPipeline = util.promisify(pipeline);
export function parseVersion(versionNo: string) {
let version = '0';
let data = null;
// NOTE: Major is allowed up to 4 digits (e.g. "6666.3.678")
if ((data = versionNo.match(/^([0-9]{1,4})\.([0-9]{1,5})\.([0-9]{1,10})$/))) {
// "1.2.3"
version = data[1] + _.padStart(data[2], 5, '0') + _.padStart(data[3], 10, '0');
} else if ((data = versionNo.match(/^([0-9]{1,4})\.([0-9]{1,5})$/))) {
// "1.2"
version = data[1] + _.padStart(data[2], 5, '0') + _.padStart('0', 10, '0');
}
return version;
}
export function validatorVersion(versionNo: string) {
let flag = false;
let min = '0';
let max = '9999999999999999999';
let data = null;
if (versionNo === '*') {
// "*"
flag = true;
} else if ((data = versionNo.match(/^([0-9]{1,4})\.([0-9]{1,5})\.([0-9]{1,10})$/))) {
// "1.2.3"
flag = true;
min = data[1] + _.padStart(data[2], 5, '0') + _.padStart(data[3], 10, '0');
max =
data[1] +
_.padStart(data[2], 5, '0') +
_.padStart(`${parseInt(data[3], 10) + 1}`, 10, '0');
} else if ((data = versionNo.match(/^([0-9]{1,4})\.([0-9]{1,5})(\.\*){0,1}$/))) {
// "1.2" "1.2.*"
flag = true;
min = data[1] + _.padStart(data[2], 5, '0') + _.padStart('0', 10, '0');
max =
data[1] + _.padStart(`${parseInt(data[2], 10) + 1}`, 5, '0') + _.padStart('0', 10, '0');
} else if ((data = versionNo.match(/^~([0-9]{1,4})\.([0-9]{1,5})\.([0-9]{1,10})$/))) {
// "~1.2.3"
flag = true;
min = data[1] + _.padStart(data[2], 5, '0') + _.padStart(data[3], 10, '0');
max =
data[1] + _.padStart(`${parseInt(data[2], 10) + 1}`, 5, '0') + _.padStart('0', 10, '0');
} else if ((data = versionNo.match(/^\^([0-9]{1,4})\.([0-9]{1,5})\.([0-9]{1,10})$/))) {
// "^1.2.3"
flag = true;
min = data[1] + _.padStart(data[2], 5, '0') + _.padStart(data[3], 10, '0');
max =
_.toString(parseInt(data[1], 10) + 1) +
_.padStart('0', 5, '0') +
_.padStart('0', 10, '0');
} else if (
(data = versionNo.match(
/^([0-9]{1,4})\.([0-9]{1,5})\.([0-9]{1,10})\s?-\s?([0-9]{1,4})\.([0-9]{1,5})\.([0-9]{1,10})$/,
))
) {
// "1.2.3 - 1.2.7"
flag = true;
min = data[1] + _.padStart(data[2], 5, '0') + _.padStart(data[3], 10, '0');
max =
data[4] +
_.padStart(data[5], 5, '0') +
_.padStart(`${parseInt(data[6], 10) + 1}`, 10, '0');
} else if (
(data = versionNo.match(
/^>=([0-9]{1,4})\.([0-9]{1,5})\.([0-9]{1,10})\s?<([0-9]{1,4})\.([0-9]{1,5})\.([0-9]{1,10})$/,
))
) {
// ">=1.2.3 <1.2.7"
flag = true;
min = data[1] + _.padStart(data[2], 5, '0') + _.padStart(data[3], 10, '0');
max = data[4] + _.padStart(data[5], 5, '0') + _.padStart(data[6], 10, '0');
}
return [flag, min, max];
}
export async function createFileFromRequest(url: string, filePath: string, logger: Logger) {
try {
await fs.promises.stat(filePath);
return;
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
logger.debug(`createFileFromRequest url:${url}`);
const response = await fetch(url);
if (!response.ok) {
throw new AppError(`unexpected response ${response.statusText}`);
}
await streamPipeline(response.body, fs.createWriteStream(filePath));
}
export function copySync(sourceDst: string, targertDst: string) {
return fsextra.copySync(sourceDst, targertDst, { overwrite: true });
}
export function copy(sourceDst: string, targertDst: string) {
return fsextra.copy(sourceDst, targertDst, { overwrite: true });
}
function deleteFolder(folderPath: string) {
return fsextra.remove(folderPath);
}
export function deleteFolderSync(folderPath: string) {
return fsextra.removeSync(folderPath);
}
export async function createEmptyFolder(folderPath: string) {
await deleteFolder(folderPath);
await fsextra.mkdirs(folderPath);
}
export function createEmptyFolderSync(folderPath: string) {
deleteFolderSync(folderPath);
fsextra.mkdirsSync(folderPath);
}
export async function unzipFile(zipFile: string, outputPath: string, logger: Logger) {
try {
logger.debug(`unzipFile check zipFile ${zipFile} fs.R_OK`);
fs.accessSync(zipFile, fs.constants.R_OK);
logger.debug(`Pass unzipFile file ${zipFile}`);
} catch (err) {
throw new AppError(err.message);
}
try {
await extract(zipFile, { dir: outputPath });
logger.debug(`unzipFile success`);
} catch (err) {
throw new AppError(`it's not a zipFile`);
}
return outputPath;
}
export function getBlobDownloadUrl(blobUrl: string): string {
let fileName = blobUrl;
const { storageType } = config.common;
const { downloadUrl } = config[storageType];
if (storageType === 'local') {
fileName = `${blobUrl.substring(0, 2).toLowerCase()}/${blobUrl}`;
}
if (!validator.isURL(downloadUrl)) {
throw new AppError(`Please config ${storageType}.downloadUrl in config.js`);
}
return `${downloadUrl}/${fileName}`;
}
export function diffCollectionsSync(
collection1: Record<string, string>,
collection2: Record<string, string>,
) {
const diff: string[] = [];
const collection1Only: string[] = [];
const collection2Keys = new Set(Object.keys(collection2));
if (collection1 instanceof Object) {
const keys = Object.keys(collection1);
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
if (!collection2Keys.has(key)) {
collection1Only.push(key);
} else {
collection2Keys.delete(key);
if (!_.eq(collection1[key], collection2[key])) {
diff.push(key);
}
}
}
}
return {
diff,
collection1Only,
collection2Only: Array.from(collection2Keys),
};
}