forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
49 lines (41 loc) · 1.33 KB
/
helpers.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
import { readFileSync, readdirSync, statSync, Stats } from 'fs';
import { join, resolve } from 'path';
const md5 = require('md5');
export const projectRoot = (relativePath) => {
return resolve(__dirname, '..', relativePath);
};
export const globalCSSImports = () => {
return [
projectRoot(join('src', 'styles', '_variables.scss')),
projectRoot(join('src', 'styles', '_mixins.scss')),
];
};
/**
* Calculates the md5 hash of a file
*
* @param filePath The path of the file
*/
export function calculateFileHash(filePath: string): string {
const fileContent: Buffer = readFileSync(filePath);
return md5(fileContent);
}
/**
* Calculate the hashes of all the files (matching the given regex) in a certain folder
*
* @param folderPath The path of the folder
* @param regExp A regex of the files in the folder for which a hash needs to be generated
*/
export function getFileHashes(folderPath: string, regExp: RegExp): { [fileName: string]: string } {
const files: string[] = readdirSync(folderPath);
let hashes: { [fileName: string]: string } = {};
for (const file of files) {
if (file.match(regExp)) {
const filePath: string = join(folderPath, file);
const stats: Stats = statSync(filePath);
if (stats.isFile()) {
hashes[file] = calculateFileHash(filePath);
}
}
}
return hashes;
}