amzn/style-dictionary

View on GitHub
lib/common/templates/scss/map-deep.template

Summary

Maintainability
Test Coverage
<%
//
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

%>
<%
    // for backward compatibility we need to have the user explicitly hide it
    var showFileHeader = (this.options && this.options.hasOwnProperty('showFileHeader')) ? this.options.showFileHeader : true;
    if(showFileHeader) {
        var header = '';
        header += "/*\n  Do not edit directly";
        header += "\n  Generated on " + new Date().toUTCString();
        header += "\n*/\n";
        print(header);
    }

    print("\n");

    // output the list of tokens as Sass variables
    //
    _.each(allProperties, function(prop) {
        var output = '';
        output += '$' + prop.name + ': ' + (prop.attributes.category==='asset' ? '"'+prop.value+'"' : prop.value) + ' !default;'
        if(prop.comment) {
        output += ' // ' + prop.comment;
        }
        output += '\n';
        print(output);
    });

    print('\n');

    // output the list of tokens as a Sass nested map
    // (the values are pointing to the variables)
    //
    print(`$${this.mapName||'tokens'}: ${processJsonNode(properties, 0)};\n`);

    // recursive function to process a properties JSON node
    //
    function processJsonNode(obj, depth) {
        var output = '';
        if (obj.hasOwnProperty('value')) {
            // if we have found a leaf (a property with a value) append the value
            output += `$${obj.name}`;
        } else {
            // if we have found a group of properties, use the Sass group "(...)" syntax and loop -recursively- on the children
            output += '(\n'
            output += Object.keys(obj).map(function(newKey) {
            var newProp = obj[newKey];
            var indent = '  '.repeat(depth+1);
            return `${indent}'${newKey}': ${processJsonNode(newProp, depth + 1)}`;
            }).join(',\n');
            output += '\n' + '  '.repeat(depth) + ')';
        }
        return output;
    }
%>