-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
140 lines (124 loc) · 3.55 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var gulp = require('gulp');
var gutil = require('gulp-util');
var Promise = require('bluebird');
var mocha = require('gulp-mocha');
const spawn = require('child_process').spawn;
const containerName = 'man-blog-web';
function dockerCommand(command, done) {
command.stdout.on('data', data => {
console.log(`stdout: ${data.toString().replace("\n",'')}`);
});
command.stderr.on('data', data => {
console.error(`stderr: ${data.toString().replace("\n",'')}`);
});
command.on('close', code => {
console.log(`child process exited with code ${code}`);
done();
});
}
gulp.task('build', function(done) {
const build = spawn('docker', ['build','-t','man-blog','.']);
dockerCommand(build, done);
});
gulp.task('run', ['stop'], function(done) {
const run = spawn('docker', [
'run',
'--rm',
'-v','/cache',
'-v','/Users/joshlevine/Dropbox/Manly Blog Posts:/posts',
'-v',__dirname + ':/src',
'-p','3000:3000',
'--name', containerName,
'man-blog',
'node',
'/src/src/app.js'
]);
dockerCommand(run, done);
});
gulp.task('stop', function(done) {
const stop = spawn('docker', [
'stop',
containerName
]);
dockerCommand(stop, done);
});
gulp.task('serve', function () {
require('./src/app');
});
gulp.task('test', function () {
return gulp.src(['./tests/*.js'])
.pipe(mocha());
});
gulp.task('new-post', function () {
var prompt = require('prompt');
var moment = require('moment');
var fs = require('fs');
prompt.message = '> ';
prompt.delimiter = '';
var defaultDate = moment().format('YYYY-MM-DD');
var dateAndTitleSchema = {
properties: {
author: {
description: 'Author:',
default: (require('./man.json') || {}).defaultAuthor || 'Anonymous'
},
date: {
description: 'Publish Date:',
pattern: /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/,
message: 'Date must be in format YYYY-MM-DD',
default: defaultDate
},
title: {
description: 'Post Title:',
required: true,
message: 'You must enter a post title!'
},
saveLocation: {
description: 'Where do you want to save this file?',
default: (require('./man.json') || {}).postsDirectory || fs.realpathSync(__dirname + '/posts')
},
published: {
description: 'Published?',
default: 'false'
}
}
};
var slugSchema = {
properties: {
slug: {}
}
};
prompt.start();
var promptGet = Promise.promisify(prompt.get);
return promptGet(dateAndTitleSchema)
.then(function (result) {
var slug = result.title.toLowerCase().replace(/[^a-z0-9 ]/gi, '').trim().replace(/ /g, '-');
slugSchema.properties.slug.description = 'Post Slug:';
slugSchema.properties.slug.default = slug;
return [
result.date,
result.title,
result.author,
promptGet(slugSchema)
.then(function (result) {
return result.slug
}),
result.saveLocation,
result.published
];
}).spread(function (date, title, author, slug, saveLocation, published) {
var path = saveLocation + '/' + slug + '.md'
if (fs.existsSync(path)) {
console.error('Ooops, that date/slug already exists!');
return;
}
var template = fs.readFileSync('posts/.default.md').toString();
file = template
.replace('#date#', date)
.replace('#title#', title)
.replace('#slug#', slug)
.replace('#author#', author)
.replace('#published#', published);
fs.writeFileSync(path, file);
});
});