Permalink
Browse files

Using the precompiler npm module

  • Loading branch information...
1 parent 64ecd8e commit 6031966a2aed093436b4ec660e84de2a6e8f1cdb @petebacondarwin petebacondarwin committed Jan 31, 2012
View
@@ -0,0 +1,2 @@
+packages
+.kansorc
View
@@ -0,0 +1,5 @@
+{exec} = require 'child_process'
+task 'build', 'Build project from src/*.coffee to lib/*.js', ->
+ exec 'coffee --compile --output build/ src/', (err, stdout, stderr) ->
+ throw err if err
+ console.log stdout + stderr
View
@@ -19,7 +19,8 @@ Add a `coffee-script` field to `kanso.json` with one or both of the following: a
},
```
-Run `kanso fetch` from your terminal to install dependencies. When you `kanso push`:
+Run `kanso intall` from your terminal to install dependencies.
+When you `kanso push`:
1. All coffeescript modules listed in `coffee-script.modules` will be compiled to javascript and uploaded as usual.
2. All coffeescript files in `attachements` directories will be uploaded as attachments at the corrosponding path with a `.js` extension. So `/lib/js/main.coffee` will be uploaded to `/lib/js/main.js`.
View
@@ -1,85 +1,49 @@
-var async, attachments, coffee, compile_attachment, compile_attachments, compile_coffee, compile_module, compile_modules, modules, path, spawn, utils;
-async = require("async");
-utils = require("kanso-utils/utils");
-spawn = require("child_process").spawn;
-path = require("path");
-modules = require("kanso-utils/modules");
-attachments = require("kanso-utils/attachments");
-coffee = require("coffee-script/lib/coffee-script/coffee-script");
-module.exports = {
- before: "properties",
- run: function(root, path, settings, doc, callback) {
- var apply_compile_attachments, apply_compile_modules, attach_paths, module_paths;
- if (!settings["coffee-script"]) {
- return callback(null, doc);
- }
- if (!settings["coffee-script"]["modules"] && !settings["coffee-script"]["attachments"]) {
- return callback(null, doc);
- }
- module_paths = settings["coffee-script"]["modules"] || [];
- if (!Array.isArray(module_paths)) {
- module_paths = [module_paths];
- }
- attach_paths = settings["coffee-script"]["attachments"] || [];
- if (!Array.isArray(attach_paths)) {
- attach_paths = [attach_paths];
- }
- apply_compile_modules = async.apply(compile_modules, doc, path);
- apply_compile_attachments = async.apply(compile_attachments, doc, path);
- return async.parallel([async.apply(async.forEach, module_paths, apply_compile_modules), async.apply(async.forEach, attach_paths, apply_compile_attachments)], function(err) {
- return callback(err, doc);
- });
- }
-};
-compile_modules = function(doc, path, paths, callback) {
- var pattern;
- pattern = /.*\.coffee$/i;
- return utils.find(utils.abspath(paths, path), pattern, function(err, data) {
- var apply_compile_module;
- if (err) {
- return callback(err);
- }
- apply_compile_module = async.apply(compile_module, doc, path);
- return async.forEach(data, apply_compile_module, callback);
- });
-};
-compile_module = function(doc, path, filename, callback) {
- var name;
- name = utils.relpath(filename, path).replace(/\.coffee$/, "");
- return compile_coffee(path, filename, function(err, js) {
- if (err) {
- return callback(err);
- }
- modules.add(doc, name, js.toString());
- return callback();
- });
-};
-compile_attachments = function(doc, path, paths, callback) {
- var pattern;
- pattern = /.*\.coffee$/i;
- return utils.find(utils.abspath(paths, path), pattern, function(err, data) {
- var apply_compile_attachment;
- if (err) {
- return callback(err);
- }
- apply_compile_attachment = async.apply(compile_attachment, doc, path);
- return async.forEach(data, apply_compile_attachment, callback);
- });
-};
-compile_attachment = function(doc, path, filename, callback) {
- var name;
- name = utils.relpath(filename, path).replace(/\.coffee$/, ".js");
- return compile_coffee(path, filename, function(err, js) {
- if (err) {
- return callback(err);
- }
- attachments.add(doc, name, name, new Buffer(js).toString("base64"));
- return callback();
- });
-};
-compile_coffee = function(project_path, filename, callback) {
- var c;
- console.log("Compiling " + utils.relpath(filename, project_path));
- c = coffee.compile(fs.readFileSync(filename, 'utf8'));
- return callback(null, c);
-};
+(function() {
+
+ module.exports = {
+ before: "modules",
+ run: function(root, path, settings, doc, callback) {
+ var async, attachments, attachmentsPaths, coffee, compileAttachment, compileModule, extension_pattern, file_pattern, modules, modulesPaths, precompiler, processAttachments, processModules, utils, _ref, _ref2;
+ modulesPaths = (_ref = settings["coffee-script"]) != null ? _ref["modules"] : void 0;
+ attachmentsPaths = (_ref2 = settings["coffee-script"]) != null ? _ref2["attachments"] : void 0;
+ if (!((modulesPaths != null) || (attachmentsPaths != null))) {
+ console.log("Coffee script precompiler must have either a modules or an attachment setting");
+ return callback(null, doc);
+ }
+ if (modulesPaths == null) modulesPaths = [];
+ if (attachmentsPaths == null) attachmentsPaths = [];
+ async = require("async");
+ utils = require("kanso-utils/utils");
+ precompiler = require("kanso-precompiler-base");
+ coffee = require("coffee-script");
+ file_pattern = /.*\.coffee$/i;
+ extension_pattern = /\.coffee$/;
+ compileAttachment = function(filename, callback) {
+ var js, name;
+ console.log("Compiling attachment");
+ js = coffee.compile(fs.readFileSync(filename, 'utf8'));
+ name = utils.relpath(filename, path).replace(extension_pattern, ".js");
+ precompiler.addAttachment(doc, name, filename, js);
+ return callback(null, doc);
+ };
+ compileModule = function(filename, callback) {
+ var js, name;
+ console.log("Compiling module");
+ js = coffee.compile(fs.readFileSync(filename, 'utf8'));
+ name = utils.relpath(filename, path).replace(extension_pattern, "");
+ precompiler.addModule(doc, name, filename, js);
+ return callback(null, doc);
+ };
+ console.log("Running coffee-script pre-compiler");
+ console.dir();
+ modules = precompiler.normalizePaths(settings["coffee-script"]["modules"], path);
+ attachments = precompiler.normalizePaths(attachmentsPaths, path);
+ processModules = async.apply(precompiler.processPaths, modules, file_pattern, compileModule);
+ processAttachments = async.apply(precompiler.processPaths, attachments, file_pattern, compileAttachment);
+ return async.parallel([processModules, processAttachments], function(err, results) {
+ return callback(err, doc);
+ });
+ }
+ };
+
+}).call(this);
View
@@ -1,12 +1,14 @@
-module.exports = {
- after: "attachments",
- run: function(root, path, settings, doc, callback) {
- var k;
- for (k in doc._attachments || {}) {
- if (/\.coffee$/.test(k)) {
- delete doc._attachments[k];
+(function() {
+
+ module.exports = {
+ after: "attachments",
+ run: function(root, path, settings, doc, callback) {
+ var k;
+ for (k in doc._attachments || {}) {
+ if (/\.coffee$/.test(k)) delete doc._attachments[k];
}
+ return callback(null, doc);
}
- return callback(null, doc);
- }
-};
+ };
+
+}).call(this);
View
@@ -1,10 +1,12 @@
{
"name": "coffee-script-precompiler",
- "version": "0.0.9",
+ "version": "0.0.10",
"categories": ["build-tools"],
"description": "Compiles CoffeeScript modules and attachments to javascript on each push",
+ "url": "https://github.com/nrw/coffee-script-precompiler/",
"preprocessors": {
"compile": "build/compile",
"remove": "build/remove"
- }
+ },
+ "dependencies": {}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 6031966

Please sign in to comment.