From 609fe7b1ef2f54b0b3ea6b78a67e6629c184584c Mon Sep 17 00:00:00 2001 From: OKURA Masafumi Date: Thu, 7 Nov 2024 18:58:04 +0900 Subject: [PATCH] feat(env): Allow disabling Typelizer with Rails development We faced an issue which is resolved by disabling Typelizer. Disabling was hard when Rails env is development, so this commit changed env name from `TYPELIZER` to `DISABLE_TYPELIZER` and the logic for disabling. This commit also adds a description of this environment variable to README. --- README.md | 4 ++++ lib/tasks/generate.rake | 2 +- lib/typelizer.rb | 4 +++- spec/spec_helper.rb | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 78d6eb3..ee80be9 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,10 @@ When [Listen](https://github.com/guard/listen) is installed, Typelizer automatic Typelizer.listen = false ``` +### Disabling Typelizer + +Sometimes we want to use Typelizer only with manual generation. To disable Typelizer during development, we can set `DISABLE_TYPELIZER` environment variable to true. This doesn't affect manual generation. + ## Configuration ### Global Configuration diff --git a/lib/tasks/generate.rake b/lib/tasks/generate.rake index 19a74f4..591e139 100644 --- a/lib/tasks/generate.rake +++ b/lib/tasks/generate.rake @@ -3,7 +3,7 @@ namespace :typelizer do task generate: :environment do require "benchmark" - ENV["TYPELIZER"] = "true" + ENV["DISABLE_TYPELIZER"] = "false" puts "Generating TypeScript interfaces..." serializers = [] diff --git a/lib/typelizer.rb b/lib/typelizer.rb index ebed7b4..6c6e86b 100644 --- a/lib/typelizer.rb +++ b/lib/typelizer.rb @@ -26,7 +26,9 @@ module Typelizer class << self def enabled? - ENV["RAILS_ENV"] == "development" || ENV["RACK_ENV"] == "development" || ENV["TYPELIZER"] == "true" + return false if ENV["DISABLE_TYPELIZER"] == "true" || ENV["DISABLE_TYPELIZER"] == "1" + + ENV["RAILS_ENV"] == "development" || ENV["RACK_ENV"] == "development" || ENV["DISABLE_TYPELIZER"] == "false" end attr_accessor :dirs diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3bd4c3a..d6a6ed8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -ENV["TYPELIZER"] = "true" +ENV["DISABLE_TYPELIZER"] = "false" ENV["RAILS_ENV"] = "test" require File.expand_path("app/config/environment", __dir__)