Browse Source

Squashed 'src/ao-lib/' changes from aab65a0..620f97f

620f97f fixed broken function
08ee908 Helper functions for managing AO system environment variables
1046e61 Renamed to remove superfluous s in filename
f34baff changed from TS to JS file as it contains no TS-specific stuff
8fdeb9c moved current ao-cli api to ao-lib to begin standardizing API files again

git-subtree-dir: src/ao-lib
git-subtree-split: 620f97f483667c0bba0c6632843e064343917595
main
deicidus 2 years ago
parent
commit
6365a488f2
  1. 1127
      api.js
  2. 0
      crypto.js
  3. 120
      settings.js
  4. 27
      util.js

1127
api.js

File diff suppressed because it is too large Load Diff

0
crypto.ts → crypto.js

120
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
utils.ts → util.js

@ -1,3 +1,5 @@
// General helper functions
export const cancelablePromise = promise => {
let isCanceled = false
@ -13,16 +15,15 @@ export const cancelablePromise = promise => {
cancel: () => (isCanceled = true),
}
}
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 => {
return Object.prototype.toString.call(obj) === '[object Object]'
}
export const isObject = obj => { return Object.prototype.toString.call(obj) === '[object Object]' }
export const convertToDuration = (milliseconds: number) => {
const stringifyTime = (time: number): string => String(time).padStart(2, '0')
export const convertToDuration = (milliseconds) => {
const stringifyTime = (time) => String(time).padStart(2, '0')
const seconds = Math.floor(milliseconds / 1000)
const minutes = Math.floor(seconds / 60)
const hours = Math.floor(minutes / 60)
@ -31,7 +32,7 @@ export const convertToDuration = (milliseconds: number) => {
)}:${stringifyTime(seconds % 60)}`
}
export const convertToTimeWorked = (milliseconds: number) => {
export const convertToTimeWorked = (milliseconds) => {
const seconds = Math.floor(milliseconds / 1000)
const minutes = Math.floor(seconds / 60)
const hours = Math.floor(minutes / 60)
@ -42,3 +43,15 @@ export const convertToTimeWorked = (milliseconds: number) => {
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