How to use the readFileSync function from graceful-fs

Find comprehensive JavaScript graceful-fs.readFileSync code examples handpicked from public code repositorys.

144
145
146
147
148
149
150
151
152
  const src = path.join(TEST_DIR, 'a-file')
  const dest = path.join(TEST_DIR, 'a-file-dest')

  await fse.move(src, dest)

  const contents = fs.readFileSync(dest, 'utf8')
  const expected = /^sonic the hedgehog\r?\n$/
  assert.ok(contents.match(expected))
})
fork icon802
star icon0
watch icon91

+ 3 other calls in file

1060
1061
1062
1063
1064
1065
1066
1067
1068
1069

const fileAbsolute = path.resolve(this._config.projectRoot, file ?? '');
try {
  return {
    content: codeFrameColumns(
      fs.readFileSync(fileAbsolute, 'utf8'),
      {
        // Metro returns 0 based columns but codeFrameColumns expects 1-based columns
        // $FlowFixMe[unsafe-addition]
        start: {column: column + 1, line: lineNumber},
fork icon615
star icon0
watch icon99

+ 22 other calls in file

478
479
480
481
482
483
484
485
486
487
			return `require(${JSON.stringify(`./${arg}`)})`;
		})
		.join(", ")});`;
} else {
	p = path.join(currentDirectory, module);
	content = fs.readFileSync(p, "utf-8");
	const lastSlash = module.lastIndexOf("/");
	let firstSlash = module.indexOf("/");

	if (lastSlash !== -1 && firstSlash !== lastSlash) {
fork icon163
star icon0
watch icon24

+ 13 other calls in file

20
21
22
23
24
25
26
27
28
29
const destFile = path.join(dest, filename);
const directory = fs.statSync(srcFile).isDirectory();
if (directory) {
	copyDiff(srcFile, destFile, initial);
} else {
	var content = fs.readFileSync(srcFile);
	if (/^DELETE\s*$/.test(content.toString("utf-8"))) {
		fs.unlinkSync(destFile);
	} else if (/^DELETE_DIRECTORY\s*$/.test(content.toString("utf-8"))) {
		rimraf.sync(destFile);
fork icon163
star icon0
watch icon24

+ 41 other calls in file

426
427
428
429
430
431
432
433
434
435
});
cleanups.push(() => (esmContext.it = undefined));
function _require(module, esmMode) {
	if (module.startsWith("./")) {
		const p = path.join(outputDirectory, module);
		const content = fs.readFileSync(p, "utf-8");
		if (p.endsWith(".mjs")) {
			let esm;
			try {
				esm = new vm.SourceTextModule(content, {
fork icon163
star icon0
watch icon24

+ 13 other calls in file

224
225
226
227
228
229
230
231
232
233
234
235
236
function readFileSync(path, options = {}) {
  if (!path) throw new TypeError('path is required!');


  if (!Object.prototype.hasOwnProperty.call(options, 'encoding')) options.encoding = 'utf8';


  const content = fs.readFileSync(path, options);


  if (options.escape == null || options.escape) {
    return escapeFileContent(content);
  }
fork icon9
star icon48
watch icon0

+ 7 other calls in file

37
38
39
40
41
42
43
44
45
46
  this.__emittedFinish = true
  t.equal(ev, 'finish', num + '. finished')
}

// make sure that one of the atomic streams won.
var res = fs.readFileSync(target, 'utf8')
var lines = res.trim().split(/\n/)
lines.forEach(function (line, lineno) {
  var first = lines[0].match(/\d+$/)[0]
  var cur = line.match(/\d+$/)[0]
fork icon1
star icon1
watch icon0

1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
    } catch (error) {
      // Ignore error
    }

    // It is file
    return stats ? fs.readFileSync(item) : item;
  }
};

/** @type {any} */
fork icon0
star icon0
watch icon1

+ 5 other calls in file

136
137
138
139
140
141
142
143
144
145
146
147
    return Function('return this')();
  }
})();


var jestReadFile =
  global[Symbol.for('jest-native-read-file')] || fs.readFileSync;


// prettier-ignore
const babelTraverse = // @ts-expect-error requireOutside Babel transform
require(require.resolve('@babel/traverse', {
fork icon0
star icon0
watch icon1

45
46
47
48
49
50
51
52
53
54
55
const mainSync = (fp, data, opts) => {
	let indent = opts.indent;


	if (opts.detectIndent) {
		try {
			const file = fs.readFileSync(fp, 'utf8');
			indent = detectIndent(file).indent;
		} catch (err) {
			if (err.code !== 'ENOENT') {
				throw err;
fork icon0
star icon0
watch icon1

+ 45 other calls in file

454
455
456
457
458
459
460
461
462
463
464
465
**/


const fs = require('graceful-fs');
const path = require('path');


const generateRaw = fs.readFileSync(path.join(__dirname, '_generate.js'), 'utf8');
const generateBlocks = generateRaw
.split(/((?:\/\*\*)((?!\*\*\/)[^\r\n]*\r?\n)+)/g)
.filter(Boolean)
.filter(str => !str.startsWith('**/'))
fork icon0
star icon0
watch icon1

+ 45 other calls in file

989
990
991
992
993
994
995
996
997
998
  continue;
}

return {
  content: codeFrameColumns(
    fs.readFileSync(file, "utf8"),
    {
      // Metro returns 0 based columns but codeFrameColumns expects 1-based columns
      start: {
        column: column + 1,
fork icon0
star icon0
watch icon1

+ 20 other calls in file

40
41
42
43
44
45
46
47
48
49
50
51
52
function file_write(fd, offset, data) {
	return fs.writeSync(fd, data, 0, data.length, offset);
}


function file_read_all(fd) {
	return fs.readFileSync(fd);
}




var loadedProts = {}; // protections
fork icon0
star icon0
watch icon0

217
218
219
220
221
222
223
224
225
226
227
228
function bucketEntries (bucket, filter) {
  return readFile(bucket, 'utf8').then((data) => _bucketEntries(data, filter))
}


function bucketEntriesSync (bucket, filter) {
  const data = fs.readFileSync(bucket, 'utf8')
  return _bucketEntries(data, filter)
}


function _bucketEntries (data, filter) {
fork icon0
star icon0
watch icon1

+ 2 other calls in file