Use path.basename() instead of fsUtils.base()

This commit is contained in:
Kevin Sawicki
2013-06-12 15:26:09 -07:00
parent 437137783c
commit eede4a386f
17 changed files with 74 additions and 75 deletions

View File

@@ -1,5 +1,6 @@
Project = require 'project'
fsUtils = require 'fs-utils'
path = require 'path'
_ = require 'underscore'
BufferedProcess = require 'buffered-process'
@@ -147,7 +148,7 @@ describe "Project", ->
expect(paths.length).toBeGreaterThan 0
it "ignores files that return true from atom.ignorePath(path)", ->
spyOn(project, 'isPathIgnored').andCallFake (path) -> fsUtils.base(path).match /a$/
spyOn(project, 'isPathIgnored').andCallFake (filePath) -> path.basename(filePath).match /a$/
paths = null
waitsForPromise ->
@@ -256,7 +257,7 @@ describe "Project", ->
expect(paths[1]).toMatch /file with spaces.txt$/
expect(paths[2]).toMatch /goddam\nnewlines$/m
expect(paths[3]).toMatch /quote".txt$/m
expect(fsUtils.base(paths[4])).toBe "utfa\u0306.md"
expect(path.basename(paths[4])).toBe "utfa\u0306.md"
it "handles breaks in the search subprocess's output following the filename", ->
spyOn(BufferedProcess.prototype, 'bufferStream')

View File

@@ -1,5 +1,6 @@
$ = require 'jquery'
fsUtils = require 'fs-utils'
path = require 'path'
Project = require 'project'
RootView = require 'root-view'
Buffer = require 'text-buffer'
@@ -83,7 +84,7 @@ describe "RootView", ->
expect(editor3.isFocused).toBeFalsy()
expect(editor4.isFocused).toBeFalsy()
expect(rootView.title).toBe "#{fsUtils.base(editor2.getPath())} - #{project.getPath()}"
expect(rootView.title).toBe "#{path.basename(editor2.getPath())} - #{project.getPath()}"
describe "where there are no open editors", ->
it "constructs the view with no open editors", ->

View File

@@ -33,13 +33,6 @@ describe "fsUtils", ->
expect(fsUtils.directory("a")).toBe ""
expect(fsUtils.directory("/a/b/c++")).toBe "/a/b"
describe ".base(path, ext)", ->
describe "when called with an extension", ->
it "return the base name without the extension when the path has the given extension", ->
expect(fsUtils.base("/a/b/c.txt", '.txt')).toBe "c"
expect(fsUtils.base("/a/b/c.txt", '.txt2')).toBe "c.txt"
expect(fsUtils.base("/a/b/c.+", '.+')).toBe "c"
describe ".exists(path)", ->
it "returns true when path exsits", ->
expect(fsUtils.exists(fsUtils.resolveOnLoadPath('fixtures'))).toBe true

View File

@@ -5,6 +5,7 @@ Theme = require 'theme'
ipc = require 'ipc'
remote = require 'remote'
crypto = require 'crypto'
path = require 'path'
window.atom =
loadedThemes: []
@@ -121,12 +122,12 @@ window.atom =
_.uniq(packagePaths)
getAvailablePackageNames: ->
fsUtils.base(path) for path in @getAvailablePackagePaths()
path.basename(packagePath) for packagePath in @getAvailablePackagePaths()
getAvailablePackageMetadata: ->
packages = []
for packagePath in atom.getAvailablePackagePaths()
name = fsUtils.base(packagePath)
name = path.basename(packagePath)
metadata = atom.getLoadedPackage(name)?.metadata ? Package.loadMetadata(packagePath, true)
packages.push(metadata)
packages
@@ -144,7 +145,7 @@ window.atom =
_.uniq(themePaths)
getAvailableThemeNames: ->
fsUtils.base(path).split('.')[0] for path in @getAvailableThemePaths()
path.basename(themePath).split('.')[0] for themePath in @getAvailableThemePaths()
loadTheme: (name) ->
@loadedThemes.push Theme.load(name)

View File

@@ -1,5 +1,6 @@
_ = require 'underscore'
fs = require 'fs'
path = require 'path'
fsUtils = require 'fs-utils'
pathWatcher = require 'pathwatcher'
File = require 'file'
@@ -13,7 +14,7 @@ class Directory
path: null
### Public ###
# Creates a new directory.
#
# path - A {String} representing the file directory
@@ -24,7 +25,7 @@ class Directory
#
# Returns a {String}.
getBaseName: ->
fsUtils.base(@path)
path.basename(@path)
# Retrieves the directory's path.
#

View File

