@cedarjs/telemetry 8.0.0-canary.3595 → 8.0.0-canary.3597

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Describes the running CLI command for telemetry, e.g.
3
+ * `generate page [name] [path] --force` for
4
+ * `cedar g page Payroll /payroll --force`.
5
+ *
6
+ * The description is built from the command's yargs definition, so it only
7
+ * contains Cedar's own command names, positional argument names and option
8
+ * names. Every value the user typed is replaced by a placeholder named after
9
+ * its argument or option. Everything after `--` is left out.
10
+ *
11
+ * Options that a command doesn't declare are forwarded to the tool it wraps,
12
+ * like Prisma or Vitest, and their names are kept. `exec` forwards them to
13
+ * the user's own script, where the names can identify the user's project, so
14
+ * for `exec` only declared options are kept.
15
+ *
16
+ * Returns `undefined` if `yargs` doesn't expose the running command.
17
+ *
18
+ * @param argv The arguments yargs parsed
19
+ * @param yargs The yargs instance, as passed to middleware
20
+ * @param rawArgs The arguments the user typed, without the node and script
21
+ * paths
22
+ */
23
+ export declare function commandForTelemetry(argv: Record<string, unknown>, yargs: unknown, rawArgs: string[]): string | undefined;
24
+ //# sourceMappingURL=commandForTelemetry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commandForTelemetry.d.ts","sourceRoot":"","sources":["../src/commandForTelemetry.ts"],"names":[],"mappings":"AAiEA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,MAAM,EAAE,GAChB,MAAM,GAAG,SAAS,CA8FpB"}
@@ -0,0 +1,90 @@
1
+ const OPTION = /^(--?)([a-zA-Z][\w-]*)/;
2
+ function getYargsInternals(yargs) {
3
+ if (typeof yargs !== "object" || yargs === null || !("getInternalMethods" in yargs) || typeof yargs.getInternalMethods !== "function" || !("getOptions" in yargs) || typeof yargs.getOptions !== "function") {
4
+ return void 0;
5
+ }
6
+ const context = yargs.getInternalMethods().getContext?.();
7
+ const options = yargs.getOptions();
8
+ if (typeof context !== "object" || context === null || !("fullCommands" in context) || !Array.isArray(context.fullCommands) || typeof options !== "object" || options === null || !("key" in options) || typeof options.key !== "object" || options.key === null || !("boolean" in options) || !Array.isArray(options.boolean) || !("alias" in options) || typeof options.alias !== "object" || options.alias === null) {
9
+ return void 0;
10
+ }
11
+ return {
12
+ fullCommands: context.fullCommands.filter(
13
+ (command) => typeof command === "string"
14
+ ),
15
+ // `key` and `alias` are plain objects that yargs builds from the option
16
+ // definitions
17
+ declared: options.key,
18
+ booleans: options.boolean.filter(
19
+ (name) => typeof name === "string"
20
+ ),
21
+ aliases: options.alias
22
+ };
23
+ }
24
+ function hasValue(value) {
25
+ return value !== void 0 && !(Array.isArray(value) && value.length === 0);
26
+ }
27
+ function commandForTelemetry(argv, yargs, rawArgs) {
28
+ const internals = getYargsInternals(yargs);
29
+ if (!internals) {
30
+ return void 0;
31
+ }
32
+ const { fullCommands, declared, booleans, aliases } = internals;
33
+ const parts = [];
34
+ for (const fullCommand of fullCommands) {
35
+ const [name, ...positionals] = fullCommand.split(/\s+/);
36
+ if (name && !name.startsWith("$")) {
37
+ parts.push(name);
38
+ }
39
+ for (const positional of positionals) {
40
+ if (!/^[<[]/.test(positional)) {
41
+ continue;
42
+ }
43
+ const key = positional.replace(/[<>[\].]/g, "");
44
+ if (hasValue(argv[key])) {
45
+ parts.push(`[${key}]`);
46
+ }
47
+ }
48
+ }
49
+ const forwardsToUserCode = parts[0] === "exec";
50
+ const passthroughIndex = rawArgs.indexOf("--");
51
+ const ownArgs = passthroughIndex === -1 ? rawArgs : rawArgs.slice(0, passthroughIndex);
52
+ const isFlag = (key) => booleans.includes(key) || typeof argv[key] === "boolean";
53
+ const placeholderFor = (key) => `[${Object.keys(aliases).find((alias) => aliases[alias]?.includes(key)) ?? key}]`;
54
+ for (const arg of ownArgs) {
55
+ const match = arg.match(OPTION);
56
+ if (!match) {
57
+ continue;
58
+ }
59
+ const [, dashes, matchedName] = match;
60
+ if (dashes === "-") {
61
+ for (const letter of matchedName) {
62
+ if (!/[a-zA-Z]/.test(letter)) {
63
+ break;
64
+ }
65
+ const flag = isFlag(letter);
66
+ const known = letter in declared;
67
+ if (!forwardsToUserCode || known) {
68
+ parts.push(
69
+ flag ? `-${letter}` : `-${letter} ${placeholderFor(letter)}`
70
+ );
71
+ }
72
+ if (!flag || !known) {
73
+ break;
74
+ }
75
+ }
76
+ continue;
77
+ }
78
+ const key = matchedName.startsWith("no-") && !(matchedName in declared) ? matchedName.slice(3) : matchedName;
79
+ if (forwardsToUserCode && !(key in declared)) {
80
+ continue;
81
+ }
82
+ parts.push(
83
+ isFlag(key) ? `--${matchedName}` : `--${matchedName} ${placeholderFor(key)}`
84
+ );
85
+ }
86
+ return parts.join(" ");
87
+ }
88
+ export {
89
+ commandForTelemetry
90
+ };
@@ -1,36 +1,19 @@
1
- interface SensitiveArgPositions {
2
- exec: {
3
- positions: number[];
4
- options?: never;
5
- redactWith: string[];
6
- allowOnly: string[];
7
- };
8
- g: {
9
- positions: number[];
10
- options?: never;
11
- redactWith: string[];
12
- allowOnly?: string[];
13
- };
14
- generate: {
15
- positions: number[];
16
- options?: never;
17
- redactWith: string[];
18
- allowOnly?: string[];
19
- };
20
- prisma: {
21
- positions?: never;
22
- options: string[];
23
- redactWith: string[];
24
- allowOnly?: string[];
25
- };
26
- lint: {
27
- positions?: number[];
28
- options?: never;
29
- redactWith: string[];
30
- allowOnly: string[];
31
- };
32
- }
33
- export declare const sanitizeArgv: (argv: [string, string, keyof SensitiveArgPositions, ...string[]]) => string;
1
+ /**
2
+ * Turns `process.argv` into the `command` string sent with telemetry, for
3
+ * events sent before the CLI has described the running command (see
4
+ * `commandForTelemetry`).
5
+ *
6
+ * Only the command and the names of the options are kept, e.g.
7
+ * `generate --force --typescript`. Option values, positional arguments and
8
+ * everything after `--` can contain names, paths, hosts and other values
9
+ * that identify a user or their project, so they are left out.
10
+ *
11
+ * The command is the first argument after the script path. It is only kept
12
+ * if it looks like a command name, like `build` or `data-migrate`.
13
+ *
14
+ * `exec` passes its options on to the user's script, so for `exec` only the
15
+ * command is kept.
16
+ */
17
+ export declare const sanitizeArgv: (argv: string[]) => string;
34
18
  export declare const sendTelemetry: () => Promise<void>;
35
- export {};
36
19
  //# sourceMappingURL=sendTelemetry.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sendTelemetry.d.ts","sourceRoot":"","sources":["../src/sendTelemetry.ts"],"names":[],"mappings":"AAeA,UAAU,qBAAqB;IAC7B,IAAI,EAAE;QACJ,SAAS,EAAE,MAAM,EAAE,CAAA;QACnB,OAAO,CAAC,EAAE,KAAK,CAAA;QACf,UAAU,EAAE,MAAM,EAAE,CAAA;QACpB,SAAS,EAAE,MAAM,EAAE,CAAA;KACpB,CAAA;IACD,CAAC,EAAE;QACD,SAAS,EAAE,MAAM,EAAE,CAAA;QACnB,OAAO,CAAC,EAAE,KAAK,CAAA;QACf,UAAU,EAAE,MAAM,EAAE,CAAA;QACpB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;KACrB,CAAA;IACD,QAAQ,EAAE;QACR,SAAS,EAAE,MAAM,EAAE,CAAA;QACnB,OAAO,CAAC,EAAE,KAAK,CAAA;QACf,UAAU,EAAE,MAAM,EAAE,CAAA;QACpB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;KACrB,CAAA;IACD,MAAM,EAAE;QACN,SAAS,CAAC,EAAE,KAAK,CAAA;QACjB,OAAO,EAAE,MAAM,EAAE,CAAA;QACjB,UAAU,EAAE,MAAM,EAAE,CAAA;QACpB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;KACrB,CAAA;IACD,IAAI,EAAE;QACJ,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;QACpB,OAAO,CAAC,EAAE,KAAK,CAAA;QACf,UAAU,EAAE,MAAM,EAAE,CAAA;QACpB,SAAS,EAAE,MAAM,EAAE,CAAA;KACpB,CAAA;CACF;AA8ED,eAAO,MAAM,YAAY,GACvB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,qBAAqB,EAAE,GAAG,MAAM,EAAE,CAAC,WAyCjE,CAAA;AAyGD,eAAO,MAAM,aAAa,qBAsCzB,CAAA"}
1
+ {"version":3,"file":"sendTelemetry.d.ts","sourceRoot":"","sources":["../src/sendTelemetry.ts"],"names":[],"mappings":"AAsEA;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,EAAE,WAsB1C,CAAA;AAgHD,eAAO,MAAM,aAAa,qBAsCzB,CAAA"}
@@ -9,29 +9,6 @@ import yargsParser from "yargs/yargs";
9
9
  import { getRawConfig, getPaths } from "@cedarjs/project-config";
10
10
  import { RWProject } from "@cedarjs/structure/dist/model/RWProject";
11
11
  import { redactErrorMessage } from "./redact.js";
12
- const SENSITIVE_ARG_POSITIONS = {
13
- exec: {
14
- positions: [1],
15
- redactWith: ["[script]"],
16
- allowOnly: ["exec"]
17
- },
18
- g: {
19
- positions: [2, 3],
20
- redactWith: ["[name]", "[path]"]
21
- },
22
- generate: {
23
- positions: [2, 3],
24
- redactWith: ["[name]", "[path]"]
25
- },
26
- prisma: {
27
- options: ["--name"],
28
- redactWith: ["[name]"]
29
- },
30
- lint: {
31
- allowOnly: ["lint", "--fix"],
32
- redactWith: ["[path]"]
33
- }
34
- };
35
12
  const getInfo = async (presets = {}) => {
36
13
  const info = JSON.parse(
37
14
  await envinfo.run(
@@ -66,35 +43,23 @@ const getInfo = async (presets = {}) => {
66
43
  experiments
67
44
  };
68
45
  };
46
+ const LONG_OPTION_NAME = /^--[a-zA-Z][\w-]*/;
47
+ const SHORT_OPTION_NAME = /^-[a-zA-Z]/;
48
+ const COMMAND_NAME = /^[a-z][\w:-]*$/i;
69
49
  const sanitizeArgv = (argv) => {
70
- const name = argv[2];
71
- const sensitiveCommand = SENSITIVE_ARG_POSITIONS[name];
72
50
  const args = argv.slice(2);
73
- if (sensitiveCommand) {
74
- if (sensitiveCommand.positions) {
75
- sensitiveCommand.positions.forEach((pos, index) => {
76
- if (args[pos] && !args[pos].includes("--")) {
77
- args[pos] = sensitiveCommand.redactWith[index];
78
- }
79
- });
80
- }
81
- if (sensitiveCommand.options) {
82
- sensitiveCommand.options.forEach((option, index) => {
83
- const argIndex = args.indexOf(option);
84
- if (argIndex !== -1) {
85
- args[argIndex + 1] = sensitiveCommand.redactWith[index];
86
- }
87
- });
88
- }
89
- if (sensitiveCommand.allowOnly) {
90
- args.forEach((arg, index) => {
91
- if (!sensitiveCommand.allowOnly?.includes(arg) && !sensitiveCommand.redactWith.includes(arg)) {
92
- args[index] = sensitiveCommand.redactWith[0];
93
- }
94
- });
95
- }
51
+ const passthroughIndex = args.indexOf("--");
52
+ const ownArgs = passthroughIndex === -1 ? args : args.slice(0, passthroughIndex);
53
+ if (ownArgs[0] === "exec") {
54
+ return "exec";
96
55
  }
97
- return args.join(" ");
56
+ return ownArgs.flatMap((arg, index) => {
57
+ const optionName = arg.match(LONG_OPTION_NAME)?.[0] ?? arg.match(SHORT_OPTION_NAME)?.[0];
58
+ if (optionName) {
59
+ return [optionName];
60
+ }
61
+ return index === 0 && COMMAND_NAME.test(arg) ? [arg] : [];
62
+ }).join(" ");
98
63
  };
99
64
  const buildPayload = async () => {
100
65
  let payload = {};
@@ -115,7 +80,8 @@ const buildPayload = async () => {
115
80
  const durationFlag = typeof rawArgv.duration === "string" ? rawArgv.duration : void 0;
116
81
  const rwVersionFlag = typeof rawArgv.rwVersion === "string" ? rawArgv.rwVersion : void 0;
117
82
  const errorFlag = typeof rawArgv.error === "string" ? rawArgv.error : void 0;
118
- const command = argvFlag ? sanitizeArgv(JSON.parse(argvFlag)) : "";
83
+ const commandFlag = typeof rawArgv.command === "string" ? rawArgv.command : void 0;
84
+ const command = commandFlag ? String(JSON.parse(commandFlag)) : argvFlag ? sanitizeArgv(JSON.parse(argvFlag)) : "";
119
85
  payload = {
120
86
  type: typeFlag || "command",
121
87
  command,
@@ -18,5 +18,10 @@ export declare const timedTelemetry: (argv: string[], options: Record<string, un
18
18
  * is sent, see `redactErrorMessage`.
19
19
  */
20
20
  export declare const errorTelemetry: (argv: string[], error: unknown) => Promise<void>;
21
- export declare const telemetryMiddleware: () => Promise<void>;
21
+ /**
22
+ * Used as yargs middleware when any command is invoked. Sends a telemetry
23
+ * event for the command, described from its yargs definition so that no
24
+ * values the user typed are sent.
25
+ */
26
+ export declare const telemetryMiddleware: (argv: Record<string, unknown>, yargs: unknown) => Promise<void>;
22
27
  //# sourceMappingURL=telemetry.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,UAuB/C;AAsDD,eAAO,MAAM,cAAc,GACzB,MAAM,MAAM,EAAE,EACd,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,MAAM,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,iBAuB9B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAU,MAAM,MAAM,EAAE,EAAE,OAAO,OAAO,kBAclE,CAAA;AAGD,eAAO,MAAM,mBAAmB,qBAS/B,CAAA"}
1
+ {"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,UAuB/C;AAsED,eAAO,MAAM,cAAc,GACzB,MAAM,MAAM,EAAE,EACd,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,MAAM,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,iBAsB9B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAU,MAAM,MAAM,EAAE,EAAE,OAAO,OAAO,kBAalE,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAC9B,MAAM,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,OAAO,kBAYf,CAAA"}
package/dist/telemetry.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { spawn } from "child_process";
2
2
  import os from "os";
3
3
  import path from "path";
4
+ import { hideBin } from "yargs/helpers";
5
+ import { commandForTelemetry } from "./commandForTelemetry.js";
4
6
  import { redactErrorMessage } from "./redact.js";
5
7
  function quoteForWindowsShell(arg) {
6
8
  let quoted = '"';
@@ -44,6 +46,8 @@ const spawnProcess = (...args) => {
44
46
  spawn(process.execPath, scriptArgs, spawnOptions).unref();
45
47
  }
46
48
  };
49
+ let currentCommand;
50
+ const commandArgs = (argv) => currentCommand === void 0 ? ["--argv", JSON.stringify(argv)] : ["--command", JSON.stringify(currentCommand)];
47
51
  const timedTelemetry = async (argv, options, func) => {
48
52
  if (process.env.CEDAR_DISABLE_TELEMETRY || process.env.REDWOOD_DISABLE_TELEMETRY) {
49
53
  return func.call(void 0);
@@ -52,8 +56,7 @@ const timedTelemetry = async (argv, options, func) => {
52
56
  const result = await func.call(void 0);
53
57
  const duration = (/* @__PURE__ */ new Date()).getTime() - start.getTime();
54
58
  spawnProcess(
55
- "--argv",
56
- JSON.stringify(argv),
59
+ ...commandArgs(argv),
57
60
  "--duration",
58
61
  duration.toString(),
59
62
  "--type",
@@ -66,17 +69,17 @@ const errorTelemetry = async (argv, error) => {
66
69
  return;
67
70
  }
68
71
  spawnProcess(
69
- "--argv",
70
- JSON.stringify(argv),
72
+ ...commandArgs(argv),
71
73
  "--error",
72
74
  JSON.stringify(redactErrorMessage(error))
73
75
  );
74
76
  };
75
- const telemetryMiddleware = async () => {
77
+ const telemetryMiddleware = async (argv, yargs) => {
76
78
  if (process.env.CEDAR_DISABLE_TELEMETRY || process.env.REDWOOD_DISABLE_TELEMETRY) {
77
79
  return;
78
80
  }
79
- spawnProcess("--argv", JSON.stringify(process.argv));
81
+ currentCommand = commandForTelemetry(argv, yargs, hideBin(process.argv));
82
+ spawnProcess(...commandArgs(process.argv));
80
83
  };
81
84
  export {
82
85
  errorTelemetry,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/telemetry",
3
- "version": "8.0.0-canary.3595",
3
+ "version": "8.0.0-canary.3597",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -22,8 +22,8 @@
22
22
  "test:watch": "vitest watch"
23
23
  },
24
24
  "dependencies": {
25
- "@cedarjs/project-config": "8.0.0-canary.3595",
26
- "@cedarjs/structure": "8.0.0-canary.3595",
25
+ "@cedarjs/project-config": "8.0.0-canary.3597",
26
+ "@cedarjs/structure": "8.0.0-canary.3597",
27
27
  "@whatwg-node/fetch": "0.12.1",
28
28
  "ci-info": "4.4.0",
29
29
  "envinfo": "7.21.0",