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
3 years ago
|
import { execSync } from 'child_process'
|
||
3 years ago
|
import { fileURLToPath } from 'url'
|
||
3 years ago
|
import path from 'path'
|
||
|
import { loadJsonFile } from '../files.js'
|
||
3 years ago
|
|
||
|
const __filename = fileURLToPath(import.meta.url)
|
||
|
const __dirname = path.dirname(__filename)
|
||
3 years ago
|
|
||
3 years ago
|
// Returns one of: off, installed, enabled, running, synced, error
|
||
|
function cliStatus() {
|
||
3 years ago
|
try {
|
||
3 years ago
|
const stdout = execSync('npm list -g @autonomousorganization/ao-cli')
|
||
|
const isAoCliInstalled = stdout.includes('@autonomousorganization/ao-cli@')
|
||
|
if(isAoCliInstalled) return 'installed'
|
||
3 years ago
|
} catch(err) {
|
||
3 years ago
|
return 'error'
|
||
3 years ago
|
}
|
||
3 years ago
|
return 'off'
|
||
3 years ago
|
}
|
||
|
|
||
3 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() {
|
||
3 years ago
|
try {
|
||
3 years ago
|
execSync('npm i -g @autonomousorganization/ao-cli 2>&1')
|
||
|
console.log('Installed ao-cli.')
|
||
3 years ago
|
} catch(err) {
|
||
3 years ago
|
console.log('Error installing ao-cli:', err)
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
async function getAoCliVersion() {
|
||
|
const packageJson = await loadJsonFile(path.join(__dirname, '../../package.json'))
|
||
3 years ago
|
return packageJson.version
|
||
|
}
|
||
|
|
||
|
// Updates the globally-installed version of this package, ao-cli, using npm
|
||
3 years ago
|
async function selfUpdate() {
|
||
3 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) {
|
||
3 years ago
|
console.log("ao-cli version is already current.")
|
||
3 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)
|
||
|
}
|
||
|
}
|
||
|
|
||
3 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'
|
||
3 years ago
|
}
|
||
|
|
||
3 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() {
|
||
3 years ago
|
try {
|
||
3 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.')
|
||
3 years ago
|
} catch(err) {
|
||
3 years ago
|
console.log('Failed to add alias, sorry.')
|
||
3 years ago
|
}
|
||
3 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' }
|
||
|
]
|
||
3 years ago
|
}
|