Skip to content

Instantly share code, notes, and snippets.

@jeremywrowe
Last active March 13, 2016 01:19
Show Gist options
  • Save jeremywrowe/f4ee5937a9aafa38dd67 to your computer and use it in GitHub Desktop.
Save jeremywrowe/f4ee5937a9aafa38dd67 to your computer and use it in GitHub Desktop.
ember-cli - Improving TTA (Time To App)
#!/usr/bin/env ruby
require 'fileutils'
include FileUtils
template_dir = ARGV[0] or fail 'You must supply a template directory'
new_project_dir = ARGV[1] or fail 'You must supply a new project name'
template_dir = File.join(Dir.home, '.ember-cli-cache', template_dir)
fail "The template you are looking for does not exist '#{template_dir}'" unless File.exist? template_dir
fail "The project you are trying to create '#{new_project_dir}' already exists" if File.exist? new_project_dir
puts "Copying #{template_dir} to #{new_project_dir}"
cp_r template_dir, new_project_dir
capitalize = lambda do |input|
result = input.capitalize
result.gsub(/([-|_])(\w)/) { Regexp.last_match[2].upcase }
end
patterns = {}
patterns[template_dir] = new_project_dir
patterns[capitalize.call(template_dir)] = capitalize.call(new_project_dir)
patterns.each do |pre, post, _|
puts "Looking at pattern #{pre} replacing with #{post}"
puts '*' * 80
paths = `ag #{pre} --nocolor #{template_dir} -l`.split("\n").map(&:strip)
paths.each do |path|
new_file = path.gsub template_dir, new_project_dir
contents = File.read(new_file)
contents.gsub! pre, post
File.open(new_file, 'w') { |file| file.write contents }
puts "Updated #{new_file}"
end
puts
end

The following is a quick and dirty ruby script that was written to show some areas that I think are worth exploring. There is a decent time cost endured when typing ember new my-rad-app. This can be optimized a great deal. Furthermore the internet is typically required when generating a new ember app. This little hack attempts to reduce and remove those two problems.

The problems are solved by generating an ember app once by typing ember new template-app and then using that template-app as a template for all newly generated apps by

  • copying the original to the new app directory
  • replacing all original app name references with the new app name

Note This is not meant to be me complaining about, nor badgering what is available today in ember-cli. There has been a tremendous amount of effort put into ember-cli and I am forever grateful of that fact. This little snippet is more to collect thoughts and start down the path of fixing the problem.

ember new my-thing @ 60+ seconds https://dr.tt/view?d=5ono6hw226vqcas%2F2016-03-06%20at%209.32%20PM.png%2F

fast-ember template my-thing @ 3 seconds http://bit.ly/24KGtF5

The script requires a couple of things

  • ruby 2.0+
  • the_silver_searcher (ag)
  • the ruby script provided below (executable chmod a+x fast-ember)
  • an ember project for templating
  • cached apps must be in ~/.ember-cli-cache

note the current state of the script requires that all of this happens in the same directory. It is a POC ;)

@rtablada
Copy link

rtablada commented Mar 7, 2016

I've seen a similar approach taken with the laravel scaffolding command. But the thing is that the dependencies can get quite stagnant and may require further work when upgrading from version to version.

Let's say we created a project in ~/.ember-cli-template in an after-install hook for NPM install, well when would that get cleaned up?

There's arguments to be made about improving NPM caching or possibly even wrapping some of the deps as a provides using package.json?

@jeremywrowe
Copy link
Author

I totally agree there, I guess I should clarify. I am not suggesting that this be on by default. What if say there was a public interface like this: ember new my-rad-app --cache Where the first time it was ran and from then on out you get the speed benefit. I guess another thing to think about there is a way of refreshing the "cache"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment