npm start hangs on React Native boilerplate Expo app.

Followed all the documentation to get this booted

Looking for JS files in
   /Users/cex/repos/GamingVideos 

Loading dependency graph, done.

It just hangs… This is apparently no that uncommon https://github.com/facebook/react-native/issues/25650 One suggestion is that node >= 12.5 has memory issues and that downgrading can help. I’m one of those people that tries to push the latest version of everthing and if you’ve ever dealt with huge amounts of legacy dependencies in Rails apps with lots of gem you’d probably agree this is mostly sane.

Not sure how you downgrade using Brew https://medium.com/@georgeenathomas/3-step-process-to-downgrade-node-version-using-homebrew-bc0b0a72ae27

New-MacBook-Pro:GamingVideos cex$ brew search node
==> Formulae
leafnode            node ✔              node@12             nodebrew
libbitcoin-node     node-build          node@8              nodeenv
llnode              node@10             node_exporter       nodenv

==> Casks
nodebox                    nodeclipse                 soundnode

If you meant "node" specifically:
true

Seems like I have version 12 installed.

New-MacBook-Pro:GamingVideos cex$ brew unlink node
Unlinking /usr/local/Cellar/node/13.1.0... 7 symlinks removed
New-MacBook-Pro:GamingVideos cex$ brew link node@12
Error: No such keg: /usr/local/Cellar/node@12

Well perhaps not…

New-MacBook-Pro:GamingVideos cex$ brew install node@12
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
bit                 ruby-build          swiftformat         vte3
gjs                 stellar-core        v8

==> Downloading https://homebrew.bintray.com/bottles/node@12-12.13.1.mojave.bott
==> Downloading from https://akamai.bintray.com/70/7080b09d842a2c05d7b61c2ac121f
######################################################################## 100.0%
==> Pouring node@12-12.13.1.mojave.bottle.tar.gz
==> Caveats
node@12 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have node@12 first in your PATH run:
  echo 'export PATH="/usr/local/opt/node@12/bin:$PATH"' >> ~/.bash_profile

For compilers to find node@12 you may need to set:
  export LDFLAGS="-L/usr/local/opt/node@12/lib"
  export CPPFLAGS="-I/usr/local/opt/node@12/include"

==> Summary
  /usr/local/Cellar/node@12/12.13.1: 4,225 files, 52.8MB

Now to relink it

New-MacBook-Pro:GamingVideos cex$ brew link node@12
Warning: node@12 is keg-only and must be linked with --force

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/node@12/bin:$PATH"' >> ~/.bash_profile
New-MacBook-Pro:GamingVideos cex$ brew link node@12 --force
Linking /usr/local/Cellar/node@12/12.13.1... 
Error: Could not symlink bin/npm
Target /usr/local/bin/npm
already exists. You may want to remove it:
  rm '/usr/local/bin/npm'

To force the link and overwrite all conflicting files:
  brew link --overwrite node@12

To list all files that would be deleted:
  brew link --overwrite --dry-run node@12

I’ve got to wing it here because I don’t know what is going to happen.

New-MacBook-Pro:GamingVideos cex$ brew link --overwrite node@12 --force
Linking /usr/local/Cellar/node@12/12.13.1... 3744 symlinks created

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/node@12/bin:$PATH"' >> ~/.bash_profile
New-MacBook-Pro:GamingVideos cex$ node -v
v12.13.1

Looks like it’s done something right…. ok same damn thing. I am now going to nuke the project re-install everything and hopefully this time…

npm install -g expo-cli
expo init GamingVideos
cd GamingVideos
yarn android

Yeah….um…no.

New-MacBook-Pro:GamingVideos cex$ yarn android
yarn run v1.19.2
$ react-native run-android
info Starting JS server...
info Building and installing the app on the device (cd android && ./gradlew app:installDebug)...
Downloading https://services.gradle.org/distributions/gradle-4.10.1-all.zip

Exception in thread "main" javax.net.ssl.SSLException: Received fatal alert: protocol_version

Ok forget it. No React Native for me today.

Update: After incidentally upgrading to the latest MacOSX, the error message changes

<New-MacBook-Pro:GamingVideos cex$ yarn android
yarn run v1.19.2
$ react-native run-android
info JS server already running.
info Building and installing the app on the device (cd android && ./gradlew app:installDebug)…
No Java runtime present, requesting install.
error Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
error Command failed: ./gradlew app:installDebug. Run CLI with —verbose flag for more details.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Ok, now I need to install a Java Runtime, there was clearly one here before but hey, if that solves it…

https://www.oracle.com/technetwork/java/javase/downloads/jdk13-downloads-5672538.html

New-MacBook-Pro:GamingVideos cex$ yarn android
yarn run v1.19.2
$ react-native run-android
info JS server already running.
info Building and installing the app on the device (cd android && ./gradlew app:installDebug)...
Downloading https://services.gradle.org/distributions/gradle-4.10.1-all.zip
...............................................................................................................
Unzipping /Users/cex/.gradle/wrapper/dists/gradle-4.10.1-all/455itskqi2qtf0v2sja68alqd/gradle-4.10.1-all.zip to /Users/cex/.gradle/wrapper/dists/gradle-4.10.1-all/455itskqi2qtf0v2sja68alqd
Set executable permissions for: /Users/cex/.gradle/wrapper/dists/gradle-4.10.1-all/455itskqi2qtf0v2sja68alqd/gradle-4.10.1/bin/gradle

Welcome to Gradle 4.10.1!

Here are the highlights of this release:
 - Incremental Java compilation by default
 - Periodic Gradle caches cleanup
 - Gradle Kotlin DSL 1.0-RC6
 - Nested included builds
 - SNAPSHOT plugin versions in the `plugins {}` block

For more details see https://docs.gradle.org/4.10.1/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)
cache fileHashes.bin (/Users/cex/repos/GamingVideos/android/.gradle/4.10.1/fileHashes/fileHashes.bin) is corrupt. Discarding.

> Configure project :app

Installing unimodules:
 unimodules-core@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/@unimodules/core
 unimodules-react-native-adapter@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/@unimodules/react-native-adapter
 expo-app-loader-provider@7.0.0 from /Users/cex/repos/GamingVideos/node_modules/expo-app-loader-provider
 expo-constants@7.0.0 from /Users/cex/repos/GamingVideos/node_modules/expo-constants
 expo-file-system@7.0.0 from /Users/cex/repos/GamingVideos/node_modules/expo-file-system
 expo-font@7.0.0 from /Users/cex/repos/GamingVideos/node_modules/expo-font
 expo-keep-awake@7.0.0 from /Users/cex/repos/GamingVideos/node_modules/expo-keep-awake
 expo-linear-gradient@7.0.0 from /Users/cex/repos/GamingVideos/node_modules/expo-linear-gradient
 expo-location@7.0.0 from /Users/cex/repos/GamingVideos/node_modules/expo-location
 expo-permissions@7.0.0 from /Users/cex/repos/GamingVideos/node_modules/expo-permissions
 expo-sqlite@7.0.0 from /Users/cex/repos/GamingVideos/node_modules/expo-sqlite
 expo-web-browser@7.0.1 from /Users/cex/repos/GamingVideos/node_modules/expo-web-browser
 unimodules-barcode-scanner-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-barcode-scanner-interface
 unimodules-camera-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-camera-interface
 unimodules-constants-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-constants-interface
 unimodules-face-detector-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-face-detector-interface
 unimodules-file-system-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-file-system-interface
 unimodules-font-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-font-interface
 unimodules-image-loader-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-image-loader-interface
 unimodules-permissions-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-permissions-interface
 unimodules-sensors-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-sensors-interface
 unimodules-task-manager-interface@4.0.0 from /Users/cex/repos/GamingVideos/node_modules/unimodules-task-manager-interface

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 45s
error Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
error Command failed: ./gradlew app:installDebug. Run CLI with --verbose flag for more details.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Maybe this can help.

https://stackoverflow.com/questions/32634352/react-native-android-build-failed-sdk-location-not-found

Ok, followed that, now getting a error to do with missing licences. I couldn’t see a way on forcing licences to install. and checking in Android Studio I can see that Android 10.0 (Q) was installed. I selected Android 9.0 (Pie) just to see if it forced me to click yes on a licence agreement.

It didn’t but now yarn android on my project is doing something more.

Checking the license for package Android SDK Build-Tools 28.0.3 in /Users/cex/Library/Android/sdk/licenses
License for package Android SDK Build-Tools 28.0.3 accepted.
Preparing "Install Android SDK Build-Tools 28.0.3 (revision: 28.0.3)".
"Install Android SDK Build-Tools 28.0.3 (revision: 28.0.3)" ready.
Installing Android SDK Build-Tools 28.0.3 in /Users/cex/Library/Android/sdk/build-tools/28.0.3
"Install Android SDK Build-Tools 28.0.3 (revision: 28.0.3)" complete.
"Install Android SDK Build-Tools 28.0.3 (revision: 28.0.3)" finished.

But then…

> Task :app:installDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:installDebug'.
> com.android.builder.testing.api.DeviceException: No connected devices!

Ok, so let’s start again on a different machine…and this is not making me happy. Documentation also says you can use npm start or expo start

Ok, so it was hanging because it needed permissions. Works. There was other wise nothing wrong at all. I love technology, it makes me happy.

Deploying application that includes Fog

I’ve deployed this app so many times and this time we’re missing libxml2

