Skip to content

Commit 8c9ed69

Browse files
deralaxoice-chillios
authored andcommitted
feat: improve migration logic
1 parent c91bdb5 commit 8c9ed69

1 file changed

Lines changed: 65 additions & 19 deletions

File tree

‎src/migration.ts‎

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,100 @@
1-
import { env, ExtensionContext, Uri, window } from 'vscode';
1+
import {
2+
commands,
3+
env,
4+
ExtensionContext,
5+
extensions,
6+
Uri,
7+
window,
8+
} from 'vscode';
29

3-
const FIRST_SHOWN_KEY = 'r5n.migrated.firstShownAt';
10+
const LAST_SHOWN_KEY = 'r5n.migrated.lastShownAt';
411
const DISMISSED_KEY = 'r5n.migrated.dismissed';
512
const NEW_EXTENSION_ID = 'r5n.es-js-snippets';
13+
const OLD_EXTENSION_ID = 'dsznajder.es7-react-js-snippets';
614
const REMINDER_AFTER_MS = 7 * 24 * 60 * 60 * 1000;
15+
const MIGRATE_ACTION = 'Migrate to new extension';
16+
const DISMISS_ACTION = "Don't show again";
17+
const RELOAD_ACTION = 'Reload Window';
718

819
const INITIAL_MESSAGE =
920
'ES7+ React/Redux/React-Native snippets has moved. Install the new version (r5n.es-js-snippets) for React 17–19 support and ongoing updates.';
1021

11-
const FINAL_MESSAGE =
12-
'Final reminder: this extension has moved to r5n.es-js-snippets and is no longer maintained here.';
22+
const REMINDER_MESSAGE =
23+
'Reminder: this extension has moved to r5n.es-js-snippets and is no longer maintained here.';
24+
25+
const FINISH_MESSAGE =
26+
'The new r5n.es-js-snippets extension is installed. Finish migration by uninstalling this deprecated extension.';
1327

1428
export async function showMigrationNotice(
1529
context: ExtensionContext,
1630
): Promise<void> {
1731
if (context.globalState.get(DISMISSED_KEY) === true) return;
1832

1933
const now = Date.now();
20-
const firstShownAt = context.globalState.get<number>(FIRST_SHOWN_KEY);
34+
const lastShownAt = context.globalState.get<number>(LAST_SHOWN_KEY);
2135

22-
if (firstShownAt === undefined) {
23-
await context.globalState.update(FIRST_SHOWN_KEY, now);
24-
await showToast(context, INITIAL_MESSAGE, false);
36+
if (lastShownAt !== undefined && now - lastShownAt < REMINDER_AFTER_MS) {
2537
return;
2638
}
2739

28-
if (now - firstShownAt >= REMINDER_AFTER_MS) {
29-
await showToast(context, FINAL_MESSAGE, true);
30-
await context.globalState.update(DISMISSED_KEY, true);
31-
}
40+
await context.globalState.update(LAST_SHOWN_KEY, now);
41+
await showToast(
42+
context,
43+
extensions.getExtension(NEW_EXTENSION_ID) !== undefined
44+
? FINISH_MESSAGE
45+
: lastShownAt === undefined
46+
? INITIAL_MESSAGE
47+
: REMINDER_MESSAGE,
48+
);
3249
}
3350

3451
async function showToast(
3552
context: ExtensionContext,
3653
message: string,
37-
finalNudge: boolean,
3854
): Promise<void> {
3955
const action = await window.showInformationMessage(
4056
message,
41-
'Install new version',
42-
"Don't show again",
57+
MIGRATE_ACTION,
58+
DISMISS_ACTION,
4359
);
4460

45-
if (action === 'Install new version') {
46-
await env.openExternal(Uri.parse(`vscode:extension/${NEW_EXTENSION_ID}`));
47-
await context.globalState.update(DISMISSED_KEY, true);
61+
if (action === MIGRATE_ACTION) {
62+
await migrateToNewExtension();
4863
return;
4964
}
5065

51-
if (action === "Don't show again" || finalNudge) {
66+
if (action === DISMISS_ACTION) {
5267
await context.globalState.update(DISMISSED_KEY, true);
5368
}
5469
}
70+
71+
async function migrateToNewExtension(): Promise<void> {
72+
try {
73+
if (extensions.getExtension(NEW_EXTENSION_ID) === undefined) {
74+
await commands.executeCommand(
75+
'workbench.extensions.installExtension',
76+
NEW_EXTENSION_ID,
77+
{ enable: true },
78+
);
79+
}
80+
81+
await commands.executeCommand(
82+
'workbench.extensions.uninstallExtension',
83+
OLD_EXTENSION_ID,
84+
);
85+
86+
const action = await window.showInformationMessage(
87+
'Migration started. Reload VS Code to finish removing this deprecated extension.',
88+
RELOAD_ACTION,
89+
);
90+
91+
if (action === RELOAD_ACTION) {
92+
await commands.executeCommand('workbench.action.reloadWindow');
93+
}
94+
} catch {
95+
await env.openExternal(Uri.parse(`vscode:extension/${NEW_EXTENSION_ID}`));
96+
await window.showWarningMessage(
97+
'Automatic migration failed. Install r5n.es-js-snippets from the opened extension page, then uninstall this deprecated extension.',
98+
);
99+
}
100+
}

0 commit comments

Comments
 (0)