Skip to content

Commit 38eca29

Browse files
committed
Fully implement and test config/generator/generate/write
1 parent 7ef4fce commit 38eca29

7 files changed

+41
-73
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ GIT
77

88
GIT
99
remote: https://github.com/foobara/files-generator.git
10-
revision: 8e9439014484892bb49a1d720399d924f853baea
10+
revision: e51ee030f697e7e7669df7ad329c407b3acead11
1111
specs:
1212
foobara-files-generator (0.1.0)
1313

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
RSpec.describe Foobara::Generators::EmptyTypescriptReactProjectGenerator::GenerateEmptyTypescriptReactProject do
2-
let(:project_dir) { "SomePrefix::SomeOrg" }
2+
let(:project_dir) { "test-project" }
33

44
let(:inputs) do
55
{
6-
project_dir:,
7-
description: "whatever"
6+
project_dir:
87
}
98
end
109
let(:empty_typescript_react_project) { described_class.new(inputs) }
@@ -14,8 +13,6 @@
1413
it "generates a empty_typescript_react_project" do
1514
expect(outcome).to be_success
1615

17-
empty_typescript_react_project_file = result["src/some_prefix/some_org.rb"]
18-
expect(empty_typescript_react_project_file).to include("module SomeOrg")
19-
expect(empty_typescript_react_project_file).to include("module SomePrefix")
16+
expect(result.keys).to contain_exactly("tsconfig.json", ".env", ".eslintrc.js")
2017
end
2118
end

spec/foobara/generators/write_empty_typescript_react_project_to_disk_spec.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RSpec.describe Foobara::Generators::EmptyTypescriptReactProjectGenerator::WriteEmptyTypescriptReactProjectToDisk do
22
let(:command) { described_class.new(inputs) }
3-
let(:outcome) { empty_typescript_react_project.run }
3+
let(:outcome) { command.run }
44
let(:result) { outcome.result }
55
let(:errors) { outcome.errors }
66
let(:inputs) do
@@ -11,26 +11,22 @@
1111
end
1212
let(:empty_typescript_react_project_config) do
1313
{
14-
project_dir:,
15-
description: "whatever"
14+
project_dir:
1615
}
1716
end
18-
let(:project_dir) { "SomeOrg" }
19-
let(:output_directory) { "#{__dir__}/../../../tmp/empty_typescript_react_project_test_suite_output" }
17+
let(:project_dir) { "test-project" }
18+
let(:output_directory) { "#{__dir__}/../../../tmp/rspec-output" }
2019

2120
before do
22-
# rubocop:disable RSpec/AnyInstance
23-
allow_any_instance_of(described_class).to receive(:git_commit).and_return(nil)
24-
allow_any_instance_of(described_class).to receive(:rubocop_autocorrect).and_return(nil)
25-
# rubocop:enable RSpec/AnyInstance
21+
allow(command).to receive_messages(git_commit: nil, rubocop_autocorrect: nil)
2622
FileUtils.rm_rf output_directory
2723
end
2824

2925
describe "#run" do
3026
it "contains base files" do
3127
expect(outcome).to be_success
3228

33-
expect(command.paths_to_source_code.keys).to include("src/some_org.rb")
29+
expect(File.exist?("#{output_directory}/#{project_dir}/.eslintrc.js")).to be(true)
3430
end
3531
end
3632

@@ -44,7 +40,7 @@
4440

4541
it "writes files to the current directory" do
4642
command.cast_and_validate_inputs
47-
expect(command.output_directory).to eq(".")
43+
expect(command.output_parent_directory).to eq(".")
4844
end
4945
end
5046
end

src/empty_typescript_react_project_config.rb

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,6 @@ module EmptyTypescriptReactProjectGenerator
66
class EmptyTypescriptReactProjectConfig < Foobara::Model
77
attributes do
88
project_dir :string, :required
9-
description :string, :allow_nil
10-
end
11-
12-
attr_accessor :module_path
13-
14-
def initialize(attributes = nil, options = {})
15-
project_dir = attributes[:project_dir]
16-
description = attributes[:description]
17-
18-
module_path = project_dir.split("::")
19-
20-
super(
21-
{
22-
project_dir:,
23-
description:
24-
},
25-
options
26-
)
27-
28-
self.module_path = module_path
299
end
3010
end
3111
end

