Skip to content

Commit

Permalink
File: support prev map (issue #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Jul 16, 2017
1 parent af4478f commit a8cba72
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions lib/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var os = require('os'),
EOL = os.EOL,
path = require('path'),
url = require('url'),
fs = require('fs'),
mozilla = require('source-map'),
SourceMapGenerator = mozilla.SourceMapGenerator,
utils = require('./utils');
Expand Down Expand Up @@ -37,14 +39,67 @@ File.prototype = {
return this;
},

writeFileFromPrevMap: function (file, prev) {
if (!prev) {
return;
}

if (prev) {
if (typeof prev === 'string') {
return prev;
}

if (typeof prev === 'function') {
var prevPath = prev(file);

if (prevPath && fs.existsSync && fs.existsSync(prevPath)) {
return fs.readFileSync(prevPath, 'utf-8').toString().trim();
} else {
throw new Error('Unable to load previous source map: ' + prevPath.toString());
}
}

if (prev instanceof mozilla.SourceMapConsumer) {
return mozilla.SourceMapGenerator.fromSourceMap(prev).toString();
}

if (prev instanceof mozilla.SourceMapGenerator) {
return prev.toString();
}

if (isMap(prev)) {
return JSON.stringify(prev);
}

throw new Error('Unsupported previous source map format: ' + prev.toString());
}

if (this.inline) {
return this.decodeInline(this.annotation);
}

if (this.annotation) {
var map = this.annotation;
if (file) {
map = path.join(path.dirname(file), map);
}

this.root = path.dirname(map);

return (fs.existsSync && fs.existsSync(map)) ?
fs.readFileSync(map, 'utf-8').toString().trim() :
false;
}
},

writeLine: function (line) {
this.write(line + '\n');
this.write(line + EOL);

return this;
},

writeContent: function (content) {
this.write(content + '\n');
this.write(content + EOL);

return this;
},
Expand Down Expand Up @@ -121,7 +176,7 @@ File.prototype = {
* @returns {String}
*/
getContent: function () {
return this._content.join(os.EOL);
return this._content.join(EOL);
},

render: function () {
Expand All @@ -131,6 +186,14 @@ File.prototype = {
}
};

function isMap(map) {
if (typeof map !== 'object') {
return false;
}

return typeof map.mappings === 'string' || typeof map._mappings === 'string';
}

function getLines(text) {
return text.split(/\r\n|\r|\n/);
}
Expand Down

0 comments on commit a8cba72

Please sign in to comment.