Comparing version 1.0.0 to 1.1.0
151
index.js
@@ -9,5 +9,6 @@ // built-in node modules | ||
var mkdirp = require('mkdirp'); | ||
var bfy = require('browserify')(); | ||
var Bfy = require('browserify'); | ||
var marked = require('marked'); | ||
var concat = require('concat-stream'); | ||
var debug = require('debug')('bpm-bundle'); | ||
@@ -17,5 +18,7 @@ var template = require.resolve('./bundle.ejs'); | ||
exports.bundle = function(opts, cb) { | ||
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(process.cwd(), 'package.json'); | ||
var package_json_path = path.join(repoDir, 'package.json'); | ||
var pkg = null; | ||
@@ -25,26 +28,71 @@ try { | ||
} catch(e) { | ||
console.error('unable to read package.json'); | ||
debug('unable to read package.json'); | ||
return cb(e); | ||
} | ||
if (typeof(pkg.brain) === 'undefined') | ||
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); | ||
var code = renderTemplate(template, repoDir, pkg, html); | ||
mkdirp(bundleDir, function(err) { | ||
if (err) return cb(err); | ||
runBrowserify(code, bundleDir, function(err) { | ||
if (err) { | ||
debug(err.message); | ||
} else { | ||
debug('done bundling ' + pkg.name); | ||
} | ||
return cb(err); | ||
//write bundler name and version to episode's package.JSON | ||
/* | ||
TODO: We mustn't touch the original package.json, | ||
since it is part of the source code. Doing so will cause | ||
the git working directory to be unclean after bundling and that's | ||
suprising and annoying to users. | ||
Instead we should manipulate a *copy* of package.json immediatley prior | ||
to browserifying (could also be a browserify transform). | ||
var bundler = require('./package.json'); | ||
pkg.brain.bundler = {name: bundler.name, version: bundler.version}; | ||
fs.writeFile(package_json_path, JSON.stringify(pkg, null, 4), function(err){ | ||
cb(err); | ||
}); | ||
*/ | ||
}); | ||
}); | ||
} | ||
}; | ||
function findContentMarkDown(pkg, repoDir) { | ||
var filenames = [pkg.brain.content || 'content.md', 'readme.md', 'ReadMe.md', 'README.md']; | ||
var markdown_path = null; | ||
for(var i=0; i<filenames.length; ++i) { | ||
var p = path.join(process.cwd(), filenames[i]); | ||
var p = path.join(repoDir, filenames[i]); | ||
try { | ||
fs.statSync(p); | ||
markdown_path = p; | ||
break; | ||
return p; | ||
} catch(e) {} | ||
} | ||
if (markdown_path === null) { | ||
throw new Error('unable to fund markdown file'); | ||
} | ||
// render markdown | ||
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, '-'); | ||
var escapedText = pkg.name + '_' + text.toLowerCase().replace(/[^\w]+/g, '-'); | ||
return '<h' + level + '><a name="' + | ||
@@ -70,8 +118,35 @@ escapedText + | ||
var html = marked(markDown); | ||
return html; | ||
} | ||
// apply transforms | ||
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(process.cwd(), 'node_modules', t); | ||
var tp = path.join(repoDir, 'node_modules', t); | ||
tp = path.resolve(tp); | ||
debug('try to require transform module at '+ tp); | ||
return require(tp)(); | ||
@@ -83,5 +158,7 @@ }); | ||
var cs = concat(htmlReady); | ||
var cs = concat(function(html) { | ||
cb(null, html); | ||
}); | ||
combined.on('error', function(err) { | ||
return cb(new Error('error while content-transform:'+ error.message)); | ||
cb(new Error('error while content-transform:'+ error.message)); | ||
}); | ||
@@ -92,31 +169,13 @@ combined.pipe(cs); | ||
} else { | ||
htmlReady(html); | ||
cb(null, html); | ||
} | ||
function htmlReady(html) { | ||
var ctx = { | ||
cwd: process.cwd(), | ||
content: html, | ||
pkg: pkg | ||
}; | ||
var code = ejs.render(template, ctx); | ||
mkdirp('./.bpm', function(err) { | ||
if (err) throw err; | ||
fs.writeFileSync('./.bpm/_index.js', code, 'utf-8'); | ||
bfy.add('./.bpm/_index.js'); | ||
//bfy.transform(require.resolve('markdownify')); | ||
bfy.transform(require.resolve('cssify'), {global: true}); | ||
} | ||
var stream = bfy.bundle(); | ||
stream.pipe(fs.createWriteStream('./.bpm/index.js')); | ||
stream.on('error', function(err) { | ||
return cb(new Error('error while browserify.bundle:'+ err.message)); | ||
}); | ||
stream.on('end', function() { | ||
console.log('done bundling ' + pkg.name); | ||
fs.unlinkSync('./.bpm/_index.js'); | ||
cb(null); | ||
}); | ||
}); | ||
} | ||
}; | ||
function renderTemplate(template, repoDir, pkg, html) { | ||
var ctx = { | ||
cwd: repoDir, | ||
content: html, | ||
pkg: pkg | ||
}; | ||
return ejs.render(template, ctx); | ||
} |
{ | ||
"name": "bpm-bundle", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "episode bundler for package manager for the brain (brainpm)", | ||
@@ -19,2 +19,3 @@ "main": "index.js", | ||
"cssify": "^0.7.0", | ||
"debug": "^2.2.0", | ||
"ejs": "^2.3.1", | ||
@@ -21,0 +22,0 @@ "lodash": "^3.9.3", |
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
2
7177
8
5
153
1
+ Addeddebug@^2.2.0
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)