UNPKG

@amplitude/ampli

Version:

Amplitude CLI

65 lines (64 loc) 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path = require("path"); const chalk_1 = require("chalk"); const info_strings_1 = require("../info-strings"); const TerminalWriter_1 = require("../stdout/TerminalWriter"); const codegen_1 = require("../codegen"); const FileService_1 = require("../services/FileService"); const fs = require("../util/fs"); const { bold, cyan, } = chalk_1.default; const stdout = new TerminalWriter_1.default(); const cc = (t) => t; async function SdkSetupAndUsageInfo(zone, org, workspace, source, codegenDir, runtime, projectDir) { var _a; const sdkInfo = info_strings_1.PULL_INFO[runtime.id]; const { installDependencies, commentCharacter: c, } = sdkInfo; const packageName = codegen_1.getPackageName(source.runtime.id, codegenDir); if (installDependencies) { const dependencies = await getRequiredDependencies(path.resolve(projectDir, codegenDir)); if (dependencies.length > 0) { const command = installDependencies(dependencies); const installDepsCommand = command.split('\n').map(line => ` ${line}`).join('\n'); stdout.println(cyan(`Install dependencies:`)) .print(installDepsCommand) .print('\n\n'); } } stdout.println(cyan(`Track events from your app:`)); const sdkImport = (_a = sdkInfo.import) === null || _a === void 0 ? void 0 : _a.call(sdkInfo, packageName); if (sdkImport) { stdout.println(cc(` ${c} Import the tracking library`)); sdkImport.split('\n').forEach(line => stdout.println(` ${bold(line)}`)); stdout.println(); } stdout.println(cc(` ${c} Load the tracking library once on app start`)) .println(` ${bold(sdkInfo.init)}`) .println() .println(cc(` ${c} Track an event e.g. ${bold(sdkInfo.trackExample)}`)) .println(` ${bold(sdkInfo.track)}`) .println(); stdout.println(cyan(`Verify event usage in your code:`)) .println(` $ ${bold(`ampli status -u`)}`); if (sdkInfo.documentationUrl) { stdout.println() .println(cyan(`Full setup instructions:`)) .println(` ${bold(sdkInfo.documentationUrl)}`); } } exports.default = SdkSetupAndUsageInfo; async function getRequiredDependencies(codegenDir) { if (!await fs.exists(codegenDir)) { return []; } const requiredDependenciesRegExp = /^(\W)*Required dependencies:(.*)$/m; const codegenFilePaths = await new FileService_1.FileService().findFiles(codegenDir, '*', [], []); for (const codegenFilePath of codegenFilePaths) { const content = await fs.readTextFile(codegenFilePath); const match = requiredDependenciesRegExp.exec(content); if (match) { return match[2].trim().split(/,\s+/); } } return []; }