Skip to content

Kddimitrov/track command options #4181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 4, 2018
1 change: 1 addition & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ $injector.require("bundleValidatorHelper", "./helpers/bundle-validator-helper");
$injector.require("androidBundleValidatorHelper", "./helpers/android-bundle-validator-helper");
$injector.require("liveSyncCommandHelper", "./helpers/livesync-command-helper");
$injector.require("deployCommandHelper", "./helpers/deploy-command-helper");
$injector.require("optionsTrackHelper", "./helpers/options-track-helper");

$injector.requirePublicClass("localBuildService", "./services/local-build-service");
$injector.requirePublicClass("liveSyncService", "./services/livesync/livesync-service");
Expand Down
4 changes: 3 additions & 1 deletion lib/common/services/commands-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class CommandsService implements ICommandsService {
private $resources: IResourceLoader,
private $staticConfig: Config.IStaticConfig,
private $helpService: IHelpService,
private $extensibilityService: IExtensibilityService) {
private $extensibilityService: IExtensibilityService,
private $optionsTrackHelper: IOptionsTrackHelper) {
}

public allCommands(opts: { includeDevCommands: boolean }): string[] {
Expand Down Expand Up @@ -63,6 +64,7 @@ export class CommandsService implements ICommandsService {
}

await analyticsService.trackInGoogleAnalytics(googleAnalyticsPageData);
await this.$optionsTrackHelper.trackOptions(this.$options);
}

const shouldExecuteHooks = !this.$staticConfig.disableCommandHooks && (command.enableHooks === undefined || command.enableHooks === true);
Expand Down
3 changes: 2 additions & 1 deletion lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export const enum TrackActionNames {
LiveSync = "LiveSync",
RunSetupScript = "Run Setup Script",
CheckLocalBuildSetup = "Check Local Build Setup",
CheckEnvironmentRequirements = "Check Environment Requirements"
CheckEnvironmentRequirements = "Check Environment Requirements",
Options = "Options"
}

export const AnalyticsEventLabelDelimiter = "__";
Expand Down
4 changes: 4 additions & 0 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,10 @@ interface IAndroidBundleValidatorHelper {
validateRuntimeVersion(projectData: IProjectData): void
}

interface IOptionsTrackHelper {
trackOptions(options: IOptions): Promise<void>
}

interface INativeScriptCloudExtensionService {
/**
* Installs nativescript-cloud extension
Expand Down
77 changes: 77 additions & 0 deletions lib/helpers/options-track-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as path from "path";
import { TrackActionNames } from "../constants";

export class OptionsTrackHelper {
public static PASSWORD_DETECTION_STRING = "password";
public static PASSOWRD_REPLACE_VALUE = "private";
public static PATH_REPLACE_VALUE = "_localpath";
public static SIZE_EXEEDED_REPLACE_VALUE = "sizeExceeded";

constructor(
private $analyticsService: IAnalyticsService) {
}

public async trackOptions(options: IOptions) {
const trackObject = this.getTrackObject(options);

await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.Options,
additionalData: JSON.stringify(trackObject)
});
}

private getTrackObject(options: IOptions): IDictionary<any> {
const optionsArgvCopy = _.cloneDeep(options.argv);

return this.sanitizeTrackObject(optionsArgvCopy, options);
}

private sanitizeTrackObject(data: IDictionary<any>, options?: IOptions): IDictionary<any> {
const shorthands = options ? options.shorthands : [];
const optionsDefinitions = options ? options.options : {};

_.forEach(data, (value, key) => {
if (this.shouldSkipProperty(key, value, shorthands, optionsDefinitions)) {
delete data[key];
} else {
if (key.toLowerCase().indexOf(OptionsTrackHelper.PASSWORD_DETECTION_STRING) >= 0) {
value = OptionsTrackHelper.PASSOWRD_REPLACE_VALUE;
} else if (_.isString(value) && value !== path.basename(value)) {
value = OptionsTrackHelper.PATH_REPLACE_VALUE;
} else if (_.isObject(value) && !_.isArray(value)) {
value = this.sanitizeTrackObject(value);
}
data[key] = value;
}
});

return data;
}

private shouldSkipProperty(key: string, value: any, shorthands: string[] = [], options: IDictionary<IDashedOption> = {}): Boolean {
if (shorthands.indexOf(key) >= 0) {
return true;
}

if (key.indexOf("-") >= 0) {
return true;
}

if (key === "_") {
return true;
}

const optionDef = options[key];
if (optionDef && optionDef.type === OptionType.Boolean) {
if (optionDef.default !== true && value === false || optionDef.default === true && value === true) {
return true;
}
}

if (_.isUndefined(value)) {
return true;
}
}
}

$injector.register("optionsTrackHelper", OptionsTrackHelper);