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.
85 lines
2.8 KiB
85 lines
2.8 KiB
2 years ago
|
import { execSync } from 'child_process'
|
||
2 years ago
|
import { fileURLToPath } from 'url'
|
||
2 years ago
|
import path from 'path'
|
||
|
import { loadJsonFile } from '../files.js'
|
||
2 years ago
|
|
||
|
const __filename = fileURLToPath(import.meta.url)
|
||
|
const __dirname = path.dirname(__filename)
|
||
2 years ago
|
|
||
2 years ago
|
// Returns one of: off, installed, enabled, running, synced, error
|
||
|
function cliStatus() {
|
||
2 years ago
|
try {
|
||
2 years ago
|
const stdout = execSync('npm list -g @autonomousorganization/ao-cli')
|
||
|
const isAoCliInstalled = stdout.includes('@autonomousorganization/ao-cli@')
|
||
|
if(isAoCliInstalled) return 'installed'
|
||
2 years ago
|
} catch(err) {
|
||
2 years ago
|
return 'error'
|
||
2 years ago
|
}
|
||
2 years ago
|
return 'off'
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// It is possible to run ao-cli with npx @autonomousorganization/ao-cli. In this case, it can help you install it permanently.
|
||
|
function installAoCli() {
|
||
2 years ago
|
try {
|
||
2 years ago
|
execSync('npm i -g @autonomousorganization/ao-cli 2>&1')
|
||
|
console.log('Installed ao-cli.')
|
||
2 years ago
|
} catch(err) {
|
||
2 years ago
|
console.log('Error installing ao-cli:', err)
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
async function getAoCliVersion() {
|
||
|
const packageJson = await loadJsonFile(path.join(__dirname, '../../package.json'))
|
||
2 years ago
|
return packageJson.version
|
||
|
}
|
||
|
|
||
|
// Updates the globally-installed version of this package, ao-cli, using npm
|
||
2 years ago
|
async function selfUpdate() {
|
||
2 years ago
|
try {
|
||
|
const beforeVersionNumber = await getAoCliVersion()
|
||
|
const result = execSync('npm update -g @autonomousorganization/ao-cli 2>&1')
|
||
|
const afterVersionNumber = await getAoCliVersion()
|
||
|
if(beforeVersionNumber === afterVersionNumber) {
|
||
2 years ago
|
console.log("ao-cli version is already current.")
|
||
2 years ago
|
} 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)
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
// 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'
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// Adds a line to .bashrc to make 'ao' an alias for 'ao-cli', to simplify using the AO from the command line
|
||
|
function installAoAlias() {
|
||
2 years ago
|
try {
|
||
2 years ago
|
execSync('echo alias ao=ao-cli >> $HOME/.bashrc')
|
||
|
console.log('Added alias line to ~/.bashrc. You can now type \'ao\' to launch ao-cli.')
|
||
2 years ago
|
} catch(err) {
|
||
2 years ago
|
console.log('Failed to add alias, sorry.')
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
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' }
|
||
|
]
|
||
2 years ago
|
}
|