-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: CLI Auto-updates #7635
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
Merged
+657
−178
Merged
feat: CLI Auto-updates #7635
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
75fd033
feat: update service part 1
RomneyDa a93aeda
wip: global context for auto update for cli
RomneyDa 126f5ac
Merge branch 'main' of https://github.com/continuedev/continue into d…
RomneyDa 6d4f891
merge main
RomneyDa ecae90a
Merge branch 'main' of https://github.com/continuedev/continue into d…
RomneyDa a1dc341
fix: update service notifications and state
RomneyDa 53fda4b
test: updatenotification
RomneyDa fb2688a
fix: tests for service container
RomneyDa 0011e9f
fix: lint and format
RomneyDa cdfd13b
fix: address feedback
RomneyDa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
import { exec, spawn } from "child_process"; | ||
import { promisify } from "util"; | ||
|
||
import { GlobalContext } from "core/util/GlobalContext.js"; | ||
|
||
import { logger } from "src/util/logger.js"; | ||
|
||
import { compareVersions, getLatestVersion, getVersion } from "../version.js"; | ||
|
||
import { BaseService } from "./BaseService.js"; | ||
import { serviceContainer } from "./ServiceContainer.js"; | ||
import { UpdateServiceState, UpdateStatus } from "./types.js"; | ||
const execAsync = promisify(exec); | ||
|
||
/** | ||
* Service for checking and performing CLI updates | ||
*/ | ||
export class UpdateService extends BaseService<UpdateServiceState> { | ||
constructor() { | ||
super("update", { | ||
autoUpdate: true, | ||
isAutoUpdate: true, | ||
status: UpdateStatus.IDLE, | ||
message: "", | ||
error: null, | ||
isUpdateAvailable: false, | ||
latestVersion: null, | ||
currentVersion: getVersion(), | ||
}); | ||
} | ||
|
||
/** | ||
* Initialize the update service | ||
*/ | ||
async doInitialize(headless?: boolean) { | ||
// Don't automatically check in tests/headless | ||
if (!headless && process.env.NODE_ENV !== "test") { | ||
void this.checkAndAutoUpdate(); | ||
} | ||
|
||
return this.currentState; | ||
} | ||
|
||
private async checkAndAutoUpdate() { | ||
// First get auto update setting from global context | ||
const globalContext = new GlobalContext(); | ||
const autoUpdate = globalContext.get("autoUpdateCli") ?? true; | ||
this.setState({ | ||
autoUpdate, | ||
}); | ||
|
||
try { | ||
// Check for updates | ||
this.setState({ | ||
status: UpdateStatus.CHECKING, | ||
message: "Checking for updates", | ||
}); | ||
|
||
const latestVersion = await getLatestVersion(); | ||
this.setState({ | ||
latestVersion, | ||
}); | ||
|
||
if (!latestVersion) { | ||
this.setState({ | ||
status: UpdateStatus.IDLE, | ||
message: "Continue CLI", | ||
isUpdateAvailable: false, | ||
}); | ||
return; | ||
} | ||
|
||
const comparison = compareVersions( | ||
this.currentState.currentVersion, | ||
latestVersion, | ||
); | ||
const isUpdateAvailable = comparison === "older"; | ||
this.setState({ | ||
isUpdateAvailable, | ||
}); | ||
|
||
if (this.currentState.currentVersion === "0.0.0-dev") { | ||
this.setState({ | ||
status: UpdateStatus.IDLE, | ||
message: `Continue CLI`, | ||
isUpdateAvailable, | ||
latestVersion, | ||
}); | ||
return; // Uncomment to test auto-update behavior in dev | ||
} | ||
|
||
// If update is available, automatically update | ||
if ( | ||
autoUpdate && | ||
isUpdateAvailable && | ||
this.currentState.status !== "updating" && | ||
!process.env.CONTINUE_CLI_AUTO_UPDATED //Already auto updated, preventing sequential auto-update | ||
) { | ||
await this.performUpdate(true); | ||
} else { | ||
this.setState({ | ||
status: UpdateStatus.IDLE, | ||
message: isUpdateAvailable | ||
? `Update available: v${latestVersion}` | ||
: `Continue CLI v${this.currentState.currentVersion}`, | ||
isUpdateAvailable, | ||
latestVersion, | ||
}); | ||
} | ||
} catch (error: any) { | ||
logger.error("Error checking for updates:", error); | ||
this.setState({ | ||
status: UpdateStatus.ERROR, | ||
message: `Continue CLI v${this.currentState.currentVersion}`, | ||
error, | ||
}); | ||
} | ||
} | ||
|
||
public async setAutoUpdate(value: boolean) { | ||
const globalContext = new GlobalContext(); | ||
globalContext.update("autoUpdateCli", value); | ||
this.setState({ | ||
autoUpdate: value, | ||
}); | ||
} | ||
|
||
// TODO this is a hack because our service state update code is broken | ||
// Currently all things that need update use serviceContainer.set manually | ||
// Rather than actually using the stateChanged event | ||
setState(newState: Partial<UpdateServiceState>): void { | ||
super.setState(newState); | ||
serviceContainer.set("update", this.currentState); | ||
} | ||
|
||
async performUpdate(isAutoUpdate?: boolean) { | ||
if (this.currentState.status === "updating") { | ||
return; | ||
} | ||
|
||
try { | ||
this.setState({ | ||
isAutoUpdate, | ||
status: UpdateStatus.UPDATING, | ||
message: `${isAutoUpdate ? "Auto-updating" : "Updating"} to v${this.currentState.latestVersion}`, | ||
}); | ||
|
||
// Install the update | ||
const { stdout, stderr } = await execAsync("npm i -g @continuedev/cli"); | ||
logger.debug("Update output:", { stdout, stderr }); | ||
|
||
if (stderr) { | ||
const errLines = stderr.split("\n"); | ||
for (const line of errLines) { | ||
const lower = line.toLowerCase().trim(); | ||
if ( | ||
!line || | ||
lower.includes("debugger") || | ||
lower.includes("npm warn") | ||
) { | ||
continue; | ||
} | ||
this.setState({ | ||
status: UpdateStatus.ERROR, | ||
message: `Error updating to v${this.currentState.latestVersion}`, | ||
error: new Error(stderr), | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
this.setState({ | ||
status: UpdateStatus.UPDATED, | ||
message: `${isAutoUpdate ? "Auto-updated to" : "Restart for"} v${this.currentState.latestVersion}`, | ||
isUpdateAvailable: false, | ||
}); | ||
if (isAutoUpdate) { | ||
this.restartCLI(); | ||
} | ||
} catch (error: any) { | ||
logger.error("Error updating CLI:", error); | ||
this.setState({ | ||
status: UpdateStatus.ERROR, | ||
message: isAutoUpdate ? "Auto-update failed" : "Update failed", | ||
error, | ||
}); | ||
setTimeout(() => { | ||
this.setState({ | ||
status: UpdateStatus.IDLE, | ||
message: `/update to v${this.currentState.latestVersion}`, | ||
}); | ||
}, 4000); | ||
} | ||
} | ||
|
||
private restartCLI(): void { | ||
try { | ||
const entryPoint = process.argv[1]; | ||
const cliArgs = process.argv.slice(2); | ||
const nodeExecutable = process.execPath; | ||
|
||
logger.debug( | ||
`Preparing for CLI restart with: ${nodeExecutable} ${entryPoint} ${cliArgs.join( | ||
" ", | ||
)}`, | ||
); | ||
|
||
// Halt/clean up parent cn process | ||
try { | ||
// Remove all input listeners | ||
global.clearTimeout = () => {}; | ||
global.clearInterval = () => {}; | ||
process.stdin.removeAllListeners(); | ||
process.stdin.pause(); | ||
// console.clear(); // Don't want to clear things that were in console before cn started | ||
} catch (e) { | ||
logger.debug("Error cleaning up terminal:", e); | ||
} | ||
|
||
// Spawn a new detached cn process | ||
const child = spawn(nodeExecutable, [entryPoint, ...cliArgs], { | ||
detached: true, | ||
stdio: "inherit", | ||
env: { | ||
...process.env, | ||
CONTINUE_CLI_AUTO_UPDATED: "true", | ||
}, | ||
}); | ||
|
||
// I did not find a way on existing to avoid a bug where next process has input glitches without leaving parent in place | ||
// So instead of existing, parent will exit when child exits | ||
// process.exit(0); | ||
child.on("exit", (code) => { | ||
process.exit(code); | ||
}); | ||
child.unref(); | ||
} catch (error) { | ||
logger.error("Failed to restart CLI:", error); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.