forked from ant-design/antd-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckDiff.ts
More file actions
161 lines (138 loc) · 4.73 KB
/
checkDiff.ts
File metadata and controls
161 lines (138 loc) · 4.73 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
import { getProjectPath } from '../utils/projectHelper';
import { join } from 'path';
import chalk from 'chalk';
import fetch from 'node-fetch';
import readline from 'readline';
import minimist from 'minimist';
import Arborist from '@npmcli/arborist';
import packlist from 'npm-packlist';
const argv = minimist(process.argv.slice(2));
// Added interface to replace "any"
interface FileItem {
path: string;
files?: FileItem[];
}
function getMajorVersion(version: string, specificVersion?: string): string {
if (specificVersion) {
return `@${specificVersion}`;
}
const match = version && version.match(/^\d+/);
if (match) {
return `@${match[0]}.x`;
}
return '';
}
function getVersionFromURL(url: string, name: string): string {
const affix = url.slice(url.indexOf(name) + name.length + 1);
return affix.slice(0, affix.indexOf('/'));
}
export default function (
packageName: string,
packageVersion: string,
done: (err?: Error) => void
): void {
const mergedVersion = getMajorVersion(packageVersion, argv.version);
console.log(chalk.cyan(`Fetching latest version file list...${packageName}${mergedVersion}`));
function getLatestVersionFileList() {
return fetch(`https://unpkg.com/${packageName}${mergedVersion}/?meta`)
.then(res => {
const version = getVersionFromURL(res.url, packageName);
return res.json().then((json: object) => ({ version, ...json }));
})
.then(({ version, files: pkgFiles }: { version: string; files: FileItem[] }) => {
function flattenPath(files: FileItem[], fileList: string[] = []): string[] {
(files || []).forEach(({ path, files: subFiles }) => {
const realPath = argv.path ? join(argv.path, path) : path
fileList.push(realPath);
flattenPath(subFiles, fileList);
});
return fileList;
}
return { version, fileList: flattenPath(pkgFiles) };
})
}
function getLocalVersionFileList() {
const arborist = new Arborist({ path: getProjectPath() });
return arborist.loadActual().then(packlist) as Promise<string[]>;
}
Promise.all([
getLocalVersionFileList(),
getLatestVersionFileList(),
])
.then(([localFiles, { version, fileList }]) => {
const localSet = new Set(localFiles);
const remoteSet = new Set(fileList);
const missingFiles: string[] = [];
const addedFiles: string[] = [];
const allFiles = new Set([...fileList, ...localFiles]);
allFiles.forEach(filePath => {
if (!localSet.has(filePath)) {
missingFiles.push(filePath);
} else if (!remoteSet.has(filePath)) {
addedFiles.push(filePath);
}
});
return { missingFiles, addedFiles, version };
})
.then(({ missingFiles, addedFiles, version }) => {
if (addedFiles.length) {
console.log(
chalk.yellow(`⚠️ Some file added in current build (last version: ${version}):`)
);
addedFiles.forEach(filePath => {
console.log(` + ${filePath}`);
});
// Separator
console.log();
console.log(chalk.gray(`-`.repeat(process.stdout.columns || 64)));
console.log();
}
if (missingFiles.length) {
console.log(
chalk.red(`⚠️ Some file missing in current build (last version: ${version}):`)
);
missingFiles.forEach(filePath => {
console.log(` - ${filePath}`);
});
}
const total = missingFiles.length + addedFiles.length;
if (total) {
return Promise.reject(
new Error(`Please double confirm with files. ${missingFiles.length} missing, ${addedFiles.length} added.`)
);
}
console.log(
chalk.green('✅ Nothing missing compare to latest version:'),
chalk.gray(version)
);
return 0;
})
.then(() => done())
.catch(err => {
console.error(err);
console.log(chalk.yellow('\nNeed confirm for file diff:'));
function userConfirm(): void {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
['Type "YES" to confirm it is safe.', 'Type "NO" to exit process.', ''].join('\n'),
answer => {
rl.close();
if (answer === 'YES') {
console.log(chalk.green('✅ Confirm it is OK.'));
done();
} else if (answer === 'NO') {
console.log(chalk.red('🚫 Aha! Catch you!'));
done(new Error('User cancel the process.'));
} else {
console.log(chalk.yellow('Invalidate input. Type again!'));
userConfirm();
}
}
);
}
userConfirm();
});
}