@@ -9,6 +9,7 @@ Subscriber = require 'subscriber'
Range = require 'range'
_ = require 'underscore'
fsUtils = require 'fs-utils'
path = require 'path'
TextMateScopeSelector = require 'text-mate-scope-selector'
# An `EditSession` manages the states between {Editor}s, {Buffer}s, and the project as a whole.
@@ -98,8 +99,8 @@ class EditSession
#
# Returns a {String}.
getTitle: ->
if path = @getPath()
fsUtils.base(path)
if sessionPath = @getPath()
path.basename(sessionPath)
else
'untitled'
@@ -109,9 +110,9 @@ class EditSession
#
# Returns a {String}.
getLongTitle: ->
if path = @getPath()
fileName = fsUtils.base(path)
directory = fsUtils.base(fsUtils.directory(path))
if sessionPath = @getPath()
fileName = path.basename(sessionPath)
directory = path.basename(fsUtils.directory(sessionPath))
"#{fileName} - #{directory}"
else
'untitled'

View File

@@ -1,6 +1,6 @@
EventEmitter = require 'event-emitter'
fs = require 'fs'
path = require 'path'
fsUtils = require 'fs-utils'
pathWatcher = require 'pathwatcher'
_ = require 'underscore'
@@ -38,7 +38,7 @@ class File
#
# Returns a {String}.
getBaseName: ->
fsUtils.base(@path)
path.basename(@path)
# Writes (and saves) new contents to the file.
#

View File

@@ -4,6 +4,7 @@ _ = require 'underscore'
$ = require 'jquery'
humanize = require 'humanize-plus'
fsUtils = require 'fs-utils'
path = require 'path'
LoadPathsTask = require './load-paths-task'
Point = require 'point'
@@ -38,18 +39,18 @@ class FuzzyFinderView extends SelectList
@miniEditor.command 'pane:split-up', =>
@splitOpenPath (pane, session) -> pane.splitUp(session)
itemForElement: ({path, projectRelativePath}) ->
itemForElement: ({filePath, projectRelativePath}) ->
$$ ->
@li class: 'two-lines', =>
if git?
status = git.statuses[path]
status = git.statuses[filePath]
if git.isStatusNew(status)
@div class: 'status new'
else if git.isStatusModified(status)
@div class: 'status modified'
ext = fsUtils.extension(path)
if fsUtils.isReadmePath(path)
ext = fsUtils.extension(filePath)
if fsUtils.isReadmePath(filePath)
typeClass = 'readme-name'
else if fsUtils.isCompressedExtension(ext)
typeClass = 'compressed-name'
@@ -62,13 +63,13 @@ class FuzzyFinderView extends SelectList
else
typeClass = 'text-name'
@div fsUtils.base(path), class: "primary-line file #{typeClass}"
@div path.basename(filePath), class: "primary-line file #{typeClass}"
@div projectRelativePath, class: 'secondary-line path'
openPath: (path, lineNumber) ->
return unless path
openPath: (filePath, lineNumber) ->
return unless filePath
rootView.open(path, {@allowActiveEditorChange})
rootView.open(filePath, {@allowActiveEditorChange})
@moveToLine(lineNumber)
moveToLine: (lineNumber=-1) ->
@@ -81,23 +82,23 @@ class FuzzyFinderView extends SelectList
editor.moveCursorToFirstCharacterOfLine()
splitOpenPath: (fn) ->
{path} = @getSelectedElement()
return unless path
{filePath} = @getSelectedElement()
return unless filePath
lineNumber = @getLineNumber()
if pane = rootView.getActivePane()
fn(pane, project.open(path))
fn(pane, project.open(filePath))
@moveToLine(lineNumber)
else
@openPath(path, lineNumber)
@openPath(filePath, lineNumber)
confirmed : ({path}) ->
return unless path.length
confirmed : ({filePath}) ->
return unless filePath
if fsUtils.isFile(path)
if fsUtils.isFile(filePath)
lineNumber = @getLineNumber()
@cancel()
@openPath(path, lineNumber)
@openPath(filePath, lineNumber)
else
@setError('Selected path does not exist')
setTimeout((=> @setError()), 2000)
@@ -168,15 +169,15 @@ class FuzzyFinderView extends SelectList
parseInt(query[colon+1..]) - 1
setArray: (paths) ->
projectRelativePaths = paths.map (path) ->
projectRelativePath = project.relativize(path)
{path, projectRelativePath}
projectRelativePaths = paths.map (filePath) ->
projectRelativePath = project.relativize(filePath)
{filePath, projectRelativePath}
super(projectRelativePaths)
populateGitStatusPaths: ->
paths = []
paths.push(path) for path, status of git.statuses when fsUtils.isFile(path)
paths.push(filePath) for filePath, status of git.statuses when fsUtils.isFile(filePath)
@setArray(paths)
@@ -184,8 +185,8 @@ class FuzzyFinderView extends SelectList
if @projectPaths?
listedItems =
if options.filter?
@projectPaths.filter (path) ->
path.indexOf(options.filter) >= 0
@projectPaths.filter (filePath) ->
filePath.indexOf(options.filter) >= 0
else
@projectPaths
@setArray(listedItems)

