snips-sam
Version:
The Snips Assistant Manager
90 lines (79 loc) • 2.52 kB
text/typescript
const fs = require('fs-extra');
const os = require('os');
const localSamFolder = os.homedir() + '/.sam';
const localConfigurationFile = localSamFolder + '/raspi-credentials.json';
export interface Credentials {
hostname: string;
username: string;
}
export class Config {
static getCredentials = async (): Promise<Credentials> => {
return fs.ensureDir(localSamFolder)
.then(() => {
return fs.stat(localConfigurationFile);
})
.then((success) => {
return Config.readCredentialsOnDisk();
})
.then((credentials) => {
return credentials;
})
.catch((error) => {
// .sam folder created at this point, but not credentials,
throw new Error('No credentials found on disk');
});
}
static deleteCredentials = async () => {
return await fs.remove(localConfigurationFile);
}
static saveCredentialsOnDisk = async (credentials: Credentials) => {
return fs.outputFile(localConfigurationFile, JSON.stringify(credentials));
}
private static readCredentialsOnDisk = async (): Promise<Credentials> => {
try {
const credentials = await fs.readJson(localConfigurationFile);
return credentials;
} catch (e) {
throw new Error('Credentials not found');
}
}
static hostnameQuestion = [
{
name: 'hostname',
type: 'input',
default: 'hostname.local',
message: 'Enter your device hostname or IP:',
validate: (value) => {
if (value.length) {
return true;
}
return 'Hostname cannot be blank';
},
},
];
static usernameQuestion = [
{
name: 'username',
type: 'username',
default: 'pi',
message: 'Enter username for the device:',
validate: (value) => {
if (value.length) {
return true;
}
return 'Username cannot be blank';
},
},
];
static passwordQuestion = [
{
name: 'password',
type: 'password',
default: 'raspberry',
message: 'Enter password for the device:',
validate: (value) => {
return true;
},
},
];
}