-
Notifications
You must be signed in to change notification settings - Fork 1
/
utility.js
39 lines (33 loc) · 1.04 KB
/
utility.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'use strict';
const fs = require('fs');
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const writeFile = (fileName, programName) => {
// do work
const today = new Date();
const fileLine = `\n\nhello from the ${programName} program ${today.toLocaleDateString()} ${today.toLocaleTimeString()}`;
fs.appendFileSync(fileName, fileLine, (err) => {
if (err) {
console.error(err);
}
});
};
const outputFile = (fileName) => {
// originally copied from https://nodejs.dev/en/learn/reading-files-with-nodejs
// write out file contents to screen
console.log('\n\n final contents of file');
try {
const fileContents = fs.readFileSync(fileName, 'utf8');
console.log(fileContents);
} catch (error) {
console.error(error);
}
};
const exampleFile = 'files/HelloWorld.txt';
module.exports.sleep = sleep;
module.exports.exampleFile = exampleFile;
module.exports.writeFile = writeFile;
module.exports.outputFile = outputFile;