-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelperFunctions.js
More file actions
98 lines (98 loc) · 3.13 KB
/
Copy pathhelperFunctions.js
File metadata and controls
98 lines (98 loc) · 3.13 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var readline = require("readline");
// Login function
function ProcessLine(user, pass, line) {
console.log("This is user: " + user);
console.log("this is pass: " + pass);
console.log("This is line: " + line);
var userPassArray = line.split(' ');
var lineUser = userPassArray[0];
var linePass = userPassArray[1];
console.log("This is lineUser: " + lineUser);
console.log("This is linePass: " + linePass);
return user === lineUser && pass === linePass;
}
function login(user, pass) {
try {
var fileStream = fs.createReadStream('./loginCredentials.txt', 'utf-8');
var readingInterface_1 = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
var successfulLogin_1 = false;
readingInterface_1.on('line', function (line) {
if (ProcessLine(user, pass, line)) {
console.log("successful log in");
successfulLogin_1 = true;
readingInterface_1.close();
}
});
readingInterface_1.on('close', function () {
console.log('File reading completed.');
console.log(successfulLogin_1);
return successfulLogin_1;
});
}
catch (error) {
console.error(error);
}
}
// Test the login function
console.log(login("angie", "password"));
function postText(fileName, info) {
// need to check for unique file otherwise would overwrite old file and we dont want that +-
var newFileName = fileName + ".txt";
var filePath = "./posts/" + newFileName;
console.log(newFileName);
fs.access(filePath, fs.constants.F_OK, function (err) {
if (!err) {
console.error('Error: File already exists');
}
else {
fs.writeFile(filePath, info, function (err) {
if (err) {
console.error('Error writing to file:', err);
return true;
}
else {
console.log('File created and written successfully.');
return false;
}
});
}
});
}
// test postText function
postText("hello", "hello my name is Alvin");
postText("hi", "hello my name is Alvin");
function editPost(fileName, editedInfo) {
var filePath = "./posts/" + fileName;
if (fs.existsSync(filePath)) {
fs.writeFileSync(filePath, editedInfo);
console.log('File written successfully.');
return true;
}
else {
console.error('File does not exist.');
return false;
}
}
//test editPost function
editPost("hello.txt", "hello my name is not alvin");
function getfileInformation(fileName) {
var filePath = "./posts/" + fileName;
try {
var data = fs.readFileSync(filePath, 'utf8');
console.log(data);
return data;
}
catch (err) {
console.error('Error reading file:', err);
}
}
//test getFileInformation()
getfileInformation("hello.txt");
//function getImg
// function postImg() Add this function later