/home/rails/.rvm/rubies/ruby-2.3.0/bin/ruby -r
./siteconf20191124-17839-153ycdh.rb extconf.rb
checking for xml2-config... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/home/rails/.rvm/rubies/ruby-2.3.0/bin/$(RUBY_BASE_NAME)
	--with-libxml2-config
	--without-libxml2-config
	--with-pkg-config
	--without-pkg-config
extconf.rb:29:in `<main>': The "libxml2" package isn't available. (RuntimeError)

I’m not sure why this has happened now but.

sudo apt-get install libxml2-dev

And now libcurl is missing.

/home/rails/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20191124-680-1dnvkzc.rb
extconf.rb
checking for xml2-config... yes
checking for curl-config... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/home/rails/.rvm/rubies/ruby-2.3.0/bin/$(RUBY_BASE_NAME)
	--with-libcurl-config
	--without-libcurl-config
	--with-pkg-config
	--without-pkg-config
extconf.rb:40:in `<main>': The "libcurl" package isn't available. (RuntimeError)

Not quite as simple a library to install apparently Ubuntu has got three different flavours. https://packages.ubuntu.com/bionic/libcurl-dev

sudo apt-get install libcurl4-openssl-dev

Deploying now…

I'm not angry but...

I spent much of yesterday trying to Paperclip working with some S3 extensions that I’ve used in countless projects and it just didn’t work. If you’re interested, well, here’s a slice of it.

module Paperclip
  module Storage
    S3.prepend(Module.new do
      def url(style_name = default_style, options = {})
        with_environment { super }
      end

      def expiring_url(time = 3600, style_name = default_style)
        with_environment { super }
      end
    end)
  end

  module Storage
    module S3
      def download_url(time = 3600, style_name = default_style)
        with_content_disposition("attachment; filename=#{instance_read(:file_name).without_extended_characters}") do
          expiring_url(time, style_name)
        end
      end

      def inline_url(time = 3600, style_name = default_style)
        with_content_disposition('inline') do
          expiring_url(time, style_name)
        end
      end

      private def with_content_disposition(disposition)
        @options[:s3_url_options] ||= {}
        @options[:s3_url_options][:response_content_disposition] = "attachment; filename=#{instance_read(:file_name).without_extended_characters}"
        yield
      ensure
        @options[:s3_url_options].delete(:response_content_disposition)
        @options.delete(:s3_url_options) if @options[:s3_url_options].empty?
      end

      # overrides s3 credentials to serve production assets when in
      # staging or development
      private def with_environment
        # only override if dev or staging
        return yield unless Rails.env.development? || Rails.env.staging?
        # only override if updated in the last day
        return yield if (instance_read(:updated_at) || 2.days.ago) > 1.day.ago

        begin
          @s3_interface = nil
          @bucket = nil
          @s3_bucket = nil
          @s3_credentials = parse_credentials(Settings.get(:s3, 'production-readonly')[:s3_credentials])

          yield
        ensure
          @s3_interface = nil
          @bucket = nil
          @s3_bucket = nil
          @s3_credentials = parse_credentials(Settings.get(:s3)[:s3_credentials])
        end
      end
    end
  end
end

With Settings to extract settings and cope with overriding different environments. Say, to display production images in development using the above. I’ve found it very useful.

module Settings
  def self.get(name, environment = Rails.env.to_s)
    fetch("#{name}.yml")[environment]
  end

  def self.fetch(config)
    yaml = paths(config).inject([]) do |yamls, path|
      yamls << IO.read(path)
    rescue Errno::ENOENT
      yamls
    end.reject(&:blank?).join("\n")

    if Rails.env.production? && yaml.include?('&defaults') && !yaml.include?("production:\n")
      yaml += "\nproduction:\n  <<: *defaults"
    end

    YAML.load(ERB.new(yaml).result)
  end

  def self.paths(config)
    [
      Rails.root.join('config', 'settings', config), # repo
      File.join(ENV['HOME'], application, 'shared', 'settings', config), # server
      File.join(ENV['HOME'], '.settings', application, config) # local
    ]
  end

  def self.application
    Rails.application.class.name.split('::')[0].underscore
  end
end

Except for yesterday when I forgot or didn’t realise that the storage option in Paperclip needs to be a symbol. If it’s a string it gets ignored.

def self.get(name, environment = Rails.env.to_s)
    fetch("#{name}.yml")[environment].deep_symbolize_keys # <---- hours wasted
  end

And finally the methods that extend Attachment::Storage::S3 don’t appear because it defaults to :filesystem unless you supply :s3 as a symbol. Ok, finally I am there… No.

AWS::S3::Errors::AccessDenied Access Denied

