forked from volojs/volo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start support for volofile. Still has rough edges
- Loading branch information
Showing
6 changed files
with
182 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {name}. Your code is {code} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
/*jslint */ | ||
/*global define, console, process */ | ||
|
||
define(function (require) { | ||
var fs = require('fs'), | ||
voloTemplate = require('volo/template'), | ||
templatePath = require.toUrl('./sample.template'), | ||
template = require('text!./sample.template'); | ||
|
||
return { | ||
onCreate: function (deferred, namedArgs) { | ||
//Generate templatized output | ||
debugger; | ||
var output = voloTemplate(template, namedArgs); | ||
|
||
//Write out the contents of the generated template | ||
fs.writeFileSync(require.toUrl('./output.text'), output, 'utf8'); | ||
|
||
//Clean up. | ||
fs.unlinkSync(templatePath); | ||
fs.unlinkSync(require.toUrl('./volofile.').replace(/\.$/, '')); | ||
|
||
deferred.resolve(); | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @license Copyright (c) 2011, The Dojo Foundation All Rights Reserved. | ||
* Available via the MIT or new BSD license. | ||
* see: http://github.com/volojs/volo for details | ||
*/ | ||
|
||
'use strict'; | ||
/*jslint */ | ||
/*global define */ | ||
|
||
/** | ||
* Reads a volofile from a target directory, and exports the data as a | ||
* set of modules. | ||
*/ | ||
define(function (require) { | ||
var tokenRegExp = /\{(\w+)\}/g; | ||
|
||
function template(contents, data) { | ||
return contents.replace(tokenRegExp, function (match, token) { | ||
var result = data[token]; | ||
|
||
//Just use empty string for null or undefined | ||
if (result === null || result === undefined) { | ||
result = ''; | ||
} | ||
|
||
return result; | ||
}); | ||
} | ||
|
||
return template; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @license Copyright (c) 2011, The Dojo Foundation All Rights Reserved. | ||
* Available via the MIT or new BSD license. | ||
* see: http://github.com/volojs/volo for details | ||
*/ | ||
|
||
'use strict'; | ||
/*jslint */ | ||
/*global define */ | ||
|
||
/** | ||
* Reads a volofile from a target directory, and exports the data as a | ||
* set of modules. | ||
*/ | ||
define(function (require) { | ||
var path = require('path'), | ||
q = require('q'), | ||
qutil = require('volo/qutil'); | ||
|
||
function volofile(basePath, callback, errback) { | ||
var d = qutil.convert(callback, errback), | ||
volofilePath = path.resolve(path.join(basePath, 'volofile')); | ||
|
||
if (path.existsSync(volofilePath)) { | ||
require([volofilePath], function (value) { | ||
d.resolve(value); | ||
}); | ||
} else { | ||
d.resolve(); | ||
} | ||
|
||
return d.promise; | ||
} | ||
|
||
/** | ||
* Loads the volofile inside basePath, and if there, and if it | ||
* supports the command, then runs it, running dependencies for | ||
* the command if specified. | ||
* @returns {Promise} that resolves to false exactly, otherwise it has the | ||
* commmand output, if any. | ||
*/ | ||
volofile.run = function (basePath, commandName, namedArgs /*other args can be passed*/) { | ||
var d = q.defer(), | ||
args = [].slice.call(arguments, 2); | ||
|
||
volofile(basePath).then(function (vfMod) { | ||
var command = vfMod && vfMod[commandName]; | ||
|
||
if (command) { | ||
if (typeof command === 'function') { | ||
//Just normalize to advanced structure. | ||
command = { | ||
before: [], | ||
run: command | ||
}; | ||
} | ||
d.resolve(volofile.runCommand.apply(volofile, [command].concat(args))); | ||
} else { | ||
d.resolve(false); | ||
} | ||
}); | ||
|
||
return d.promise; | ||
}; | ||
|
||
volofile.runCommand = function (command, namedArgs /*other args can be passed*/) { | ||
var d = q.defer(), | ||
args; | ||
|
||
if (!command) { | ||
d.resolve(); | ||
} else { | ||
args = [].slice.call(arguments, 1); | ||
|
||
q.call(function () { | ||
if (command.before.length) { | ||
return command.before.reduce(function (done, command) { | ||
return q.wait(done, | ||
volofile.runCommand.apply(volofile, | ||
[command].concat(args))); | ||
}); | ||
} | ||
return undefined; | ||
}) | ||
.then(function () { | ||
var commandDeferred = q.defer(), | ||
err; | ||
|
||
//Call validate if it is on the command. | ||
if (command.validate) { | ||
err = command.validate(args); | ||
if (err) { | ||
commandDeferred.reject(err); | ||
return commandDeferred.promise; | ||
} | ||
} | ||
|
||
command.run.apply(command, [commandDeferred].concat(args)); | ||
return commandDeferred.promise; | ||
}) | ||
.then(d.resolve, d.reject); | ||
} | ||
|
||
return d.promise; | ||
}; | ||
|
||
return volofile; | ||
}); |