forked from turboext/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-filesystem.ts
65 lines (52 loc) · 2.11 KB
/
lint-filesystem.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
import { resolve } from 'path';
import { readdir, lstatSync } from 'fs-extra';
import pascalCase = require('pascal-case');
import chalk from 'chalk';
const { yellow, green, cyanBright: cyan, redBright: red } = chalk;
const componentsRoot = resolve(__dirname, '..', 'components');
const getErrs = () => readdir(componentsRoot)
.then(dirs => {
const errs: string[] = [];
const promises = dirs.map((dir: string): Promise<void> => {
const dirName = resolve(componentsRoot, dir);
if (!lstatSync(dirName).isDirectory()) {
errs.push(`expected ${green(dirName)} to be a ${yellow('directory')}`);
return Promise.resolve();
}
const pascal = pascalCase(dir);
if (pascal !== dir) {
errs.push(`expected ${green(dir)} component to be named using pascal-case, suggestion: ${yellow(pascal)}`);
return Promise.resolve();
}
if (!dir.startsWith('Ext')) {
errs.push(`expected ${green(dir)} component to start with Ext, suggestion: ${green(`Ext${dir}`)}`);
return Promise.resolve();
}
const expected = `${dir}.tsx`;
const checkFileExistance = (file: string) => file === expected;
return readdir(dirName)
.then(files => files.some(checkFileExistance))
.then(isExists => {
if (!isExists) {
errs.push(`expected to find ${green(expected)} in ${yellow(dirName)}`)
}
})
});
return Promise.all(promises)
.then(() => errs);
});
/**
* Index errors for better reporting
* @param error - string representing an error
* @param index - its index it array
*/
const indexify = (error: string, index: number) => `${cyan(`${index + 1})`)} ${error}`;
getErrs().then(errs => {
if (!errs.length) {
return;
}
console.log(red('\nSome directory linting errors were found:'));
const result = errs.map(indexify).join('\n');
console.log(result);
process.exit(1);
});