letter-press
Advanced tools
Comparing version 1.1.5 to 2.0.0
@@ -8,5 +8,5 @@ // based on 1000ch/node-github-markdown | ||
module.exports = (title, markdown) => { | ||
module.exports = (title, markdown, opts) => { | ||
return new Promise((resolve, reject) => { | ||
const markdownIt = new MarkdownIt({ | ||
const markdownIt = new MarkdownIt(opts || { | ||
html: true, | ||
@@ -13,0 +13,0 @@ breaks: true, |
49
index.js
@@ -6,32 +6,37 @@ const fs = require('fs') | ||
module.exports = (id, md, opts) => { | ||
const dist = opts && opts.dist ? opts.dist : path.resolve('dist') | ||
if (!fs.existsSync(dist)) fs.mkdirSync(dist) | ||
press(id, md, dist).catch(console.error) | ||
module.exports = (id, markdown, opts) => { | ||
const o = {} | ||
o.quiet = opts && opts.quiet ? opts.quiet : false | ||
o.dist = opts && opts.dist ? opts.dist : path.resolve('dist') | ||
o.ghmd = opts && opts.markdown ? opts.markdown : null | ||
o.pdf = opts && opts.pdf ? opts.pdf : { | ||
format: 'A4' | ||
} | ||
if (!fs.existsSync(o.dist)) fs.mkdirSync(o.dist) | ||
if (!fs.existsSync(o.dist)) return Promise.reject(new Error('Error: dist directory does not exist and could not be created: ' + o.dist)) | ||
return press(id, markdown, o).catch(e => console.error('🚨 ' + e)) | ||
} | ||
async function press (id, md, dist) { | ||
const mdName = id + '.md' | ||
const htmlName = id + '.html' | ||
const pdfName = id + '.pdf' | ||
const mdPath = path.join(dist, mdName) | ||
const htmlPath = path.join(dist, htmlName) | ||
const pdfPath = path.join(dist, pdfName) | ||
async function press (id, markdown, opts) { | ||
const pathMd = path.join(opts.dist, id + '.md') | ||
const pathHtml = path.join(opts.dist, id + '.html') | ||
const pathPdf = path.join(opts.dist, id + '.pdf') | ||
try { | ||
// write md | ||
write(mdPath, md) | ||
.then(file => console.log('✨ ' + mdName + ' done')) | ||
write(pathMd, markdown) | ||
.then(file => opts.quiet || console.log('✨ ' + id + '.md done')) | ||
.catch(e => console.error('🚨 ' + e)) | ||
// write html | ||
const html = await ghmd(id, md) | ||
const html = await ghmd(id, markdown, opts.ghmd) | ||
.catch(e => console.error('🚨 ' + e)) | ||
await write(htmlPath, html) | ||
.then(file => console.log('✨ ' + htmlName + ' done')) | ||
if (!html) throw new Error('Error: Something went wrong while generating HTML.') | ||
await write(pathHtml, html) | ||
.then(file => opts.quiet || console.log('✨ ' + id + '.html done')) | ||
.catch(e => console.error('🚨 ' + e)) | ||
// write pdf | ||
pdf(htmlPath, pdfPath) | ||
.then(file => console.log('✨ ' + pdfName + ' done')) | ||
await pdf(pathHtml, pathPdf, opts.pdf) | ||
.then(file => opts.quiet || console.log('✨ ' + id + '.pdf done')) | ||
.catch(e => console.error('🚨 ' + e)) | ||
@@ -43,3 +48,4 @@ } catch (e) { | ||
async function pdf (file, out) { | ||
async function pdf (file, out, opts) { | ||
opts.path = out | ||
let browser | ||
@@ -52,6 +58,3 @@ try { | ||
}) | ||
await page.pdf({ | ||
path: out, | ||
format: 'A4' | ||
}) | ||
await page.pdf(opts) | ||
browser.close() | ||
@@ -58,0 +61,0 @@ return out |
{ | ||
"name": "letter-press", | ||
"version": "1.1.5", | ||
"version": "2.0.0", | ||
"main": "index.js", | ||
@@ -22,3 +22,5 @@ "scripts": { | ||
"devDependencies": { | ||
"standard": "^10.0.3" | ||
"rimraf": "^2.6.2", | ||
"standard": "^10.0.3", | ||
"tape": "^4.8.0" | ||
}, | ||
@@ -25,0 +27,0 @@ "bugs": { |
41
test.js
@@ -5,2 +5,4 @@ #!/usr/bin/env node | ||
const path = require('path') | ||
const rimraf = require('rimraf') | ||
const test = require('tape') | ||
const press = require('.') | ||
@@ -12,6 +14,35 @@ const letter = require('./test.md') | ||
const dateString = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() | ||
data.from.forEach(from => data.to.forEach(to => { | ||
const md = letter(dateString, from, to) | ||
const id = from.id + '_' + to.id | ||
press(id, md) | ||
})) | ||
test('press test', function (t) { | ||
const dist = path.resolve('test') | ||
const paths = ['dapibus_bar.html', 'dapibus_foo.md', 'parturient_bar.pdf', 'dapibus_bar.md', 'dapibus_foo.pdf', 'parturient_foo.html', 'dapibus_bar.pdf', 'parturient_bar.html', 'parturient_foo.md', 'dapibus_foo.html', 'parturient_bar.md', 'parturient_foo.pdf'] | ||
t.plan(paths.length) | ||
t.comment('removing dir ' + dist) | ||
rimraf(dist, function (err) { | ||
if (err) { | ||
t.fail(err.toString()) | ||
return | ||
} | ||
const proms = run({ | ||
dist: dist, | ||
quiet: true | ||
}) | ||
Promise.all(proms).then(() => { | ||
paths.forEach(p => { | ||
t.ok(fs.existsSync(path.join(dist, p)), 'file exists: ' + p) | ||
}) | ||
t.end() | ||
}).catch(e => t.fail(e.toString())) | ||
}) | ||
}) | ||
function run (opts) { | ||
const proms = [] | ||
data.from.forEach(from => data.to.forEach(to => { | ||
const md = letter(dateString, from, to) | ||
const id = from.id + '_' + to.id | ||
proms.push(press(id, md, opts)) | ||
})) | ||
return proms | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9169
202
3