-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog-image.js
164 lines (144 loc) · 4.45 KB
/
blog-image.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
import minimist from 'minimist';
import fs from 'fs-extra';
import _ from 'lodash';
import moment from 'moment';
import child_process from 'child_process';
import path from 'path';
import layouts from './src/layouts.js';
import scriptDirname from './src/script_dirname.js';
import readline from 'readline';
const __dirname = scriptDirname(import.meta);
const layoutRenderer = layouts.load([`${__dirname}/layouts`]);
const argv = minimist(process.argv.slice(2));
const SITE_DIR = `${__dirname}/../site`;
const ASSETS = `${__dirname}/../site-assets`;
const BLOG_IMAGES = `${ASSETS}/blog/images`;
const RESIZE_WIDTH = 1024;
if(_.isEmpty(argv._) || _.isEmpty(argv.r)){
usage();
process.exit(1);
}
function usage(){
console.log();
console.log('Usage: node blog-image.js <options> file1 file2 ...')
console.log('');
console.log('Where <options> are:');
console.log(" -s 'My subject' ");
console.log(" -t 'tag,tag,tag'");
console.log(" -r 'user@host:/path/to/target'");
console.log();
}
// TODO: Prompt for missing guys
run(argv).then(() => {
console.log('All done');
});
async function run(args){
if(!args.s){
args.s = await readLine('Subject: ');
}
if(!args.t){
args.t = await readLine('Tags (comma separated): ');
}
const now = moment();
await resizeAndCopyImages(args._, now);
console.log('Done resizing images');
await publishImages(args._, args.r, now);
const templ = makeTemplate(args._, argv.s, argv.t, now);
console.log();
console.log(`Customize your file: ${templ}`);
console.log();
console.log("...and don't forget to publish! (npm run build && npm run publish)");
console.log();
}
async function resizeAndCopyImages(images, now){
const result = [];
for(const image of images){
const copy = outFilename(image, now, false);
console.log(`Copying ${image} to ${copy}`);
fs.copySync(image, copy);
result.push(await resizeImage(image, now));
}
return result;
}
async function resizeImage(image, now){
console.log(`Resizing ${image}`);
const outImage = outFilename(image, now, true);
return new Promise((fulfill,reject) => {
const cmd = `convert -resize '${RESIZE_WIDTH}x>' '${image}' '${outImage}'`;
console.log(`Exec: ${cmd}`);
child_process.exec(cmd, (err, stdout, stderr) => {
if(err) {
return reject(err);
}
fulfill(outImage);
});
});
}
async function publishImages(images, rsyncTarget, now){
for(const image of images){
await rsync(outFilename(image, now, true), rsyncTarget);
await rsync(outFilename(image, now, false), rsyncTarget);
}
}
async function rsync(file, target){
return new Promise((fulfill,reject) => {
const cmd = `rsync -avv --progress "${file}" "${target}/"`;
console.log(cmd);
child_process.exec(cmd, (err, stdout, stderr) => {
if(err) {
return reject(err);
}
fulfill();
});
});
}
function makeTemplate(images, subject, tags, now){
const markdown = layoutRenderer.render('blog_md', {
subject: subject,
tags: tags.split(','),
date: now.format(),
images: images.map(image => {
const scaled = path.basename(outFilename(image, now, true));
const full = path.basename(outFilename(image, now, false));
return {
base: path.basename(scaled),
scaled: `https://noisybox.net/blog/images/${scaled}`,
full: `https://noisybox.net/blog/images/${full}`
}
})
}, {});
const outDir = outTemplateDir(now);
const outFile = outTemplateFileName(subject, now);
const fullOutFile = `${outDir}/${outFile}`;
fs.ensureDirSync(outDir);
fs.writeFileSync(fullOutFile, markdown);
return fullOutFile;
}
function outTemplateDir(now){
return `${__dirname}/../site/blog/${now.year()}`;
}
function outTemplateFileName(subject, now){
const outDir = outTemplateDir(now);
const snakeSubject = _.snakeCase(subject);
return `${now.format('YYYY-MM-DD')}-${snakeSubject}.md`;
}
function outFilename(image, now, sized){
const extension = path.extname(image);
const base = path.basename(image, extension);
const date = now.format('YYYYMMDD');
const resizePart = sized ? `_${RESIZE_WIDTH}` : '';
return `${BLOG_IMAGES}/${date}-${base}${resizePart}${extension}`;
}
async function readLine(prompt){
return new Promise((fulfill,reject) => {
const reader = readline.createInterface({
input: process.stdin,
output: process.stdout
});
reader.question(prompt, answer => {
reader.close();
fulfill(answer);
});
});
}