How to use the readFileSync function from fs-extra

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

439
440
441
442
443
444
445
446
447
448
 */
function addMissingConfiguration() {
  const templateFolder = path.resolve(__dirname, '..', 'template');
  const packageJsonPath = path.join('.', 'package.json');
  const templatePackageJson = JSON.parse(
    fs.readFileSync(path.join(templateFolder, 'package.json'), 'utf8')
  );
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));

  let changed = false;
fork icon58
star icon932
watch icon20

412
413
414
415
416
417
418
419
420
421
	maxEnrollments: -1,
	enrollmentSecret: 'userpw'
};
await caService.register(newUser, admin);

const myCsr = fs.readFileSync(path.resolve(__dirname, '../fixtures/fabricca/test.csr'), 'utf8');

const req = {
	enrollmentID: newUser.enrollmentID,
	enrollmentSecret: newUser.enrollmentSecret,
fork icon522
star icon768
watch icon62

+ 27 other calls in file

185
186
187
188
189
190
191
192
193
194
	const files = fs.readdirSync(dir);
	const certs = [];
	files.forEach((file_name) => {
		const file_path = path.join(dir, file_name);
		logger.debug(' looking at file ::' + file_path);
		const data = fs.readFileSync(file_path);
		certs.push(data);
	});
	return certs;
}
fork icon522
star icon768
watch icon62

+ 13 other calls in file

458
459
460
461
462
463
464
465
466
467
468
});


gulp.task('validateTelemetry', async () => {
    const gdprTS = fs.readFileSync(path.join(__dirname, 'src', 'gdpr.ts'), 'utf-8');
    await generateTelemetry();
    const gdprTS2 = fs.readFileSync(path.join(__dirname, 'src', 'gdpr.ts'), 'utf-8');
    if (gdprTS2.trim() !== gdprTS.trim()) {
        console.error('src/gdpr.ts is not valid, please re-run `npm run generateTelemetry`');
        throw new Error('src/gdpr.ts is not valid, please re-run `npm run generateTelemetry`');
    }
fork icon208
star icon950
watch icon43

+ 23 other calls in file

111
112
113
114
115
116
117
118
119
120
  uploadTestDataToBucket(config.bucket, s3data, testDataFolder),
  addCollections(config.stackName, config.bucket, collectionsDir, testSuffix, testId, collectionDupeHandling),
  apiTestUtils.addProviderApi({ prefix: config.stackName, provider: providerData }),
]);

const inputPayloadJson = fs.readFileSync(inputPayloadFilename, 'utf8');
// update test data filepaths
inputPayload = await setupTestGranuleForIngest(config.bucket, inputPayloadJson, granuleRegex, testSuffix, testDataFolder);
pdrFilename = inputPayload.pdr.name;
const granuleId = inputPayload.granules[0].granuleId;
fork icon100
star icon223
watch icon24

+ 14 other calls in file

57
58
59
60
61
62
63
64
65
66
67
68
  return exports.file(opts)
}


exports.fromDisk = function (filename) {
  const filepath = path.join(__dirname, '..', 'test-fixtures', filename)
  return fs.readFileSync(filepath)
}


exports.createTempStructure = function (files) {
  const tmpdir = tmp.dirSync({ unsafeCleanup: true }).name
fork icon80
star icon649
watch icon20

+ 9 other calls in file

2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
};


var safeReadFileSync = toolkit.safeReadFileSync = function(filePath, type) {
  var data = '';
  try {
    data = fs.readFileSync(filePath).toString();
  } catch(err) {
    console.log(`Reading ${filePath} failed. Origin error:\n${err.toString()}`);
  }

fork icon34
star icon183
watch icon6

+ 16 other calls in file

76
77
78
79
80
81
82
83
84
85
	}
}

let author_file = path.join(mods.path, file, "thunderstore_author.txt");
if (fs.existsSync(author_file)) {
	obj.Author = fs.readFileSync(author_file, "utf8");
}

