Skip to content

Commit

Permalink
Upgrade grunt task
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfiana Sibuea committed Oct 25, 2017
1 parent fe4de98 commit 6ba6481
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 99 deletions.
165 changes: 77 additions & 88 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,93 +90,6 @@ module.exports = function(grunt) {
dest: 'docs/static'
}
},

//convert less to stylus
execute: {
less2stylus: {
call: function(grunt, options, async) {
var done = async();
var exec = require('child_process').exec;
exec('cd node_modules/less2stylus && node ./less2stylus ../../src/less/waves.less', function (error, stdout, stderr) {
grunt.log.writeln('Executing less2styus...');

if (error) {
grunt.log.writeln('Error! ' + error);
}

var fs = require('fs');
fs.writeFile("src/stylus/waves.styl", stdout, function(err) {
if(err) {
grunt.log.writeln(err);
} else {
grunt.log.writeln("Stylus file was saved!");
}

done(); // let grunt resume
});
});
}
},

less2scss: {
//FUTURE: Put less2scss as it's own script
call: function(grunt, options, async) {
var done = async();
var text = fs.readFileSync('src/less/waves.less', {encoding:'utf8'});

//replace @ with $
text = text.replace(/@(?!import|media|keyframes|-)/g, '$');
//replace mixins
text = text.replace(/\.([\w\-]*)\s*\((.*)\)\s*\{/g, '@mixin $1($2){');
//replace includes
text = text.replace(/\.([\w\-]*\(.*\)\s*;)/g, '@include $1');
//replace string literals
//eg. ~'!important' -> #{"!important"}
text = text.replace(/~(?:\"|\')(.*)(?:\"|\')/g, '#{"$1"}');

//NOTE: for true less->scss transpiling we'd need to replace spin to adjust-hue (not needed but anyway)

fs.writeFileSync('src/scss/waves.scss', text);

done();
}
},

test: {
call: function(grunt, options, async) {
var done = async();
var lessTest = fs.readFileSync('tests/less/waves.min.css', {encoding:'utf8'});
var sassTest = fs.readFileSync('tests/sass/waves.min.css', {encoding:'utf8'});
var scssTest = fs.readFileSync('tests/scss/waves.min.css', {encoding:'utf8'});
var stylusTest = fs.readFileSync('tests/stylus/waves.min.css', {encoding:'utf8'});

var failure = false;
if (lessTest != sassTest) {
grunt.log.writeln('ERROR: sass failed test.');
failure = true;
}

if (lessTest != scssTest) {
grunt.log.writeln('ERROR: scss failed test.');
failure = true;
}

if (lessTest != stylusTest) {
grunt.log.writeln('ERROR: stylus failed test.');
failure = true;
}

if (sassTest != scssTest) {
grunt.log.writeln('WARNING: sass files aren\'t equal?');
failure = true;
}

if (!failure) grunt.log.writeln('PASS: conversions generated same CSS');

done();
}
}
},

'sass-convert': {
options: {
Expand Down Expand Up @@ -229,6 +142,83 @@ module.exports = function(grunt) {
}
}
});

grunt.registerTask('execute:less2stylus', 'Convert less to stylus.', function() {
var done = this.async();
var exec = require('child_process').exec;
exec('cd node_modules/less2stylus && node ./less2stylus ../../src/less/waves.less', function (error, stdout, stderr) {
grunt.log.writeln('Executing less2styus...');

if (error) {
grunt.log.writeln('Error! ' + error);
}

var fs = require('fs');
fs.writeFile("src/stylus/waves.styl", stdout, function(err) {
if(err) {
grunt.log.writeln(err);
} else {
grunt.log.writeln("Stylus file was saved!");
}

done(); // let grunt resume
});
});
});

grunt.registerTask('execute:less2scss', 'Convert less to scss.', function() {
var done = this.async();
var text = fs.readFileSync('src/less/waves.less', {encoding:'utf8'});

//replace @ with $
text = text.replace(/@(?!import|media|keyframes|-)/g, '$');
//replace mixins
text = text.replace(/\.([\w\-]*)\s*\((.*)\)\s*\{/g, '@mixin $1($2){');
//replace includes
text = text.replace(/\.([\w\-]*\(.*\)\s*;)/g, '@include $1');
//replace string literals
//eg. ~'!important' -> #{"!important"}
text = text.replace(/~(?:\"|\')(.*)(?:\"|\')/g, '#{"$1"}');

//NOTE: for true less->scss transpiling we'd need to replace spin to adjust-hue (not needed but anyway)

fs.writeFileSync('src/scss/waves.scss', text);

done();
});

grunt.registerTask('execute:test', 'Convert less to scss.', function() {
var done = this.async();
var lessTest = fs.readFileSync('tests/less/waves.min.css', {encoding:'utf8'});
var sassTest = fs.readFileSync('tests/sass/waves.min.css', {encoding:'utf8'});
var scssTest = fs.readFileSync('tests/scss/waves.min.css', {encoding:'utf8'});
var stylusTest = fs.readFileSync('tests/stylus/waves.min.css', {encoding:'utf8'});

var failure = false;
if (lessTest != sassTest) {
grunt.log.writeln('ERROR: sass failed test.');
failure = true;
}

if (lessTest != scssTest) {
grunt.log.writeln('ERROR: scss failed test.');
failure = true;
}

if (lessTest != stylusTest) {
grunt.log.writeln('ERROR: stylus failed test.');
failure = true;
}

if (sassTest != scssTest) {
grunt.log.writeln('WARNING: sass files aren\'t equal?');
failure = true;
}

if (!failure) grunt.log.writeln('PASS: conversions generated same CSS');

done();
});

// Load module
grunt.loadNpmTasks('grunt-contrib-concat');
Expand All @@ -239,7 +229,6 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-stylus');
grunt.loadNpmTasks('grunt-execute');
grunt.loadNpmTasks('grunt-sass-convert');
grunt.loadNpmTasks('grunt-contrib-clean');

Expand Down
21 changes: 10 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
"main": "src/js/waves.js",
"types": "src/typings/waves.d.ts",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-concat": "^0.5.0",
"grunt-contrib-copy": "^0.7.0",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-less": "^0.12.0",
"grunt-contrib-sass": "^0.8.1",
"grunt-contrib-stylus": "^0.20.0",
"grunt-contrib-uglify": "^0.6.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-execute": "^0.2.2",
"grunt": "1.0.1",
"grunt-contrib-clean": "1.1.0",
"grunt-contrib-concat": "1.0.1",
"grunt-contrib-copy": "1.0.0",
"grunt-contrib-jshint": "1.1.0",
"grunt-contrib-less": "1.4.1",
"grunt-contrib-sass": "1.0.0",
"grunt-contrib-stylus": "1.2.0",
"grunt-contrib-uglify": "3.1.0",
"grunt-contrib-watch": "1.0.0",
"grunt-sass-convert": "^0.2.0",
"less2stylus": "^0.1.0"
},
Expand Down

0 comments on commit 6ba6481

Please sign in to comment.