|
| 1 | +import { info } from 'console'; |
| 2 | +import type { Answers } from 'inquirer'; |
| 3 | +import { prompt } from 'inquirer'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +import type { Args } from '../../Constants'; |
| 7 | +import { nl } from '../../Helper/Logging'; |
| 8 | +import { checkPackageVersion, hasPackageInstalled } from '../../Helper/Package'; |
| 9 | +import { getPackageManagerChoice } from '../../Helper/PackageManager'; |
| 10 | +import { SentryCli } from '../../Helper/SentryCli'; |
| 11 | +import { BaseIntegration } from './BaseIntegration'; |
| 12 | + |
| 13 | +const SVELTEKIT_SDK_PACKAGE = '@sentry/sveltekit'; |
| 14 | +const COMPATIBLE_SVELTEKIT_VERSIONS = '>=1.0.0'; |
| 15 | +const COMPATIBLE_SDK_VERSIONS = '>=7.48.0'; |
| 16 | + |
| 17 | +let appPackage: any = {}; |
| 18 | + |
| 19 | +try { |
| 20 | + appPackage = require(path.join(process.cwd(), 'package.json')); |
| 21 | +} catch { |
| 22 | + // We don't need to have this |
| 23 | +} |
| 24 | + |
| 25 | +export class SvelteKit extends BaseIntegration { |
| 26 | + private _sentryCli: SentryCli; |
| 27 | + public constructor(protected _argv: Args) { |
| 28 | + super(_argv); |
| 29 | + this._sentryCli = new SentryCli(this._argv); |
| 30 | + } |
| 31 | + |
| 32 | + public async emit(_answers: Answers): Promise<Answers> { |
| 33 | + info('Setting up SvelteKit SDK'); |
| 34 | + |
| 35 | + // TODO: The actual SDK setup |
| 36 | + |
| 37 | + return {}; |
| 38 | + } |
| 39 | + |
| 40 | + public async shouldConfigure(_answers: Answers): Promise<Answers> { |
| 41 | + if (this._shouldConfigure) { |
| 42 | + return this._shouldConfigure; |
| 43 | + } |
| 44 | + |
| 45 | + nl(); |
| 46 | + |
| 47 | + let userAnswers: Answers = { continue: true }; |
| 48 | + const hasCompatibleSvelteKitVersion = checkPackageVersion( |
| 49 | + appPackage, |
| 50 | + '@sveltejs/kit', |
| 51 | + COMPATIBLE_SVELTEKIT_VERSIONS, |
| 52 | + true, |
| 53 | + ); |
| 54 | + |
| 55 | + const packageManager = getPackageManagerChoice(); |
| 56 | + const hasSdkInstalled = hasPackageInstalled( |
| 57 | + appPackage, |
| 58 | + SVELTEKIT_SDK_PACKAGE, |
| 59 | + ); |
| 60 | + |
| 61 | + let hasCompatibleSdkVersion = false; |
| 62 | + // if no SDK is installed but SvelteKit was detected, let's add the SDK if we can |
| 63 | + if (!hasSdkInstalled && packageManager && hasCompatibleSvelteKitVersion) { |
| 64 | + await packageManager.installPackage(SVELTEKIT_SDK_PACKAGE); |
| 65 | + // can assume it's compatible since we just installed it |
| 66 | + hasCompatibleSdkVersion = true; |
| 67 | + } else { |
| 68 | + // otherwise, let's check the version and spit out the appropriate error |
| 69 | + hasCompatibleSdkVersion = checkPackageVersion( |
| 70 | + appPackage, |
| 71 | + SVELTEKIT_SDK_PACKAGE, |
| 72 | + COMPATIBLE_SDK_VERSIONS, |
| 73 | + true, |
| 74 | + ); |
| 75 | + } |
| 76 | + const hasAllPackagesCompatible = |
| 77 | + hasCompatibleSvelteKitVersion && hasCompatibleSdkVersion; |
| 78 | + |
| 79 | + if (!hasAllPackagesCompatible && !this._argv.quiet) { |
| 80 | + userAnswers = await prompt({ |
| 81 | + message: |
| 82 | + 'There were errors while checking your project config. Do you still want to continue?', |
| 83 | + name: 'continue', |
| 84 | + default: false, |
| 85 | + type: 'confirm', |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + nl(); |
| 90 | + |
| 91 | + if (!userAnswers['continue']) { |
| 92 | + throw new Error('Please install the required dependencies to continue.'); |
| 93 | + } |
| 94 | + |
| 95 | + this._shouldConfigure = Promise.resolve({ sveltekit: true }); |
| 96 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 97 | + return this.shouldConfigure; |
| 98 | + } |
| 99 | +} |
0 commit comments