3
3
const packageJson = require ( '../package.json' ) ;
4
4
const { program } = require ( 'commander' ) ;
5
5
6
- const path = require ( 'path' ) ;
7
6
const fs = require ( 'fs' ) ;
8
- const promisify = require ( 'util' ) . promisify ;
9
- const imageSize = require ( 'image-size' ) ;
10
- const musicMetadata = require ( 'music-metadata' ) ;
11
7
12
- const { isDirectory } = require ( '../lib/path-validation.js' ) ;
13
- const { formatHRTFileSize, formatMsToHRT } = require ( '../lib/format.js' ) ;
14
- const { parseSRASConfig } = require ( '../lib/srasConfig.js' ) ;
15
- const { isPowerOfTwo } = require ( '../lib/mathHelpers.js' ) ;
8
+ const assetScanner = require ( '../src/' ) . assetScanner ;
9
+ const { isDirectory } = require ( '../src/lib/path-validation.js' ) ;
16
10
17
- let scannedFiles = 0 ;
18
- let skippedFiles = 0 ;
19
- let ignoredFiles = 0 ;
20
-
21
- const scannedTypes = { } ;
22
- const unpermittedTypes = { } ;
23
- const scanReports = [ ] ;
24
-
25
- const fsReadDir = promisify ( fs . readdir ) ;
26
- const fsStat = promisify ( fs . stat ) ;
27
- const sizeOf = promisify ( imageSize ) ;
28
-
29
- const buildReportMsg = ( ...segments ) => segments . join ( ' ' ) ;
30
11
const fail = ( err ) => {
31
12
console . log ( 'Scan failed:' ) ;
32
13
console . error ( err ) ;
@@ -46,205 +27,8 @@ if (!isDirectory(program.path)) {
46
27
}
47
28
48
29
const config = JSON . parse ( fs . readFileSync ( program . config ) ) ;
49
- const scanConfig = parseSRASConfig ( config ) ;
50
-
51
- const scanJs = async ( filePath , scanRules ) => {
52
- try {
53
- const stat = await fsStat ( filePath ) ;
54
- const maxSize = scanRules . maxSize || Infinity ;
55
- if ( maxSize > 0 && stat . size > maxSize ) {
56
- const fileName = path . basename ( filePath ) ;
57
- scanReports . push ( buildReportMsg (
58
- 'JS file size is larger than the recommended file size' ,
59
- `[recommended = ${ formatHRTFileSize ( maxSize ) } ],` ,
60
- `[${ fileName } = ${ formatHRTFileSize ( stat . size ) } ]` ,
61
- ) ) ;
62
- }
63
- } catch ( e ) {
64
- fail ( e ) ;
65
- }
66
- } ;
67
-
68
- const scanImage = async ( filePath , scanRules ) => {
69
- try {
70
- const stat = await fsStat ( filePath ) ;
71
- const dimensions = await sizeOf ( filePath ) ;
72
-
73
- const maxWidth = scanRules . maxWidth || Infinity ;
74
- const maxHeight = scanRules . maxHeight || Infinity ;
75
- const maxSize = scanRules . maxSize || Infinity ;
76
-
77
- const fileName = filePath . split ( '\\' ) . pop ( ) ;
78
-
79
- if ( dimensions . width > maxWidth || dimensions . height > maxHeight ) {
80
- scanReports . push ( buildReportMsg (
81
- 'Image is larger than the recommended max dimensions' ,
82
- `[recommended = ${ maxHeight } x${ maxHeight } ],` ,
83
- `[${ fileName } = ${ dimensions . width } x${ dimensions . height } ]` ,
84
- ) ) ;
85
- }
86
-
87
- if ( ! ! scanRules . powerOfTwo && ( ! isPowerOfTwo ( dimensions . width ) || ! isPowerOfTwo ( dimensions . height ) ) ) {
88
- scanReports . push ( buildReportMsg (
89
- 'Image dimensions are recommended to be powers of two' ,
90
- `[${ fileName } = ${ dimensions . width } x${ dimensions . height } ]` ,
91
- ) ) ;
92
- }
93
-
94
- if ( maxSize > 0 && stat . size > maxSize ) {
95
- scanReports . push ( buildReportMsg (
96
- 'Image file size is larger than the recommended file size' ,
97
- `[recommended = ${ formatHRTFileSize ( maxSize ) } ],` ,
98
- `[${ fileName } = ${ formatHRTFileSize ( stat . size ) } ]` ,
99
- ) ) ;
100
- }
101
- } catch ( err ) {
102
- fail ( err ) ;
103
- }
104
- } ;
105
-
106
- const scanAudio = async ( filePath , scanRules ) => {
107
- try {
108
- const stat = await fsStat ( filePath ) ;
109
- const metadata = await musicMetadata . parseFile ( filePath ) ;
110
-
111
- const maxSize = scanRules . maxSize || 0 ;
112
- const maxChannels = scanRules . maxChannels || 0 ;
113
- const sampleRate = scanRules . sampleRate || 0 ;
114
- const duration = scanRules . duration || 0 ;
115
-
116
- const fileName = filePath . split ( '\\' ) . pop ( ) ;
117
-
118
- if ( maxSize > 0 && stat . size > maxSize ) {
119
- scanReports . push ( buildReportMsg (
120
- 'Audio file size is larger than the recommended file size' ,
121
- `[recommended = ${ maxSize } ],` ,
122
- `[${ fileName } = ${ formatHRTFileSize ( stat . size ) } ]` ,
123
- ) ) ;
124
- }
125
-
126
- if ( maxChannels > 0 && metadata . format . numberOfChannels > maxChannels ) {
127
- scanReports . push ( buildReportMsg (
128
- 'Audio file contains more than the recommended number of channels' ,
129
- `[recommended = ${ maxChannels } ],` ,
130
- `[${ fileName } = ${ metadata . format . numberOfChannels } ]` ,
131
- ) ) ;
132
- }
133
-
134
- if ( sampleRate > 0 && metadata . format . sampleRate !== sampleRate ) {
135
- scanReports . push ( buildReportMsg (
136
- 'Audio sample rate does not match the recommended sample rate' ,
137
- `[recommended = ${ sampleRate } ],` ,
138
- `[${ fileName } = ${ metadata . format . sampleRate } ]` ,
139
- ) ) ;
140
- }
141
-
142
- const convertedDuration = metadata . format . duration * 1000 ; // S to MS
143
- if ( duration > 0 && convertedDuration > duration ) {
144
- scanReports . push ( buildReportMsg (
145
- 'Audio duration is larger than recommended duration' ,
146
- `[recommended = ${ duration } ],` ,
147
- `[${ fileName } = ${ convertedDuration } ]` ,
148
- ) ) ;
149
- }
150
- } catch ( err ) {
151
- fail ( err ) ;
152
- }
153
- } ;
154
-
155
- const scanDirectory = async ( filePath ) => {
156
- if ( ! isDirectory ( filePath ) ) {
157
- const ext = path . extname ( filePath ) . slice ( 1 ) ;
158
-
159
- if ( scanConfig . ignored . test ( ext ) ) {
160
- // Track total ignored files.
161
- ignoredFiles ++ ;
162
- return ;
163
- } else if ( scanConfig . unpermitted . test ( ext ) ) {
164
- // Track how times we have scanned an this unpermitted file type.
165
- unpermittedTypes [ ext ] = ( unpermittedTypes [ ext ] || 0 ) + 1 ;
166
- skippedFiles ++ ; // skipped or ignored ??????
167
- return ;
168
- }
169
-
170
- // Check if there is a rule category for this file extension.
171
- const hasJsRule = scanConfig . codeRules && scanConfig . codeRules . js && ext === 'js' ;
172
- const hasImgRule = scanConfig . imgRules [ ext ] !== undefined ;
173
- const hasAudRule = ! hasImgRule && scanConfig . audRules [ ext ] !== undefined ;
174
-
175
- if ( hasJsRule ) {
176
- await scanJs ( filePath , scanConfig . codeRules . js ) ;
177
- } else if ( hasImgRule ) {
178
- await scanImage ( filePath , scanConfig . imgRules [ ext ] ) ;
179
- } else if ( hasAudRule ) {
180
- await scanAudio ( filePath , scanConfig . audRules [ ext ] ) ;
181
- } else {
182
- // Cannot find rules for this file type so it will be skipped.
183
- // Track total skipped files.
184
- skippedFiles ++ ;
185
- return ;
186
- }
187
-
188
- // Track how many times we have scanned this file type.
189
- scannedTypes [ ext ] = ( scannedTypes [ ext ] || 0 ) + 1 ;
190
-
191
- // Track total scanned files.
192
- scannedFiles ++ ;
193
- } else {
194
- try {
195
- const files = await fsReadDir ( filePath ) ;
196
- for ( let i = 0 ; i < files . length ; i ++ ) {
197
- await scanDirectory ( path . join ( filePath , files [ i ] ) ) ;
198
- }
199
- } catch ( err ) {
200
- fail ( err ) ;
201
- }
202
- }
203
- } ;
204
-
205
- const assetScan = async ( ) => {
206
- const startTime = Date . now ( ) ;
207
-
208
- console . log ( `Scanning assets at [path = ${ program . path } ]` ) ;
209
-
210
- await scanDirectory ( program . path ) ;
211
-
212
- const counts = [ ] ;
213
- const unpermittedReports = [ ] ;
214
- const missingRequiredReports = [ ] ;
215
-
216
- Object . keys ( scannedTypes ) . forEach ( ( i ) => counts . push ( `${ scannedTypes [ i ] } ${ i } (s)` ) ) ;
217
- Object . keys ( unpermittedTypes ) . forEach ( ( i ) => unpermittedReports . push (
218
- `Detected ${ unpermittedTypes [ i ] } unpermitted file type(s) [type = ${ i } ]` ,
219
- ) ) ;
220
-
221
- scanConfig . requiredTypes . forEach ( ( type ) => {
222
- if ( scannedTypes [ type ] === undefined ) {
223
- missingRequiredReports . push ( `Could not find file types matching [type = ${ type } ]` ) ;
224
- }
225
- } ) ;
226
-
227
- // RENDER SCAN REPORTS
228
-
229
- if ( scanReports . length > 0 ) {
230
- console . log ( scanReports . join ( '\n' ) ) ;
231
- }
232
- if ( unpermittedReports . length > 0 ) {
233
- console . log ( unpermittedReports . join ( '\n' ) ) ;
234
- }
235
- if ( missingRequiredReports . length > 0 ) {
236
- console . log ( missingRequiredReports . join ( '\n' ) ) ;
237
- }
238
- if ( counts . length > 0 ) {
239
- console . log ( `Scanned ${ counts . join ( ', ' ) } ` ) ;
240
- }
241
- console . log (
242
- `Scan complete [time = ${ formatMsToHRT ( Date . now ( ) - startTime ) } ]:` ,
243
- `${ scannedFiles } file(s) scanned, ${ skippedFiles } file(s) skipped, ${ ignoredFiles } file(s) ignored` ,
244
- ) ;
245
-
30
+ assetScanner . run ( program . path , config , true ) . then ( ( ) => {
246
31
process . exit ( ) ;
247
- } ;
248
-
249
- // Begin scanning
250
- assetScan ( ) ;
32
+ } ) . catch ( ( err ) => {
33
+ fail ( err ) ;
34
+ } ) ;
0 commit comments