Skip to content

Instantly share code, notes, and snippets.

@mopcweb
Last active September 29, 2019 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mopcweb/c5f5e535fb060f40cac51ea41f13d191 to your computer and use it in GitHub Desktop.
Save mopcweb/c5f5e535fb060f40cac51ea41f13d191 to your computer and use it in GitHub Desktop.
Inserts data into file into
/* ------------------------------------------------------------------- */
/**
* Inserts data into file
*
* @param path - File path
* @param data - Data to insert
* @param [after] - String | RegExp, after which to insert
* @param [before] - String | RegExp, before which to insert
* @param [removeNewLine] - Whether to remove empty newLine
* after / before insertion position
*/
/* ------------------------------------------------------------------- */
export const insert = (
path: string, data: any, after?: string | RegExp, before?: string | RegExp,
removeNewLine = false
) => {
// Read file
const readed = fs.readFileSync(path, 'utf-8');
// Get position to insert (to the end of file by default)
let index = readed.length;
let dataWithOffset: any = '\n' + data;
if (after) {
// Find as RegExp and as string
const match = readed.match(after);
const indexOf = readed.indexOf(after as string);
// If as RegExp found
if (match && match[0] && match.index !== -1)
removeNewLine
? index = match.index + match[0].length + 1
: index = match.index + match[0].length;
// Else if as string found
else if (indexOf !== -1 && typeof after === 'string')
removeNewLine
? index = indexOf + after.length + 1
: index = indexOf + after.length;
}
else if (before) {
// Find as RegExp and as string
const match = readed.match(before);
const indexOf = readed.indexOf(before as string);
// If as RegExp found
if (match && match[0] && match.index !== -1) {
removeNewLine
? index = match.index - 1
: index = match.index;
dataWithOffset = data + '\n';
}
// Else if as string found
else if (indexOf !== -1 && typeof before === 'string') {
removeNewLine
? index = indexOf - 1
: index = indexOf;
dataWithOffset = data + '\n';
}
}
// Get file text, which would be overwriten by new
const substring = readed.substring(index);
// Data to insert
const text = Buffer.from(dataWithOffset + substring);
// Open file to get its file descriptor (fd)
const file = fs.openSync(path, 'r+');
// Write
fs.writeSync(file, text, 0, text.length, index);
// Close
fs.closeSync(file);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment