-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(api): addition of a new uninstall command #776
Changes from 15 commits
a015480
565adf9
1f85dab
890b445
7e8e897
e24510e
e97a96f
afbcb06
cdfdda0
3640e60
8eb800d
9fadfcf
118ce03
6b07ddd
ce379d2
4a0fc90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,12 @@ | |
"sha512-ld+djZk8uRWmzXC+JYla1PTBScg0NjP/8x9vOOKRW+DuJ3NNMRjrpfbY7T77Jgnc87dZZsU49robbQfYe3ukug==" | ||
] | ||
}, | ||
"language": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping track of what language we generated the SDK for will allow us to know how to uninstall it from that languages package manager. And because this didn't exist before this we'll default to |
||
"type": "string", | ||
"description": "The language that this SDK was generated for.", | ||
"default": "js", | ||
"enum": ["js"] | ||
}, | ||
"private": { | ||
"type": "boolean", | ||
"description": "Was this SDK installed as a private, unpublished, package to the filesystem?" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import installCommand from './install.js'; | ||
import listCommand from './list.js'; | ||
import uninstallCommand from './uninstall.js'; | ||
|
||
export default { | ||
install: installCommand, | ||
list: listCommand, | ||
uninstall: uninstallCommand, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import chalk from 'chalk'; | ||
import { Command } from 'commander'; | ||
|
||
import { SupportedLanguages } from '../codegen/factory.js'; | ||
import logger from '../logger.js'; | ||
import Storage from '../storage.js'; | ||
|
||
|
@@ -14,7 +15,6 @@ cmd | |
Storage.setStorageDir(); | ||
|
||
const lockfile = Storage.getLockfile(); | ||
|
||
if (!lockfile.apis.length) { | ||
logger('😔 You do not have any SDKs installed.'); | ||
return; | ||
|
@@ -30,6 +30,7 @@ cmd | |
logger(`package name (${chalk.red('private')}): ${chalk.grey(`@api/${api.identifier}`)}`); | ||
} | ||
|
||
logger(`language: ${chalk.grey(api.language || SupportedLanguages.JS)}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
logger(`source: ${chalk.grey(api.source)}`); | ||
logger(`installer version: ${chalk.grey(api.installerVersion)}`); | ||
logger(`created at: ${chalk.grey(api.createdAt || 'n/a')}`); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import path from 'node:path'; | ||
|
||
import chalk from 'chalk'; | ||
import { Command, Option } from 'commander'; | ||
import ora from 'ora'; | ||
|
||
import { SupportedLanguages, uninstallerFactory } from '../codegen/factory.js'; | ||
import promptTerminal from '../lib/prompt.js'; | ||
import logger from '../logger.js'; | ||
import Storage from '../storage.js'; | ||
|
||
interface Options { | ||
yes?: boolean; | ||
} | ||
|
||
const cmd = new Command(); | ||
cmd | ||
.name('uninstall') | ||
.description('uninstall an SDK from your codebase') | ||
.argument('<identifier>', 'the SDK to uninstall') | ||
.addOption(new Option('-y, --yes', 'Automatically answer "yes" to any prompts printed')) | ||
.action(async (identifier: string, options: Options) => { | ||
// We don't know if we have `identifier` in the storage system yet, we just need to preload the | ||
// system so we can access lockfiles. | ||
const storage = new Storage('', SupportedLanguages.JS, identifier); | ||
|
||
const entry = Storage.getFromLockfile(identifier); | ||
if (!entry) { | ||
logger( | ||
`You do not appear to have ${identifier} installed. You can run \`npx api list\` to see what SDKs are present.`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is great 🫶🏽 |
||
true, | ||
); | ||
process.exit(1); | ||
} | ||
|
||
storage.setLanguage(entry?.language); | ||
storage.setIdentifier(identifier); | ||
|
||
const directory = path.relative(process.cwd(), storage.getIdentifierStorageDir()); | ||
if (!options.yes) { | ||
await promptTerminal({ | ||
type: 'confirm', | ||
name: 'value', | ||
message: `Are you sure you want to uninstall ${chalk.yellow(identifier)}? This will delete the ${chalk.yellow( | ||
directory, | ||
)} directory and potentially any changes you may have made there.`, | ||
initial: true, | ||
}).then(({ value }) => { | ||
if (!value) { | ||
process.exit(1); | ||
} | ||
}); | ||
} | ||
|
||
let spinner = ora(`Uninstalling ${chalk.grey(identifier)}`).start(); | ||
|
||
// If we have a known package name for this then we can uninstall it from within cooresponding | ||
// package manager. | ||
const pkgName = storage.getPackageName(); | ||
if (pkgName) { | ||
const language = storage.getSDKLanguage(); | ||
await uninstallerFactory(language, storage) | ||
.then(() => { | ||
spinner.succeed(spinner.text); | ||
}) | ||
.catch(err => { | ||
spinner.fail(spinner.text); | ||
logger(err.message, true); | ||
process.exit(1); | ||
}); | ||
} | ||
|
||
spinner = ora(`Removing ${chalk.grey(directory)}`).start(); | ||
await storage | ||
.remove() | ||
.then(() => { | ||
spinner.succeed(spinner.text); | ||
}) | ||
.catch(err => { | ||
spinner.fail(spinner.text); | ||
logger(err.message, true); | ||
process.exit(1); | ||
}); | ||
|
||
logger('🚀 All done!'); | ||
}) | ||
.addHelpText( | ||
'after', | ||
` | ||
Examples: | ||
$ npx api uninstall petstore`, | ||
); | ||
|
||
export default cmd; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for all the feedback in this file — i definitely plan on doing a full comb through our docs before we release v7