From bdb641ec94663af250b8415b4efa594deca04842 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 12 Jun 2013 15:43:58 -0700 Subject: [PATCH] Use path.extname() instead of fsUtils.extension() --- spec/jasmine-helper.coffee | 3 +- spec/stdlib/fs-utils-spec.coffee | 8 ---- src/app/atom-theme.coffee | 3 +- src/app/window.coffee | 37 ++++++++++--------- .../fuzzy-finder/lib/fuzzy-finder-view.coffee | 2 +- .../image-view/lib/image-edit-session.coffee | 2 +- src/packages/tree-view/lib/dialog.coffee | 2 +- src/packages/tree-view/lib/file-view.coffee | 3 +- .../tree-view/spec/tree-view-spec.coffee | 2 +- src/stdlib/fs-utils.coffee | 17 +-------- 10 files changed, 31 insertions(+), 48 deletions(-) diff --git a/spec/jasmine-helper.coffee b/spec/jasmine-helper.coffee index 7a1337d9d..b7c717e52 100644 --- a/spec/jasmine-helper.coffee +++ b/spec/jasmine-helper.coffee @@ -1,8 +1,9 @@ window.nakedLoad = (file) -> fsUtils = require 'fs-utils' + path = require 'path' file = require.resolve(file) code = fsUtils.read(file) - if fsUtils.extension(file) is '.coffee' + if path.extname(file) is '.coffee' require('coffee-script').eval(code, filename: file) else window.eval("#{code}\n//@ sourceURL=#{file}") diff --git a/spec/stdlib/fs-utils-spec.coffee b/spec/stdlib/fs-utils-spec.coffee index 787ae8296..04f5c8f32 100644 --- a/spec/stdlib/fs-utils-spec.coffee +++ b/spec/stdlib/fs-utils-spec.coffee @@ -54,14 +54,6 @@ describe "fsUtils", -> expect(fsUtils.split("/a/b/c.txt")).toEqual ["", "a", "b", "c.txt"] expect(fsUtils.split("a/b/c.txt")).toEqual ["a", "b", "c.txt"] - describe ".extension(path)", -> - it "returns the extension of a file", -> - expect(fsUtils.extension("a/b/corey.txt")).toBe '.txt' - expect(fsUtils.extension("a/b/corey.txt.coffee")).toBe '.coffee' - - it "returns an empty string for paths without an extension", -> - expect(fsUtils.extension("a/b.not-extension/a-dir")).toBe '' - describe ".makeTree(path)", -> beforeEach -> fsUtils.remove("/tmp/a") if fsUtils.exists("/tmp/a") diff --git a/src/app/atom-theme.coffee b/src/app/atom-theme.coffee index 10015a1eb..482c630b8 100644 --- a/src/app/atom-theme.coffee +++ b/src/app/atom-theme.coffee @@ -1,4 +1,5 @@ fsUtils = require 'fs-utils' +path = require 'path' Theme = require 'theme' # Internal: Represents a theme that Atom can use. @@ -13,7 +14,7 @@ class AtomTheme extends Theme # Loads the stylesheets found in a `package.cson` file. load: -> - if fsUtils.extension(@path) in ['.css', '.less'] + if path.extname(@path) in ['.css', '.less'] @loadStylesheet(@path) else metadataPath = fsUtils.resolveExtension(fsUtils.join(@path, 'package'), ['cson', 'json']) diff --git a/src/app/window.coffee b/src/app/window.coffee index 8a033b0a7..8c6b64c22 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -1,4 +1,5 @@ fsUtils = require 'fs-utils' +path = require 'path' $ = require 'jquery' less = require 'less' ipc = require 'ipc' @@ -140,46 +141,46 @@ window.deserializeConfigWindow = -> window.stylesheetElementForId = (id) -> $("""head style[id="#{id}"]""") -window.resolveStylesheet = (path) -> - if fsUtils.extension(path).length > 0 - fsUtils.resolveOnLoadPath(path) +window.resolveStylesheet = (stylesheetPath) -> + if path.extname(stylesheetPath).length > 0 + fsUtils.resolveOnLoadPath(stylesheetPath) else - fsUtils.resolveOnLoadPath(path, ['css', 'less']) + fsUtils.resolveOnLoadPath(stylesheetPath, ['css', 'less']) -window.requireStylesheet = (path) -> - if fullPath = window.resolveStylesheet(path) +window.requireStylesheet = (stylesheetPath) -> + if fullPath = window.resolveStylesheet(stylesheetPath) content = window.loadStylesheet(fullPath) window.applyStylesheet(fullPath, content) else - throw new Error("Could not find a file at path '#{path}'") + throw new Error("Could not find a file at path '#{stylesheetPath}'") -window.loadStylesheet = (path) -> - if fsUtils.extension(path) == '.less' - loadLessStylesheet(path) +window.loadStylesheet = (stylesheetPath) -> + if path.extname(stylesheetPath) is '.less' + loadLessStylesheet(stylesheetPath) else - fsUtils.read(path) + fsUtils.read(stylesheetPath) -window.loadLessStylesheet = (path) -> +window.loadLessStylesheet = (lessStylesheetPath) -> parser = new less.Parser syncImport: true paths: config.lessSearchPaths - filename: path + filename: patlessStylesheetPath try content = null - parser.parse fsUtils.read(path), (e, tree) -> + parser.parse fsUtils.read(lessStylesheetPath), (e, tree) -> throw e if e? content = tree.toCSS() content catch e console.error """ - Error compiling less stylesheet: #{path} + Error compiling less stylesheet: #{lessStylesheetPath} Line number: #{e.line} #{e.message} """ -window.removeStylesheet = (path) -> - unless fullPath = window.resolveStylesheet(path) - throw new Error("Could not find a file at path '#{path}'") +window.removeStylesheet = (stylesheetPath) -> + unless fullPath = window.resolveStylesheet(stylesheetPath) + throw new Error("Could not find a file at path '#{stylesheetPath}'") window.stylesheetElementForId(fullPath).remove() window.applyStylesheet = (id, text, ttype = 'bundled') -> diff --git a/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee b/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee index cbb2999b0..c684bf133 100644 --- a/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee +++ b/src/packages/fuzzy-finder/lib/fuzzy-finder-view.coffee @@ -49,7 +49,7 @@ class FuzzyFinderView extends SelectList else if git.isStatusModified(status) @div class: 'status modified' - ext = fsUtils.extension(filePath) + ext = path.extname(filePath) if fsUtils.isReadmePath(filePath) typeClass = 'readme-name' else if fsUtils.isCompressedExtension(ext) diff --git a/src/packages/image-view/lib/image-edit-session.coffee b/src/packages/image-view/lib/image-edit-session.coffee index 48f2176b0..ac0410350 100644 --- a/src/packages/image-view/lib/image-edit-session.coffee +++ b/src/packages/image-view/lib/image-edit-session.coffee @@ -14,7 +14,7 @@ class ImageEditSession imageExtensions = ['.gif', '.jpeg', '.jpg', '.png'] Project = require 'project' Project.registerOpener (filePath) -> - if _.include(imageExtensions, fsUtils.extension(filePath)) + if _.include(imageExtensions, path.extname(filePath)) new ImageEditSession(filePath) @deserialize: ({path}={}) -> diff --git a/src/packages/tree-view/lib/dialog.coffee b/src/packages/tree-view/lib/dialog.coffee index 56d0d4d1f..05af8db84 100644 --- a/src/packages/tree-view/lib/dialog.coffee +++ b/src/packages/tree-view/lib/dialog.coffee @@ -22,7 +22,7 @@ class Dialog extends View @miniEditor.setText(initialPath) if select - extension = fsUtils.extension(initialPath) + extension = path.extname(initialPath) baseName = path.basename(initialPath) if baseName is extension selectionEnd = initialPath.length diff --git a/src/packages/tree-view/lib/file-view.coffee b/src/packages/tree-view/lib/file-view.coffee index 2223b8807..ac72b9351 100644 --- a/src/packages/tree-view/lib/file-view.coffee +++ b/src/packages/tree-view/lib/file-view.coffee @@ -1,6 +1,7 @@ {View} = require 'space-pen' $ = require 'jquery' fsUtils = require 'fs-utils' +path = require 'path' module.exports = class FileView extends View @@ -16,7 +17,7 @@ class FileView extends View if @file.symlink @fileName.addClass('symlink-icon') else - extension = fsUtils.extension(@getPath()) + extension = path.extname(@getPath()) if fsUtils.isReadmePath(@getPath()) @fileName.addClass('readme-icon') else if fsUtils.isCompressedExtension(extension) diff --git a/src/packages/tree-view/spec/tree-view-spec.coffee b/src/packages/tree-view/spec/tree-view-spec.coffee index c881a2914..d289507a8 100644 --- a/src/packages/tree-view/spec/tree-view-spec.coffee +++ b/src/packages/tree-view/spec/tree-view-spec.coffee @@ -773,7 +773,7 @@ describe "TreeView", -> waits 50 # The move specs cause too many false positives because of their async nature, so wait a little bit before we cleanup it "opens a move dialog with the file's current path (excluding extension) populated", -> - extension = fsUtils.extension(filePath) + extension = path.extname(filePath) fileNameWithoutExtension = path.basename(filePath, extension) expect(moveDialog).toExist() expect(moveDialog.prompt.text()).toBe "Enter the new path for the file." diff --git a/src/stdlib/fs-utils.coffee b/src/stdlib/fs-utils.coffee index 904e6f370..360207063 100644 --- a/src/stdlib/fs-utils.coffee +++ b/src/stdlib/fs-utils.coffee @@ -35,18 +35,6 @@ module.exports = exists: (path) -> path? and fs.existsSync(path) - # Returns the extension of a file. The extension of a file is the - # last dot (excluding any number of initial dots) followed by one or - # more non-dot characters. Returns an empty string if no valid - # extension exists. - extension: (path) -> - return '' unless typeof path is 'string' - match = Path.basename(path).match(/\.[^\.]+$/) - if match - match[0] - else - "" - join: (paths...) -> return paths[0] if paths.length == 1 [first, rest...] = paths @@ -113,8 +101,7 @@ module.exports = ext else '.' + ext.replace(/^\./, '') - paths.filter (path) => - _.include(extensions, @extension(path)) + paths.filter (path) -> _.include(extensions, Path.extname(path)) listTree: (rootPath) -> paths = [] @@ -309,7 +296,7 @@ module.exports = ], ext, true) >= 0 isReadmePath: (path) -> - extension = @extension(path) + extension = Path.extname(path) base = Path.basename(path, extension).toLowerCase() base is 'readme' and (extension is '' or @isMarkdownExtension(extension))