@gravityforms/design-tokens
Version:
Design tokens as PostCSS variables, media queries, and mixins for Gravity Forms development.
124 lines (111 loc) • 2.79 kB
JavaScript
const fs = require( 'fs' );
const pathUtil = require( 'path' );
/**
* @function untrailingSlashIt
* @description Remove trailing slash from a string.
*
* @param {string} str The string to remove the trailing slash from.
*/
function untrailingSlashIt( str ) {
return str.replace( /\/$/, '' );
}
module.exports = {
createCssRules(
data = {},
ruleProperty = '',
nameReplaceSearch = '',
nameReplaceValue = '',
projectSlug = 'gform',
) {
return Object.entries( data ).map( ( [ name, value ] ) => {
const className = nameReplaceSearch ? name.replace( nameReplaceSearch, nameReplaceValue ) : name;
return ` .${ projectSlug }-util${ className.substring( 1 ) } {
${ ruleProperty }: ${ value };
}
`;
} ).join( '' );
},
cssHeader(
fileName = '',
title = '',
message = ''
) {
return `/*
* ${ title }
*
* Used in ${ fileName }.
*
* ${ message }
*/
`;
},
rootCssHeader(
fileName = '',
title = ''
) {
return `/*
----------------------------------------------------------------
${ fileName }
${ title }
https://www.gravityforms.com
Gravity Forms is a Rocketgenius project
copyright 2008-2023 Rocketgenius Inc.
https://www.rocketgenius.com
this may not be re-distributed without the
express written permission of the author.
NOTE: DO NOT EDIT THIS FILE!
THIS FILE IS REPLACED DURING AUTO UPGRADE
AND ANY CHANGES MADE HERE WILL BE OVERWRITTEN.
----------------------------------------------------------------
*/
`;
},
trailingSlashIt( str ) {
return untrailingSlashIt( str ) + '/';
},
writeCssFile( {
content = '',
data = {},
fileName = '',
headerFileName = '',
headerTitle = '',
headerMessage = '',
nameReplaceSearch = '',
nameReplaceValue = '',
path = '',
projectSlug = 'gform',
root = true,
ruleProperty = '',
scope = 'admin',
} ) {
const header = root ? module.exports.rootCssHeader : module.exports.cssHeader;
let css = `${ header( headerFileName, headerTitle, headerMessage ) }`;
if ( ! root ) {
css += `
.${ projectSlug }-${ scope } {
`;
}
css += root ? content : module.exports.createCssRules( data, ruleProperty, nameReplaceSearch, nameReplaceValue, projectSlug );
if ( ! root ) {
css += `}
`;
}
const filePath = `${ module.exports.trailingSlashIt( path ) }${ fileName }`;
const dirPath = pathUtil.dirname( filePath );
// create directory if it doesn't exist
fs.mkdir( dirPath, { recursive: true }, ( err ) => {
if ( err ) {
console.error( `Problem creating directory ${ dirPath }: ${ err }` );
return;
}
// write file
fs.writeFile( filePath, css, ( error ) => {
if ( err ) {
console.error( `Problem writing ${ fileName } to ${ path }: ${ error }` );
return;
}
console.log( `${ fileName } written to ${ path }` );
} );
} );
},
};