UNPKG

2.14 kBtext/coffeescriptView Raw
1cp = require 'child_process'
2fs = require 'fs'
3_ = require 'underscore'
4_.str = require 'underscore.string'
5async = require 'async'
6
7CONTENT_TYPES = require './content'
8
9class Finder
10 constructor: ( @site, kind, cb ) ->
11 site = @site
12 kind = _.str.rtrim( kind, 's' )
13 klass = _.str.capitalize(kind)
14 @["_get#{klass}Paths"] ( err, filenames ) ->
15 # filter the paths to exclude . and special files, etc
16 filtered = _.reject( filenames || [], ( f ) -> f[0] == '.' || f[0] == '_' )
17 # generate Content instances for each path
18 contentGenerator = ( path, done ) ->
19 content = new CONTENT_TYPES[klass]( site, path )
20 content.process( done )
21 async.map( filtered, contentGenerator, cb )
22 _getStaticPaths: ( cb ) ->
23 fs.readdir "#{@site.root}/_static", ( err, statics ) ->
24 cb( err, statics || [] )
25 _getStylePaths: ( cb ) ->
26 fs.readdir "#{@site.root}/_styles", ( err, styles ) ->
27 cb( err, styles || [] )
28 _getScriptPaths: ( cb ) ->
29 fs.readdir "_scripts", ( err, scripts ) ->
30 cb( err, scripts || [] )
31 _getPostPaths: ( cb ) ->
32 fs.readdir "#{@site.root}/_posts", ( err, listings ) ->
33 # error is ok, no posts.
34 cb( null, listings || [] )
35 _getPagePaths: ( cb ) ->
36 site = @site
37 paths = []
38 processor = ( listing, done ) ->
39 if listing == null
40 done()
41 return
42 fs.stat "#{site.root}/_pages/#{listing}", ( err, stat ) ->
43 if err
44 Logger.error( "Could not stat file #{listing}" )
45 done()
46 else if stat.isFile()
47 paths.push listing
48 done()
49 else
50 fs.readdir "#{site.root}/_pages/#{listing}", ( err2, sublistings ) ->
51 sublistings.forEach ( sublisting ) ->
52 q.push( "#{listing}/#{sublisting}" )
53 done()
54
55 q = async.queue processor, 1
56
57 fs.readdir "#{@site.root}/_pages", ( err, listings ) ->
58 # again, error is ok
59 (listings || []).forEach ( listing ) -> q.push( listing )
60
61 q.push( null ) # kick off queue, even if there are no posts
62 q.drain = () -> cb( null, paths );
63
64module.exports = Finder