Skip to content

Generate the source based on custom inputs? #224

Closed
@matthewmueller

Description

@matthewmueller

Giving tachyons a try for a client project :-) The paddings were given to me and I needed to format tachyons to support the various sizes. This turned out to be tedious to do by hand, so I wrote a quick script to do it.

How do you feel about something that prompts for inputs and then writes the files? Maybe the media queries for other ones aren't so cut and dry, I'm not sure. But here's one for spacing (could easily add margins):

/**
 * Module Dependencies
 */

let tpl = require('colon-template')

/**
 * Prompts
 */

let base = 16
let px = [0, 12, 16, 24, 32, 40, 48, 56, 64, 96, 112, 128, 256]
let medias = {
  'ns': 'screen and (min-width: 48em)',
  'm': 'screen and (min-width: 48em) and (max-width: 64em)',
  'l': 'screen and (min-width: 64em)'
}

/**
 * Computed
 */

let rems = sizes.map(size => size / base)
let directions = {
  all: 'pa',
  left: 'pl',
  right: 'pr',
  top: 'pt',
  bottom: 'pb',
  vertical: 'pv',
  horizontal: 'ph'
}

let rules = {
  'all': '{ padding: :valuerem; }',
  'left': '{ padding-left: :valuerem; }',
  'right': '{ padding-right: :valuerem; }',
  'top': '{ padding-top: :valuerem; }',
  'bottom': '{ padding-bottom: :valuerem; }',
  'vertical': '{ padding-top: :valuerem; padding-bottom: :valuerem; }',
  'horizontal': '{ padding-left: :valuerem; padding-right: :valuerem; }',
}

/**
 * Write
 */

for (let direction in directions) {
  let selector = directions[direction]
  console.log()
  console.log(`/* ${direction} */`)
  rems.forEach(function (value, i) {
    console.log(`.${selector}${i}  ${tpl(rules[direction])({ value })}`)
  })
}

for (let suffix in medias) {
  let media = medias[suffix]
  console.log(`@media ${media} {`)
  for (let direction in directions) {
    let selector = directions[direction]
    console.log()
    rems.forEach(function (value, i) {
      console.log(`  .${selector}${i}-${suffix}  ${tpl(rules[direction])({ value })}`)
    })
  }
  console.log('}')
}

Usage:

node spacing.js > spacing.css

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions