@yarnpkg/fslib
Advanced tools
Comparing version 2.0.0-rc.13 to 2.0.0-rc.14
@@ -6,4 +6,7 @@ "use strict"; | ||
}); | ||
exports.normalizeLineEndings = normalizeLineEndings; | ||
exports.BasePortableFakeFS = exports.FakeFS = void 0; | ||
var _os = require("os"); | ||
var _copyPromise = require("./algorithms/copyPromise"); | ||
@@ -190,26 +193,30 @@ | ||
async changeFilePromise(p, content) { | ||
async changeFilePromise(p, content, { | ||
automaticNewlines | ||
} = {}) { | ||
let current = ''; | ||
try { | ||
const current = await this.readFilePromise(p, `utf8`); | ||
if (current === content) { | ||
return; | ||
} | ||
current = await this.readFilePromise(p, `utf8`); | ||
} catch (error) {// ignore errors, no big deal | ||
} | ||
await this.writeFilePromise(p, content); | ||
const normalizedContent = automaticNewlines ? normalizeLineEndings(current, content) : content; | ||
if (current === normalizedContent) return; | ||
await this.writeFilePromise(p, normalizedContent); | ||
} | ||
changeFileSync(p, content) { | ||
changeFileSync(p, content, { | ||
automaticNewlines = false | ||
} = {}) { | ||
let current = ''; | ||
try { | ||
const current = this.readFileSync(p, `utf8`); | ||
if (current === content) { | ||
return; | ||
} | ||
current = this.readFileSync(p, `utf8`); | ||
} catch (error) {// ignore errors, no big deal | ||
} | ||
this.writeFileSync(p, content); | ||
const normalizedContent = automaticNewlines ? normalizeLineEndings(current, content) : content; | ||
if (current === normalizedContent) return; | ||
this.writeFileSync(p, normalizedContent); | ||
} | ||
@@ -379,2 +386,14 @@ | ||
exports.BasePortableFakeFS = BasePortableFakeFS; | ||
exports.BasePortableFakeFS = BasePortableFakeFS; | ||
function getEndOfLine(content) { | ||
const matches = content.match(/\r?\n/g); | ||
if (matches === null) return _os.EOL; | ||
const crlf = matches.filter(nl => nl === `\r\n`).length; | ||
const lf = matches.length - crlf; | ||
return crlf > lf ? `\r\n` : `\n`; | ||
} | ||
function normalizeLineEndings(originalContent, newContent) { | ||
return newContent.replace(/\r?\n/g, getEndOfLine(originalContent)); | ||
} |
@@ -50,2 +50,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "normalizeLineEndings", { | ||
enumerable: true, | ||
get: function () { | ||
return _FakeFS.normalizeLineEndings; | ||
} | ||
}); | ||
Object.defineProperty(exports, "FakeFS", { | ||
@@ -52,0 +58,0 @@ enumerable: true, |
{ | ||
"name": "@yarnpkg/fslib", | ||
"version": "2.0.0-rc.13", | ||
"version": "2.0.0-rc.14", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
107343
3264