UNPKG

4.62 kBtext/coffeescriptView Raw
1path = require('path')
2Promise = require('bluebird')
3_ = require('lodash')
4del = require('del')
5moment = require('moment')
6streamify = require('stream-array')
7through2 = require('through2')
8gulp = require('gulp')
9handlebars = require('gulp-compile-handlebars')
10rename = require('gulp-rename')
11deploy = require('gulp-gh-pages')
12minimist = require('minimist')
13fs = require('fs')
14os = require('os')
15exec = Promise.promisify(require('child_process').exec)
16ps = require('promise-streams')
17
18
19inkpad = require('./lib/inkpad')
20util = require('./lib/util')
21
22
23knownOptions =
24 string: ['id', 'per-page', 'templates-path', 'deploy-to']
25 alias:
26 'inkpadId': 'id'
27 default:
28 'templates-path': path.join(__dirname, 'templates')
29
30options = minimist(process.argv.slice(2), knownOptions)
31
32
33data =
34 perPage: parseInt(options['per-page'], 10) or 3
35 inkpads: {}
36 posts: []
37
38handlebarsOptions =
39 helpers:
40 datetime: (timestamp, format) ->
41 format = 'YYYY-MM-DD' unless _.isString(format)
42 moment(timestamp).format(format)
43
44
45templatesPath = path.resolve process.env.INIT_CWD, options['templates-path']
46paths =
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
55gulp.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
60gulp.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
65gulp.task "check:templates", ["check:templates:index", "check:templates:show"]
66
67gulp.task "check", ["check:templates"]
68
69
70gulp.task "clean", ["check"], (cb) ->
71 del "#{paths.build}/*", cb
72
73
74gulp.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
91gulp.task "load:posts", ["load:inkpads"], ->
92 data.posts = (data.inkpads[id] for id in data.inkpads[options.inkpadId].linkedInkpads)
93
94gulp.task "load", ["load:inkpads", "load:posts"]
95
96
97gulp.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
122gulp.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
141gulp.task "templates", ["templates:index", "templates:show"]
142
143
144gulp.task "copy", ["clean"], ->
145 gulp.src paths.templates.public
146 .pipe gulp.dest(paths.build)
147
148
149gulp.task "compile", ["templates", "copy"]
150
151
152gulp.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
161gulp.task "deploy", ["compile", "update:deploy-repo"], ->
162 gulp.src path.join(paths.build, "**/*")
163 .pipe deploy(remoteUrl: options['deploy-to'])
164
165
166gulp.task "default", ["compile"]
167