Permalink
Browse files

Move all processing to Processor module.

1 parent a9390b2 commit c842f7add3f9287ab2a7919685c928ef66abfe3f @mikaeljorhult committed Jul 18, 2016
Showing with 65 additions and 31 deletions.
  1. +0 −2 main.js
  2. +7 −27 modules/AutoprefixOnChange.js
  3. +58 −2 modules/Processor.js
View
@@ -11,15 +11,13 @@ define( function( require ) {
// Get module dependencies.
var Menus = brackets.getModule( 'command/Menus' ),
CommandManager = brackets.getModule( 'command/CommandManager' ),
- EditorManager = brackets.getModule( 'editor/EditorManager' ),
AppInit = brackets.getModule( 'utils/AppInit' ),
// Get extension modules.
AutoprefixOnChange = require( 'modules/AutoprefixOnChange' ),
AutoprefixOnSave = require( 'modules/AutoprefixOnSave' ),
AutoprefixSelection = require( 'modules/AutoprefixSelection' ),
Preferences = require( 'modules/Preferences' ),
- Processor = require( 'modules/Processor' ),
Strings = require( 'modules/Strings' ),
SettingsDialog = require( 'modules/SettingsDialog' ),
@@ -25,24 +25,11 @@ define( function( require ) {
return;
}
- // Read file.
- file.read( function( error, content ) {
- var processedContent;
-
- // Only process file if it was read without errors.
- if ( !error ) {
- processedContent = Processor.process( content );
-
- // Store result if text was successfully prefixed.
- if ( processedContent ) {
- // Add filename to list of files being processed.
- processing.push( file.fullPath );
-
- // Write processed content to file.
- file.write( processedContent );
- }
- }
- } );
+ // Pass file for processing.
+ if ( Processor.process( file ) ) {
+ // Add filename to list of files being processed.
+ processing.push( file.fullPath );
+ }
}
/**
@@ -51,8 +38,7 @@ define( function( require ) {
AppInit.appReady( function() {
// Listeners for file changes.
FileSystem.on( 'change.autoprefixer', function( event, file ) {
- var document,
- processedText;
+ var document;
// Bail if change detection is not enabled.
if ( !Preferences.get( 'onChange' ) ) {
@@ -75,13 +61,7 @@ define( function( require ) {
return;
}
- // Process text from document.
- processedText = Processor.process( document.getText() )
-
- // Check that process was successful before replacing text.
- if ( processedText ) {
- document.refreshText( processedText, document.diskTimestamp );
- }
+ Processor.process( document );
}
// Go ahead and process.
View
@@ -7,13 +7,69 @@ define( function( require ) {
/**
* Process text using Autoprefixer.
*/
- function process( originalText ) {
+ function process( toProcess ) {
+ var type = toProcess.constructor.name.toLowerCase(),
+ processedText;
+
+ switch ( type ) {
+ case 'document':
+ processedText = processDocument( toProcess );
+ break;
+
+ case 'file':
+ processedText = processFile( toProcess );
+ break;
+
+ default:
+ processedText = processText( toProcess );
+ break;
+ }
+
+ return processedText;
+ }
+
+ function processDocument( document ) {
+ var processedText = processText( document.getText() )
+
+ // Check that process was successful before replacing text.
+ if ( processedText ) {
+ document.refreshText( processedText, document.diskTimestamp );
+
+ return true;
+ }
+
+ return false;
+ }
+
+ function processFile( file ) {
+ // Read file.
+ file.read( function( error, content ) {
+ var processedContent;
+
+ // Only process file if it was read without errors.
+ if ( !error ) {
+ processedContent = processText( content );
+
+ // Store result if text was successfully prefixed.
+ if ( processedContent ) {
+ // Write processed content to file.
+ file.write( processedContent );
+ }
+
+ return true;
+ }
+
+ return false;
+ } );
+ }
+
+ function processText( text ) {
var processedText = false,
browsers = Preferences.get( 'browsers' );
// Return false if not able to process.
try {
- processedText = autoprefixer.process( originalText, {
+ processedText = autoprefixer.process( text, {
browsers: browsers.length > 0 ? browsers : Preferences.defaults.browsers,
cascade: Preferences.get( 'visualCascade' )
} ).css;

0 comments on commit c842f7a

Please sign in to comment.