@sprucelabs/spruce-skill-utils
Version:
Loosely coupled classes and functions to make skill development faster! 🏎
123 lines (122 loc) • 3.99 kB
JavaScript
import os from 'os';
import { buildSchema, normalizeSchemaValues, SchemaError, validateSchemaValues, } from '@sprucelabs/schema';
import diskUtil from '../utilities/disk.utility.js';
import namesUtil from '../utilities/names.utility.js';
import EnvService from './EnvService.js';
import PkgService from './PkgService.js';
class AuthService {
constructor(envService, pkgService) {
this.env = envService;
this.pkg = pkgService;
}
static Auth(cwd) {
var _a;
if (!cwd) {
throw new SchemaError({
code: 'MISSING_PARAMETERS',
parameters: ['cwd'],
});
}
const pkgService = new PkgService(cwd);
const envService = new EnvService(cwd);
if (!pkgService.doesExist()) {
throw new SchemaError({
code: 'INVALID_PARAMETERS',
parameters: ['cwd'],
friendlyMessage: 'Could not find a package.json file!',
});
}
const auth = new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)(envService, pkgService);
return auth;
}
getLoggedInPerson() {
if (diskUtil.doesFileExist(this.personJsonPath)) {
const contents = diskUtil.readFile(this.personJsonPath);
const person = JSON.parse(contents);
return person;
}
return null;
}
setLoggedInPerson(person) {
const normalized = normalizeSchemaValues(personWithTokenSchema, person);
validateSchemaValues(personWithTokenSchema, normalized);
const destination = this.personJsonPath;
diskUtil.writeFile(destination, JSON.stringify(Object.assign(Object.assign({}, normalized), { isLoggedIn: true }), null, 2));
}
logOutPerson() {
diskUtil.deleteFile(this.personJsonPath);
}
getCurrentSkill() {
const id = this.env.get('SKILL_ID');
const apiKey = this.env.get('SKILL_API_KEY');
const name = this.env.get('SKILL_NAME');
const slug = this.pkg.get('skill.namespace');
if (id && apiKey) {
return {
id,
apiKey,
name,
slug,
};
}
return null;
}
logoutCurrentSkill() {
this.env.unset('SKILL_ID');
this.env.unset('SKILL_API_KEY');
this.env.unset('SKILL_NAME');
}
updateCurrentSkill(skill) {
this.env.set('SKILL_ID', skill.id);
this.env.set('SKILL_API_KEY', skill.apiKey);
this.env.set('SKILL_NAME', skill.name);
this.updateCurrentSkillNamespace(skill.slug);
}
updateCurrentSkillNamespace(namespace) {
this.pkg.set({
path: 'skill.namespace',
value: namesUtil.toKebab(namespace),
});
}
get personJsonPath() {
return diskUtil.resolvePath(AuthService.homeDir, '.spruce', 'person.json');
}
}
AuthService.homeDir = os.homedir();
export default AuthService;
const personWithTokenSchema = buildSchema({
id: 'personWithToken',
version: 'v2020_07_22',
namespace: 'SpruceCli',
name: '',
description: 'A stripped down cli user with token details for login',
fields: {
/** Id. */
id: {
label: 'Id',
type: 'id',
isRequired: true,
options: undefined,
},
/** Casual name. The name you can use when talking to this person. */
casualName: {
label: 'Casual name',
type: 'text',
isRequired: true,
hint: 'The name you can use when talking to this person.',
options: undefined,
},
/** . */
token: {
type: 'text',
isRequired: true,
options: undefined,
},
/** Logged in. */
isLoggedIn: {
label: 'Logged in',
type: 'boolean',
options: undefined,
},
},
});