How to use the ensureFileSync function from fs-extra

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

fs-extra.ensureFileSync is a function in the fs-extra library that creates a file if it does not already exist.

63
64
65
66
67
68
69
70
71
72
73
}


exports.createTempStructure = function (files) {
  const tmpdir = tmp.dirSync({ unsafeCleanup: true }).name
  _.each(files, (content, filepath) => {
    fs.ensureFileSync(`${tmpdir}/${filepath}`)
    fs.writeFileSync(`${tmpdir}/${filepath}`, content)
  })
  return tmpdir
}
fork icon80
star icon649
watch icon21

+ 9 other calls in file

513
514
515
516
517
518
519
520
521
522
// try to create folder/file - if fail, return error
try {
    if ( params.cmd === 'newfolder') {
        fs.ensureDirSync(fullname)
    } else {
        fs.ensureFileSync(fullname)
    }
} catch (e) {
    const statusMsg = `could not create ${params.cmd === 'newfolder' ? 'folder' : 'file'}. url=${params.url}, cmd=${params.cmd}, folder=${params.folder}, error=${e.message}`
    log.error(`[uibuilder:adminRouterV3:POST] Admin API. ${statusMsg}`)
fork icon75
star icon346
watch icon24

+ 12 other calls in file

How does fs-extra.ensureFileSync work?

The fs-extra.ensureFileSync function is a part of the fs-extra library, which is a collection of utility functions for working with the file system in Node.js. When you call the fs-extra.ensureFileSync function, you pass in a path to a file as the first argument. The function then checks whether the file exists, and creates it if it does not already exist. If the file already exists, the function does nothing and returns immediately. The fs-extra.ensureFileSync function is synchronous, meaning that it blocks the execution of the rest of your code until it has finished creating the file. Overall, the fs-extra.ensureFileSync function provides a simple and reliable way to ensure that a file exists in a Node.js application, creating the file if necessary.

495
496
497
498
499
500
501
502
503
504

// *****
// File to persist plugin data
function get_cache() {
    let cache_file = path.join(RED.settings.userDir, "mcu-plugin-cache", "cache.json");
    fs.ensureFileSync(cache_file);
    let cache_data;
    try {
        cache_data = fs.readFileSync(cache_file, 'utf8');
    } catch (err) {
fork icon6
star icon21
watch icon3

+ 9 other calls in file

70
71
72
73
74
75
76
77
78
79
80
81
        16
    );
}


async function writeImage16Bit(filename, w, h, color_func, scale = 1, bit_depth = 16) {
    fs.ensureFileSync(filename);
    let ww = w * scale;
    let hh = h * scale;


    let img = new Image(ww, hh, {bitDepth: bit_depth} );
fork icon8
star icon11
watch icon8

+ 10 other calls in file

Ai Example

1
2
3
const fs = require("fs-extra");

fs.ensureFileSync("/path/to/file.txt");

In this example, we first require the fs-extra module in our Node.js application. We then call the ensureFileSync function with the path to the file /path/to/file.txt. The ensureFileSync function checks whether the file exists, and creates it if it does not already exist. If the file already exists, the function does nothing and returns immediately. Overall, this example demonstrates how to use fs-extra.ensureFileSync to create a file if it does not already exist in a Node.js application using the fs-extra library.

1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
const fileObj = generateTestFile();
let s;

before(done => {
  fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  fs.ensureFileSync(fileObj.path);
  fs.writeFileSync(fileObj.path, "exist");
  s = new RollingFileWriteStream(fileObj.path);
  s.write("now", "utf8", done);
});
fork icon0
star icon1
watch icon1

+ 41 other calls in file

1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
}
const templateString = fs.readFileSync(template, 'utf8')
const rendered = ejs.render(templateString, data, { async: false })
if (output) {
  const outputRendered = ejs.render(output, data, { async: false })
  fs.ensureFileSync(outputRendered)
  fs.writeFileSync(outputRendered, rendered, { encoding: 'utf-8' })
  ipcRenderer.send('console-log', `[StarUML] ${outputRendered}`)
} else {
  ipcRenderer.send('console-log', rendered)
fork icon0
star icon1
watch icon1

+ 2 other calls in file

115
116
117
118
119
120
121
122
123
124
  if (!result[brand][company].includes(type)) {
    result[brand][company].push(type)
  }
})

fs.ensureFileSync(dataFile)
fs.writeJSONSync(dataFile, result, { spaces: 2 })

ctx.body = {
  success: true
fork icon0
star icon0
watch icon1

+ 4 other calls in file

55
56
57
58
59
60
61
62
63
64
    const article = articles[i];
    const files = readArticleMenu(article);
    const name = article.replace(/.+\//, '');
    sidebar[`/${name}/`] = [generateMenu({ name, files })];
  }
  fs.ensureFileSync(resolve('../config/sidebar.json'));
  fs.writeFileSync(resolve('../config/sidebar.json'), JSON.stringify(sidebar, undefined, '  '));
  console.log('已生成sidebar');
}

fork icon0
star icon0
watch icon1

+ 6 other calls in file

function icon

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