Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] add ability to upload the built npm assets to a s3 compatible… #4

Merged
merged 2 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/constructor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const zlib = require('zlib');
const fs = require('fs');
const os = require('os');

const assign = Object.assign;

//
// Available build systems.
//
Expand Down Expand Up @@ -62,6 +64,7 @@ function Constructor(app, horn, options) {

this.app = app;
this.horn = horn;
this.cdnup = app.cdnup
this.models = app.models;
this.throttle = options.throttle || app.config.get('throttle') || 10;

Expand Down Expand Up @@ -390,9 +393,11 @@ Constructor.prototype.prepare = function prepare(spec, content, next) {
async.series({
unpack: this.unpack.bind(this, { content, outputPath }),
install: this.install.bind(this, spec, pkgDir),
pack: this.pack.bind(this, pkgDir, tarPath)
pack: this.pack.bind(this, pkgDir, tarPath),
upload: this.upload.bind(this, spec, tarPath)
}, (err) => {
if (err) return next(err);

next(null, { install: outputPath, tarball: tarPath });
});
});
Expand Down Expand Up @@ -437,6 +442,33 @@ Constructor.prototype.unpack = function unpack(opts, next) {
stream.end(new Buffer(opts.content, 'base64'));
};

/**
* Upload the given file to our configured endpoint
*
* @param {Object} spec Defines this package
* @param {Function} next Optional completion callback.
* @api public
*/
Constructor.prototype.upload = function upload(spec, tarball, next) {
if (!this.cdnup) return setImmediate(next);
const app = this.app;
const filePath = `${encodeURIComponent(spec.name)}-${spec.version}.tgz`;

//
// TODO: We ccould also check to see if the asset exists before we try and upload
//
const logOpts = assign({ tarball }, spec)
this.cdnup.upload(tarball, filePath, (err, url) => {
if (err) {
return app.contextLog.error('Failed to upload tarball for package',
assign({ error: err.message }, logOpts));
}

app.contextLog.info('Uploaded tarball for package', assign({ url }, logOpts));
next();
});
};

/**
* Install the dependencies of the package with npm.
* Uses the provided environment.
Expand Down
23 changes: 23 additions & 0 deletions lib/preboots/cdnup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const Cdnup = require('cdnup');

/**
*
* Preboot to setuo an optional instance of cdnup for uploading fully built npm
* package tarballs to an `s3` compatible store.
* @param {slay.App} app App instance
* @param {Object} options Configurable options
* @param {Function} done Continuation function when finished
*
*/
module.exports = function (app, options, done) {
//
// Remark: (jcrugzz) do we have a more meaningful config name?
//
const config = app.config.get('cdnup') || options.cdnup;
if (!config) return done();

app.cdnup = new Cdnup(config);
done();
};
1 change: 1 addition & 0 deletions lib/preboots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function preboot(app, options, next) {

app.preboot(require('slay-contextlog'));
app.preboot(require('./datastar'));
app.preboot(require('./cdnup'));
app.preboot(require('../constructor/bffs'));
app.preboot(require('../constructor'));
app.preboot(require('./terminate'));
Expand Down