@amplitude/ampli
Version:
Amplitude CLI
172 lines (171 loc) • 8.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageName = exports.ITLY_DIRS = void 0;
const path = require("path");
const chalk_1 = require("chalk");
const fs = require("../util/fs");
const settings_1 = require("../settings");
const stdout_1 = require("../stdout");
const types_1 = require("../types");
const javaKotlin_1 = require("./javaKotlin");
const icons_1 = require("../ui/icons");
const { bold: b, } = chalk_1.default;
const ITLY_JAVA_PACKAGE_PATH_V3 = 'ly/iterative/itly';
const ITLY_JAVA_PACKAGE_PATH_V1 = 'io/itly';
const AMPLI_JAVA_PACKAGE_PATH_V1 = 'com/amplitude/ampli';
const JVM_V3_RUNTIMES = [
types_1.RuntimeId.AndroidJavaV3,
types_1.RuntimeId.AndroidKotlinV3,
types_1.RuntimeId.JreJavaV3,
types_1.RuntimeId.JreKotlinV3,
];
exports.ITLY_DIRS = {
[]: './src/itly',
[]: './src/itly',
[]: './src/itly',
[]: './src/ampli',
[]: './src/ampli',
[]: './src/itly',
[]: './src/itly',
[]: './src/itly',
[]: './src/ampli',
[]: './src/ampli',
[]: './src/itly',
[]: './src/itly',
[]: './src/itly',
[]: './src/ampli',
[]: './src/ampli',
[]: './src/itly',
[]: './src/itly',
[]: './src/itly',
[]: './src/ampli',
[]: './src/ampli',
[]: './Itly',
[]: './Itly',
[]: './Ampli',
[]: './Ampli',
[]: './Itly',
[]: './Itly',
[]: './Ampli',
[]: './Ampli',
[]: './app/src/main/java/io/itly',
[]: `./app/src/main/java/${ITLY_JAVA_PACKAGE_PATH_V3}`,
[]: `./app/src/main/java/${ITLY_JAVA_PACKAGE_PATH_V3}`,
[]: `./app/src/main/java/${AMPLI_JAVA_PACKAGE_PATH_V1}`,
[]: `./app/src/main/java/${AMPLI_JAVA_PACKAGE_PATH_V1}`,
[]: './app/src/main/java/io/itly',
[]: './app/src/main/java/io/itly',
[]: `./app/src/main/java/${ITLY_JAVA_PACKAGE_PATH_V3}`,
[]: `./app/src/main/java/${ITLY_JAVA_PACKAGE_PATH_V3}`,
[]: `./app/src/main/java/${AMPLI_JAVA_PACKAGE_PATH_V1}`,
[]: `./app/src/main/java/${AMPLI_JAVA_PACKAGE_PATH_V1}`,
[]: './src/main/java/io/itly',
[]: `./src/main/java/${ITLY_JAVA_PACKAGE_PATH_V3}`,
[]: `./src/main/java/${ITLY_JAVA_PACKAGE_PATH_V3}`,
[]: `./src/main/java/${AMPLI_JAVA_PACKAGE_PATH_V1}`,
[]: `./src/main/kotlin/${ITLY_JAVA_PACKAGE_PATH_V3}`,
[]: `./src/main/kotlin/${ITLY_JAVA_PACKAGE_PATH_V3}`,
[]: `./src/main/kotlin/${AMPLI_JAVA_PACKAGE_PATH_V1}`,
[]: './itly',
[]: './itly',
[]: './ampli',
[]: './itly',
[]: './itly',
[]: './Itly',
[]: './src/itly',
[]: './src/itly',
[]: './src/ampli',
[]: './src/ampli',
[]: './src/ampli',
[]: './src/ampli',
[]: './ampli',
};
function getMandatoryPathPostfix(runtimeId) {
switch (runtimeId) {
case types_1.RuntimeId.AndroidJavaV3:
case types_1.RuntimeId.AndroidKotlinV3:
case types_1.RuntimeId.JreJavaV3:
return ITLY_JAVA_PACKAGE_PATH_V3;
case types_1.RuntimeId.AndroidJavaAmpli:
case types_1.RuntimeId.AndroidJavaAmpliV2:
case types_1.RuntimeId.AndroidKotlinAmpli:
case types_1.RuntimeId.AndroidKotlinAmpliV2:
case types_1.RuntimeId.JreJavaAmpli:
case types_1.RuntimeId.JreKotlinAmpli:
return 'ampli';
default:
return undefined;
}
}
class Generator {
setPath(runtimeId, userPath, projectDir) {
const warnings = [];
const settings = settings_1.getSettings();
const cachedPath = settings.getPath();
let effectivePath = exports.ITLY_DIRS[runtimeId];
if (userPath) {
effectivePath = userPath;
}
else if (cachedPath) {
effectivePath = cachedPath;
}
const mandatoryPath = getMandatoryPathPostfix(runtimeId);
if (mandatoryPath && !effectivePath.endsWith(mandatoryPath)) {
warnings.push(`${icons_1.ICON_WARNING_W_TEXT} Path should end with '${mandatoryPath}'.`);
if (JVM_V3_RUNTIMES.includes(runtimeId) && effectivePath.endsWith(ITLY_JAVA_PACKAGE_PATH_V1)) {
effectivePath = effectivePath.replace(ITLY_JAVA_PACKAGE_PATH_V1, ITLY_JAVA_PACKAGE_PATH_V3);
warnings.push(`${icons_1.ICON_WARNING_W_TEXT} Path updated automatically to '${effectivePath}'.`);
}
}
if (!cachedPath || cachedPath !== effectivePath) {
settings.setPath(effectivePath);
if (cachedPath) {
warnings.push(`${icons_1.ICON_WARNING_W_TEXT} Path has changed, please delete your old generated tracking library at ${b(cachedPath)}.`);
}
}
if (warnings.length > 0) {
stdout_1.default.writeLine();
warnings.forEach(message => stdout_1.default.writeLine(message));
}
this.codegenDir = path.resolve(projectDir, effectivePath);
return effectivePath;
}
async prepDir() {
if (!(await fs.exists(this.codegenDir))) {
await fs.mkdir(this.codegenDir, { recursive: true });
}
}
async saveCode(sdk) {
await this.prepDir();
if (await fs.exists(this.codegenDir)) {
await fs.rimraf(this.codegenDir);
}
if (sdk.code) {
await Promise.all(sdk.code.map(async (file) => {
if (file != null) {
const dirPath = path.join(this.codegenDir, path.dirname(file.path));
if (!(await fs.exists(dirPath))) {
await fs.mkdir(dirPath, { recursive: true });
}
await fs.writeFile(path.join(this.codegenDir, file.path), file.content);
}
}));
}
}
}
function getPackageName(runtimeId, codegenDir) {
switch (runtimeId) {
case types_1.RuntimeId.AndroidJavaAmpli:
case types_1.RuntimeId.AndroidJavaAmpliV2:
case types_1.RuntimeId.JreJavaAmpli:
return javaKotlin_1.default(codegenDir, 'java');
case types_1.RuntimeId.AndroidKotlinAmpli:
case types_1.RuntimeId.AndroidKotlinAmpliV2:
case types_1.RuntimeId.JreKotlinAmpli:
return javaKotlin_1.default(codegenDir, 'kotlin');
default:
}
return undefined;
}
exports.getPackageName = getPackageName;
exports.default = new Generator();