if (obj.Disabled) {
	disabled.push(obj);
fork icon17
star icon106
watch icon4

+ 22 other calls in file

70
71
72
73
74
75
76
77
78
79
if (features.includes("Web Manifest")) {
  integrateManifest(projectDir, lang, dir);
}

// Register worker in `entry.client.tsx`
const remoteClientContent: string = fse.readFileSync(projectDir + `/${dir}/entry.client.` + lang + "x").toString();
const ClientContent = fse.readFileSync(appDir + "/entry.client." + lang).toString();

if (features.includes("Service Workers")) {
  remoteClientContent.includes(ClientContent)
fork icon16
star icon257
watch icon5

+ 215 other calls in file

81
82
83
84
85
86
87
88
89
90
  PG_DATABASE: testDbName,
};

const messageConsumer = await awsServices.lambda().createFunction({
  Code: {
    ZipFile: fs.readFileSync(require.resolve('@cumulus/test-data/fake-lambdas/hello.zip')),
  },
  FunctionName: randomId('messageConsumer'),
  Role: randomId('role'),
  Handler: 'index.handler',
fork icon100
star icon223
watch icon23

+ 23 other calls in file

1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
var _match = email.html.match(`cid:${_contentid}`);
if (_match) {
	var _data_string;
	// Read file as base64
	if ( _cid === 'base64' ) {
		var _imageAsBase64 = fs.readFileSync(_attachment_full_path, 'base64');
		// Replace
		_data_string = `data:${attachment.contentType};base64,${_imageAsBase64}`;
	}
	else if ( _cid === 'path' ) {
fork icon17
star icon28
watch icon6

+ 9 other calls in file

1660
1661
1662
1663
1664
1665
1666
1667
1668
var allFiles = this.vlocity.datapacksutils.getFiles(path.join(jobInfo.projectPath, jobInfo.importFolderName));

for (var i = 0; i < allFiles.length; i++) {
    var basename = allFiles[i];
       
    var dataJson = fs.readFileSync(path.join(jobInfo.projectPath, jobInfo.importFolderName, basename), 'utf8');

    try {
        let result = await this.vlocity.datapacks.import(JSON.parse(dataJson), this.getOptionsFromJobInfo(jobInfo));
fork icon80
star icon94
watch icon26

+ 35 other calls in file

983
984
985
986
987
988
989
990
991
992
993
    }
}


function changeCodecH264() {
    try {
        const jsonString = fs.readFileSync(path.join(__dirname, '..', "/src/codecs.json"));
        var json = JSON.parse(jsonString);
    } catch (err) {
        console.log(err);
        return;
fork icon14
star icon162
watch icon7

+ 269 other calls in file

116
117
118
119
120
121
122
123
124
125
if (!fs.existsSync(extDataPath)) {
    console.log('No ' + packageJson + ' found for: ' + extension);
    continue;
}

const content = fs.readFileSync(extDataPath, 'utf-8');
const extData = JSON.parse(content);

const extVsixPath = dist(extData.name + '-' + packVersion + '.vsix');
if (!(await isAvailable(extVsixPath, extData.name, packVersion))) {
fork icon32
star icon58
watch icon15

257
258
259
260
261
262
263
264
265
266
 * @param {string} file - Path to file
 * @param {string} a - Old text.
 * @param {string} b - New text.
 */
function replaceInFile(file, a, b) {
  let content = fs.readFileSync(file, 'utf8');
  content = content.replace(a, b);
  fs.writeFileSync(file, content);
}

fork icon29
star icon92
watch icon20

+ 103 other calls in file

13
14
15
16
17
18
19
20
21
get: (target, prop) => {
  const fullPath = path.join(cwd, filename);

  switch (prop) {
    case 'content':
      return fs.readFileSync(fullPath, 'utf8');

    case 'exists':
      return fs.existsSync(fullPath);
fork icon10
star icon59
watch icon42

248
249
250
251
252
253
254
255
256
257
}

avatar(owner) {
	let afile = this.avatar_path(owner);
	if (fs.existsSync(afile)) {
		return fs.readFileSync(afile).toString();
	} else {
		return "";
	}
}
fork icon8
star icon20
watch icon4

+ 4 other calls in file

232
233
234
235
236
237
238
239
240
241
    return file.indexOf(PART_META_EXT) !== -1;
});
let rangeMap = {};
for (const metaPartFile of metaPartFiles) {
    const metaPartFilePath = path.join(tmpPartPaths, metaPartFile);
    const content = fs.readFileSync(metaPartFilePath, {encoding: 'utf-8'});
    // Range string is of the form "bytes 67108864-100663295/*"
    const startRange = Number(content.split('-')[0].split(' ')[1].trim());
    rangeMap[startRange] = path.join(tmpPartPaths, path.parse(metaPartFilePath).name);
}
fork icon6
star icon30
watch icon1

2284
2285
2286
2287
2288
2289
2290
2291
2292
        not_found = true;
        files[f] = undefined;
        continue;
    }

    let fb = fs.readFileSync(fp);
    // files[f] = fb.toJSON();
    files[f] = fb.toString("binary");
}
fork icon5
star icon18
watch icon3

+ 44 other calls in file

315
316
317
318
319
320
321
322
323
324
  await this.addAssessment(updatingData);
} else {
  if (data.name !== undefined) {
    data.name = `${exerciseDetails.name}_${data.name}`;
    if (fs.existsSync(`${courseFolderName}`)) {
      const courseData = fs.readFileSync(
        `${courseFolderName}/${assessmentData.name}.json`
      );
      const allJsonData = courseData.toString().trim();
      fs.renameSync(
fork icon18
star icon15
watch icon4

+ 15 other calls in file

function icon

fs-extra.readFileSync is the most popular function in fs-extra (9724 examples)