View File

@@ -5,6 +5,7 @@ _ = require 'underscore'
$ = require 'jquery'
{$$} = require 'space-pen'
fsUtils = require 'fs-utils'
path = require 'path'
describe 'FuzzyFinder', ->
[finderView] = []
@@ -55,8 +56,8 @@ describe 'FuzzyFinder', ->
runs ->
expect(finderView.list.children('li').length).toBe paths.length
for path in paths
expect(finderView.list.find("li:contains(#{fsUtils.base(path)})")).toExist()
for filePath in paths
expect(finderView.list.find("li:contains(#{path.basename(filePath)})")).toExist()
expect(finderView.list.children().first()).toHaveClass 'selected'
expect(finderView.find(".loading")).not.toBeVisible()

View File

@@ -1,4 +1,5 @@
fsUtils = require 'fs-utils'
path = require 'path'
_ = require 'underscore'
# Public: Manages the states between {Editor}s, images, and the project as a whole.
@@ -39,8 +40,8 @@ class ImageEditSession
#
# Returns a {String}.
getTitle: ->
if path = @getPath()
fsUtils.base(path)
if sessionPath = @getPath()
path.basename(sessionPath)
else
'untitled'

View File

@@ -3,6 +3,7 @@ Editor = require 'editor'
$ = require 'jquery'
_ = require 'underscore'
fsUtils = require 'fs-utils'
path = require 'path'
module.exports =
class PackageGeneratorView extends View
@@ -44,7 +45,7 @@ class PackageGeneratorView extends View
getPackagePath: ->
packagePath = @miniEditor.getText()
packageName = _.dasherize(fsUtils.base(packagePath))
packageName = _.dasherize(path.basename(packagePath))
fsUtils.join(fsUtils.directory(packagePath), packageName)
validPackagePath: ->
@@ -57,7 +58,7 @@ class PackageGeneratorView extends View
createPackageFiles: ->
templatePath = fsUtils.resolveOnLoadPath(fsUtils.join("package-generator", "template"))
packageName = fsUtils.base(@getPackagePath())
packageName = path.basename(@getPackagePath())
for path in fsUtils.listTree(templatePath)
relativePath = path.replace(templatePath, "")

View File

@@ -4,6 +4,7 @@ TagGenerator = require './tag-generator'
TagReader = require './tag-reader'
Point = require 'point'
fsUtils = require 'fs-utils'
path = require 'path'
$ = require 'jquery'
module.exports =
@@ -30,7 +31,7 @@ class SymbolsView extends SelectList
if position
text = "Line #{position.row + 1}"
else
text = fsUtils.base(file)
text = path.basename(file)
@div text, class: 'secondary-line'
toggleFileSymbols: ->
@@ -119,7 +120,7 @@ class SymbolsView extends SelectList
continue unless position
tags.push
file: match.file
name: fsUtils.base(match.file)
name: path.basename(match.file)
position: position
@miniEditor.show()
@setArray(tags)

View File

@@ -1,6 +1,7 @@
$ = require 'jquery'
{View} = require 'space-pen'
fsUtils = require 'fs-utils'
path = require 'path'
module.exports =
class TabView extends View
@@ -46,7 +47,7 @@ class TabView extends View
if fileNameText?
duplicates = @editor.getEditSessions().filter (session) -> fileNameText is session.buffer.getBaseName()
if duplicates.length > 1
directory = fsUtils.base(fsUtils.directory(@editSession.getPath()))
directory = path.basename(fsUtils.directory(@editSession.getPath()))
fileNameText = "#{fileNameText} - #{directory}" if directory
else
fileNameText = 'untitled'

View File

