Optionally throw an error when attempting to open a non-existent dir

This commit is contained in:
Ash Wilson
2017-09-18 10:32:47 -04:00
parent 2e57312380
commit 156cd02953
2 changed files with 17 additions and 2 deletions

View File

@@ -498,6 +498,11 @@ describe "Project", ->
atom.project.addPath('/this-definitely/does-not-exist')
expect(atom.project.getPaths()).toEqual(previousPaths)
it "optionally throws on non-existent directories", ->
expect ->
atom.project.addPath '/this-definitely/does-not-exist', mustExist: true
.toThrow()
describe ".removePath(path)", ->
onDidChangePathsSpy = null

View File

@@ -226,8 +226,18 @@ class Project extends Model
# Public: Add a path to the project's list of root paths
#
# * `projectPath` {String} The path to the directory to add.
addPath: (projectPath, options) ->
# * `options` An optional {Object} that may contain the following keys:
# * `mustExist` If `true`, throw an Error if `projectPath` does not exist.
addPath: (projectPath, options = {}) ->
directory = @getDirectoryForProjectPath(projectPath)
unless directory.existsSync()
if options.mustExist is true
err = new Error "Project directory #{directory} does not exist"
err.missingProjectPaths = [directory]
throw err
else
return
return unless directory.existsSync()
for existingDirectory in @getDirectories()
return if existingDirectory.getPath() is directory.getPath()
@@ -248,7 +258,7 @@ class Project extends Model
break if repo = provider.repositoryForDirectorySync?(directory)
@repositories.push(repo ? null)
unless options?.emitEvent is false
unless options.emitEvent is false
@emitter.emit 'did-change-paths', @getPaths()
getDirectoryForProjectPath: (projectPath) ->