@edirect/frontend-foundations
Advanced tools
Comparing version 0.0.71 to 0.0.72
@@ -14,2 +14,3 @@ "use strict"; | ||
headings: { | ||
h0: 'h1', | ||
h1: 'h1', | ||
@@ -66,3 +67,3 @@ h1Light: 'h1', | ||
const renderTypography = localChildren => /*#__PURE__*/_react.default.createElement(S.TyporaphyDefault, { | ||
as: elements[type][variant] || elements[type], | ||
as: elements[type][variant] || elements[type] || 'span', | ||
className: className, | ||
@@ -69,0 +70,0 @@ type: type, |
#! /usr/bin/env node | ||
export {}; | ||
export declare const generateTheme: (tokensFile: object, pathToSave?: string) => Promise<object | undefined>; |
@@ -5,9 +5,8 @@ #! /usr/bin/env node | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _tokensTemplate = require("./tokens.template.js"); | ||
var _themeTemplate = require("./theme.template.js"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
const args = process.argv; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.generateTheme = void 0; | ||
var _factory = require("./factory.js"); | ||
var _utils = require("./utils.js"); | ||
const ignoreObjectProperties = ['type', 'value']; | ||
@@ -17,36 +16,9 @@ const ignoreLeafFields = ['type', 'description']; | ||
const object = {}; | ||
const shadows = {}; | ||
const toCamelCase = (unformattedWord = '') => { | ||
if (!unformattedWord.includes('-') || unformattedWord.startsWith('-') && unformattedWord.match(/[0-9]+/g)) return unformattedWord; | ||
return unformattedWord.toLowerCase().replace(/[^a-zA-Z0-9{]+(.)/g, (_, chr) => chr.toUpperCase()); | ||
}; | ||
const format = (word = '') => { | ||
const ignoreObjectPropertiesFormatted = ignoreObjectProperties.map(prop => `${prop}\\.+`); | ||
const propertiesToIgnoreRegex = new RegExp(ignoreObjectPropertiesFormatted.join('|'), 'gi'); | ||
const formattedWord = word.replace(propertiesToIgnoreRegex, '').replace(/\.value$/gi, '').split('.').map(dotSeparatedWord => toCamelCase(dotSeparatedWord)).join('.'); | ||
const formattedWord = word.replace(propertiesToIgnoreRegex, '').replace(/\.value$/gi, '').split('.').map(dotSeparatedWord => (0, _utils.toCamelCase)(dotSeparatedWord)).join('.'); | ||
return formattedWord; | ||
}; | ||
const stringToObject = (key = '', value) => { | ||
const propertiesObject = key.split('.').reduceRight((acc, currentVal) => { | ||
return { | ||
[currentVal]: acc | ||
}; | ||
}, value); | ||
return propertiesObject; | ||
}; | ||
const mergeRecursive = (object1, object2) => { | ||
for (const p in object2) { | ||
try { | ||
if (object2[p].constructor == Object) { | ||
object1[p] = mergeRecursive(object1[p], object2[p]); | ||
} else { | ||
object1[p] = object2[p]; | ||
} | ||
} catch (e) { | ||
object1[p] = object2[p]; | ||
} | ||
} | ||
return object1; | ||
}; | ||
const generateTheme = (currentObject, property = '') => { | ||
const parseThemeFile = (currentObject, property = '') => { | ||
if (typeof currentObject === 'string') { | ||
@@ -65,94 +37,22 @@ if (ignoreObjectProperties.includes(currentObject)) { | ||
} | ||
return generateTheme(currentObject[currentObjectKey], property ? `${property}.${currentObjectKey}` : currentObjectKey); | ||
return parseThemeFile(currentObject[currentObjectKey], property ? `${property}.${currentObjectKey}` : currentObjectKey); | ||
}).join(''); | ||
}; | ||
const saveFile = (dir, object, generateFunction, generateIndexFunction) => { | ||
const pathToSave = args[4]; | ||
const keys = Object.keys(object); | ||
const dirPath = `${pathToSave}/${dir}`; | ||
keys.forEach(key => { | ||
if (!_fs.default.existsSync(dirPath)) { | ||
_fs.default.mkdirSync(dirPath, { | ||
recursive: true | ||
}); | ||
} | ||
_fs.default.writeFileSync(`${dirPath}/${key}.ts`, generateFunction(key, stringToReferenceObject(JSON.stringify(object[key])), keys)); | ||
}); | ||
_fs.default.writeFileSync(`${dirPath}/index.ts`, generateIndexFunction(keys)); | ||
}; | ||
const stringToReferenceObject = (object = '') => { | ||
return object.replace(/"\{|\}"/g, ''); | ||
}; | ||
const formatEffectString = object => { | ||
return `${object.x} ${object.y} ${object.blur} ${object.spread} ${object.color}`; | ||
}; | ||
const formatEffects = object => { | ||
Object.keys(object).map(elevation => { | ||
Object.keys(elevation).map(() => { | ||
if (isNaN(parseInt(Object.keys(object[elevation])[0]))) { | ||
shadows[elevation] = formatEffectString(object[elevation]); | ||
return; | ||
} | ||
shadows[elevation] = Object.entries(object[elevation]).map(value => formatEffectString(value[1])).join(', '); | ||
}); | ||
}); | ||
return shadows; | ||
}; | ||
(async function run() { | ||
const generateTheme = async (tokensFile, pathToSave) => { | ||
try { | ||
const pathToTokensFile = args[3]; | ||
const file = await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(pathToTokensFile); | ||
Object.keys(file).filter(key => !key.startsWith('$')).map(key => generateTheme(file[key])); | ||
Object.keys(tokensFile).filter(key => !key.startsWith('$')).map(key => parseThemeFile(tokensFile[key])); | ||
Object.keys(auxTheme).map(property => { | ||
// remove fontFamily from old components that still have tokens for it | ||
if (property.match(/(comp.|typography.).+(fontFamily)/)) return; | ||
mergeRecursive(object, stringToObject(property, auxTheme[property])); | ||
(0, _utils.mergeRecursive)(object, (0, _utils.stringToObject)(property, auxTheme[property])); | ||
}); | ||
const tokens = { | ||
color: object.ref.color, | ||
effect: { | ||
elevation: formatEffects(object.ref.elevation) | ||
}, | ||
spacing: { | ||
padding: object.ref.padding, | ||
flexbox: object.ref.flexbox, | ||
borderRadius: object.ref.borderRadius, | ||
borderWidth: object.ref.borderWidth | ||
}, | ||
typography: { | ||
fontFamily: object.ref.fontFamily, | ||
fontWeight: object.ref.fontWeight, | ||
letterSpacing: object.ref.letterSpacing, | ||
fontSize: object.ref.fontSize, | ||
lineHeight: object.ref.lineHeight, | ||
textDecoration: object.ref.textDecoration, | ||
textCase: object.ref.textCase, | ||
paragraphSpacing: object.ref.paragraphSpacing | ||
} | ||
}; | ||
const theme = { | ||
colors: { | ||
content: object.sys.content, | ||
contentState: object.sys.contentState, | ||
background: object.sys.background, | ||
backgroundState: object.sys.backgroundState, | ||
border: object.sys.border, | ||
borderState: object.sys.borderState | ||
}, | ||
effects: { | ||
shadow: object.sys.boxShadow | ||
}, | ||
spacing: { | ||
borderRadius: object.sys.borderRadius, | ||
borderWidth: object.sys.borderWidth | ||
}, | ||
typography: object.sys.typography, | ||
components: object.comp | ||
}; | ||
saveFile('tokens', tokens, _tokensTemplate.generateTokenTemplate, _tokensTemplate.generateTokenIndexFile); | ||
saveFile('theme', theme, _themeTemplate.generateThemeTemplate, _themeTemplate.generateThemeIndexFile); | ||
const themeGenerator = (0, _factory.generatorFactory)(object, pathToSave); | ||
const result = await themeGenerator.run(); | ||
console.log('Theme generated successfully.'); | ||
return result; | ||
} catch (error) { | ||
console.error('Error while generating the theme: ', error); | ||
return; | ||
} | ||
})(); | ||
}; | ||
exports.generateTheme = generateTheme; |
@@ -9,2 +9,3 @@ "use strict"; | ||
var _prettierrc = _interopRequireDefault(require("../../../.prettierrc.json")); | ||
var _utils = require("./utils.js"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -15,13 +16,2 @@ const prettierConfig = { | ||
}; | ||
const replaceWordsMap = { | ||
boxShadow: 'shadow', | ||
'ref.': 'tokens.', | ||
'sys.': 'theme.' | ||
}; | ||
const formatObject = tokenObject => { | ||
const regex = new RegExp(Object.keys(replaceWordsMap).join('|'), 'gi'); | ||
return tokenObject.replaceAll(regex, matched => { | ||
return replaceWordsMap[matched]; | ||
}).replaceAll(/\.color|\.typography/gi, '').replaceAll(/\.[0-9]+([A-z]?)+|\.[A-z][+-]+/gi, word => `['${word.replace('.', '')}']`); | ||
}; | ||
const generateThemeTemplate = (tokenKey, tokenObject, keys) => { | ||
@@ -40,3 +30,3 @@ const filteredKeys = keys.filter(key => key !== 'components'); | ||
export const ${tokenKey} = ${formatObject(tokenObject)} | ||
export const ${tokenKey} = ${(0, _utils.formatObject)(tokenObject)} | ||
`, prettierConfig); | ||
@@ -43,0 +33,0 @@ }; |
{ | ||
"name": "@edirect/frontend-foundations", | ||
"version": "0.0.71", | ||
"version": "0.0.72", | ||
"description": "Frontend Foundations", | ||
@@ -21,3 +21,3 @@ "main": "dist/src/index.js", | ||
"bin": { | ||
"generate-theme": "./dist/src/tools/theme-generator/index.js" | ||
"generate-theme": "./dist/src/tools/theme-generator/cli.js" | ||
}, | ||
@@ -24,0 +24,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1356478
369
32135
2