Skip to content

Commit 21f94fd

Browse files
author
Krzysztof Antczak
committed
initial commit
0 parents  commit 21f94fd

File tree

7 files changed

+210
-0
lines changed

7 files changed

+210
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/

Gruntfile.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
module.exports = function(grunt) {
4+
5+
// Project configuration.
6+
grunt.initConfig({
7+
test: {
8+
files: ['test/**/*_test.js']
9+
},
10+
lint: {
11+
files: ['Gruntfile.js', 'tasks/**/*.js', '<config:test.files>']
12+
},
13+
watch: {
14+
files: '<config:lint.files>',
15+
tasks: 'default'
16+
},
17+
devcode : {
18+
files : {
19+
html: true,
20+
js: true,
21+
css: true,
22+
clean: true,
23+
block: {
24+
open: 'devcode',
25+
close: 'endcode'
26+
}
27+
}
28+
},
29+
jshint: {
30+
options: {
31+
curly: true,
32+
eqeqeq: true,
33+
immed: true,
34+
latedef: true,
35+
newcap: true,
36+
noarg: true,
37+
sub: true,
38+
undef: true,
39+
boss: true,
40+
eqnull: true,
41+
node: true,
42+
es5: true
43+
},
44+
globals: {}
45+
}
46+
});
47+
48+
// Load local tasks.
49+
grunt.loadTasks('tasks');
50+
51+
// Default task.
52+
grunt.registerTask('default', 'test');
53+
54+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

bin/grunt-devcode

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('grunt').npmTasks('grunt-devcode').cli();

package.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "grunt-devcode",
3+
"description": "Remove code blocks based on environment configuration",
4+
"version": "0.1.0",
5+
"homepage": "https://github.com/livedata/grunt-devcode",
6+
"author": {
7+
"name": "Krzysztof Antczak",
8+
"email": "[email protected]",
9+
"url": "http://inventic.it/"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git://github.com/livedata/grunt-devcode.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/livedata/grunt-devcode/issues"
17+
},
18+
"licenses": [
19+
{
20+
"type": "Apache",
21+
"url": "https://github.com/livedata/grunt-devcode/blob/master/LICENSE"
22+
}
23+
],
24+
"main": "Gruntfile.js",
25+
"bin": {
26+
"grunt-devcode": "bin/grunt-devcode"
27+
},
28+
"engines": {
29+
"node": ">= 0.6.0"
30+
},
31+
"scripts": {
32+
"test": "grunt test"
33+
},
34+
"dependencies": {
35+
"grunt": "~0.3.15"
36+
},
37+
"devDependencies": {
38+
"grunt": "~0.4.0a"
39+
},
40+
"keywords": [
41+
"gruntplugin",
42+
"directive",
43+
"var",
44+
"ENV",
45+
"environment",
46+
"ifdef",
47+
"echo",
48+
"include",
49+
"exclude",
50+
"process",
51+
"yeoman",
52+
"pragma"
53+
],
54+
55+
"_from": "grunt-devcode"
56+
}

tasks/devcode.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* devcode
3+
* https://github.com/livedata/grunt-devcode
4+
*
5+
* Written by Krzysztof Antczak
6+
* Licensed under the Apache 2.0 license.
7+
*/
8+
/*jshint node:true*/
9+
10+
'use strict';
11+
12+
module.exports = init;
13+
14+
var grunt = require('grunt'),
15+
path = require('path');
16+
17+
// remove when 0.4.0
18+
grunt.util = grunt.util || grunt.utils;
19+
20+
var _ = grunt.util._;
21+
var defaultEnv = {};
22+
23+
function init(grunt)
24+
{
25+
grunt.registerMultiTask('devcode', 'Remove code blocks based on environment configuration', function()
26+
{
27+
var context = _.extend({},defaultEnv,process.env), files;
28+
context.NODE_ENV = context.NODE_ENV || 'development';
29+
30+
var cOpen = this.data.block.open || 'devcode';
31+
var cClose = this.data.block.close || 'endcode';
32+
var workDir = path.resolve(process.cwd(), context.DEST);
33+
var _this = this;
34+
35+
var replaceCode = function ( files, type )
36+
{
37+
if ( type == 'html' || typeof type == 'undefined' )
38+
{
39+
var startblock = '<!--\\s*'+cOpen+':\\s*([^-]+)-->';
40+
var endblock = '<!--\\s*'+cClose+'\\s*-->';
41+
}
42+
else if ( type == 'js' || type == 'css' )
43+
{
44+
var startblock = '\/\/\\s*'+cOpen+':\\s*?([^\\n]+)';
45+
var endblock = '\/\/\\s*'+cClose+'\\s*';
46+
}
47+
48+
files.forEach(function(file)
49+
{
50+
file = path.resolve(workDir,file);
51+
var body = grunt.file.read(file);
52+
53+
var regex = new RegExp(startblock + '[\\s\\S]*?' + endblock + '[\\s\\r\\n]?', 'g');
54+
55+
// replace code accordingly to current environment
56+
body = body.replace(regex, function($0, $1)
57+
{
58+
var m = $1.replace(/^\s+|\s+$/g, ''); // trim
59+
if ( type == 'js' ) { console.log(' >> ',m); }
60+
61+
if ( m.indexOf('!') == -1 )
62+
{
63+
if ( context.NODE_ENV != m ) return ''; else return $0;
64+
}
65+
else
66+
{
67+
if ( '!'+context.NODE_ENV == m ) return ''; else return $0;
68+
}
69+
});
70+
71+
if ( _this.data.clean == true ) // remove devcode tags
72+
{
73+
body = body.replace(new RegExp(startblock + '\\n?$', 'gm'), '');
74+
body = body.replace(new RegExp(endblock + '\\n?$', 'gm'), '');
75+
}
76+
77+
grunt.file.write(file, body);
78+
});
79+
};
80+
81+
// { html: true, js: true, css: true }
82+
if ( this.data.html == true )
83+
{
84+
replaceCode(grunt.file.expand({cwd: workDir},'**/*.html'), 'html');
85+
}
86+
if ( this.data.js == true )
87+
{
88+
replaceCode(grunt.file.expand({cwd: workDir},'**/*.js'), 'js');
89+
}
90+
if ( this.data.css == true )
91+
{
92+
replaceCode(grunt.file.expand({cwd: workDir},'**/*.css'), 'css');
93+
}
94+
});
95+
};

0 commit comments

Comments
 (0)