Skip to content

Commit

Permalink
New configuration system, finished async.
Browse files Browse the repository at this point in the history
  • Loading branch information
nporteschaikin committed Jun 19, 2014
1 parent be95692 commit d5187a3
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 123 deletions.
13 changes: 4 additions & 9 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,23 @@ Commands.prototype = {
.on('init', this.initEvent.bind(this))
.on('done', this.doneEvent.bind(this))
.on('start', this.startEvent.bind(this))
.on('misc', this.miscEvent.bind(this))
if (this.process) this.process.on('SIGINT', this.exit.bind(this));
},

write: function (str) {
if (this.process) this.process.stdout.write(str);
},

initEvent: function (action, resource) {
this.write(('\n' + String(action) + ' ').blue + (String(resource) + '... ').grey);
initEvent: function (action) {
this.write(('\n' + String(action) + '... ').grey);
},

doneEvent: function () {
this.write('done!'.green);
},

startEvent: function (action, resource) {
this.write(('\nstarted ' + String(action)).green + ' ' + String(resource).grey);
},

miscEvent: function (event, resource) {
this.write('\n' + String(resource).grey + ' ' + (String(event) + '!').green)
startEvent: function (action) {
this.write(('\nstarted ' + String(action) + '!').green);
}

}
Expand Down
53 changes: 29 additions & 24 deletions lib/compiler/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ var Adapter = require('../adapter')
, symlink = fs.symlink
, exists = fs.existsSync;

var File = function (path, dest_path) {
var File = function (path, options) {
this.path = path;
this.dest_path = dest_path;
this.options = options || {};
}

File.prototype = {

save: function () {
save: function (dest_path, context) {
return this.compile()
.with(this)
.then(this.write_or_symlink);
.then(this.write_or_symlink(dest_path, context));
},

compile: function () {
var __this = this;
return when.promise(function (resolve, reject, notify) {
return when.promise(function (resolve) {
if (Adapter.supports({path: __this.path})) {
__this.compile_theme(resolve, reject, notify);
__this.compile_theme(resolve);
} else {
resolve(__this);
}
});
},

compile_theme: function (resolve, reject, notify) {
compile_theme: function (resolve) {
var __this = this
, theme;
read(__this.path, 'utf8', function (error, source) {
Expand All @@ -43,30 +42,36 @@ File.prototype = {
});
},

write_or_symlink: function () {
if (this.theme) {
return when.promise(this.write());
}
return when.promise(this.symlink());
render_theme: function (context) {
return this.theme.render(context, this.options);
},

write: function () {
write_or_symlink: function (dest_path, context) {
var __this = this;
return function (resolve, reject, notify) {
write(__this.dest_path.replace(/.([a-zA-Z]*)$/, __this.theme.kind.extension), __this.theme.render(),
function (error) {
if (error) throw error;
resolve();
return function () {
return when.promise(function (resolve) {
if (__this.theme) {
__this.write(dest_path, context, resolve);
} else {
__this.symlink(dest_path, resolve);
}
);
});
}
},

symlink: function () {
write: function (dest_path, context, resolve) {
var __this = this;
return function (resolve, reject, notify) {
symlink(__this.path, __this.dest_path, resolve);
}
write(dest_path.replace(/.([a-zA-Z]*)$/, __this.theme.kind.extension), __this.render_theme(context),
function (error) {
if (error) throw error;
resolve();
}
);
},

symlink: function (dest_path, resolve) {
var __this = this;
symlink(__this.path, dest_path, resolve);
}

}
Expand Down
47 changes: 36 additions & 11 deletions lib/compiler/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Tree = require('./tree')
, Tumblr = require('./tumblr')
, File = require('./file')

, when = require('when')
Expand All @@ -9,21 +10,38 @@ var Tree = require('./tree')
, exists = fs.existsSync
, basename = path.basename
, relative = path.relative
, join = path.join;
, join = path.join
, resolve = path.resolve;

var Compiler = function (source_path, destination_path) {
this.source = source_path;
this.destination = destination_path;
this.tree = new Tree(source_path, [basename(destination_path)]);
var Compiler = function (peak, options) {
this.peak = peak;
this.options = options || {};
this.emitter = peak.emitter;

this.destination = resolve(this.peak.path, options.output_path);
this.tree = new Tree(this.peak.path, [options.output_path]);
this.tree.parse();

if (this.options.blog) this.tumblr = new Tumblr(this.options.blog);
}

Compiler.prototype = {

compile: function () {
return when.try(this.create_folders.bind(this))
return when.try(this.emit_init.bind(this))
.with(this)
.then(this.compile_and_save_files);
.then(this.create_folders)
.then(this.get_tumblr)
.then(this.compile_and_save_files)
.then(this.emit_done);
},

emit_init: function () {
this.emitter.emit('init', 'compiling');
},

emit_done: function () {
this.emitter.emit('done');
},

create_folders: function () {
Expand All @@ -34,19 +52,26 @@ Compiler.prototype = {
}
},

get_tumblr: function () {
if (this.tumblr) return this.tumblr.get();
},

compile_and_save_files: function () {
var paths = this.tree.files
, file
, compilers = [];

for (var x=0; x<paths.length; x++) {
file = new File(paths[x], this.destination_path(paths[x]));
compilers.push(file.save());
var file = new File(paths[x], this.options.compiler)
, compiler;

compiler = file.save(this.destination_path(paths[x]), this.tumblr.json);
compilers.push(compiler);
}
return when.all(compilers);
},

destination_path: function (path) {
return join(this.destination, relative(this.source, path));
return join(this.destination, relative(this.peak.path, path));
}

}
Expand Down
33 changes: 33 additions & 0 deletions lib/compiler/tumblr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var when = require('when')
, request = require('request');

var Tumblr = function (blog) {
this.blog = blog;
this.uri = 'http://www.tumblr.com/'
+ 'customize_api/'
+ 'demo_content/'
+ blog;
}

Tumblr.prototype = {

get: function () {
return when.promise(this.request());
},

request: function () {
var __this = this;
return function (resolve, reject, notify) {
var options = {};
options.uri = __this.uri;
options.json = true;
request(options, function (error, response, body) {
if (error) reject(error);
resolve(__this.json = body);
});
}
},

}

module.exports = Tumblr;
35 changes: 24 additions & 11 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@ var yaml = require('js-yaml')
, resolve = path.resolve
, basename = path.basename;

var Config = function (peak) {
var Config = function (peak, options) {
options = options || {};
this.peak = peak;
this.file_path = join(this.peak.path, '.peakconfig.yml');
this.config = {};

this.load_defaults();
this.load_file();
this.load_arg(options);
}

Config.prototype = {

load: function (options) {
this.load_defaults();
this.load_file();
this.load_arg(options);
return this.config;
},

load_defaults: function () {
this.config.watch = {};
this.config.deploy = {};
var config = {};

config.output_path = '.peak';
config.port = 1111;

this.config = config;
},

load_file: function () {
Expand All @@ -41,6 +42,18 @@ Config.prototype = {
for (opt in options) {
if (typeof options[opt] === 'string') this.config[opt] = options[opt];
}
},

options: function (action) {
var config = this.config
, options = {};
for (option in config) {
options[option] = config[option];
}
if (typeof config[action] == 'object') {
for (option in config[action]) options[option] = config[action][option];
}
return options;
}

}
Expand Down
30 changes: 12 additions & 18 deletions lib/deployer/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Compiler = require('../compiler')
var File = require('../compiler/file')

, fs = require('fs')
, when = require('when')
Expand All @@ -9,36 +9,25 @@ var Compiler = require('../compiler')

var Deployer = function Deployer (peak, options) {
this.peak = peak;
this.options = peak.options;
this.options = options || {};
this.emitter = peak.emitter;

this.file = new File(this.options.index, options.compiler);
}

Deployer.prototype = {

start: function () {
this.emitter.emit('start', 'deploying', this.peak.path);
return when(this.compile)
return this.get_sign_in()
.with(this)
.then(this.get_sign_in)
.then(this.parse_sign_in)
.then(this.sign_in)
.then(this.get_customize)
.then(this.compile)
.then(this.deploy)
.then(this.confirm);
},

compile: function () {
if (!this.options.index) throw new Error('An index theme must be specified to deploy.');
var compiler = new Compiler(read(this.options.index, 'utf8'), { path: this.options.index })
, render;
if (render = compiler.compile()) {
this.theme = render(this.options.deploy.compile);
this.emitter.emit('misc', 'compiled', this.options.index);
} else {
throw new Error('The index theme could not be compiled');
}
},

get_sign_in: function () {
return this.request(url('login'), 'GET');
},
Expand All @@ -59,12 +48,17 @@ Deployer.prototype = {
return this.request(url('customize', this.options.blog), 'GET');
},

compile: function () {
return this.file.compile();
},

deploy: function (response) {
if (!this.file.theme) throw new Error(this.options.index + ' is not a valid theme.');
var options = {
json: true
, body: {
'user_form_key': element(response, 'form_key').val()
, 'custom_theme': this.theme
, 'custom_theme': this.file.render_theme()
}
}
return this.request(url('customize_api', 'blog', this.options.blog), 'POST', options);
Expand Down
Loading

0 comments on commit d5187a3

Please sign in to comment.