Skip to content

Commit

Permalink
Merge pull request #868 from pivotal/add-yarn2-support-180578287
Browse files Browse the repository at this point in the history
FEAT: [#180578287] Add Yarn2 support
  • Loading branch information
xtreme-jason-smith authored Dec 16, 2021
2 parents 8b6a40e + 2a9c93e commit fcfe740
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
36 changes: 32 additions & 4 deletions lib/license_finder/package_managers/yarn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def possible_package_paths
end

def current_packages
cmd = "#{Yarn::SHELL_COMMAND}#{production_flag}"
cmd = "#{Yarn::SHELL_COMMAND}#{yarn1_production_flag}"
suffix = " --cwd #{project_path}" unless project_path.nil?
cmd += suffix unless suffix.nil?

Expand Down Expand Up @@ -39,7 +39,7 @@ def current_packages
end

def prepare
prep_cmd = "#{prepare_command}#{production_flag}"
prep_cmd = prepare_command.to_s
_stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(prep_cmd) }
return if status.success?

Expand All @@ -56,11 +56,33 @@ def package_management_command
end

def prepare_command
'yarn install --ignore-engines --ignore-scripts'
if yarn2_project?
yarn2_prepare_command
else
yarn1_prepare_command
end
end

private

def yarn2_prepare_command
"#{yarn2_production_flag}yarn install"
end

def yarn1_prepare_command
"yarn install --ignore-engines --ignore-scripts#{yarn1_production_flag}"
end

def yarn2_project?
Dir.chdir(project_path) do
version_string, stderr_str, status = Cmd.run('yarn -v')
raise "Command 'yarn -v' failed to execute: #{stderr_str}" unless status.success?

version = version_string.split('.').map(&:to_i)
return version[0] >= 2
end
end

def packages_from_json(json_data)
body = json_data['body']
head = json_data['head']
Expand Down Expand Up @@ -98,10 +120,16 @@ def filter_yarn_internal_package(all_packages)
all_packages - [yarn_internal_package]
end

def production_flag
def yarn1_production_flag
return '' if @ignored_groups.nil?

@ignored_groups.include?('devDependencies') ? ' --production' : ''
end

def yarn2_production_flag
return '' if @ignored_groups.nil?

@ignored_groups.include?('devDependencies') ? 'yarn plugin import workspace-tools && yarn workspaces focus --all --production && ' : ''
end
end
end
74 changes: 63 additions & 11 deletions spec/lib/license_finder/package_managers/yarn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,47 @@ module LicenseFinder
FileUtils.mkdir_p(root)
end

it 'should call yarn install' do
expect(SharedHelpers::Cmd).to receive(:run).with('yarn install --ignore-engines --ignore-scripts')
.and_return([yarn_shell_command_output, '', cmd_success])
subject.prepare
context 'when using Yarn 1.x projects' do
before do
allow(SharedHelpers::Cmd).to receive(:run).with('yarn -v').and_return(['1.9.4', '', cmd_success])
end

it 'should call yarn install with expected cli parameters' do
expect(SharedHelpers::Cmd).to receive(:run).with('yarn install --ignore-engines --ignore-scripts')
.and_return([yarn_shell_command_output, '', cmd_success])
subject.prepare
end

context 'ignored_groups contains devDependencies' do
subject { Yarn.new(project_path: Pathname(root), ignored_groups: 'devDependencies') }
it 'should include a production flag' do
expect(SharedHelpers::Cmd).to receive(:run).with('yarn install --ignore-engines --ignore-scripts --production')
.and_return([yarn_shell_command_output, '', cmd_success])
subject.prepare
end
end
end
context 'ignored_groups contains devDependencies' do
subject { Yarn.new(project_path: Pathname(root), ignored_groups: 'devDependencies') }
it 'should include a production flag' do
expect(SharedHelpers::Cmd).to receive(:run).with('yarn install --ignore-engines --ignore-scripts --production')

context 'when using Yarn 2.x+ projects' do
before do
allow(SharedHelpers::Cmd).to receive(:run).with('yarn -v').and_return(['3.0.1', '', cmd_success])
end

it 'should call yarn install with no cli parameters' do
expect(SharedHelpers::Cmd).to receive(:run).with('yarn install')
.and_return([yarn_shell_command_output, '', cmd_success])
subject.prepare
end

context 'ignored_groups contains devDependencies' do
subject { Yarn.new(project_path: Pathname(root), ignored_groups: 'devDependencies') }

it 'should include a production flag' do
expect(SharedHelpers::Cmd).to receive(:run).with('yarn plugin import workspace-tools && yarn workspaces focus --all --production && yarn install')
.and_return([yarn_shell_command_output, '', cmd_success])
subject.prepare
end
end
end
end

Expand Down Expand Up @@ -144,9 +173,32 @@ module LicenseFinder
end

describe '.prepare_command' do
subject { Yarn.new(project_path: Pathname(root), logger: double(:logger, active: nil)) }
it 'returns the correct prepare method' do
expect(subject.prepare_command).to eq('yarn install --ignore-engines --ignore-scripts')
include FakeFS::SpecHelpers
before do
FileUtils.mkdir_p(Dir.tmpdir)
FileUtils.mkdir_p(root)
end

context 'when in a Yarn 1.x project' do
before do
allow(SharedHelpers::Cmd).to receive(:run).with('yarn -v').and_return(['1.9.1', '', cmd_success])
end

subject { Yarn.new(project_path: Pathname(root), logger: double(:logger, active: nil)) }
it 'returns the correct prepare method' do
expect(subject.prepare_command).to eq('yarn install --ignore-engines --ignore-scripts')
end
end

context 'when in a Yarn 2.x project' do
before do
allow(SharedHelpers::Cmd).to receive(:run).with('yarn -v').and_return(['3.5.9', '', cmd_success])
end

subject { Yarn.new(project_path: Pathname(root), logger: double(:logger, active: nil)) }
it 'returns the correct prepare method' do
expect(subject.prepare_command).to eq('yarn install')
end
end
end

Expand Down

0 comments on commit fcfe740

Please sign in to comment.