Skip to content

Commit

Permalink
workspace toJSON when empty uri get first slice in the array
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeu committed Feb 6, 2024
1 parent bae35b0 commit 10d48b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
20 changes: 16 additions & 4 deletions spec/WorkSpace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,25 @@ describe('#toJSON', () => {
const project = workSpace.toJSON()
const file = workSpace.fromFileUri(railsConfig)

console.log([project, file])

// expect(project.uri).toBe('Users/thadeu/code/atendesimples/atendesimples-app')
// expect(project.name).toBe('atendesimples-app')
expect(project.uri).toBe('app')
expect(project.name).toBe('app')
expect(file.path).toBe('app/controllers/advanced_configurations_controller.rb')
expect(file.specPath).toBe('spec/controllers/advanced_configurations_controller_spec.rb')
expect(file.inversePath).toBe('spec/controllers/advanced_configurations_controller_spec.rb')
})

test('when workspace use spec file', () => {
let fileUri = '/app/spec/controllers/advanced_configurations_controller_spec.rb'

const workSpace = new WorkSpace(fileUri)
const project = workSpace.toJSON()
const file = workSpace.fromFileUri(railsConfig)

expect(project.uri).toBe('app')
expect(project.name).toBe('app')
expect(file.path).toBe('spec/controllers/advanced_configurations_controller_spec.rb')
expect(file.specPath).toBe('spec/controllers/advanced_configurations_controller_spec.rb')
expect(file.inversePath).toBe('app/controllers/advanced_configurations_controller.rb')
})
})
})
7 changes: 6 additions & 1 deletion src/WorkSpace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import compact from 'lodash.compact'
import isEmpty from 'lodash.isempty'

import FileObject from './FileObject'

Expand All @@ -17,7 +18,11 @@ export default class WorkSpace {

const findIndexCallback = (o) => new RegExp(`^(app|spec|test|lib)$`).test(o)
const index = fileUriArray.findIndex(findIndexCallback)
const array = fileUriArray.slice(0, index)
let array = fileUriArray.slice(0, index)

if (isEmpty(array)) {
array = fileUriArray.slice(0, 1)
}

this.uri = array.join('/')
this.name = array.slice(-1)[0]
Expand Down

0 comments on commit 10d48b4

Please sign in to comment.