Skip to content

Commit

Permalink
feat(analytics): add analytics hooks for success and failure (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe authored Feb 10, 2021
1 parent 14b5797 commit f4a09d5
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 60 deletions.
24 changes: 24 additions & 0 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"@openid/appauth": "^1.3.0",
"@vue/cli": "^4.5.11",
"abortcontroller-polyfill": "^1.7.1",
"create-react-app": "^4.0.1",
"cli-ux": "^5.5.1",
"coveo.analytics": "^2.18.2",
"create-react-app": "^4.0.1",
"exponential-backoff": "^3.1.0",
"fs-extra": "^9.1.0",
"isomorphic-fetch": "^3.0.0",
Expand Down Expand Up @@ -57,7 +58,8 @@
}
},
"hooks": {
"init": "./lib/hooks/init/set-global-config"
"init": "./lib/hooks/init/set-global-config",
"analytics": "./lib/hooks/analytics/analytics"
}
},
"repository": "coveo/cli",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/auth/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ jest.mock('../../lib/oauth/oauth');
jest.mock('../../lib/oauth/storage');
jest.mock('../../lib/config/config');
jest.mock('keytar');
jest.mock('../../hooks/analytics/analytics');
import {test} from '@oclif/test';
import {mocked} from 'ts-jest/utils';
import {Config} from '../../lib/config/config';
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/src/commands/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
PlatformRegion,
} from '../../lib/platform/environment';
import {OrganizationModel} from '@coveord/platform-client';
import {
buildAnalyticsFailureHook,
buildAnalyticsSuccessHook,
} from '../../hooks/analytics/analytics';

export default class Login extends Command {
static description = 'Log into Coveo platform using OAuth2 flow';
Expand Down Expand Up @@ -64,6 +68,16 @@ export default class Login extends Command {
);
}
}
this.config.runHook('analytics', buildAnalyticsSuccessHook(this, flags));
}

async catch(err?: Error) {
const {flags} = this.parse(Login);
await this.config.runHook(
'analytics',
buildAnalyticsFailureHook(this, flags, err)
);
throw err;
}

private async pickFirstAvailableOrganization() {
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/src/commands/config/get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {Command} from '@oclif/command';
import {
buildAnalyticsFailureHook,
buildAnalyticsSuccessHook,
} from '../../hooks/analytics/analytics';
import {Config} from '../../lib/config/config';

export default class Get extends Command {
Expand All @@ -7,5 +11,14 @@ export default class Get extends Command {
async run() {
const cfg = await new Config(this.config.configDir, this.error).get();
this.log(JSON.stringify(cfg, null, 4));
await this.config.runHook('analytics', buildAnalyticsSuccessHook(this, {}));
}

async catch(err?: Error) {
await this.config.runHook(
'analytics',
buildAnalyticsFailureHook(this, {}, err)
);
throw err;
}
}
17 changes: 17 additions & 0 deletions packages/cli/src/commands/config/set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {Command, flags} from '@oclif/command';
import {
buildAnalyticsFailureHook,
buildAnalyticsSuccessHook,
} from '../../hooks/analytics/analytics';
import {Config} from '../../lib/config/config';
import {
PlatformEnvironment,
Expand Down Expand Up @@ -46,5 +50,18 @@ export default class Set extends Command {
if (flags.region) {
cfg.set('region', flags.region as PlatformRegion);
}
await this.config.runHook(
'analytics',
buildAnalyticsSuccessHook(this, flags)
);
}

async catch(err?: Error) {
const {flags} = this.parse(Set);
await this.config.runHook(
'analytics',
buildAnalyticsFailureHook(this, flags, err)
);
throw err;
}
}
17 changes: 0 additions & 17 deletions packages/cli/src/commands/hello.spec.ts

This file was deleted.

31 changes: 0 additions & 31 deletions packages/cli/src/commands/hello.ts

This file was deleted.

18 changes: 18 additions & 0 deletions packages/cli/src/commands/org/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import AuthenticationRequired from '../../lib/decorators/authenticationRequired'
import {AuthenticatedClient} from '../../lib/platform/authenticatedClient';
import {OrganizationModel} from '@coveord/platform-client';
import {cli} from 'cli-ux';
import {
buildAnalyticsFailureHook,
buildAnalyticsSuccessHook,
} from '../../hooks/analytics/analytics';

export default class List extends Command {
static description = 'List Coveo organizations.';
Expand Down Expand Up @@ -36,5 +40,19 @@ export default class List extends Command {
},
{...flags}
);

await this.config.runHook(
'analytics',
buildAnalyticsSuccessHook(this, flags)
);
}

async catch(err?: Error) {
const {flags} = this.parse(List);
await this.config.runHook(
'analytics',
buildAnalyticsFailureHook(this, flags, err)
);
throw err;
}
}
2 changes: 1 addition & 1 deletion packages/cli/src/commands/ui/create/react.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {test} from '@oclif/test';
import {EventEmitter} from 'events';
import * as child_process from 'child_process';

jest.mock('../../../hooks/analytics/analytics');
jest.mock('child_process', () => ({
spawn: jest.fn().mockImplementation(() => {
const successExitCode = 0;
Expand Down
19 changes: 14 additions & 5 deletions packages/cli/src/commands/ui/create/react.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {Command, flags} from '@oclif/command';
import {resolve} from 'path';
import {
buildAnalyticsFailureHook,
buildAnalyticsSuccessHook,
} from '../../../hooks/analytics/analytics';
import {spawnProcess} from '../../../lib/utils/process';

export default class React extends Command {
static description =
'Create a search page in React powered by Coveo Headless';

static flags = {
help: flags.help({char: 'h'}),
};

static examples = [
'$ coveo ui:create:react myapp',
'$ coveo ui:create:react --help',
Expand All @@ -20,8 +20,17 @@ export default class React extends Command {
];

async run() {
const {args, flags} = this.parse(React);
const {args} = this.parse(React);
await this.createProject(args.name);
await this.config.runHook('analytics', buildAnalyticsSuccessHook(this, {}));
}

async catch(err?: Error) {
await this.config.runHook(
'analytics',
buildAnalyticsFailureHook(this, {}, err)
);
throw err;
}

private createProject(name: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/ui/create/vue.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect, test} from '@oclif/test';

jest.mock('../../../hooks/analytics/analytics');
describe('ui:create:vue', () => {
test
.command(['ui:create:vue'])
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/src/commands/ui/create/vue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {Command, flags} from '@oclif/command';
import {dirname, resolve} from 'path';
import {
buildAnalyticsFailureHook,
buildAnalyticsSuccessHook,
} from '../../../hooks/analytics/analytics';
import {spawnProcess} from '../../../lib/utils/process';

export default class Vue extends Command {
Expand Down Expand Up @@ -41,6 +45,19 @@ export default class Vue extends Command {
await this.installPlugin(args.name);
await this.invokePlugin(args.name);
this.startServer(args.name);
await this.config.runHook(
'analytics',
buildAnalyticsSuccessHook(this, flags)
);
}

async catch(err?: Error) {
const {flags} = this.parse(Vue);
await this.config.runHook(
'analytics',
buildAnalyticsFailureHook(this, flags, err)
);
throw err;
}

private installPlugin(applicationName: string) {
Expand Down
Loading

0 comments on commit f4a09d5

Please sign in to comment.