DEV Community

Cover image for Unified component structure for my EmberJS projects
Michal Bryxí
Michal Bryxí

Posted on

4 1

Unified component structure for my EmberJS projects

Long living projects often times have a lot of experimental "styles" or "techniques" or even just "ad-hoc structure" in them. My projects are no exception.

Today I decided that I will force an unified structure for all the component files within said projects. My (currently) preferred way of nesting folders/files is: components/foo/bar/baz/index.{js,hbs}.

It requires ruby on your environment and setting COMPONENT_DIRECTORY variable. Hope this might help someone.

#!/usr/bin/env ruby
#
# Unifies component structure
#  - components/foo.hbs -> components/foo/index.hbs
#  - components/foo.js -> components/foo/index.js
#  - components/bar/template.hbs -> components/bar/index.hbs
#  - components/bar/component.js -> components/bar/index.js

COMPONENT_DIRECTORY='../your/app/or/addon/directory'

Dir.chdir(COMPONENT_DIRECTORY)

Dir.glob('./components/**/*.{hbs,js}').each do |entry|
  matches = %r{(.*)\/(.*)\.(hbs|js)$}.match(entry)
  next if matches[2] == 'index'

  is_weird_syntax = ((matches[2] == 'template') || (matches[2] == 'component'))

  new_folder = if is_weird_syntax
                 matches[1]
               else
                 File.join(matches[1], matches[2])
               end

  Dir.mkdir(new_folder) unless File.exist?(new_folder)
  new_file = File.join(new_folder, "index.#{matches[3]}")
  File.rename(entry, new_file)
  puts "#{entry} => #{new_file}"
end
Enter fullscreen mode Exit fullscreen mode

Photo by Ronaldo de Oliveira on Unsplash

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more