You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.8 KiB
84 lines
2.8 KiB
import { execSync } from 'child_process' |
|
import { fileURLToPath } from 'url' |
|
import path from 'path' |
|
import { loadJsonFile } from '../files.js' |
|
|
|
const __filename = fileURLToPath(import.meta.url) |
|
const __dirname = path.dirname(__filename) |
|
|
|
// Returns one of: off, installed, enabled, running, synced, error |
|
function cliStatus() { |
|
try { |
|
const stdout = execSync('npm list -g @autonomousorganization/ao-cli') |
|
const isAoCliInstalled = stdout.includes('@autonomousorganization/ao-cli@') |
|
if(isAoCliInstalled) return 'installed' |
|
} catch(err) { |
|
return 'error' |
|
} |
|
return 'off' |
|
} |
|
|
|
// It is possible to run ao-cli with npx @autonomousorganization/ao-cli. In this case, it can help you install it permanently. |
|
function installAoCli() { |
|
try { |
|
execSync('npm i -g @autonomousorganization/ao-cli 2>&1') |
|
console.log('Installed ao-cli.') |
|
} catch(err) { |
|
console.log('Error installing ao-cli:', err) |
|
} |
|
} |
|
|
|
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 |
|
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-cli 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) |
|
} |
|
} |
|
|
|
// Returns true if the 'ao' alias for ao-cli has already been addded to .bashrc |
|
function checkAoAlias() { |
|
try { |
|
execSync('grep "ao=ao-cli" ~/.bashrc') |
|
} catch(err) { |
|
return 'off' |
|
} |
|
return 'installed' |
|
} |
|
|
|
// Adds a line to .bashrc to make 'ao' an alias for 'ao-cli', to simplify using the AO from the command line |
|
function installAoAlias() { |
|
try { |
|
execSync('echo alias ao=ao-cli >> $HOME/.bashrc') |
|
console.log('Added alias line to ~/.bashrc. You can now type \'ao\' to launch ao-cli.') |
|
} catch(err) { |
|
console.log('Failed to add alias, sorry.') |
|
} |
|
} |
|
|
|
export default { |
|
description: 'this AO command-line interface', |
|
status: cliStatus, |
|
install: installAoCli, |
|
version: getAoCliVersion, |
|
update: selfUpdate, |
|
add_alias: installAoAlias, |
|
remove_alias: () => console.log("Not implemented yet."), |
|
menu: [ |
|
{ name: () => checkAoAlias() === 'installed' ? 'Remove \'ao\' shortcut for \'ao-cli\'' : 'Install \'ao\' shortcut for \'ao-cli\'', |
|
value: () => checkAoAlias() === 'installed' ? 'remove_alias' : 'add_alias' } |
|
] |
|
}
|
|
|