Browse Source

Merge commit '6365a488f2b679f18d9aaa8405f16fdb9255a432'

commmit

main
deicidus 2 years ago
parent
commit
0d4f66820d
  1. 1127
      src/ao-lib/api.js
  2. 0
      src/ao-lib/crypto.js
  3. 120
      src/ao-lib/settings.js
  4. 27
      src/ao-lib/util.js

1127
src/ao-lib/api.js

File diff suppressed because it is too large Load Diff

0
src/ao-lib/crypto.ts → src/ao-lib/crypto.js

120
src/ao-lib/settings.js

@ -0,0 +1,120 @@
import { execSync } from 'child_process'
import fs from 'fs'
import { parse, stringify } from 'envfile'
export const AO_ENV_FILE_PATH = process.env.HOME + '/.ao/.env'
function createAoFolderIfDoesNotExist() {
}
// Check for an AO env file at ~/.ao/.env and returns true if it exists
export function checkAoEnvFile() {
try {
execSync(`[ -f "${AO_ENV_FILE_PATH}" ]`)
return true
} catch(err) {
return false
}
}
export function aoEnv(variable) {
let envFileContents = {}
try {
envFileContents = fs.readFileSync(AO_ENV_FILE_PATH)
} catch(err) {
if(err.code === 'ENOENT') {
//console.log('The .env file does not exist, so the requested value', variable, 'is empty.')
} else {
console.log('Unknown error loading .env file in aoEnv, aborting.')
}
return null
}
const parsedFile = parse(envFileContents)
if(!parsedFile.hasOwnProperty(variable)) {
return null
}
// Convert ENV idiom to programmatic types
switch(parsedFile[variable]) {
case '1':
case 'true':
case 'TRUE':
case 'yes':
case 'YES':
return true
case '0':
case 'false':
case 'FALSE':
case 'no':
case 'NO':
return false
}
return parsedFile[variable]
}
// Sets and saves the given ENV=value to the global ~/.ao/.env file
// If value is null, the env variable will be deleted
// Returns true if a change was made, false if no change was made or if it failed
export function setAoEnv(variable, value) {
createAoFolderIfDoesNotExist()
if(typeof variable !== 'string') {
console.log('ENV variable name must be a string for setAoEnv')
return false
}
// Convert types to standard ENV file idiom
switch(value) {
case true:
case 'TRUE':
case 'yes':
case 'YES':
value = '1'
break
case false:
case 'FALSE':
case 'no':
case 'NO':
value = '0'
}
let envFileContents = {}
try {
envFileContents = fs.readFileSync(AO_ENV_FILE_PATH)
} catch(err) {
if(err.code === 'ENOENT') {
console.log('The .env file hasn\'t been created yet, creating.')
} else {
console.log('Unknown error loading .env file in setAoEnv, aborting. Error:', err)
return false
}
}
const parsedFile = parse(envFileContents)
if(parsedFile[variable] == value) {
console.log(variable, 'is already', value, 'so no change was made.')
return false
}
if(value === null) {
delete parsedFile[variable]
} else {
parsedFile[variable] = value
}
const stringified = stringify(parsedFile)
fs.writeFileSync(AO_ENV_FILE_PATH, stringified)
// Confirm the variable was set in the .env file correctly
if(aoEnv(variable) != value) {
console.log('Value was not saved correctly, sorry.')
return false
}
return true
}
function setAndSaveEnvironmentVariable(variable, value, path) {
}

27
src/ao-lib/utils.ts → src/ao-lib/util.js

@ -1,3 +1,5 @@
// General helper functions
export const cancelablePromise = promise => { export const cancelablePromise = promise => {
let isCanceled = false let isCanceled = false
@ -13,16 +15,15 @@ export const cancelablePromise = promise => {
cancel: () => (isCanceled = true), cancel: () => (isCanceled = true),
} }
} }
export const noop = () => {} export const noop = () => {}
export const delay = n => new Promise(resolve => setTimeout(resolve, n)) export const delay = (n = 550) => new Promise(resolve => setTimeout(resolve, n))
export const isObject = obj => { export const isObject = obj => { return Object.prototype.toString.call(obj) === '[object Object]' }
return Object.prototype.toString.call(obj) === '[object Object]'
}
export const convertToDuration = (milliseconds: number) => { export const convertToDuration = (milliseconds) => {
const stringifyTime = (time: number): string => String(time).padStart(2, '0') const stringifyTime = (time) => String(time).padStart(2, '0')
const seconds = Math.floor(milliseconds / 1000) const seconds = Math.floor(milliseconds / 1000)
const minutes = Math.floor(seconds / 60) const minutes = Math.floor(seconds / 60)
const hours = Math.floor(minutes / 60) const hours = Math.floor(minutes / 60)
@ -31,7 +32,7 @@ export const convertToDuration = (milliseconds: number) => {
)}:${stringifyTime(seconds % 60)}` )}:${stringifyTime(seconds % 60)}`
} }
export const convertToTimeWorked = (milliseconds: number) => { export const convertToTimeWorked = (milliseconds) => {
const seconds = Math.floor(milliseconds / 1000) const seconds = Math.floor(milliseconds / 1000)
const minutes = Math.floor(seconds / 60) const minutes = Math.floor(seconds / 60)
const hours = Math.floor(minutes / 60) const hours = Math.floor(minutes / 60)
@ -42,3 +43,15 @@ export const convertToTimeWorked = (milliseconds: number) => {
return `${minutes % 60}m` return `${minutes % 60}m`
} }
} }
// Returns a random int between min and max (inclusive)
export function randomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Returns a random item from the given array
export function selectRandom(arrayToChooseFrom) {
return arrayToChooseFrom[randomInt(0, arrayToChooseFrom.length - 1)]
}
Loading…
Cancel
Save