Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove templates #152

Merged
merged 18 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var StyleDictionary = {
transform: require('./lib/common/transforms'),
transformGroup: require('./lib/common/transformGroups'),
format: require('./lib/common/formats'),
// template: require('./lib/common/templates'),
action: require('./lib/common/actions'),

registerTransform: require('./lib/register/transform'),
Expand Down
31 changes: 3 additions & 28 deletions lib/buildFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/

var _ = require('lodash'),
buildFile = require('./buildFile');
buildFile = require('./buildFile'),
chalk = require('chalk');

/**
* Takes a platform config object and a properties
Expand All @@ -29,34 +30,8 @@ function buildFiles(dictionary, platform) {
throw new Error('Build path must end in a trailing slash or you will get weird file names.')
}

var warningText = `
templates will be removed in Style Dictionary 3.0, please change your config to use formats.
This is an example of how to fix your config.js:

Before:
"files": [{
"destination": "font_dimens.xml",
"template": "android/fontDimens"
},{
"destination": "colors.xml",
"template": "android/colors"
}]

After:
"files": [{
"destination": "font_dimens.xml",
"format": "android/fontDimens"
},{
"destination": "colors.xml",
"format": "android/colors"
}]
`;

_.each(platform.files, function (file) {
if (file.template) {
console.error(warningText);
buildFile(file.destination, file.format.bind(file), platform, dictionary, file.filter);
} else if (file.format) {
if (file.format) {
buildFile(file.destination, file.format.bind(file), platform, dictionary, file.filter);
} else {
throw new Error('Please supply a formatter');
Expand Down
28 changes: 1 addition & 27 deletions lib/cleanDirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,9 @@ function cleanDirs(dictionary, platform) {
throw new Error('Build path must end in a trailing slash or you will get weird file names.')
}

var warningText = `
templates will be removed in Style Dictionary 3.0, please change your config to use formats.
This is an example of how to fix your config.js:

Before:
"files": [{
"destination": "font_dimens.xml",
"template": "android/fontDimens"
},{
"destination": "colors.xml",
"template": "android/colors"
}]

After:
"files": [{
"destination": "font_dimens.xml",
"format": "android/fontDimens"
},{
"destination": "colors.xml",
"format": "android/colors"
}]
`;

// 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.template) {
console.error(warningText);
cleanDir(file.destination, file.format.bind(file), platform, dictionary);
} else if (file.format) {
if (file.format) {
cleanDir(file.destination, file.format.bind(file), platform, dictionary);
} else {
throw new Error('Please supply a formatter')
Expand Down
28 changes: 1 addition & 27 deletions lib/cleanFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,9 @@ function cleanFiles(dictionary, platform) {
throw new Error('Build path must end in a trailing slash or you will get weird file names.')
}

var warningText = `
templates will be removed in Style Dictionary 3.0, please change your config to use formats.
This is an example of how to fix your config.js:

Before:
"files": [{
"destination": "font_dimens.xml",
"template": "android/fontDimens"
},{
"destination": "colors.xml",
"template": "android/colors"
}]

After:
"files": [{
"destination": "font_dimens.xml",
"format": "android/fontDimens"
},{
"destination": "colors.xml",
"format": "android/colors"
}]
`;

// 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.template) {
console.error(warningText);
cleanFile(file.destination, file.template.bind(file), platform, dictionary);
} else if (file.format) {
if (file.format) {
cleanFile(file.destination, file.format.bind(file), platform, dictionary);
} else {
throw new Error('Please supply a template or formatter')
Expand Down
6 changes: 1 addition & 5 deletions lib/register/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
* and limitations under the License.
*/

var fs = require('fs'),
_ = require('lodash'),
chalk = require('chalk');

/**
* Add a custom format to the style dictionary
* @static
Expand All @@ -38,7 +34,7 @@ function registerFormat(options) {
if (typeof options.name !== 'string')
throw new Error('transform name must be a string');
if (typeof options.formatter !== 'function')
throw new Error('format formatter must be a function');
throw new Error('format formatter must be a function');

this.format[options.name] = options.formatter;

Expand Down
4 changes: 2 additions & 2 deletions lib/register/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var fs = require('fs'),
/**
* Add a custom template to the Style Dictionary
* @static
* @deprecated registerTemplate will be removed in Style Dictionary 3.0, please use registerFormat
* @deprecated registerTemplate will be removed in the future, please use registerFormat
* @memberof module:style-dictionary
* @param {Object} template
* @param {String} template.name - The name of your template. You will refer to this in your config.json file.
Expand All @@ -41,7 +41,7 @@ function registerTemplate(options) {
throw new Error('Can\'t find template: ' + chalk.red(JSON.stringify(options.template)));

var warningText = `
registerTemplate will be removed in Style Dictionary 3.0, please migrate to registerFormat.
registerTemplate will be removed in the future, please migrate to registerFormat.
You can use any templating engine you would like, you just need to require/import it.
To continue using lodash templates, this is an example of a simple substitution:

Expand Down
25 changes: 18 additions & 7 deletions lib/transform/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ function transformConfig(config, dictionary) {
return dictionary.transform[name];
});

var warningText = `
templates will be removed in Style Dictionary 3.0, please change your config to use formats.
This is an example of how to fix your config.js:
var filesUsingTemplates = [];

var warningText = chalk.bold.red(`
Templates will be removed in the future, please change your config to use formats.
This is an example of how to fix your config.json:

Before:
"files": [{
Expand All @@ -69,13 +71,13 @@ function transformConfig(config, dictionary) {
"destination": "colors.xml",
"format": "android/colors"
}]
`;
`);

to_ret.files = _.map(config.files, function(file) {
if (file.template) {
console.error(warningText);
if (dictionary.template[file.template]) {
return _.extend({}, file, {template: dictionary.template[file.template]});
filesUsingTemplates.push(file);
if (dictionary.format[file.template]) {
return _.extend({}, file, {format: dictionary.format[file.template]});
} else {
throw new Error('Can\'t find template: ' + file.template);
}
Expand All @@ -97,6 +99,15 @@ function transformConfig(config, dictionary) {
return dictionary.action[action];
});

if(filesUsingTemplates.length) {
console.error(warningText);
console.error(chalk.bold.red('Your current build generated the following file' + (filesUsingTemplates.length>1 ? 's' : '') + ' using a template:'));
filesUsingTemplates.forEach(function(file) {
console.error(chalk.bold.red(file.destination + ' (template: ' + file.template + ')'));
});
console.error("\n\n");
}

return to_ret;
}

Expand Down
8 changes: 4 additions & 4 deletions scripts/handlebars/templates/formats.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ You can create custom formats using the [`registerFormat`](api.md#registerformat

A formatter is just a simple function and created easily with most templating engines. Templates are useful if there is a lot of boilerplate code to insert (e.g. ObjectiveC files). If the output consists of just the values (e.g. a flat SCSS variables file), writing a formatter function directly may be easier.

Any templating language can work as there is a node module for it. All you need to do is register a custom format which calls your template and returns a string.
Any templating language can work as there is a node module for it. All you need to do is register a format that calls your template and returns a string.

Here is a quick example for Lodash.
Here is a quick example where someone Lodash.

```js
const StyleDictionary = require('style-dictionary').extend('config.json');
const _ = require('lodash');

const template = _.template( fs.readFileSync('templates/MyTemplate.template') );
const template = _.template( fs.readFileSync('templates/sassMap.template') );

styleDictionary.registerFormat({
name: 'my/format',
name: 'sass/map',
formatter: template
});

Expand Down
2 changes: 1 addition & 1 deletion scripts/handlebars/templates/templates.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Templates

Templates are deprecated in favor of [Formats](formats.md) and will be removed in Style Dictionary 3.0. If you want to create a custom template, create a [Format that uses templating](formats.md).
Templates are deprecated in favor of [Formats](formats.md) and will be removed in the future. If you want to create a custom template, create a [Format that uses templating](formats.md#using-a-template-templating-engine-to-create-a-format).

>*__Why are Templates being deprecated in favor of Formats?__*

Expand Down