Comparing version 1.2.0 to 1.2.1
170
index.js
@@ -1,168 +0,2 @@ | ||
// built-in node modules | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
// npm modules | ||
var _ = require('lodash'); | ||
var ejs = require('ejs'); | ||
var mkdirp = require('mkdirp'); | ||
var Bfy = require('browserify'); | ||
var marked = require('marked'); | ||
var concat = require('concat-stream'); | ||
var debug = require('debug')('bpm-bundle'); | ||
var template = require.resolve('./bundle.ejs'); | ||
template = fs.readFileSync(template, 'utf-8'); | ||
exports.bundle = function(config, opts, repoDir, bundleDir, cb) { | ||
debug(require('./package.json').version); | ||
debug('about to bundle episode at ' + repoDir); | ||
// load package.json | ||
var package_json_path = path.join(repoDir, 'package.json'); | ||
var pkg = null; | ||
try { | ||
pkg = JSON.parse(fs.readFileSync(package_json_path, 'utf-8')); | ||
} catch(e) { | ||
debug('unable to read package.json'); | ||
return cb(e); | ||
} | ||
if (typeof(pkg.brain) === 'undefined') { | ||
pkg.brain = {}; | ||
} | ||
var markdown_path = findContentMarkDown(pkg, repoDir); | ||
if (markdown_path === null) { | ||
return cb(new Error('unable to find markdown file')); | ||
} | ||
var html = renderHTML(pkg, markdown_path); | ||
applyTransforms(pkg, repoDir, html, htmlReady); | ||
function htmlReady(err, html) { | ||
if (err) return cb(err); | ||
mkdirp(bundleDir, function(err) { | ||
if (err) return cb(err); | ||
//write bundler name and version to episode's package.JSON | ||
var bundler = require('./package.json'); | ||
pkg.brain.bundler = {name: bundler.name, version: bundler.version}; | ||
fs.writeFile(path.join(bundleDir, 'package.json'), JSON.stringify(pkg, null, 2), function(err){ | ||
if (err) return cb(err); | ||
var code = renderTemplate(template, repoDir, pkg, html); | ||
runBrowserify(code, bundleDir, function(err) { | ||
if (err) { | ||
debug(err.message); | ||
} else { | ||
debug('done bundling ' + pkg.name); | ||
} | ||
return cb(err); | ||
}); | ||
}); | ||
}); | ||
} | ||
}; | ||
function findContentMarkDown(pkg, repoDir) { | ||
var filenames = [pkg.brain.content || 'content.md', 'readme.md', 'ReadMe.md', 'README.md']; | ||
for(var i=0; i<filenames.length; ++i) { | ||
var p = path.join(repoDir, filenames[i]); | ||
try { | ||
fs.statSync(p); | ||
return p; | ||
} catch(e) {} | ||
} | ||
return null; | ||
} | ||
function renderHTML(pkg, markdown_path) { | ||
var markDown = fs.readFileSync(markdown_path, 'utf8'); | ||
var myRenderer = new marked.Renderer(); | ||
//prefix anchor tags in headlines with episode name to disambiguate | ||
myRenderer.heading = function (text, level) { | ||
var escapedText = pkg.name + '_' + text.toLowerCase().replace(/[^\w]+/g, '-'); | ||
return '<h' + level + '><a name="' + | ||
escapedText + | ||
'" class="anchor" href="#' + | ||
escapedText + | ||
'"><span class="header-link"></span></a>' + | ||
text + '</h' + level + '>'; | ||
}; | ||
marked.setOptions({ | ||
renderer: myRenderer, | ||
gfm: true, | ||
tables: true, | ||
breaks: true, | ||
pedantic: false, | ||
sanitize: false, | ||
smartLists: true, | ||
smartypants: true | ||
}); | ||
var html = marked(markDown); | ||
return html; | ||
} | ||
function runBrowserify(code, bundleDir, cb) { | ||
var bfy = Bfy(); | ||
var indexPath = path.join(bundleDir, '_index.js'); | ||
var outPath = path.join(bundleDir, 'index.js'); | ||
debug('writing rendered ejs template to ' + indexPath); | ||
fs.writeFileSync(indexPath, code, 'utf-8'); | ||
// browserify _index.js | ||
bfy.add(indexPath); | ||
bfy.transform(require.resolve('cssify'), {global: true}); | ||
var stream = bfy.bundle(); | ||
stream.on('error', function(err) { | ||
cb(new Error('error while browserify.bundle:'+ err.message)); | ||
}); | ||
stream.on('end', function() { | ||
fs.unlinkSync(indexPath); | ||
cb(null); | ||
}); | ||
stream.pipe(fs.createWriteStream(outPath)); | ||
} | ||
function applyTransforms (pkg, repoDir, html, cb) { | ||
var transforms = pkg.brain['content-transform'] || []; | ||
if (transforms.length) { | ||
transforms = _.map(transforms, function(t) { | ||
var tp = path.join(repoDir, 'node_modules', t); | ||
tp = path.resolve(tp); | ||
debug('try to require transform module at '+ tp); | ||
return require(tp)(); | ||
}); | ||
var combined = _.reduce(transforms, function(combined, n) { | ||
return combined.pipe(n); | ||
}); | ||
var cs = concat(function(html) { | ||
cb(null, html); | ||
}); | ||
combined.on('error', function(err) { | ||
cb(new Error('error while content-transform:'+ error.message)); | ||
}); | ||
combined.pipe(cs); | ||
combined.write(html); | ||
combined.end(); | ||
} else { | ||
cb(null, html); | ||
} | ||
} | ||
function renderTemplate(template, repoDir, pkg, html) { | ||
var ctx = { | ||
cwd: repoDir, | ||
content: html, | ||
pkg: pkg | ||
}; | ||
return ejs.render(template, ctx); | ||
} | ||
module.exports.bundle = require('./lib/make_bundle'); | ||
module.exports.debundle = require('./lib/debundle'); |
{ | ||
"name": "bpm-bundle", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "episode bundler for package manager for the brain (brainpm)", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "tape test/*" | ||
}, | ||
@@ -25,3 +25,6 @@ "keywords": [ | ||
}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"from": "^0.1.3", | ||
"tape": "^4.2.2" | ||
}, | ||
"repository": { | ||
@@ -28,0 +31,0 @@ "type": "git", |
# bpm-bundle | ||
The episode bundler used by [regular/bpm](http://guthub.com/regular/bpm) | ||
The episode bundler used by [brainpm/bpm](https://github.com/brainpm/bpm). | ||
It turns the content of an episode repository (including package.json) into a bunch of files that can be published by bpm-github (or another publisher) | ||
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
8389
8
195
0
8
2
1