How to use the appendFileSync function from fs-extra

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

fs-extra.appendFileSync is a method in the fs-extra library in Node.js that appends data to a file synchronously.

95
96
97
98
99
100
101
102
103
104
  integratePushNotifications(projectDir, lang, dir);

  const PushContent = fse.readFileSync(appDir + "/push.entry.client." + lang).toString();
  remoteClientContent.includes(PushContent)
    ? null
    : fse.appendFileSync(projectDir + `/${dir}/entry.client.` + lang + "x", `\n${PushContent}`);
}

// Acknowledge SW in the browser
const RootDir = projectDir + `/${dir}/root.` + lang + "x";
fork icon16
star icon293
watch icon5

+ 9 other calls in file

115
116
117
118
119
120
121
122
123
});

it('truncates on opening with altered file', function() {
    index = setupIndexWithEntries(5);
    index.close();
    fs.appendFileSync(index.fileName, 'foo');
    expect(() => index = createIndex(index.name)).to.not.throwError();
    expect(index.length).to.be(5);
});
fork icon4
star icon31
watch icon0

How does fs-extra.appendFileSync work?

fs-extra.appendFileSync() is a method in the fs-extra library that synchronously appends data to a file, creating the file if it does not already exist.

68
69
70
71
72
73
74
75
76
77
78
79
80
81


new Promise(async res => {
    await clearLogs()


    if(!fs.existsSync(`./etc/`)) fs.mkdirSync(`./etc/`);
    fs.appendFileSync(`./etc/debug.log`, `\n\n-- STARTED AT ${(moment().format(`MMM DD [at] hh:mm:ss A`)).toUpperCase()} --\n`);


    let ctx = {};


    let debugAppendQueue = [];
fork icon1
star icon4
watch icon1

+ 11 other calls in file

262
263
264
265
266
267
268
269
270
271

const gitignoreExists = fs.existsSync(path.join(appPath, '.gitignore'));
if (gitignoreExists) {
  // Append if there's already a `.gitignore` file there
  const data = fs.readFileSync(path.join(appPath, 'gitignore'));
  fs.appendFileSync(path.join(appPath, '.gitignore'), data);
  fs.unlinkSync(path.join(appPath, 'gitignore'));
} else {
  // Rename gitignore after the fact to prevent npm from renaming it to .npmignore
  // See: https://github.com/npm/npm/issues/1862
fork icon1
star icon0
watch icon2

+ 4 other calls in file

Ai Example

1
2
3
4
5
6
7
8
const fs = require("fs-extra");

try {
  fs.appendFileSync("example.txt", "Hello, world!");
  console.log("Text appended to file successfully!");
} catch (err) {
  console.error(err);
}

In the example, the fs-extra.appendFileSync method is used to append the text "Hello, world!" to the file example.txt. The method call is wrapped in a try-catch block to handle any errors that might occur, and a message is logged to the console if the text is appended successfully.

854
855
856
857
858
859
860
861
862
863
864
  addGitExclusion: (repoDir, exclusion) => {
    if (util.isGitExclusionExists(repoDir, exclusion)) {
      return
    }
    const excludeFileName = util.getGitInfoExcludeFileName(repoDir, true)
    fs.appendFileSync(excludeFileName, '\n' + exclusion)
  },
}


module.exports = util
fork icon0
star icon0
watch icon1

+ 5 other calls in file

121
122
123
124
125
126
127
128
129
130
// Set .gitignore file
const gitignoreExists = fs.existsSync(path.join(projectPath, '.gitignore'));
if (gitignoreExists) {
    // Append if there's already a `.gitignore` file there
    const data = fs.readFileSync(path.join(projectPath, 'gitignore'));
    fs.appendFileSync(path.join(projectPath, '.gitignore'), data);
    fs.unlinkSync(path.join(projectPath, 'gitignore'));
} else {
    // Rename gitignore after the fact to prevent npm from renaming it to .npmignore
    // See: https://github.com/npm/npm/issues/1862
fork icon0
star icon0
watch icon3

325
326
327
328
329
330
331
332
333
334
  `    'monitoring-configs/common/finatra',\n` +
  `    'monitoring-configs/useng/templates'\n` +
  '  ]\n' +
  ')\n';

fs.appendFileSync(buildFile, newTargetTemplate);

const transformTarget = "  name='alerts',\n" +
  "  dependencies=[\n";
const transformReplacement = "  name='alerts',\n" +
fork icon0
star icon0
watch icon1

+ 3 other calls in file

1204
1205
1206
1207
1208
1209
1210
1211
1212
  //req.session.pyerror = {code: 1, msg: errorData}
  console.log('Caught ERROR', errorData)
  //
  let errorFilePath = path.join(blastDir, 'pythonerror.log')
  //console.log(errorFilePath)
  fs.appendFileSync(errorFilePath, errorData)
  //throw new Error(errordata);
  
})
fork icon0
star icon0
watch icon1

205
206
207
208
209
210
211
212
213
214
        chmodr(path + "/" + build_id, 0o665, (cherr) => {
            if (cherr) {
                console.log(`[error] Failed to execute chmodr ${cherr}`);
            } else {
                // deepcode ignore PT: the path is internally built
                fs.appendFileSync(build_log_path, logline); // lgtm [js/path-injection]
            }
        });
    }
});
fork icon0
star icon0
watch icon0

function icon

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