How to use the ensureFile function from fs-extra

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

217
218
219
220
221
222
223
224
225
226
	tmpDir = await fs.promises.mkdtemp(prefix);

	debugFilePath  = path.join(tmpDir, 'debug.log');
	errorFilePath = path.join(tmpDir, 'error.log');
	await fs.ensureFile(debugFilePath);
	await fs.ensureFile(errorFilePath);
});

it('should write to a log file with the correct logging level', async () => {
	testUtils.setHFCLogging(`{"debug": "${debugFilePath}", "error": "${errorFilePath}"}`);
fork icon522
star icon768
watch icon62

+ 27 other calls in file

34
35
36
37
38
39
40
41
42
43
await fs.outputJson(packageJSON, generatePackageJSON(dir, dbengine), { spaces: 2 });

const sql = join(cwd, dir, 'db', 'tasks.sql');
await fs.outputFile(sql, generateSQL(name, dbengine));

await fs.ensureFile(join(cwd, dir, 'db', 'development.sqlite3'));
await fs.ensureFile(join(cwd, dir, 'db', 'test.sqlite3'));
await fs.ensureFile(join(cwd, dir, 'db', 'production.sqlite3'));

// static files
fork icon36
star icon0
watch icon2

+ 5 other calls in file

34
35
36
37
38
39
40
41
42
43
await fs.outputJson(packageJSON, generatePackageJSON(dir, dbengine), { spaces: 2 });

const sql = join(cwd, dir, 'db', 'tasks.sql');
await fs.outputFile(sql, generateSQL(name, dbengine));

await fs.ensureFile(join(cwd, dir, 'db', 'development.sqlite3'))
await fs.ensureFile(join(cwd, dir, 'db', 'test.sqlite3'))
await fs.ensureFile(join(cwd, dir, 'db', 'production.sqlite3'))

// static files
fork icon36
star icon0
watch icon1

+ 5 other calls in file

134
135
136
137
138
139
140
141
142
143
  ),
  icns.encode()
);

// create an empty icon file in the app folder
await fs.ensureFile(
  path.resolve(process.cwd(), `${appDistributionName}.app`, "Icon")
);

//
fork icon15
star icon187
watch icon0

46
47
48
49
50
51
52
53
54
55
56


const filename = async (req, file, cb) => {
  try {
    const uid = nanoid()
    // creating empty file before streaming seems to fix some weird bugs with NFS
    await fs.ensureFile(path.join(tmpDir, uid))
    cb(null, uid)
  } catch (err) {
    cb(err)
  }
fork icon6
star icon26
watch icon4

+ 3 other calls in file

331
332
333
334
335
336
337
338
339
340
341
342
// Used by extender worker to produce the "full" version of the file
exports.writeExtendedStreams = async (db, dataset) => {
  if (dataset.isRest) return restDatasetsUtils.writeExtendedStreams(db, dataset)
  const tmpFullFile = await tmp.tmpName({ dir: path.join(dataDir, 'tmp') })
  // creating empty file before streaming seems to fix some weird bugs with NFS
  await fs.ensureFile(tmpFullFile)


  const writeStream = fs.createWriteStream(tmpFullFile)


  const relevantSchema = dataset.schema.filter(f => !f['x-calculated'])
fork icon6
star icon26
watch icon4

41
42
43
44
45
46
47
48
49
50
};
//restarts flows by setFlow method
let restartFlows = (Red, details) => {
    console.log('restartFlows');
    //Ensure that process is completed
    fs.ensureFile(Red.settings.userDir + "/" + DONE_FILE);
    return new Promise((resolve, reject) => {
        if (details.nodeNames && details.installer) {
            reject("Device needs to restart after customnodes installed");
        } else {
fork icon0
star icon2
watch icon0

104
105
106
107
108
109
110
111
112
113
        return false;
    }
};
// Creates Done file on Error, Ensure that process is completed
let addDoneFile = () => {
    fs.ensureFile(userDir + "/" + DONE_FILE);
};
//get called when state is installed, gets appinfo file from app installed path & trigger promise
let flowInstaller = (input, callback) => {
    console.log("flowInstaller :");
fork icon0
star icon2
watch icon0

2
3
4
5
6
7
8
9
10
11
12
const path = require('path')
const fs = require('fs-extra')


const withStreamableFile = async (filePath, fn) => {
  // creating empty file before streaming seems to fix some weird bugs with NFS
  await fs.ensureFile(filePath + '.tmp')
  await fn(fs.createWriteStream(filePath + '.tmp'))
  // Try to prevent weird bug with NFS by forcing syncing file before reading it
  const fd = await fs.open(filePath + '.tmp', 'r')
  await fs.fsync(fd)
fork icon0
star icon0
watch icon0

58
59
60
61
62
63
64
65
66
67
}

await log.step('Téléchargement du fichier')
const tmpFile = path.join(tmpDir, 'file')
// creating empty file before streaming seems to fix some weird bugs with NFS
await fs.ensureFile(tmpFile)

const url = new URL(processingConfig.url)
let filename = decodeURIComponent(path.parse(processingConfig.url).base)
if (url.protocol === 'http:' || url.protocol === 'https:') {
fork icon0
star icon0
watch icon0

11
12
13
14
15
16
17
18
19
20
// this is used only in dev
if (await fs.pathExists(file)) {
  await log.info(`Le fichier ${file} existe déjà`)
} else {
  // creating empty file before streaming seems to fix some weird bugs with NFS
  await fs.ensureFile(file)
  await log.info('Télécharge le fichier ' + pluginConfig.url)
  try {
    const res = await axios.get(pluginConfig.url, { responseType: 'stream' })
    await pump(res.data, fs.createWriteStream(file))
fork icon0
star icon0
watch icon0

315
316
317
318
319
320
321
322
323
324
ensure: callback => {
    if (!isWriter) {
        return callback();
    }

    Fs.ensureFile(clientContext.server.path(location), callback);
},
open: callback => {
    if (!isWriter) {
        return callback();
fork icon0
star icon0
watch icon0

+ 4 other calls in file

194
195
196
197
198
199
200
201
202
203
}
        }

        // Something must write to build_path/build.log where the file is tailed from to websocket...
        var build_log_path = path + "/" + build_id.replace(/\//g, '\\\\') + "/build.log"; // lgtm [js/path-injection]
        fs.ensureFile(build_log_path, function (err) { // lgtm [js/path-injection]
            if (err) {
                console.log(`[error] Log file could not be created: ${err}`);
            } else {
                // deepcode ignore PT: it's expected to be allowed to limit access
fork icon0
star icon0
watch icon0

44
45
46
47
48
49
50
51
52
 * @return {void}
 */
async createFile() {
  if (!this.filePath) { throw new Error('File path is not defined.'); }
  if (!this.fields) { throw new Error('CSV fields are not defined.'); }
  await fse.ensureFile(this.filePath);
}


fork icon0
star icon0
watch icon0

function icon

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