src/empty_typescript_react_project_generator.rb

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,15 @@ class << self
77
def manifest_to_generator_classes(manifest)
88
case manifest
99
when EmptyTypescriptReactProjectConfig
10-
[
11-
Generators::EmptyTypescriptReactProjectGenerator
12-
]
10+
# Nothing to do yet re: rendering templates. Everything is untemplated so far.
11+
[]
1312
else
1413
# :nocov:
1514
raise "Not sure how build a generator for a #{manifest}"
1615
# :nocov:
1716
end
1817
end
1918
end
20-
21-
def template_path
22-
["src", "organization.rb.erb"]
23-
end
24-
25-
def target_path
26-
*path, file = module_path.map { |part| Util.underscore(part) }
27-
28-
file = "#{file}.rb"
29-
30-
["src", *path, file]
31-
end
32-
33-
alias empty_typescript_react_project_config relevant_manifest
34-
35-
def templates_dir
36-
"#{__dir__}/../templates"
37-
end
3819
end
3920
end
4021
end

src/generate_empty_typescript_react_project.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ class MissingManifestError < RuntimeError; end
1313
inputs EmptyTypescriptReactProjectConfig
1414

1515
def execute
16+
include_non_templated_files
17+
1618
add_initial_elements_to_generate
1719

1820
each_element_to_generate do
21+
# currently there are no templated files to write and so this code path isn't hit
22+
# :nocov:
1923
generate_element
24+
# :nocov:
2025
end
2126

2227
paths_to_source_code
@@ -30,10 +35,7 @@ def base_generator
3035

3136
# TODO: delegate this to base_generator
3237
def templates_dir
33-
# TODO: implement this?
34-
# :nocov:
3538
"#{__dir__}/../templates"
36-
# :nocov:
3739
end
3840

3941
def add_initial_elements_to_generate

src/write_empty_typescript_react_project_to_disk.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def generator_key
1515
inputs do
1616
empty_typescript_react_project_config EmptyTypescriptReactProjectConfig, :required
1717
# TODO: should be able to delete this and inherit it
18-
output_directory :string
18+
output_directory :string, default: "."
1919
end
2020

2121
def execute
@@ -27,12 +27,19 @@ def execute
2727
stats
2828
end
2929

30+
# A bit confusing... we need to write the files to the output_directory/project_dir
31+
# and the code that writes the generated files assumes that output_directory contains the place
32+
# to write the files not the place to initiate the project
3033
def output_directory
31-
inputs[:output_directory] || default_output_directory
34+
project_directory
3235
end
3336

34-
def default_output_directory
35-
"."
37+
def output_parent_directory
38+
inputs[:output_directory]
39+
end
40+
41+
def project_directory
42+
"#{output_parent_directory}/#{empty_typescript_react_project_config.project_dir}"
3643
end
3744

3845
def generate_file_contents
@@ -48,8 +55,11 @@ def run_pre_generation_tasks
4855
def run_npx_create_react_app
4956
puts "created empty project with create-react-app..."
5057

51-
Dir.chdir output_directory do
52-
cmd = "npx create-react-app --template typescript whatever-frontend"
58+
cmd = "npx create-react-app --template typescript #{empty_typescript_react_project_config.project_dir}"
59+
60+
FileUtils.mkdir_p output_parent_directory
61+
62+
Dir.chdir output_parent_directory do
5363
run_cmd_and_write_output(cmd)
5464
end
5565
end
@@ -65,18 +75,20 @@ def add_necessary_dev_dependencies_for_eslint
6575
"eslint-plugin-promise@^6.1.1 " \
6676
"typescript@^4.0.0 "
6777

68-
run_cmd_and_write_output(cmd)
78+
Dir.chdir project_directory do
79+
run_cmd_and_write_output(cmd)
80+
end
6981
end
7082

7183
def run_post_generation_tasks
72-
Dir.chdir output_directory do
73-
eslint_fix
74-
end
84+
eslint_fix
7585
end
7686

7787
def eslint_fix
7888
cmd = "npx eslint 'src/**/*.{js,jsx,ts,tsx}' --fix"
79-
run_cmd_and_write_output(cmd)
89+
Dir.chdir project_directory do
90+
run_cmd_and_write_output(cmd)
91+
end
8092
end
8193
end
8294
end

0 commit comments

Comments
 (0)