-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsvelte-wizard.ts
More file actions
170 lines (141 loc) · 4.79 KB
/
svelte-wizard.ts
File metadata and controls
170 lines (141 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* eslint-disable max-lines */
import {
abort,
askForAIConsent,
confirmContinueIfNoOrDirtyGitRepo,
ensurePackageIsInstalled,
getOrAskForProjectData,
getPackageDotJson,
getPackageManager,
installPackage,
isUsingTypeScript,
printWelcome,
} from '../utils/clack-utils';
import { getPackageVersion, hasPackageInstalled } from '../utils/package-json';
import clack from '../utils/clack';
import { Integration } from '../lib/constants';
import { getSvelteDocumentation } from './docs';
import { analytics } from '../utils/analytics';
import {
generateFileChangesForIntegration,
getFilesToChange,
getRelevantFilesForIntegration,
} from '../utils/file-utils';
import type { WizardOptions } from '../utils/types';
import { askForCloudRegion } from '../utils/clack-utils';
import { addEditorRulesStep } from '../steps/add-editor-rules';
import { getOutroMessage } from '../lib/messages';
import {
addMCPServerToClientsStep,
addOrUpdateEnvironmentVariablesStep,
runPrettierStep,
} from '../steps';
export async function runSvelteWizard(options: WizardOptions): Promise<void> {
printWelcome({
wizardName: 'PostHog Svelte wizard',
});
const aiConsent = await askForAIConsent(options);
if (!aiConsent) {
await abort(
'The Svelte wizard requires AI to get setup right now. Please view the docs to setup Svelte manually instead: https://posthog.com/docs/libraries/svelte',
0,
);
}
const cloudRegion = options.cloudRegion ?? (await askForCloudRegion());
const typeScriptDetected = isUsingTypeScript(options);
await confirmContinueIfNoOrDirtyGitRepo(options);
const packageJson = await getPackageDotJson(options);
await ensurePackageIsInstalled(packageJson, '@sveltejs/kit', '@sveltejs/kit');
const svelteVersion = getPackageVersion('@sveltejs/kit', packageJson);
if (svelteVersion) {
analytics.setTag('svelte-version', svelteVersion);
}
const { projectApiKey, accessToken, host, projectId } =
await getOrAskForProjectData({
...options,
cloudRegion,
});
const sdkAlreadyInstalled = hasPackageInstalled('posthog-js', packageJson);
analytics.setTag('sdk-already-installed', sdkAlreadyInstalled);
const { packageManager: packageManagerFromInstallStep } =
await installPackage({
packageName: 'posthog-js',
packageNameDisplayLabel: 'posthog-js',
alreadyInstalled: !!packageJson?.dependencies?.['posthog-js'],
forceInstall: options.forceInstall,
askBeforeUpdating: false,
installDir: options.installDir,
integration: Integration.svelte,
});
await installPackage({
packageName: 'posthog-node',
packageNameDisplayLabel: 'posthog-node',
packageManager: packageManagerFromInstallStep,
alreadyInstalled: !!packageJson?.dependencies?.['posthog-node'],
forceInstall: options.forceInstall,
askBeforeUpdating: false,
installDir: options.installDir,
integration: Integration.svelte,
});
const relevantFiles = await getRelevantFilesForIntegration({
installDir: options.installDir,
integration: Integration.svelte,
});
const installationDocumentation = getSvelteDocumentation({
language: typeScriptDetected ? 'typescript' : 'javascript',
});
clack.log.info(`Reviewing PostHog documentation for Svelte`);
const filesToChange = await getFilesToChange({
integration: Integration.svelte,
relevantFiles,
documentation: installationDocumentation,
accessToken,
cloudRegion,
projectId,
});
await generateFileChangesForIntegration({
integration: Integration.svelte,
filesToChange,
accessToken,
installDir: options.installDir,
documentation: installationDocumentation,
cloudRegion,
projectId,
});
const { relativeEnvFilePath, addedEnvVariables } =
await addOrUpdateEnvironmentVariablesStep({
variables: {
['PUBLIC_POSTHOG_KEY']: projectApiKey,
['PUBLIC_POSTHOG_HOST']: host,
},
installDir: options.installDir,
integration: Integration.svelte,
});
const packageManagerForOutro =
packageManagerFromInstallStep ?? (await getPackageManager(options));
await runPrettierStep({
installDir: options.installDir,
integration: Integration.svelte,
});
const addedEditorRules = await addEditorRulesStep({
installDir: options.installDir,
rulesName: 'svelte-rules.md',
integration: Integration.svelte,
});
await addMCPServerToClientsStep({
cloudRegion,
integration: Integration.svelte,
ci: options.ci,
});
const outroMessage = getOutroMessage({
options,
integration: Integration.svelte,
cloudRegion,
addedEditorRules,
packageManager: packageManagerForOutro,
envFileChanged: addedEnvVariables ? relativeEnvFilePath : undefined,
uploadedEnvVars: [],
});
clack.outro(outroMessage);
await analytics.shutdown('success');
}