/ src / default-directory-provider.coffee
default-directory-provider.coffee
 1  {Directory} = require 'pathwatcher'
 2  fs = require 'fs-plus'
 3  path = require 'path'
 4  url = require 'url'
 5  
 6  module.exports =
 7  class DefaultDirectoryProvider
 8  
 9    # Public: Create a Directory that corresponds to the specified URI.
10    #
11    # * `uri` {String} The path to the directory to add. This is guaranteed not to
12    # be contained by a {Directory} in `atom.project`.
13    #
14    # Returns:
15    # * {Directory} if the given URI is compatible with this provider.
16    # * `null` if the given URI is not compatible with this provider.
17    directoryForURISync: (uri) ->
18      normalizedPath = @normalizePath(uri)
19      {host} = url.parse(uri)
20      directoryPath = if host
21        uri
22      else if not fs.isDirectorySync(normalizedPath) and fs.isDirectorySync(path.dirname(normalizedPath))
23        path.dirname(normalizedPath)
24      else
25        normalizedPath
26  
27      # TODO: Stop normalizing the path in pathwatcher's Directory.
28      directory = new Directory(directoryPath)
29      if host
30        directory.path = directoryPath
31        if fs.isCaseInsensitive()
32          directory.lowerCasePath = directoryPath.toLowerCase()
33      directory
34  
35    # Public: Create a Directory that corresponds to the specified URI.
36    #
37    # * `uri` {String} The path to the directory to add. This is guaranteed not to
38    # be contained by a {Directory} in `atom.project`.
39    #
40    # Returns a {Promise} that resolves to:
41    # * {Directory} if the given URI is compatible with this provider.
42    # * `null` if the given URI is not compatible with this provider.
43    directoryForURI: (uri) ->
44      Promise.resolve(@directoryForURISync(uri))
45  
46    # Public: Normalizes path.
47    #
48    # * `uri` {String} The path that should be normalized.
49    #
50    # Returns a {String} with normalized path.
51    normalizePath: (uri) ->
52      # Normalize disk drive letter on Windows to avoid opening two buffers for the same file
53      pathWithNormalizedDiskDriveLetter =
54        if process.platform is 'win32' and matchData = uri.match(/^([a-z]):/)
55          "#{matchData[1].toUpperCase()}#{uri.slice(1)}"
56        else
57          uri
58      path.normalize(pathWithNormalizedDiskDriveLetter)