[Pkg-javascript-commits] [less.js] 43/58: Added error handling to file write for both css file and source map file. #2408

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:28:31 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag v2.3.0
in repository less.js.

commit ea122a8ea5c914f5ac48bc627e42a5d4705f5cd9
Author: jurcovicovam <meri at meri.org>
Date:   Mon Jan 26 10:49:47 2015 +0100

    Added error handling to file write for both css file and source map file.
     #2408
---
 bin/lessc    | 88 ++++++++++++++++++++++++++++++++++++++++++++++--------------
 package.json |  7 ++---
 2 files changed, 72 insertions(+), 23 deletions(-)

diff --git a/bin/lessc b/bin/lessc
index 247bdcc..ca1cd94 100755
--- a/bin/lessc
+++ b/bin/lessc
@@ -3,8 +3,15 @@
 var path = require('path'),
     fs = require('../lib/less-node/fs'),
     os = require('os'),
+    errno,
     mkdirp;
 
+try {
+    errno = require('errno');
+} catch (err) {
+    errno = null;
+}
+
 var less = require('../lib/less-node'),
     pluginLoader = new less.PluginLoader(less),
     plugin,
@@ -341,14 +348,67 @@ if (options.depends) {
 }
 
 if (!sourceMapFileInline) {
-    var writeSourceMap = function(output) {
+    var writeSourceMap = function(output, onDone) {
         var filename = sourceMapOptions.sourceMapFullFilename;
         ensureDirectory(filename);
-        fs.writeFileSync(filename, output, 'utf8');
-        less.logger.info('lessc: wrote ' + filename);
+        fs.writeFile(filename, output, 'utf8', function (err) {
+            if (err) {
+                var description = "Error: ";
+                if (errno && errno.errno[err.errno]) {
+                    description += errno.errno[err.errno].description;
+                } else {
+                    description += err.code + " " + err.message;
+                }
+                less.logger.error('lessc: failed to create file ' + filename);
+                less.logger.error(description);
+            } else {
+                less.logger.info('lessc: wrote ' + filename);
+            }
+            onDone();
+        });
     };
 }
 
+var writeSourceMapIfNeeded = function(output, onDone) {
+    if (options.sourceMap && !sourceMapFileInline) {
+        writeSourceMap(output, onDone);
+    }
+};
+
+var writeOutput = function(output, result, onSuccess) {
+    if (output) {
+        ensureDirectory(output);
+        fs.writeFile(output, result.css, {encoding: 'utf8'}, function (err) {
+            if (err) {
+                var description = "Error: ";
+                if (errno && errno.errno[err.errno]) {
+                    description += errno.errno[err.errno].description;
+                } else {
+                    description += err.code + " " + err.message;
+                }
+                less.logger.error('lessc: failed to create file ' + output);
+                less.logger.error(description);
+            } else {
+                less.logger.info('lessc: wrote ' + output);
+                onSuccess();
+            }
+        });
+    } else if (!options.depends) {
+        process.stdout.write(result.css);
+        onSuccess();
+    }
+};
+
+var logDependencies = function(options, result) {
+    if (options.depends) {
+        var depends = "";
+        for(var i = 0; i < result.imports.length; i++) {
+            depends += result.imports[i] + " ";
+        }
+        console.log(depends);
+    }
+};
+
 var parseLessFile = function (e, data) {
     if (e) {
         console.log("lessc: " + e.message);
@@ -390,23 +450,11 @@ var parseLessFile = function (e, data) {
     less.render(data, options)
         .then(function(result) {
             if(!options.lint) {
-                if (output) {
-                    ensureDirectory(output);
-                    fs.writeFileSync(output, result.css, 'utf8');
-                    less.logger.info('lessc: wrote ' + output);
-                } else if (!options.depends) {
-                    process.stdout.write(result.css);
-                }
-                if (options.sourceMap && !sourceMapFileInline) {
-                    writeSourceMap(result.map);
-                }
-                if (options.depends) {
-                    var depends = "";
-                    for(var i = 0; i < result.imports.length; i++) {
-                        depends += result.imports[i] + " ";
-                    }
-                    console.log(depends);
-                }
+                writeOutput(output, result, function() {
+                    writeSourceMapIfNeeded(result.map, function() {
+                        logDependencies(options, result);
+                    });
+                });
             }
         },
         function(err) {
diff --git a/package.json b/package.json
index d9f686e..763dfed 100644
--- a/package.json
+++ b/package.json
@@ -40,13 +40,14 @@
     "test": "grunt test"
   },
   "optionalDependencies": {
+    "errno": "^0.1.1",
     "graceful-fs": "^3.0.5",
+    "image-size": "~0.3.5",
     "mime": "^1.2.11",
-    "request": "^2.51.0",
     "mkdirp": "^0.5.0",
-    "source-map": "^0.1.x",
     "promise": "^6.0.1",
-    "image-size": "~0.3.5"
+    "request": "^2.51.0",
+    "source-map": "^0.1.x"
   },
   "devDependencies": {
     "diff": "^1.0",

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git



More information about the Pkg-javascript-commits mailing list