@@ -12,34 +12,55 @@ var loadHtml = require('./file-handlers/html');
1212var loadCss = require ( './file-handlers/css' ) ;
1313var compareUrls = require ( 'compare-urls' ) ;
1414
15- function getLoadedResource ( resource ) {
15+ function getHandleFunction ( resource ) {
16+ var type = resource . getType ( ) ;
17+ switch ( type ) {
18+ case types . css : return loadCss ;
19+ case types . html : return function loadHtmlAndCss ( context , po ) {
20+ return loadHtml ( context , po ) . then ( function ( loaded ) {
21+ return loadCss ( context , loaded ) ;
22+ } ) ;
23+ } ;
24+ default : return _ . noop ;
25+ }
26+ }
27+
28+ function Scraper ( options ) {
29+ this . originalResources = [ ] ;
30+ this . loadedResources = [ ] ;
31+
32+ this . options = _ . extend ( { } , defaults , options ) ;
33+ this . options . directory = path . resolve ( process . cwd ( ) , this . options . directory || '' ) ;
34+ }
35+
36+ Scraper . prototype . getLoadedResource = function getLoadedResource ( resource ) {
1637 return _ . find ( this . loadedResources , function ( lr ) {
1738 return compareUrls ( resource . getUrl ( ) , lr . getUrl ( ) ) ;
1839 } ) ;
19- }
40+ } ;
2041
21- function addLoadedResource ( resource ) {
42+ Scraper . prototype . addLoadedResource = function addLoadedResource ( resource ) {
2243 this . loadedResources . push ( resource ) ;
23- }
44+ } ;
2445
25- function getOccupiedFilenames ( ) {
46+ Scraper . prototype . getOccupiedFilenames = function getOccupiedFilenames ( ) {
2647 var subdirectories = _ . map ( this . options . subdirectories , function ( dir ) { return dir . directory ; } ) ;
2748 var loadedFiles = _ . map ( this . loadedResources , function ( r ) { return r . getFilename ( ) ; } ) ;
2849 return subdirectories . concat ( loadedFiles ) ;
29- }
50+ } ;
3051
31- function getHtmlSources ( ) {
52+ Scraper . prototype . getHtmlSources = function getHtmlSources ( ) {
3253 return this . options . sources ;
33- }
54+ } ;
3455
35- function generateFilename ( resource ) {
56+ Scraper . prototype . generateFilename = function generateFilename ( resource ) {
3657 var self = this ;
3758
3859 var occupiedFilenames = self . getOccupiedFilenames ( ) ;
3960
40- var preferedFilename = resource . getFilename ( ) ; // which was set in options
61+ var preferredFilename = resource . getFilename ( ) ; // which was set in options
4162 var urlFilename = utils . getFilenameFromUrl ( resource . getUrl ( ) ) ; // try to get filename from url
42- var filename = preferedFilename || urlFilename || self . options . defaultFilename ;
63+ var filename = preferredFilename || urlFilename || self . options . defaultFilename ;
4364
4465 var ext = path . extname ( filename ) ;
4566 var dir = self . getDirectoryByExtension ( ext ) ;
@@ -52,30 +73,17 @@ function generateFilename (resource) {
5273 index ++ ;
5374 }
5475 return currentFilename ;
55- }
76+ } ;
5677
57- function getDirectoryByExtension ( ext ) {
78+ Scraper . prototype . getDirectoryByExtension = function getDirectoryByExtension ( ext ) {
5879 return _ . chain ( this . options . subdirectories )
5980 . filter ( function ( dir ) { return _ . contains ( dir . extensions , ext ) ; } )
6081 . map ( function ( dir ) { return dir . directory ; } )
6182 . first ( )
6283 . value ( ) || '' ;
63- }
64-
65- function getHandleFunction ( resource ) {
66- var type = resource . getType ( ) ;
67- switch ( type ) {
68- case types . css : return loadCss ;
69- case types . html : return function loadHtmlAndCss ( context , po ) {
70- return loadHtml ( context , po ) . then ( function ( loaded ) {
71- return loadCss ( context , loaded ) ;
72- } ) ;
73- } ;
74- default : return _ . noop ;
75- }
76- }
84+ } ;
7785
78- function loadResource ( resource ) {
86+ Scraper . prototype . loadResource = function loadResource ( resource ) {
7987 var self = this ;
8088
8189 var loaded = self . getLoadedResource ( resource ) ; // try to find already loaded
@@ -104,18 +112,17 @@ function loadResource (resource) {
104112 return Promise . resolve ( resource ) ;
105113 } ) ;
106114 }
107-
108115 return Promise . resolve ( loaded ) ;
109- }
116+ } ;
110117
111- function validate ( ) {
118+ Scraper . prototype . validate = function validate ( ) {
112119 if ( fs . existsSync ( this . options . directory ) ) {
113120 return Promise . reject ( new Error ( 'Path ' + this . options . directory + ' exists' ) ) ;
114121 }
115122 return Promise . resolve ( ) ;
116- }
123+ } ;
117124
118- function prepare ( ) {
125+ Scraper . prototype . prepare = function prepare ( ) {
119126 var self = this ;
120127 fs . ensureDirSync ( self . options . directory ) ;
121128
@@ -130,9 +137,9 @@ function prepare () {
130137 return new Resource ( url , filename ) ;
131138 } ) ;
132139 return Promise . resolve ( ) ;
133- }
140+ } ;
134141
135- function load ( ) {
142+ Scraper . prototype . load = function load ( ) {
136143 var self = this ;
137144 return Promise . map ( self . originalResources , function loadPage ( po ) {
138145 return self . loadResource ( po ) . then ( function pageLoaded ( loaded ) {
@@ -142,39 +149,19 @@ function load () {
142149 } ) ;
143150 } ) ;
144151 } ) ;
145- }
152+ } ;
146153
147- function errorCleanup ( error ) {
154+ Scraper . prototype . errorCleanup = function errorCleanup ( error ) {
148155 if ( ! _ . isEmpty ( this . loadedResources ) ) {
149156 fs . removeSync ( this . options . directory ) ;
150157 }
151158 throw error ;
152- }
153-
154- function Scraper ( options ) {
155- this . originalResources = [ ] ;
156- this . loadedResources = [ ] ;
157-
158- this . options = _ . extend ( { } , defaults , options ) ;
159- this . options . directory = path . resolve ( process . cwd ( ) , this . options . directory || '' ) ;
160-
161- this . validate = validate . bind ( this ) ;
162- this . prepare = prepare . bind ( this ) ;
163- this . load = load . bind ( this ) ;
164- this . errorCleanup = errorCleanup . bind ( this ) ;
165-
166- this . loadResource = loadResource . bind ( this ) ;
167- this . getLoadedResource = getLoadedResource . bind ( this ) ;
168- this . addLoadedResource = addLoadedResource . bind ( this ) ;
169- this . getOccupiedFilenames = getOccupiedFilenames . bind ( this ) ;
170- this . generateFilename = generateFilename . bind ( this ) ;
171- this . getDirectoryByExtension = getDirectoryByExtension . bind ( this ) ;
172- this . getHtmlSources = getHtmlSources . bind ( this ) ;
173- }
159+ } ;
174160
175161Scraper . prototype . scrape = function scrape ( callback ) {
176162 var self = this ;
177- return self . validate ( )
163+ return Promise . bind ( self )
164+ . then ( self . validate )
178165 . then ( self . prepare )
179166 . then ( self . load )
180167 . catch ( self . errorCleanup )
0 commit comments