-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
103 lines (85 loc) · 3.3 KB
/
parser.js
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
import through2 from "through2";
import klaw from "klaw";
export class Parser {
constructor( input ) {
this.input = input;
}
indexSets = ( path ) => {
const excludeFilesFilter = through2.obj( function ( item, enc, next ) {
if ( item.stats.isDirectory() ) this.push( item );
next();
} );
return new Promise( ( resolve, reject ) => {
const hits = [];
klaw( path )
.pipe( excludeFilesFilter )
.on( "data", item => hits.push( item.path ) )
.on( "end", () => resolve( hits ) )
.on( "error", error => reject( error ) );
} );
}
indexAnimations = ( path ) => {
const excludeDirsFilter = through2.obj( function ( item, enc, next ) {
if ( !item.stats.isDirectory() ) this.push( item );
next();
} );
return new Promise( ( resolve, reject ) => {
const hits = [];
klaw( path )
.pipe( excludeDirsFilter )
.on( "data", item => hits.push( item.path ) )
.on( "end", () => resolve( hits ) )
.on( "error", error => reject( error ) );
} );
}
async buildIndex() {
const removeRoot = animations => animations.splice( 1 );
const getDirName = path => path.split( "\\" ).pop();
const getFileName = path => path.split( "\\" ).pop();
const getOrientation = name => name.split( "_" ).shift();
const removeExtension = name => name.split( "." ).shift();
const summariseFile = ( path, animation, i ) => {
const fileName = getFileName( path );
const name = removeExtension( fileName );
const orientation = getOrientation( name );
return {
i,
path,
name,
fileName,
animation,
orientation
};
};
const sets = removeRoot( await this.indexSets( this.input ) );
const animationNames = sets.map( getDirName );
const summary = [];
let totalFiles = [];
let orientations;
for await ( const path of sets ) {
const animationName = getDirName( path );
let files = await this.indexAnimations( path );
files = files.map( ( file, i ) => summariseFile( file, animationName, i + totalFiles.length ) );
totalFiles = totalFiles.concat( files );
orientations = files.reduce( ( total, current ) => {
const { orientation } = current;
if ( !total.includes( orientation ) ) total.push( orientation );
return total;
}, [] );
const animationsPerOrientation = orientations.map( orientation => {
return {
animation : animationName,
animations : files.filter( file => file.orientation == orientation ),
orientation
};
} );
summary.push( { name : animationName, path, animations : animationsPerOrientation } );
}
return {
summary,
files : totalFiles,
orientations : orientations,
animations : animationNames
};
}
}