Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: files splitted into configurable chunks, fix memory out #139

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/helpers/chopfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fs = require('fs');

module.exports = {
chopByFileSizeSync(filePath, encoding, chunkSize) {
const file = fs.readFileSync(filePath, encoding);
var chunks = Math.ceil(fs.statSync(filePath).size / chunkSize, chunkSize);
var chunk = 0;
const files = [];
while (chunk <= chunks) {
var offset = chunk * chunkSize;
files.push((file.slice(offset, offset + chunkSize)));
chunk++;
}
return files.filter(v => v);
},
chopByFileSizeAsync(filePath, encoding, chunkSize) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, encoding, (error, file) => {
if (error) {
return reject(error);
}
var chunks = Math.ceil(fs.statSync(filePath).size / chunkSize, chunkSize);
var chunk = 0;
const files = [];
while (chunk <= chunks) {
var offset = chunk * chunkSize;
files.push((file.slice(offset, offset + chunkSize)));
chunk++;
}
return resolve(files.filter(v => v));
});
});
}
}
1 change: 1 addition & 0 deletions lib/helpers/parse-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const defaults = {
verbose: false,
quiet: false,
dry: false,
chunkSize: 300 * 300,
glob: {},
cwd: null,
};
Expand Down
35 changes: 17 additions & 18 deletions lib/helpers/replace-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,40 @@
*/
const fs = require('fs');
const makeReplacements = require('./make-replacements');
const chopByFileSize = require('./chopfile').chopByFileSizeAsync;

/**
* Helper to replace in a single file (async)
*/
module.exports = function replaceAsync(file, from, to, config) {
module.exports = function replaceAsync(filePath, from, to, config) {

//Extract relevant config
const {encoding, dry, countMatches} = config;
const { encoding, dry, countMatches, chunkSize } = config;

//Wrap in promise
return new Promise((resolve, reject) => {
fs.readFile(file, encoding, (error, contents) => {
//istanbul ignore if
if (error) {
return reject(error);
}

//Make replacements
const [result, newContents] = makeReplacements(
contents, from, to, file, countMatches
);

chopByFileSize(filePath, encoding, chunkSize).then(chunks => {
let result = [];
chunks.forEach((chunk, i) => {
//Make replacements
const response = makeReplacements(
chunk, from, to, chunk, countMatches
);
chunks[i] = response[1];
result.push(response[0].hasChanged);
});
//Not changed or dry run?
if (!result.hasChanged || dry) {
if (result.filter(v => v).length === 0 || dry) {
return resolve(result);
}

//Write to file
fs.writeFile(file, newContents, encoding, error => {
fs.writeFile(filePath, chunks.join(''), encoding, error => {
//istanbul ignore if
if (error) {
return reject(error);
}
resolve(result);
});
});
}, err => reject(err));
});
};
}
30 changes: 17 additions & 13 deletions lib/helpers/replace-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@
*/
const fs = require('fs');
const makeReplacements = require('./make-replacements');
const chopByFileSize = require('./chopfile').chopByFileSizeSync;

/**
* Helper to replace in a single file (sync)
*/
module.exports = function replaceSync(file, from, to, config) {

module.exports = function replaceSync(filePath, from, to, config) {
//Extract relevant config and read file contents
const {encoding, dry, countMatches} = config;
const contents = fs.readFileSync(file, encoding);

//Replace contents and check if anything changed
const [result, newContents] = makeReplacements(
contents, from, to, file, countMatches
);
const { encoding, dry, countMatches, chunkSize } = config;
const chunks = chopByFileSize(filePath, encoding, chunkSize);
let result;
let newContents;
chunks.forEach((chunk, i) => {

//Contents changed and not a dry run? Write to file
if (result.hasChanged && !dry) {
fs.writeFileSync(file, newContents, encoding);
}
//Replace contents and check if anything changed
[result, newContents] = makeReplacements(
chunk, from, to, chunk, countMatches
);

//Contents changed and not a dry run? Write to file
if (result.hasChanged && !dry) {
chunks[i] = newContents;
}
});
fs.writeFileSync(filePath, chunks.join(''), encoding);
//Return result
return result;
};