Skip to content

Commit

Permalink
feat(formats): add file object to formatter method (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanksdesign authored Nov 7, 2020
1 parent 37b06e8 commit 1481c46
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 62 deletions.
26 changes: 16 additions & 10 deletions __tests__/buildFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,30 @@ describe('buildFile', () => {

it('should error if format doesnt exist or isnt a function', () => {
expect(
buildFile.bind(null, '__tests__/__output/test.txt', {}, {}, {})
buildFile.bind(null, {destination: '__tests__/__output/test.txt'}, {}, {})
).toThrow('Please enter a valid file format');
expect(
buildFile.bind(null, '__tests__/__output/test.txt', [], {}, {})
buildFile.bind(null, {destination: '__tests__/__output/test.txt', format: {}}, {}, {})
).toThrow('Please enter a valid file format');
expect(
buildFile.bind(null, '__tests__/__output/test.txt', null, {}, {})
buildFile.bind(null, {destination: '__tests__/__output/test.txt', format: []}, {}, {})
).toThrow('Please enter a valid file format');
});

it('should error if destination doesnt exist or isnt a string', () => {
expect(
buildFile.bind(null, {}, format, {}, {})
buildFile.bind(null, {format}, {}, {})
).toThrow('Please enter a valid destination');
expect(
buildFile.bind(null, [], format, {}, {})
buildFile.bind(null, {format, destination: []}, {}, {})
).toThrow('Please enter a valid destination');
expect(
buildFile.bind(null, null, format, {}, {})
buildFile.bind(null, {format, destination: {}}, {}, {})
).toThrow('Please enter a valid destination');
});

let dest = './__tests__/__output/test.collisions';
var PROPERTY_NAME_COLLISION_WARNINGS = GroupMessages.GROUP.PropertyNameCollisionWarnings + ":" + dest;
let destination = './__tests__/__output/test.collisions';
var PROPERTY_NAME_COLLISION_WARNINGS = GroupMessages.GROUP.PropertyNameCollisionWarnings + ":" + destination;
it('should generate warning messages for output name collisions', () => {
let name = 'someName';
let properties = {
Expand All @@ -71,7 +71,7 @@ describe('buildFile', () => {
};

GroupMessages.clear(PROPERTY_NAME_COLLISION_WARNINGS);
buildFile(dest, format, {}, properties);
buildFile({destination, format}, {}, properties);

let collisions = properties.allProperties.map((properties) => {
let propertyPathText = chalk.keyword('orangered')(properties.path.join('.'));
Expand All @@ -86,7 +86,13 @@ describe('buildFile', () => {
});

it('should write to a file properly', () => {
buildFile('test.txt', format, {buildPath: '__tests__/__output/'}, {});
buildFile({
destination: 'test.txt',
format
},{
buildPath: '__tests__/__output/'
},{}
);
expect(helpers.fileExists('./__tests__/__output/test.txt')).toBeTruthy();
});
});
6 changes: 3 additions & 3 deletions __tests__/cleanDir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('cleanDir', () => {
});

it('should delete a dir properly', () => {
buildFile('test.txt', format, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
cleanFile('test.txt', format, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
cleanDir('test.txt', format, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
buildFile({destination:'test.txt', format}, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
cleanFile({destination:'test.txt', format}, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
cleanDir({destination:'test.txt', format}, {buildPath: '__tests__/__output/extradir1/extradir2/'}, {});
expect(helpers.dirDoesNotExist('./__tests__/__output/extradir1/extradir2')).toBeTruthy();
expect(helpers.dirDoesNotExist('./__tests__/__output/extradir1')).toBeTruthy();
});
Expand Down
6 changes: 3 additions & 3 deletions __tests__/cleanFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ describe('cleanFile', () => {
});

it('should delete a file properly', () => {
buildFile('test.txt', format, {buildPath: '__tests__/__output/'}, {});
cleanFile('test.txt', format, {buildPath: '__tests__/__output/'}, {});
buildFile({destination:'test.txt', format}, {buildPath: '__tests__/__output/'}, {});
cleanFile({destination:'test.txt', format}, {buildPath: '__tests__/__output/'}, {});
expect(helpers.fileDoesNotExist('./__tests__/__output/test.txt')).toBeTruthy();
});

describe('if a file does not exist', () => {
it('should not throw', () => {
expect(() => cleanFile('non-existent.txt', format, { buildPath: '__tests__/__output/' }, {})).not.toThrow();
expect(() => cleanFile({destination: 'non-existent.txt', format}, { buildPath: '__tests__/__output/' }, {})).not.toThrow();
})
})

Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('formats', () => {
global.Date = function() { return constantDate };

var formatter = formats[key].bind(file);
var output = formatter(dictionary, file);
var output = formatter(dictionary, {}, file);

// reset the global Date object
global.Date = globalDate;
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/es6Constants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('formats', () => {
});

it('should be a valid JS file', () => {
fs.writeFileSync('./__tests__/__output/output.js', formatter(dictionary) );
fs.writeFileSync('./__tests__/__output/output.js', formatter(dictionary, {}, file) );
var test = require('../__output/output.js');
expect(test.red).toEqual(dictionary.allProperties[0].value);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/javascriptModule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('formats', () => {
});

it('should be a valid JS file', () => {
fs.writeFileSync('./__tests__/__output/output.js', formatter(dictionary) );
fs.writeFileSync('./__tests__/__output/output.js', formatter(dictionary, {}, file) );
var test = require('../__output/output.js');
expect(test.color.red.value).toEqual(dictionary.properties.color.red.value);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/javascriptObject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('formats', () => {

it('should be valid JS syntax', done => {
try {
vm.runInNewContext(formatter(dictionary))
vm.runInNewContext(formatter(dictionary, {}, file))
return done();
} catch (err) {
return done(new Error(err));
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/javascriptUmd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('formats', () => {
});

it('should be a valid JS file', () => {
fs.writeFileSync('./__tests__/__output/umd.js', formatter(dictionary) );
fs.writeFileSync('./__tests__/__output/umd.js', formatter(dictionary, {}, file) );
var test = require('../__output/umd.js');
expect(test.color.red.value).toEqual(dictionary.properties.color.red.value);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('formats', () => {
});

it('should be a valid JSON file', () => {
fs.writeFileSync('./__tests__/__output/output.json', formatter(dictionary) );
fs.writeFileSync('./__tests__/__output/output.json', formatter(dictionary, {}, file) );
var test = require('../__output/output.json');
expect(test.color.red.value).toEqual(dictionary.properties.color.red.value);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/jsonFlat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('formats', () => {
});

it('should be a valid JSON file', () => {
fs.writeFileSync('./__tests__/__output/output.flat.json', formatter(dictionary) );
fs.writeFileSync('./__tests__/__output/output.flat.json', formatter(dictionary, {}, file) );
var test = require('../__output/output.flat.json');
expect(test['color-base-red']).toEqual(dictionary.allProperties[0].value);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/jsonNested.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('formats', function() {
});

it('should be a valid JSON file', function() {
fs.writeFileSync('./__tests__/__output/json-nested.json', formatter(dictionary));
fs.writeFileSync('./__tests__/__output/json-nested.json', formatter(dictionary, {}, file));
var test = require('../__output/json-nested.json');
expect(test.color.base.red.primary)
.toEqual(dictionary.properties.color.base.red.primary.value);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/lessIcons.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('formats', () => {
describe('less/icons', () => {

it('should have a valid less syntax', done => {
less.render(formatter(dictionary, config))
less.render(formatter(dictionary, config, file))
.then(function(output) {
expect(output).toBeDefined();
done();
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/lessVariables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('formats', () => {
describe('less/variables', () => {

it('should have a valid less syntax', done => {
less.render(formatter(dictionary))
less.render(formatter(dictionary, {}, file))
.then(function(output) {
expect(output).toBeDefined();
done();
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/scssIcons.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('formats', () => {

it('should have a valid scss syntax', done => {
scss.render({
data: formatter(dictionary, config),
data: formatter(dictionary, config, file),
}, function(err, result) {
if(err) {
return done(new Error(err));
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/scssMaps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('formats', () => {
global.Date = function() { return constantDate };

var formatter = formats[key].bind(file);
var output = formatter(dictionary, file);
var output = formatter(dictionary, {}, file);

// reset the global Date object (or node-sass will complain!)
global.Date = globalDate;
Expand Down
2 changes: 1 addition & 1 deletion __tests__/formats/scssVariables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('formats', () => {

it('should have a valid scss syntax', done => {
scss.render({
data: formatter(dictionary),
data: formatter(dictionary, {}, file),
}, function(err, result) {
if(err) {
return done(new Error(err));
Expand Down
12 changes: 7 additions & 5 deletions lib/buildFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ var path = require('path'),
* Takes the style property object and a format and returns a
* string that can be written to a file.
* @memberOf StyleDictionary
* @param {String} destination
* @param {Function} format
* @param {Object} file
* @param {Object} platform
* @param {Object} dictionary
* @param {Function} filter
* @returns {null}
*/
function buildFile(destination, format, platform, dictionary, filter) {
function buildFile(file = {}, platform = {}, dictionary = {}) {
var { destination, filter, format } = file || {};

if (typeof format !== 'function')
throw new Error('Please enter a valid file format');
if (typeof destination !== 'string')
throw new Error('Please enter a valid destination');

// to maintain backwards compatability we bind the format to the file object
format = format.bind(file);
var fullDestination = destination;

// if there is a build path, prepend the full destination with it
Expand Down Expand Up @@ -75,7 +77,7 @@ function buildFile(destination, format, platform, dictionary, filter) {

let propertyNamesCollisionCount = GroupMessages.count(PROPERTY_NAME_COLLISION_WARNINGS);

fs.writeFileSync(fullDestination, format(filteredProperties, platform));
fs.writeFileSync(fullDestination, format(filteredProperties, platform, file));
console.log((propertyNamesCollisionCount>0 ? '⚠️ ' : chalk.bold.green('✔︎ ')) + ' ' + fullDestination);

if(propertyNamesCollisionCount > 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/buildFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function buildFiles(dictionary, platform) {

_.each(platform.files, function (file) {
if (file.format) {
buildFile(file.destination, file.format.bind(file), platform, dictionary, file.filter);
buildFile(file, platform, dictionary);
} else {
throw new Error('Please supply a format');
}
Expand Down
7 changes: 3 additions & 4 deletions lib/cleanDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ var path = require('path'),
* Takes the style property object and a format and returns a
* string that can be written to a file.
* @memberOf StyleDictionary
* @param {String} destination
* @param {Function} format (unused)
* @param {Object} file
* @param {Object} platform
* @param {Object} dictionary (unused)
* @param {Function} filter (unused)
* @returns {null}
*/
function cleanDir(destination, format, platform, dictionary, filter) {
function cleanDir(file = {}, platform = {}, dictionary = {}) {
var { destination } = file;

if (typeof destination !== 'string')
throw new Error('Please enter a valid destination');
Expand Down
2 changes: 1 addition & 1 deletion lib/cleanDirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function cleanDirs(dictionary, platform) {
// while neither the format or dictionary are used by clean file I'm passing them in for symmetry to buildFile
_.each(platform.files, function (file) {
if (file.format) {
cleanDir(file.destination, file.format.bind(file), platform, dictionary);
cleanDir(file, platform, dictionary);
} else {
throw new Error('Please supply a format')
}
Expand Down
5 changes: 2 additions & 3 deletions lib/cleanFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var path = require('path'),
* @param {Function} filter (unused)
* @returns {null}
*/
function cleanFile(destination, format, platform, dictionary, filter) {
function cleanFile(file = {}, platform = {}, dictionary = {}) {
var { destination } = file;

if (typeof destination !== 'string')
throw new Error('Please enter a valid destination');
Expand All @@ -37,8 +38,6 @@ function cleanFile(destination, format, platform, dictionary, filter) {
destination = platform.buildPath + destination;
}

var dirname = path.dirname(destination);

if (!fs.existsSync(destination)) {
console.log(chalk.bold.red('!') + ' ' + destination + ', does not exist');
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/cleanFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function cleanFiles(dictionary, platform) {
// while neither the format or dictionary are used by clean file I'm passing them in for symmetry to buildFile
_.each(platform.files, function (file) {
if (file.format) {
cleanFile(file.destination, file.format.bind(file), platform, dictionary);
cleanFile(file, platform, dictionary);
} else {
throw new Error('Please supply a template or formatter')
}
Expand Down
Loading

0 comments on commit 1481c46

Please sign in to comment.