Skip to content

Commit e4f7eee

Browse files
committed
Merge pull request #13 from Brightspace/dbatiste/add-sass-formatter
Adding the ability to create scss variables.
2 parents 3444e49 + 7d52f24 commit e4f7eee

File tree

9 files changed

+175
-98
lines changed

9 files changed

+175
-98
lines changed

bin/lesscli

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ if (argv._.length === 0) {
1313
process.exit(1);
1414
}
1515

16-
var imagesToLess = require('../lib/index');
16+
var imagesToVariables = require('../lib/create'),
17+
formatter = require('../lib/formatters/less');
1718

18-
imagesToLess(
19+
imagesToVariables(
1920
argv._,
2021
{
2122
dest: argv.output,
22-
prefix: argv.prefix
23+
prefix: argv.prefix,
24+
formatter: formatter
2325
}
2426
).catch(function(err) {
2527
console.error(err);

bin/scsscli

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#! /usr/bin/env node
2+
3+
var chalk = require('chalk'),
4+
argv = require('yargs')
5+
.usage('Usage: imgToLess [--prefix|-p] [--output|-o output.scss] [image.png]')
6+
.example('imgtoscss --prefix -vui --output images.scss *.png')
7+
.alias('o', 'output')
8+
.alias('p', 'prefix')
9+
.argv;
10+
11+
if (argv._.length === 0) {
12+
console.error('At least one image file must be specified.');
13+
process.exit(1);
14+
}
15+
16+
var imagesToVariables = require('../lib/create'),
17+
formatter = require('../lib/formatters/scss');
18+
19+
imagesToVariables(
20+
argv._,
21+
{
22+
dest: argv.output,
23+
prefix: argv.prefix,
24+
formatter: formatter
25+
}
26+
).catch(function(err) {
27+
console.error(err);
28+
process.exit(1);
29+
}).then(function() {
30+
console.log(chalk.green('Less variables created successfully.'));
31+
process.exit(0);
32+
});

lib/create.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
'use strict';
2+
3+
var assign = require('object-assign'),
4+
fs = require('fs'),
5+
minify = require('./minify'),
6+
q = require('q'),
7+
through2 = require('through2'),
8+
vfs = require('vinyl-fs');
9+
10+
var createVariables = function( src, options ) {
11+
12+
var deferred = q.defer();
13+
14+
if ( !src ) {
15+
deferred.reject( new Error( 'The src argument is required.' ) );
16+
return deferred.promise;
17+
}
18+
19+
options = assign( { optimize: true, minify: { log: true } }, options || {} );
20+
21+
var variableStream;
22+
if ( options.dest && options.dest.length > 0 ) {
23+
variableStream = fs.createWriteStream( options.dest );
24+
}
25+
26+
var format = options.formatter;
27+
if ( !format ) {
28+
format = require('./formatters/less');
29+
}
30+
31+
var variables = [];
32+
33+
vfs.src( src, { base: './' } ).pipe(
34+
35+
through2.obj( function( file, enc, done ) {
36+
37+
var writeVariable = function( tempFile ) {
38+
39+
var fileName = file.path.replace( /^.*[\\\/]/, '' );
40+
41+
var variableName = '';
42+
if ( options.prefix ) {
43+
variableName += options.prefix;
44+
}
45+
variableName += fileName.substr( 0, fileName.lastIndexOf( '.' ) );
46+
47+
var value = 'url("data:image/png;base64,' + tempFile.contents.toString( 'base64' ) + '")';
48+
49+
if ( variableStream ) {
50+
variableStream.write( format( variableName, value ) + '\n' );
51+
}
52+
53+
variables.push( {
54+
name: variableName,
55+
value: value,
56+
length: tempFile.contents.length
57+
} );
58+
59+
};
60+
61+
if ( options.optimize ) {
62+
minify( file, options.minify ).then( function( minified ) {
63+
writeVariable( minified );
64+
done();
65+
} ).catch( function() {
66+
deferred.reject( new Error( 'Minify failed.' ) );
67+
done();
68+
} );
69+
} else {
70+
writeVariable( file );
71+
done();
72+
}
73+
74+
}, function() {
75+
76+
if ( variableStream ) {
77+
variableStream.end();
78+
}
79+
80+
deferred.resolve( variables );
81+
82+
} )
83+
84+
);
85+
86+
return deferred.promise;
87+
88+
};
89+
90+
module.exports = createVariables;

lib/formatters/scss.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var format = function(name, value) {
4+
return '$' + name + ': ' + value + ';';
5+
};
6+
7+
module.exports = format;

lib/index.js

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,5 @@
11
'use strict';
22

3-
var assign = require('object-assign'),
4-
fs = require('fs'),
5-
minify = require('./minify'),
6-
q = require('q'),
7-
through2 = require('through2'),
8-
vfs = require('vinyl-fs');
9-
10-
var createVariables = function( src, options ) {
11-
12-
var deferred = q.defer();
13-
14-
if ( !src ) {
15-
deferred.reject( new Error( 'The src argument is required.' ) );
16-
return deferred.promise;
17-
}
18-
19-
options = assign( { optimize: true, minify: { log: true } }, options || {} );
20-
21-
var variableStream;
22-
if ( options.dest && options.dest.length > 0 ) {
23-
variableStream = fs.createWriteStream( options.dest );
24-
}
25-
26-
var format = options.formatter;
27-
if ( !format ) {
28-
format = require('./less-formatter');
29-
}
30-
31-
var variables = [];
32-
33-
vfs.src( src, { base: './' } ).pipe(
34-
35-
through2.obj( function( file, enc, done ) {
36-
37-
var writeVariable = function( tempFile ) {
38-
39-
var fileName = file.path.replace( /^.*[\\\/]/, '' );
40-
41-
var variableName = '';
42-
if ( options.prefix ) {
43-
variableName += options.prefix;
44-
}
45-
variableName += fileName.substr( 0, fileName.lastIndexOf( '.' ) );
46-
47-
var value = 'url("data:image/png;base64,' + tempFile.contents.toString( 'base64' ) + '")';
48-
49-
if ( variableStream ) {
50-
variableStream.write( format( variableName, value ) + '\n' );
51-
}
52-
53-
variables.push( {
54-
name: variableName,
55-
value: value,
56-
length: tempFile.contents.length
57-
} );
58-
59-
};
60-
61-
if ( options.optimize ) {
62-
minify( file, options.minify ).then( function( minified ) {
63-
writeVariable( minified );
64-
done();
65-
} ).catch( function() {
66-
deferred.reject( new Error( 'Minify failed.' ) );
67-
done();
68-
} );
69-
} else {
70-
writeVariable( file );
71-
done();
72-
}
73-
74-
}, function() {
75-
76-
if ( variableStream ) {
77-
variableStream.end();
78-
}
79-
80-
deferred.resolve( variables );
81-
82-
} )
83-
84-
);
85-
86-
return deferred.promise;
87-
88-
};
89-
90-
module.exports = createVariables;
3+
module.exports.create = require('./create');
4+
module.exports.lessFormatter = require('./formatters/less');
5+
module.exports.scssFormatter = require('./formatters/scss');

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "images-to-variables",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "A simple utility for generating Sass/Less variables for images.",
55
"main": "lib/index.js",
66
"bin": {
7-
"imgtoless": "bin/lesscli"
7+
"imgtoless": "bin/lesscli",
8+
"imgtoscss": "bin/scsscli"
89
},
910
"scripts": {
1011
"test": "gulp"
@@ -16,6 +17,7 @@
1617
"keywords": [
1718
"d2l",
1819
"less",
20+
"sass",
1921
"images"
2022
],
2123
"author": "D2L Corporation",

test/createVariablesSpec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var imagesToLess = require('../'),
3+
var imagesToLess = require('../lib/create'),
44
path = require('path');
55

66
var dataPath = path.join( __dirname, 'data' );
@@ -24,7 +24,7 @@ describe( 'createVariables', function() {
2424

2525
imagesToLess( imagesPath, { optimize: false } ).then( function( variables ) {
2626
expect( variables.length ).not.toBe( 0 );
27-
expect( variables[0].name ).toBe( '@icon' );
27+
expect( variables[0].name ).toBe( 'icon' );
2828
expect( variables[0].length ).toBe( expectedUncompressedLength );
2929
expect( variables[0].value ).toBe( expectedUncompressedValue );
3030
done();
@@ -36,7 +36,7 @@ describe( 'createVariables', function() {
3636

3737
imagesToLess( imagesPath ).then( function( variables ) {
3838
expect( variables.length ).not.toBe( 0 );
39-
expect( variables[0].name ).toBe( '@icon' );
39+
expect( variables[0].name ).toBe( 'icon' );
4040
expect( variables[0].length ).toBe( expectedCompressedLength );
4141
expect( variables[0].value ).toBe( expectedCompressedValue );
4242
done();
@@ -48,7 +48,7 @@ describe( 'createVariables', function() {
4848

4949
imagesToLess( path.join( dataPath, 'empty.png' ) ).then( function( variables ) {
5050
expect( variables.length ).not.toBe( 0 );
51-
expect( variables[0].name ).toBe( '@empty' );
51+
expect( variables[0].name ).toBe( 'empty' );
5252
expect( variables[0].length ).toBe( expectedEmptyLength );
5353
expect( variables[0].value ).toBe( expectedEmptyValue );
5454
done();
@@ -60,7 +60,7 @@ describe( 'createVariables', function() {
6060

6161
imagesToLess( imagesPath, { prefix: 'abc-' } ).then( function( variables ) {
6262
expect( variables.length ).not.toBe( 0 );
63-
expect( variables[0].name ).toBe( '@abc-icon' );
63+
expect( variables[0].name ).toBe( 'abc-icon' );
6464
expect( variables[0].length ).toBe( expectedCompressedLength );
6565
expect( variables[0].value ).toBe( expectedCompressedValue );
6666
done();

test/formattersSpec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
describe( 'formatters', function() {
4+
5+
describe( 'less', function() {
6+
7+
var lessFormatter = require('../lib/formatters/less');
8+
9+
it( 'should create less variable', function() {
10+
11+
expect( lessFormatter( 'myvar', 'somevalue' ) ).toBe( '@myvar: somevalue;' );
12+
13+
} );
14+
15+
} );
16+
17+
describe( 'scss', function() {
18+
19+
var scssFormatter = require('../lib/formatters/scss');
20+
21+
it( 'should create scss variable', function() {
22+
23+
expect( scssFormatter( 'myvar', 'somevalue' ) ).toBe( '$myvar: somevalue;' );
24+
25+
} );
26+
27+
} );
28+
29+
} );

0 commit comments

Comments
 (0)