-
Notifications
You must be signed in to change notification settings - Fork 56
/
file-gatherer.ts
89 lines (75 loc) · 3.11 KB
/
file-gatherer.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
84
85
86
87
88
89
import * as fs from 'fs';
import _ from 'lodash';
import * as find from 'find';
import * as _path from 'path';
import { DependencyPointer } from '../domain';
function isRealElmPaths(sourceDir: string, filePath: String): boolean {
const modulePath = filePath.replace(_path.normalize(sourceDir + '/'), '');
const moduleParts = modulePath.split('/');
return _.every(moduleParts, m => m.match('^[A-Z].*'));
}
function includedInFileSet(path: string): boolean {
if (!path.match(/\.elm$/)) {
return false;
}
return path.indexOf('elm-stuff') === -1 && path.indexOf('node_modules') === -1;
}
interface ElmPackage {
'source-directories': string[];
'exposed-modules': string[];
}
function targetFilesForPathAndPackage(directory: string, path: string, pack: ElmPackage): string[] {
const packTargetDirs: string[] = pack['source-directories'] || ['src'];
const targetFiles = _.uniq(
_.flatten(
packTargetDirs.map(x => {
const sourceDir = _path.normalize(path + '/' + x);
const exists = fs.existsSync(sourceDir);
if (!exists) {
return [];
}
const dirFiles = find.fileSync(/\.elm$/, sourceDir).filter(x => {
const resolvedX = _path.resolve(x);
const resolvedPath = _path.resolve(path);
const relativePath = resolvedX.replace(resolvedPath, '');
return includedInFileSet(relativePath) && x.length > 0;
});
return dirFiles.filter(x => isRealElmPaths(sourceDir, x));
})
)
).map(function(s) {
const sParts = s.split(_path.sep);
const dirParts = directory.split(_path.sep);
while (sParts.length > 0 && dirParts.length > 0) {
if (sParts[0] == dirParts[0]) {
sParts.shift();
dirParts.shift();
} else {
break;
}
}
const result = dirParts.map(() => '../').join('') + sParts.join('/');
return _path.normalize(result);
});
return targetFiles;
}
function getDependencyFiles(directory: string, dep: DependencyPointer) {
const depPath = `${directory}/elm-stuff/packages/${dep.name}/${dep.version}`;
const depPackageFile: ElmPackage = require(depPath + '/elm.json');
const unfilteredTargetFiles: string[] = targetFilesForPathAndPackage(directory, depPath, depPackageFile);
const exposedModules: string[] = depPackageFile['exposed-modules'].map(x =>
_path.normalize('/' + x.replace(new RegExp('\\.', 'g'), '/') + '.elm')
);
return unfilteredTargetFiles.filter(function(x) {
return exposedModules.filter(e => _.endsWith(x, e))[0];
});
}
function gather(directory: string): { interfaceFiles: Array<string[]>; sourceFiles: string[] } {
const packageFile = require(directory + '/elm.json');
const input = {
interfaceFiles: [],
sourceFiles: targetFilesForPathAndPackage(directory, directory, packageFile)
};
return input;
}
export { gather, getDependencyFiles, includedInFileSet };