Skip to content
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

fix: bump to core 3, remove command dep #282

Merged
merged 6 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions bin/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node

require('@oclif/command').run()
.catch(require('@oclif/errors/handle'))
require('@oclif/command').run().catch(require('@oclif/errors/handle'));
22 changes: 0 additions & 22 deletions messages/messages.json

This file was deleted.

6 changes: 0 additions & 6 deletions messages/org.json

This file was deleted.

38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
{
"name": "@salesforce/plugin-telemetry",
"description": "Command usage and error telemetry for the Salesforce CLI",
"version": "1.4.0",
"version": "2.0.0",
"author": "Salesforce",
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
"@oclif/config": "^1",
"@salesforce/command": "^4.2.0",
"@salesforce/core": "^2.31.0",
"@salesforce/telemetry": "^3.1.0",
"@oclif/core": "^1.8.0",
"@salesforce/core": "^3.16.0",
"@salesforce/telemetry": "^3.2.1",
"debug": "^4.3.4",
"tslib": "^2"
},
"devDependencies": {
"@oclif/dev-cli": "^1",
"@oclif/plugin-command-snapshot": "^2.0.0",
"@salesforce/cli-plugins-testkit": "^1.1.5",
"@salesforce/dev-config": "^2.1.3",
"@salesforce/dev-scripts": "^1.0.0",
"@salesforce/plugin-command-reference": "^1.3.0",
"@oclif/plugin-command-snapshot": "^3",
"@oclif/plugin-help": "^3.0.1",
"@salesforce/cli-plugins-testkit": "^1.3.0",
"@salesforce/dev-config": "^3.0.1",
"@salesforce/dev-scripts": "^2.0.1",
"@salesforce/plugin-command-reference": "^1.3.16",
"@salesforce/prettier-config": "^0.0.2",
"@salesforce/ts-sinon": "1.3.21",
"@salesforce/ts-sinon": "^1.3.21",
"@types/debug": "^4.1.5",
"@typescript-eslint/eslint-plugin": "^4.2.0",
"@typescript-eslint/parser": "^4.2.0",
"chai": "^4.2.0",
"cz-conventional-changelog": "^3.2.0",
"eslint": "^7.27.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-salesforce": "^0.1.6",
"eslint-config-salesforce-license": "^0.1.6",
"eslint-config-salesforce-typescript": "^0.2.7",
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-header": "^3.0.0",
"eslint-plugin-import": "2.25.4",
"eslint-plugin-jsdoc": "^35.1.2",
"eslint-plugin-prettier": "^3.1.3",
"husky": "^7.0.4",
"lint-staged": "^11.0.0",
"mocha": "^8.4.0",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"prettier": "^2.0.5",
"oclif": "^2.6.1",
"prettier": "^2.4.1",
"pretty-quick": "^3.1.0",
"shx": "0.3.4",
"sinon": "10.0.0",
Expand All @@ -51,7 +51,7 @@
}
},
"engines": {
"node": ">=12.0.0"
"node": ">=14.0.0"
},
"files": [
"/lib",
Expand Down Expand Up @@ -99,7 +99,7 @@
"test:command-reference": "./bin/run commandreference:generate --erroronwarnings",
"test:deprecation-policy": "./bin/run snapshot:compare",
"test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel",
"version": "oclif-dev readme"
"version": "oclif readme"
},
"publishConfig": {
"access": "public"
Expand Down
24 changes: 12 additions & 12 deletions src/commandExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
*/

import { join } from 'path';
import { Command, IConfig } from '@oclif/config';
import { parse, Input } from '@oclif/parser';
import { fs, SfdxProject } from '@salesforce/core';
import * as fs from 'fs';
import { Config, Interfaces, Parser } from '@oclif/core';
import { SfProject } from '@salesforce/core';
import { AsyncCreatable } from '@salesforce/kit';
import { isNumber, JsonMap, Optional } from '@salesforce/ts-types';
import { debug } from './debuger';
import { InitData } from './hooks/telemetryInit';

export type CommandExecutionOptions = {
command: Command.Class;
command: Partial<Interfaces.Command.Class>;
argv: string[];
config: IConfig;
config: Partial<Config>;
};

interface PluginInfo {
Expand All @@ -31,9 +31,9 @@ export class CommandExecution extends AsyncCreatable {
private upTimeAtCmdStart: number;
private specifiedFlags: string[] = [];
private specifiedFlagFullNames: string[] = [];
private command: Command.Class;
private command: Partial<Interfaces.Command.Class>;
private argv: string[];
private config: IConfig;
private config: Partial<Config>;
private vcs?: string;

// These will be removed by the uploader
Expand All @@ -58,15 +58,15 @@ export class CommandExecution extends AsyncCreatable {
public static async resolveVCSInfo(): Promise<string> {
let possibleVcsPath: string;
try {
possibleVcsPath = await SfdxProject.resolveProjectPath();
possibleVcsPath = await SfProject.resolveProjectPath();
} catch (err) {
debug('Not in a sfdx project, using current working directory');
possibleVcsPath = process.cwd();
}

const gitPath = join(possibleVcsPath, '.git');
try {
await fs.access(gitPath, fs.constants.R_OK);
await fs.promises.access(gitPath, fs.constants.R_OK);
return 'git';
} catch (err) {
return 'other';
Expand Down Expand Up @@ -146,10 +146,10 @@ export class CommandExecution extends AsyncCreatable {
const commandDef = { flags: flagDefinitions, args: this.command.args, strict: !anyCmd.varargs };

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let flags: Input<any> = {};
let flags: Interfaces.Input<any> = {};
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
flags = parse(argv, commandDef).flags;
flags = (await Parser.parse(argv, commandDef)).flags;
} catch (error) {
debug('Error parsing flags');
}
Expand All @@ -162,7 +162,7 @@ export class CommandExecution extends AsyncCreatable {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private determineSpecifiedFlags(argv: string[], flags: any, flagDefinitions: Input<any>): void {
private determineSpecifiedFlags(argv: string[], flags: any, flagDefinitions: Interfaces.Input<any>): void {
// Help won't be in the parsed flags
const shortHelp = argv.find((arg) => /^-h$/.test(arg));
const fullHelp = argv.find((arg) => /^--help$/.test(arg));
Expand Down
2 changes: 1 addition & 1 deletion src/commands/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { Command } from '@oclif/command';
import { Command } from '@oclif/core';
import TelemetryReporter from '@salesforce/telemetry';
import Telemetry from '../telemetry';
import { TelemetryGlobal } from '../telemetryGlobal';
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/telemetryInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { Hook } from '@oclif/config';
import { Hook } from '@oclif/core';

export class InitData {
public static upTimeAtInit: number;
Expand Down
13 changes: 7 additions & 6 deletions src/hooks/telemetryPrerun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
*/

import { join } from 'path';
import { Hook, Hooks } from '@oclif/config';
import { Org, SfdxError, Lifecycle } from '@salesforce/core';
import { Hook } from '@oclif/core';
import { Org, SfError, Lifecycle } from '@salesforce/core';
import { TelemetryReporter } from '@salesforce/telemetry';
import Telemetry from '../telemetry';
import { TelemetryGlobal } from '../telemetryGlobal';
import { CommandExecution } from '../commandExecution';
import { debug } from '../debuger';

declare const global: TelemetryGlobal;

interface CommonData {
Expand All @@ -28,7 +29,7 @@ interface CommonData {
* 2. Writes logs to a file, including execution and errors.
* 3. Logs command usage data to the server right after the process ends by spawning a detached process.
*/
const hook: Hook.Prerun = async function (options: Hooks['prerun']): Promise<void> {
const hook: Hook.Prerun = async function (options): Promise<void> {
const telemetryEnabled = await TelemetryReporter.determineSfdxTelemetryEnabled();
// Don't even bother logging if telemetry is disabled
if (!telemetryEnabled) {
Expand Down Expand Up @@ -65,7 +66,7 @@ const hook: Hook.Prerun = async function (options: Hooks['prerun']): Promise<voi
});
} catch (err) {
// even if this throws, the rest of telemetry is not affected
const error = err as SfdxError;
const error = err as SfError;
debug('Error subscribing to telemetry events', error.message);
}

Expand Down Expand Up @@ -113,7 +114,7 @@ const hook: Hook.Prerun = async function (options: Hooks['prerun']): Promise<voi
process.on(
'cmdError',
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async (cmdErr: SfdxError, _, org?: Org): Promise<void> => {
async (cmdErr: SfError, _, org?: Org): Promise<void> => {
const apiVersion = org ? org.getConnection().getApiVersion() : undefined;
let orgType: string | undefined;

Expand Down Expand Up @@ -153,7 +154,7 @@ const hook: Hook.Prerun = async function (options: Hooks['prerun']): Promise<voi
return commonData;
};
} catch (err) {
const error = err as SfdxError;
const error = err as SfError;
debug('Error with logging or sending telemetry:', error.message);
}
};
Expand Down
37 changes: 18 additions & 19 deletions src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import { spawn } from 'child_process';
import { randomBytes } from 'crypto';
import * as systemFs from 'fs';
import * as fs from 'fs';
import { EOL, tmpdir } from 'os';
import { join } from 'path';
import { Attributes } from '@salesforce/telemetry';
import { AsyncCreatable, env } from '@salesforce/kit';
import { fs, SfdxError } from '@salesforce/core';
import { SfError } from '@salesforce/core';
import { isBoolean, isNumber, isString, JsonMap } from '@salesforce/ts-types';
import { debug } from './debuger';

Expand Down Expand Up @@ -105,10 +105,10 @@ export default class Telemetry extends AsyncCreatable {

const acknowledgementFilePath = join(Telemetry.cacheDir, USAGE_ACKNOWLEDGEMENT_FILE_NAME);
try {
await fs.access(acknowledgementFilePath, fs.constants.R_OK);
await fs.promises.access(acknowledgementFilePath, fs.constants.R_OK);
debug('Usage acknowledgement file already exists');
} catch (error) {
const err = error as SfdxError;
const err = error as SfError;
if (err.code === 'ENOENT') {
if (!env.getBoolean('SFDX_TELEMETRY_DISABLE_ACKNOWLEDGEMENT', false)) {
// eslint-disable-next-line no-console
Expand All @@ -117,8 +117,8 @@ export default class Telemetry extends AsyncCreatable {
);
}
Telemetry.acknowledged = true;
await fs.mkdirp(Telemetry.cacheDir);
await fs.writeJson(acknowledgementFilePath, { acknowledged: true });
await fs.promises.mkdir(Telemetry.cacheDir, { recursive: true });
await fs.promises.writeFile(acknowledgementFilePath, JSON.stringify({ acknowledged: true }));
debug('Wrote usage acknowledgement file', acknowledgementFilePath);
} else {
debug('Could not access', acknowledgementFilePath, 'DUE TO:', err.code, err.message);
Expand Down Expand Up @@ -208,11 +208,11 @@ export default class Telemetry extends AsyncCreatable {
const cliIdPath = join(Telemetry.cacheDir, CLI_ID_FILE_NAME);

try {
this.cliId = systemFs.readFileSync(cliIdPath, 'utf8');
this.cliId = fs.readFileSync(cliIdPath, 'utf8');
} catch (err) {
debug('Unique CLI ID not found, generating and writing new ID to ', cliIdPath);
this.cliId = Telemetry.generateRandomId();
systemFs.writeFileSync(cliIdPath, this.cliId, 'utf8');
fs.writeFileSync(cliIdPath, this.cliId, 'utf8');

// If there is not a unique ID for this CLI, consider it a first run.
this.firstRun = true;
Expand Down Expand Up @@ -263,9 +263,9 @@ export default class Telemetry extends AsyncCreatable {
dataToRecord.ci = Telemetry.guessCISystem();
dataToRecord.executable = Telemetry.executable;
try {
systemFs.writeSync(this.fileDescriptor, JSON.stringify(dataToRecord) + EOL);
fs.writeSync(this.fileDescriptor, JSON.stringify(dataToRecord) + EOL);
} catch (err) {
const error = err as SfdxError;
const error = err as SfError;
debug(`Error saving telemetry line to file: ${error.message}`);
}
}
Expand All @@ -288,20 +288,19 @@ export default class Telemetry extends AsyncCreatable {

public async clear(): Promise<void> {
debug('Deleting the log file', this.getTelemetryFilePath());
await fs.unlink(this.getTelemetryFilePath());
await fs.promises.unlink(this.getTelemetryFilePath());
}

public async read(): Promise<Attributes[]> {
try {
debug(`Reading ${this.getTelemetryFilePath()}`);
const data = await fs.readFile(this.getTelemetryFilePath(), 'utf8');
const events = data
const data = await fs.promises.readFile(this.getTelemetryFilePath(), 'utf8');
return data
.split(EOL)
.filter((line) => !!line)
.map((line) => JSON.parse(line) as Attributes);
return events;
} catch (error) {
const err = error as SfdxError;
const err = error as SfError;
debug(`Error reading: ${err.message}`);
// If anything goes wrong, it just means a couple of lost telemetry events.
return [];
Expand Down Expand Up @@ -338,16 +337,16 @@ export default class Telemetry extends AsyncCreatable {

// Make sure the tmp dir is created.
try {
await fs.access(Telemetry.tmpDir, fs.constants.W_OK);
await fs.promises.access(Telemetry.tmpDir, fs.constants.W_OK);
} catch (error) {
const err = error as SfdxError;
const err = error as SfError;
if (err.code === 'ENOENT') {
debug('Telemetry temp dir does not exist, creating...');
await fs.mkdirp(Telemetry.tmpDir);
await fs.promises.mkdir(Telemetry.tmpDir, { recursive: true });
}
}
// Create a file descriptor to be used
this.fileDescriptor = systemFs.openSync(this.getTelemetryFilePath(), 'a');
this.fileDescriptor = fs.openSync(this.getTelemetryFilePath(), 'a');
debug(`Using telemetry logging file ${this.getTelemetryFilePath()}`);
}
}