@@ -1,6 +1,7 @@
{View} = require 'space-pen'
Editor = require 'editor'
fsUtils = require 'fs-utils'
path = require 'path'
$ = require 'jquery'
module.exports =
@@ -11,23 +12,23 @@ class Dialog extends View
@span prompt, outlet: 'promptText'
@subview 'miniEditor', new Editor(mini: true)
initialize: ({path, @onConfirm, select, iconClass} = {}) ->
initialize: ({initialPath, @onConfirm, select, iconClass} = {}) ->
@prompt.addClass(iconClass) if iconClass
@miniEditor.focus()
@on 'core:confirm', => @onConfirm(@miniEditor.getText())
@on 'core:cancel', => @cancel()
@miniEditor.on 'focusout', => @remove()
@miniEditor.setText(path)
@miniEditor.setText(initialPath)
if select
extension = fsUtils.extension(path)
baseName = fsUtils.base(path)
extension = fsUtils.extension(initialPath)
baseName = path.basename(initialPath)
if baseName is extension
selectionEnd = path.length
selectionEnd = initialPath.length
else
selectionEnd = path.length - extension.length
range = [[0, path.length - baseName.length], [0, selectionEnd]]
selectionEnd = initialPath.length - extension.length
range = [[0, initialPath.length - baseName.length], [0, selectionEnd]]
@miniEditor.setSelectedBufferRange(range)
close: ->

View File

@@ -222,7 +222,7 @@ class TreeView extends ScrollView
dialog = new Dialog
prompt: prompt
path: project.relativize(oldPath)
initialPath: project.relativize(oldPath)
select: true
iconClass: 'move'
onConfirm: (newPath) =>
@@ -267,7 +267,7 @@ class TreeView extends ScrollView
dialog = new Dialog
prompt: "Enter the path for the new file/directory. Directories end with a '/'."
path: relativeDirectoryPath
initialPath: relativeDirectoryPath
select: false
iconClass: 'add-directory'

View File

@@ -5,6 +5,7 @@ TreeView = require 'tree-view/lib/tree-view'
RootView = require 'root-view'
Directory = require 'directory'
fsUtils = require 'fs-utils'
path = require 'path'
describe "TreeView", ->
[treeView, sampleJs, sampleTxt] = []
@@ -640,13 +641,13 @@ describe "TreeView", ->
dirView.directory.trigger 'contents-changed'
expect(directoryChangeHandler).toHaveBeenCalled()
expect(treeView.find('.selected').text()).toBe fsUtils.base(filePath)
expect(treeView.find('.selected').text()).toBe path.basename(filePath)
describe "when the path without a trailing '/' is changed and confirmed", ->
describe "when no file exists at that location", ->
it "add a file, closes the dialog and selects the file in the tree-view", ->
newPath = fsUtils.join(dirPath, "new-test-file.txt")
addDialog.miniEditor.insertText(fsUtils.base(newPath))
addDialog.miniEditor.insertText(path.basename(newPath))
addDialog.trigger 'core:confirm'
expect(fsUtils.exists(newPath)).toBeTruthy()
expect(fsUtils.isFile(newPath)).toBeTruthy()
@@ -657,13 +658,13 @@ describe "TreeView", ->
dirView.entries.find("> .file").length > 1
runs ->
expect(treeView.find('.selected').text()).toBe fsUtils.base(newPath)
expect(treeView.find('.selected').text()).toBe path.basename(newPath)
describe "when a file already exists at that location", ->
it "shows an error message and does not close the dialog", ->
newPath = fsUtils.join(dirPath, "new-test-file.txt")
fsUtils.write(newPath, '')
addDialog.miniEditor.insertText(fsUtils.base(newPath))
addDialog.miniEditor.insertText(path.basename(newPath))
addDialog.trigger 'core:confirm'
expect(addDialog.prompt.text()).toContain 'Error'
@@ -773,11 +774,11 @@ describe "TreeView", ->
it "opens a move dialog with the file's current path (excluding extension) populated", ->
extension = fsUtils.extension(filePath)
fileNameWithoutExtension = fsUtils.base(filePath, extension)
fileNameWithoutExtension = path.basename(filePath, extension)
expect(moveDialog).toExist()
expect(moveDialog.prompt.text()).toBe "Enter the new path for the file."
expect(moveDialog.miniEditor.getText()).toBe(project.relativize(filePath))
expect(moveDialog.miniEditor.getSelectedText()).toBe fsUtils.base(fileNameWithoutExtension)
expect(moveDialog.miniEditor.getSelectedText()).toBe path.basename(fileNameWithoutExtension)
expect(moveDialog.miniEditor.isFocused).toBeTruthy()
describe "when the path is changed and confirmed", ->

View File

@@ -22,13 +22,6 @@ module.exports =
catch e
path
# Return the basename of the given path. That is the path with
# any leading directory components removed. If specified, also
# remove a trailing extension.
base: (path, ext) ->
base = path.replace(/\/$/, '').split("/").pop()
if ext then base.replace(RegExp("#{_.escapeRegExp(ext)}$"), '') else base
# Returns the path of a file's containing directory, albeit the
# parent directory if the file is a directory. A terminal directory
# separator is ignored.