Skip to content

Commit

Permalink
Move file type check to Paths module.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaeljorhult committed Jul 18, 2016
1 parent b63dc41 commit a9390b2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 1 addition & 2 deletions modules/AutoprefixOnChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ define( function( require ) {
DocumentManager = brackets.getModule( 'document/DocumentManager' ),
EditorManager = brackets.getModule( 'editor/EditorManager' ),
FileSystem = brackets.getModule( 'filesystem/FileSystem' ),
LanguageManager = brackets.getModule( 'language/LanguageManager' ),

// Get extension modules.
Paths = require( 'modules/Paths' ),
Expand Down Expand Up @@ -66,7 +65,7 @@ define( function( require ) {
}

// Only process CSS files.
if ( [ 'css' ].indexOf( LanguageManager.getLanguageForPath( file.fullPath ).getId() ) > -1 ) {
if ( Paths.isFileType( file, [ 'css' ] ) ) {
// Check if file is open in an editor.
document = DocumentManager.getOpenDocumentForPath( file.fullPath );

Expand Down
3 changes: 2 additions & 1 deletion modules/AutoprefixOnSave.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ define( function( require ) {
EditorManager = brackets.getModule( 'editor/EditorManager' ),

// Get extension modules.
Paths = require( 'modules/Paths' ),
Preferences = require( 'modules/Preferences' ),
Processor = require( 'modules/Processor' ),

Expand Down Expand Up @@ -61,7 +62,7 @@ define( function( require ) {
}

// Only check CSS documents.
if ( document === DocumentManager.getCurrentDocument() && document.language.getName() === 'CSS' ) {
if ( document === DocumentManager.getCurrentDocument() && Paths.isFileType( document, [ 'css' ] ) ) {
process();
}
} );
Expand Down
21 changes: 20 additions & 1 deletion modules/Paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ define( function() {
'use strict';

// Get dependencies.
var ProjectManager = brackets.getModule( 'project/ProjectManager' ),
var LanguageManager = brackets.getModule( 'language/LanguageManager' ),
ProjectManager = brackets.getModule( 'project/ProjectManager' ),

// Variables.
projectRoot;
Expand All @@ -23,6 +24,23 @@ define( function() {
return ( file !== null && file.isFile === true && file.fullPath.indexOf( getProjectRoot() ) > -1 );
}

/**
* Check if file is of specified types.
*/
function isFileType( file, types ) {
var type = file.constructor.name.toLowerCase(),
path;

// Check type of object and get file path.
if ( type === 'file' ) {
path = file.fullPath;
} else if ( type === 'document' ) {
path = file.file.fullPath;
}

return types.indexOf( LanguageManager.getLanguageForPath( path ).getId() ) > -1
}

/**
* Make a full path relative to project root.
*/
Expand All @@ -38,6 +56,7 @@ define( function() {
// Return module.
return {
isFileInProjectRoot: isFileInProjectRoot,
isFileType: isFileType,
makeRelative: makeRelative,
projectRoot: getProjectRoot
};
Expand Down

0 comments on commit a9390b2

Please sign in to comment.