From 156cd0295301e43d21980eed25adfb1d6a5e8bce Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Mon, 18 Sep 2017 10:32:47 -0400 Subject: [PATCH] Optionally throw an error when attempting to open a non-existent dir --- spec/project-spec.coffee | 5 +++++ src/project.coffee | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index 4ce84617a..bf31d0509 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -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 diff --git a/src/project.coffee b/src/project.coffee index cad5f03ac..ef8681fe4 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -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) ->