Skip to content

Instantly share code, notes, and snippets.

@alancasagrande
Created November 16, 2020 00:48
Show Gist options
  • Select an option

  • Save alancasagrande/065636333ff494dbb4e858b05b4b9ca6 to your computer and use it in GitHub Desktop.

Select an option

Save alancasagrande/065636333ff494dbb4e858b05b4b9ca6 to your computer and use it in GitHub Desktop.
import { isArray } from 'util';
const fs = require('fs');
const storagePath = './storage';
const usersPath = `${storagePath}/users.json`;
const initialUsers = {
alan: {
username: 'alan',
password: '1',
},
jhon: {
username: 'jhon',
password: '2',
},
};
export function initStorage() {
if (!fs.existsSync(storagePath)) fs.mkdirSync(storagePath);
if (!fs.existsSync(usersPath)) setUsers(initialUsers);
}
export function getUser(username) {
return getUsers()[username];
}
export function setUser(user) {
const users = getUsers();
users[user.username] = user;
setUsers(users);
}
function getUsers() {
let users;
if (fs.existsSync(usersPath)) {
users = JSON.parse(fs.readFileSync(usersPath).toString());
}
return users || {};
}
function setUsers(users) {
fs.writeFileSync(usersPath, JSON.stringify(users, null, 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment