-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathfind-redundant.ts
83 lines (70 loc) · 2.7 KB
/
find-redundant.ts
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
import * as path from 'path';
import { bold, gray, red } from 'colorette';
import { Command, CommandLineInputs, CommandLineOptions, CommandMetadata } from '@ionic/cli-framework';
import { getFileChecksum, readdirp, stat } from '@ionic/utils-fs';
import {
IONIC_TYPE_DIRECTORIES,
REPO_DIRECTORY,
buildStarterId,
getStarterDirectories,
getStarterInfoFromPath,
} from '../lib/build';
import { log } from '../utils';
export class FindRedundantCommand extends Command {
async getMetadata(): Promise<CommandMetadata> {
return {
name: 'find-redundant',
summary: 'Find redundant files in starters that exist as base files',
options: [
{
name: 'community',
summary: 'Include community starters',
type: Boolean,
},
],
};
}
async run(inputs: CommandLineInputs, options: CommandLineOptions) {
const community = options['community'] ? true : false;
const redundantFiles: string[] = [];
for (const ionicType of IONIC_TYPE_DIRECTORIES) {
const baseDir = path.resolve(REPO_DIRECTORY, ionicType, 'base');
const starterDirs = await getStarterDirectories(ionicType, { community });
for (const starterDir of starterDirs) {
const [, starterType] = getStarterInfoFromPath(starterDir);
const id = buildStarterId(ionicType, starterType, starterDir);
const contents = (await readdirp(starterDir)).map((p) => p.substring(starterDir.length + 1));
for (const file of contents) {
const filePath = path.resolve(starterDir, file);
const baseFilePath = path.resolve(baseDir, file);
try {
const [fileStat, baseFileStat] = await Promise.all([stat(filePath), stat(baseFilePath)]);
if (!fileStat.isDirectory() && !baseFileStat.isDirectory()) {
const [fileChecksum, baseFileChecksum] = await Promise.all([
getFileChecksum(filePath),
getFileChecksum(baseFilePath),
]);
if (fileChecksum === baseFileChecksum) {
log(id, red(`${bold(file)}: same file in base files`));
redundantFiles.push(filePath);
} else {
log(id, gray(`${bold(file)}: found in base files, but checksum differs`));
}
}
} catch (e) {
// ignore
}
}
}
}
if (redundantFiles.length > 0) {
console.log(
`The following files were identified as redundant and should be deleted:\n` +
` - ${redundantFiles.map((f) => bold(f.substring(REPO_DIRECTORY.length + 1))).join('\n - ')}\n`
);
process.exit(1);
} else {
console.log('No redundant files found!');
}
}
}