Permalink
Browse files

First

  • Loading branch information...
0 parents commit 871c6bf60f3084efc451b4e3f3b538bfc1f0729c @GulinSS committed Feb 4, 2013
Showing with 193 additions and 0 deletions.
  1. +9 −0 .npmignore
  2. +2 −0 build.bat
  3. +34 −0 package.json
  4. +28 −0 setup.js
  5. +120 −0 src/index.coffee
@@ -0,0 +1,9 @@
+.lock-wscript
+.svn/
+.hg/
+.git/
+CVS/
+*~
+*.bak
+.DS_Store
+npm-debug.log
@@ -0,0 +1,2 @@
+rd /s /q lib
+coffee -wo lib/ src/
@@ -0,0 +1,34 @@
+{
+ "name": "jade-angularjs-brunch",
+ "version": "0.0.1",
+ "description": "Adds Jade support to brunch with angularjs template cache modules.",
+ "author": {
+ "name": "Gulin Serge",
+ "url": "http://gulin.pro/"
+ },
+ "homepage": "https://github.com/GulinSS/jade-angularjs-brunch",
+ "repository": {
+ "type": "git",
+ "url": "git@github.com:GulinSS/jade-angularjs-brunch.git"
+ },
+ "main": "./lib/index",
+ "scripts": {
+ "prepublish": "coffee -o lib/ src/",
+ "postinstall": "node setup.js postinstall",
+ "test": "node setup.js test"
+ },
+ "engines": {
+ "node": "~0.6.10 || 0.8 || 0.9"
+ },
+ "dependencies": {
+ "coffee-script": "1.3.3",
+ "jade": "0.26.3",
+ "mkdirp": "*"
+ },
+ "devDependencies": {
+ "mocha": "1.2.2",
+ "chai": "1.1.0"
+ },
+ "_id": "jade-angularjs-brunch@0.0.1",
+ "_from": "jade-angularjs-brunch@>= 0.0.1 < 1.5"
+}
@@ -0,0 +1,28 @@
+var exec = require('child_process').exec;
+var sysPath = require('path');
+var fs = require('fs');
+
+var mode = process.argv[2];
+
+var fsExists = fs.exists || sysPath.exists;
+
+var execute = function(pathParts, params, callback) {
+ if (callback == null) callback = function() {};
+ var path = sysPath.join.apply(null, pathParts);
+ var command = 'node ' + path + ' ' + params;
+ console.log('Executing', command);
+ exec(command, function(error, stdout, stderr) {
+ if (error != null) return process.stderr.write(stderr.toString());
+ console.log(stdout.toString());
+ });
+};
+
+if (mode === 'postinstall') {
+ fsExists(sysPath.join(__dirname, 'lib'), function(exists) {
+ if (exists) return;
+ execute(['node_modules', 'coffee-script', 'bin', 'coffee'], '-o lib/ src/');
+ });
+} else if (mode === 'test') {
+ execute(['node_modules', 'mocha', 'bin', 'mocha'],
+ '--require test/common.js --colors');
+}
@@ -0,0 +1,120 @@
+jade = require 'jade'
+sysPath = require 'path'
+mkdirp = require 'mkdirp'
+fs = require 'fs'
+
+fileWriter = (newFilePath) -> (err, content) ->
+ throw err if err?
+ return if not content?
+ dirname = sysPath.dirname newFilePath
+ mkdirp dirname, '0775', (err) ->
+ throw err if err?
+ fs.writeFile newFilePath, content, (err) -> throw err if err?
+
+module.exports = class JadeAngularJsCompiler
+ brunchPlugin: yes
+ type: 'template'
+ extension: 'jade'
+
+ constructor: (config) ->
+ @pretty = !!config.plugins?.jade?.pretty
+ @locals = config.plugins?.jade_angular?.locals
+ @modulesFolder = config.plugins?.jade_angular?.modules_folder
+
+ compile: (data, path, callback) ->
+ try
+ content = jade.compile data,
+ compileDebug: no,
+ client: no,
+ filename: path,
+ pretty: @pretty
+ catch err
+ error = err
+ finally
+ callback error, ""
+
+ preparePair: (pair) ->
+ pair.path.push(pair.path.pop()[...-@extension.length] + 'html')
+ pair.path.splice 0, 1, '_public'
+
+ writeStatic: (pair) ->
+ @preparePair pair
+ writer = fileWriter sysPath.join.apply(this, pair.path)
+ writer null, pair.result
+
+ setupModule: (pair) ->
+ @preparePair pair
+ pair.path.splice 1, 1, 'js'
+ modulePath = pair.path.slice 2, pair.path.lastIndexOf(@modulesFolder)+1
+ moduleName = modulePath.join '.'
+ jsFileName = moduleName + '.js'
+ modulePath.push pair.path[pair.path.length-1]
+ copyfolder = pair.path.slice 0, 2
+ copyfolder.push jsFileName
+
+ result =
+ moduleName: moduleName
+ modulePath: sysPath.join.apply this, copyfolder
+ virtualPath: '/' + [modulePath[0], modulePath[2]].join('/')
+ content: pair.result
+
+ parseStringToJSArray: (str) ->
+ stringArray = '['
+ str.split('\n').map (e, i) ->
+ stringArray += "\n'" + e.replace(/'/g, "\\'") + "',"
+ stringArray += "''" + '].join("\\n")'
+
+ writeModules: (modules) ->
+ for own moduleName, templates of modules
+ content = """
+ angular.module('#{moduleName}', [])
+ """
+ templates.map (e, i) =>
+ inlineContent = @parseStringToJSArray(e.content)
+ content += """
+ \n.run(['$templateCache', function($templateCache) {
+ return $templateCache.put('#{e.virtualPath}', #{inlineContent});
+ }])
+ """
+
+ content += ";"
+
+ writer = fileWriter templates[0].modulePath
+ writer null, content
+
+ #TODO: сделать async
+ prepareResult: (compiled) ->
+ pathes = (result.sourceFiles for result in compiled when result.path is '_public\\js\\dontUseMe')[0]
+
+ return [] if pathes is undefined
+
+ pathes.map (e, i) =>
+ data = fs.readFileSync e.path, 'utf8'
+ content = jade.compile data,
+ compileDebug: no,
+ client: no,
+ filename: e.path,
+ pretty: @pretty
+
+ result =
+ path: e.path.split sysPath.sep
+ result: content @locals
+
+ onCompile: (compiled) ->
+ preResult = @prepareResult compiled
+
+ @writeStatic pair for pair in preResult \
+ when pair.path.indexOf(@modulesFolder) is -1 and \
+ pair.path.indexOf('assets') is -1
+
+ modulesRows = (@setupModule pair for pair in preResult \
+ when pair.path.indexOf(@modulesFolder) > -1 and \
+ pair.path.indexOf('assets') is -1)
+
+ modules = {}
+ modulesRows.map (element, index) ->
+ if Object.keys(modules).indexOf(element.moduleName) is -1
+ modules[element.moduleName] = []
+ modules[element.moduleName].push(element)
+
+ @writeModules modules

0 comments on commit 871c6bf

Please sign in to comment.