// Functions to add and remove AO features
import { execSync , exec } from 'child_process'
import { AO _MANUAL _PATH } from './manual.js'
import { loadJsonFile } from './files.js'
import path from 'path'
import { fileURLToPath } from 'url'
const _ _filename = fileURLToPath ( import . meta . url )
const _ _dirname = path . dirname ( _ _filename )
// Adds a line to .bashrc to make 'ao' an alias for 'ao-cli', to simplify using the AO from the command line
export function installAoAlias ( ) {
try {
execSync ( 'grep "ao=\'ao-cli\'" ~/.bashrc' )
console . log ( 'You can already type \'ao\' to launch ao-cli; the alias line already exists in ~/.bashrc.' )
} catch ( err ) {
execSync ( 'echo alias ao=\'ao-cli\' >> .bashrc' )
console . log ( 'Added alias line to ~/.bashrc. You can now type \'ao\' to launch ao-cli.' )
}
}
export async function getAoCliVersion ( ) {
const packageJson = await loadJsonFile ( path . join ( _ _dirname , '../package.json' ) )
return packageJson . version
}
// Updates the globally-installed version of this package, ao-cli, using npm
export async function selfUpdate ( ) {
try {
const beforeVersionNumber = await getAoCliVersion ( )
const result = execSync ( 'npm update -g @autonomousorganization/ao-cli 2>&1' )
const afterVersionNumber = await getAoCliVersion ( )
if ( beforeVersionNumber === afterVersionNumber ) {
console . log ( "AO version is already current." )
} else {
console . log ( '\nao-cli self-updated automatically from version' , beforeVersionNumber , 'to version' , afterVersionNumber , 'from the official npm repository.' )
}
} catch ( err ) {
console . log ( 'Failed to update ao-cli: ' , err )
}
}
export async function updateManual ( ) {
exec ( 'cd ' + process . env . HOME + '/.ao/manual && git pull origin main 2>&1' , ( error , stdout , stderr ) => {
//console.log('error:', error, 'stdout:', stdout, 'stderr:', stderr)
if ( error ) {
console . log ( 'git pull failed with error:' , error )
}
if ( stdout . includes ( 'Already up to date.' ) ) {
return
}
console . log ( '/nAO User Manual was updated.' )
} )
}
// Downloads the ao-manual repo to ~/.ao/manual/
export function downloadManual ( ) {
try {
execSync ( 'git clone http://git.coalitionofinvisiblecolleges.org:3009/autonomousorganization/ao-manual.git ' + AO _MANUAL _PATH + ' 2>&1' )
} catch ( err ) {
switch ( err . code ) {
case 128 :
return false
}
}
return true
}