But I’ve added the Bucket Policy to use my IAM user, and set the correct permissions. I know this because I compared it to another policy I set up a week prior.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::1234567890:user/username"
            },
            "Action": [
                "s3:ListBucket",
                "s3:GetBucketPolicy",
                "s3:PutBucketPolicy"
            ],
            "Resource": "arn:aws:s3:::bucketname"
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::1234567890:user/username"
            },
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:DeleteObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::bucketname/*"
        }
    ]
}

Files didn’t upload at all. S3 credentials are correct. But still the same error. As then as it turns out, S3 buckets can be on this occasion was set to Bucket and Objects not public. So eventually I realise that the Paperclip upload default is :public and if I am trying to do that then I get AWS::S3::Errors::AccessDenied Access Denied sigh

defaults: &defaults
  storage: :s3
  bucket: bucket
  s3_protocol: :https
  s3_permissions: :private
  s3_credentials:
    access_key_id: accesskey
    secret_access_key: secretaccesskey
    s3_region: us-east-1

development:
  <<: *defaults

production:
  <<: *defaults
  bucket: bucket

So finally…at 2am. This is where we are. I enjoy the chase, I also enjoy sleep.

We need a name for our new board game

We painstakingly put together this prototype in just under 10 mins.

Ruby 1.8.7 and Rails 1.2

We have recently been looking at hiring so I created a stupid question just to get a developer talking. When I first started working with Rails we were building Euro-reg.com with Ruby 1.8.6 and Rails 1.2.3 as far as I can recall. So the question ended up being…

Suppose we hired you and on day one said we have this Rails 1.2 Application on Ruby 1.8.7, using SQLite in production that is mission critical. How would you approach bringing it up to date?

Some feedback we got was “…Rails v1.0—2.1 are incompatible with Ruby v1.8.7…” which made me question my stupid question. Actually it’s been so long I can’t even remember. So 10 minutes later.

The flow was

That was nicely quick, I guess it could have been horrible.

Want to find files with exactly the right size

Have you ever come across some files in your application that just appear to be generated by err…..a generator and actually do nothing. Well I find a couple of javascript files with the same content (and therefore the same size) and wondered if there were any more.

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

Well fortunately you can use find with a specific option

Robs-MBP:blah rl$ find app/assets/ -size 229c
app/assets//javascripts/some.js.coffee

Excellent, I can ditch that file too then.

HAML whitespace

I came across a weird HAML syntax in a client codebase that I was just confused about.

Haml::Engine.new(%q{
- title = '1'
.title== #{title}<br/>
}).to_html
=> <div class='title'>1<br/></div>

I found an alternative syntax, I am not sure I like it but it’s certainly better than the above.

Haml::Engine.new(%q{
- title = '1'
.title<
 #{title}
 %br>
}).to_html
=> <div class='title'>1<br></div>

Basically you can kill whitespace with < >

Bundle deprecations

If you want to search your bundle for anything in particular, in this case the deprecated Digest::Digest syntax

New-MacBook-Pro:that cex$ bundle show --paths | xargs grep -r Digest::Digest
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/aws-sdk-1.6.4/lib/aws/core/signer.rb:        OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new(digest), key, value)
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/fog-1.4.0/lib/fog/core/hmac.rb:      @digest = OpenSSL::Digest::Digest.new('sha1')
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/fog-1.4.0/lib/fog/core/hmac.rb:        @digest = OpenSSL::Digest::Digest.new('sha256')
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/fog-1.4.0/lib/fog/cloudstack.rb:    @@digest  = OpenSSL::Digest::Digest.new('sha1')
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_20/objects/root.dat matches
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_20/object_types matches
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_19/objects/root.dat matches
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_19/object_types matches

Everything you need in life

I’ve thought about this plenty and I’m more convinced than ever that the list of things you need in your life are

If there any suggestions on things to add to this list I will consider them but probably not too thoroughly.

When Postgresql doesnt start on OSX

New-MacBook-Pro:mini-epic cex$ ./bin/server 
=> Booting Puma
=> Rails 4.2.1 application starting in development on http://0.0.0.0:8001
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/cex/.rvm/gems/ruby-2.2.2@epic-invite/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize': could not connect to server: No such file or directory (PG::ConnectionBad)
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

I tried restarting with brew

New-MacBook-Pro:mini-epic cex$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Same deal. Thanks to https://stackoverflow.com/questions/13410686/postgres-could-not-connect-to-server Remove the PID and you’re sorted.

-rw-------  1 cex  admin  88  2 Mar 15:02 /usr/local/var/postgres/postmaster.pid
New-MacBook-Pro:mini-epic cex$ rm /usr/local/var/postgres/postmaster.pid
New-MacBook-Pro:mini-epic cex$ brew services start postgresql
Service `postgresql` already started, use `brew services restart postgresql` to restart.
New-MacBook-Pro:mini-epic cex$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)