1 | path = require('path')
|
2 | Promise = require('bluebird')
|
3 | _ = require('lodash')
|
4 | del = require('del')
|
5 | moment = require('moment')
|
6 | streamify = require('stream-array')
|
7 | through2 = require('through2')
|
8 | gulp = require('gulp')
|
9 | handlebars = require('gulp-compile-handlebars')
|
10 | rename = require('gulp-rename')
|
11 | deploy = require('gulp-gh-pages')
|
12 | minimist = require('minimist')
|
13 | fs = require('fs')
|
14 | os = require('os')
|
15 | exec = Promise.promisify(require('child_process').exec)
|
16 | ps = require('promise-streams')
|
17 |
|
18 |
|
19 | inkpad = require('./lib/inkpad')
|
20 | util = require('./lib/util')
|
21 |
|
22 |
|
23 | knownOptions =
|
24 | string: ['id', 'per-page', 'templates-path', 'deploy-to']
|
25 | alias:
|
26 | 'inkpadId': 'id'
|
27 | default:
|
28 | 'templates-path': path.join(__dirname, 'templates')
|
29 |
|
30 | options = minimist(process.argv.slice(2), knownOptions)
|
31 |
|
32 |
|
33 | data =
|
34 | perPage: parseInt(options['per-page'], 10) or 3
|
35 | inkpads: {}
|
36 | posts: []
|
37 |
|
38 | handlebarsOptions =
|
39 | helpers:
|
40 | datetime: (timestamp, format) ->
|
41 | format = 'YYYY-MM-DD' unless _.isString(format)
|
42 | moment(timestamp).format(format)
|
43 |
|
44 |
|
45 | templatesPath = path.resolve process.env.INIT_CWD, options['templates-path']
|
46 | paths =
|
47 | build: path.join(__dirname, '_build')
|
48 | templates:
|
49 | index: path.join(templatesPath, 'index.html.handlebars')
|
50 | show: path.join(templatesPath, 'show.html.handlebars')
|
51 | public: path.join(templatesPath, 'public', '**', '*')
|
52 |
|
53 |
|
54 |
|
55 | gulp.task "check:templates:index", ->
|
56 | unless fs.existsSync paths.templates.index
|
57 | console.log "Index template not found, expected index.html.handlebars to be in '#{templatesPath}'."
|
58 | process.exit(1)
|
59 |
|
60 | gulp.task "check:templates:show", ->
|
61 | unless fs.existsSync paths.templates.show
|
62 | console.log "Show template not found, expected show.html.handlebars to be in '#{templatesPath}'."
|
63 | process.exit(1)
|
64 |
|
65 | gulp.task "check:templates", ["check:templates:index", "check:templates:show"]
|
66 |
|
67 | gulp.task "check", ["check:templates"]
|
68 |
|
69 |
|
70 | gulp.task "clean", ["check"], (cb) ->
|
71 | del "#{paths.build}/*", cb
|
72 |
|
73 |
|
74 | gulp.task "load:inkpads", ["clean"], ->
|
75 | reg = inkpad.registry()
|
76 |
|
77 | streamify([id: options.inkpadId])
|
78 | .pipe reg
|
79 | .pipe inkpad.loadPads()
|
80 | .pipe inkpad.scanForSubPages(reg)
|
81 | .pipe inkpad.slicePads()
|
82 | .pipe inkpad.extractTitle()
|
83 | .pipe inkpad.extractTimestamp()
|
84 | .pipe inkpad.extractHeaderImage()
|
85 | .pipe inkpad.extractTeaser()
|
86 | .pipe util.buffer()
|
87 | .pipe through2.obj (pad, enc, done) ->
|
88 | data.inkpads[pad.id] = pad
|
89 | done()
|
90 |
|
91 | gulp.task "load:posts", ["load:inkpads"], ->
|
92 | data.posts = (data.inkpads[id] for id in data.inkpads[options.inkpadId].linkedInkpads)
|
93 |
|
94 | gulp.task "load", ["load:inkpads", "load:posts"]
|
95 |
|
96 |
|
97 | gulp.task "templates:index", ["load"], ->
|
98 | pages = _.chain(data.posts).groupBy((n,i) -> i // data.perPage).values().value()
|
99 | Promise.all pages
|
100 | .map (posts, i) ->
|
101 | d = posts: posts
|
102 | page = i + 1
|
103 | destPath = if page == 1 then "/" else "/page/#{page}"
|
104 | lastPage = page == pages.length
|
105 |
|
106 | unless lastPage
|
107 | d.nextPageLink = "/page/#{page + 1}/"
|
108 |
|
109 | switch
|
110 | when page == 2
|
111 | d.prevPageLink = "/"
|
112 | when page > 2
|
113 | d.prevPageLink = "/page/#{page - 1}/"
|
114 |
|
115 | gulp.src paths.templates.index
|
116 | .pipe handlebars(d, handlebarsOptions)
|
117 | .pipe rename("#{destPath}/index.html")
|
118 | .pipe gulp.dest(paths.build)
|
119 | .map (stream) ->
|
120 | ps.wait(stream)
|
121 |
|
122 | gulp.task "templates:show", ["load"], ->
|
123 | Promise.all data.posts
|
124 | .map (post, i) ->
|
125 | d = post: post, availableKeys: _.keys(post)
|
126 |
|
127 | prevPost = data.posts[i - 1]
|
128 | nextPost = data.posts[i + 1]
|
129 | if prevPost
|
130 | d.prevPostLink = prevPost.path
|
131 | if nextPost
|
132 | d.nextPostLink = nextPost.path
|
133 |
|
134 | gulp.src paths.templates.show
|
135 | .pipe handlebars(d, handlebarsOptions)
|
136 | .pipe rename("#{post.path}/index.html")
|
137 | .pipe gulp.dest(paths.build)
|
138 | .map (stream) ->
|
139 | ps.wait(stream)
|
140 |
|
141 | gulp.task "templates", ["templates:index", "templates:show"]
|
142 |
|
143 |
|
144 | gulp.task "copy", ["clean"], ->
|
145 | gulp.src paths.templates.public
|
146 | .pipe gulp.dest(paths.build)
|
147 |
|
148 |
|
149 | gulp.task "compile", ["templates", "copy"]
|
150 |
|
151 |
|
152 | gulp.task "update:deploy-repo", ->
|
153 | deployRepoPath = path.join(os.tmpdir(), 'tmpRepo')
|
154 | if fs.existsSync deployRepoPath
|
155 | exec("git pull", cwd: deployRepoPath)
|
156 | .catch (e) ->
|
157 | console.log "Deleting the deploy repository (#{deployRepoPath}); please re-run the command to get a freshly cloned one."
|
158 | del.sync deployRepoPath, force: true
|
159 |
|
160 |
|
161 | gulp.task "deploy", ["compile", "update:deploy-repo"], ->
|
162 | gulp.src path.join(paths.build, "**/*")
|
163 | .pipe deploy(remoteUrl: options['deploy-to'])
|
164 |
|
165 |
|
166 | gulp.task "default", ["compile"]
|
167 |
|