Skip to content

Commit bab894d

Browse files
feat: create analyticsLogFile during preuninstall
In case the environment variable `NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE` is set and `npm un -g nativescript` or `npm i -g nativescript` is executed, CLI will use the value of the variable as path for the analyticsLogFile. This way it will be easy to check what information is tracked during install/uninstall of the CLI.
1 parent 2d2e0a3 commit bab894d

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

preuninstall.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
var path = require("path");
44
var child_process = require("child_process");
55
var commandArgs = [path.join(__dirname, "bin", "tns"), "dev-preuninstall"];
6+
if (process.env.NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE) {
7+
commandArgs.push("--analyticsLogFile", process.env.NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE);
8+
}
69

710
var childProcess = child_process.spawn(process.execPath, commandArgs, { stdio: "inherit"})
811

test/preuninstall.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const childProcess = require("child_process");
66
import { SpawnOptions, ChildProcess } from "child_process";
77
import * as path from "path";
88
import { EventEmitter } from "events";
9+
import { readFileSync } from "fs";
910

1011
describe("preuninstall.js", () => {
1112
let isSpawnCalled = false;
@@ -46,10 +47,7 @@ describe("preuninstall.js", () => {
4647

4748
const expectedPathToCliExecutable = path.join(__dirname, "..", "bin", "tns");
4849

49-
assert.isTrue(argsPassedToSpawn.indexOf(expectedPathToCliExecutable) !== -1, `The spawned args must contain path to TNS.
50-
Expected path is: ${expectedPathToCliExecutable}, current args are: ${argsPassedToSpawn}.`);
51-
assert.isTrue(argsPassedToSpawn.indexOf("dev-preuninstall") !== -1, `The spawned args must contain the name of the preuninstall command.
52-
Expected path is: ${expectedPathToCliExecutable}, current args are: ${argsPassedToSpawn}.`);
50+
assert.deepEqual(argsPassedToSpawn, [expectedPathToCliExecutable, "dev-preuninstall"]);
5351
assert.deepEqual(optionsPassedToSpawn, [{ stdio: "inherit" }], "The stdio must be inherit as this way CLI's command can determine correctly if terminal is in interactive mode.");
5452
assert.deepEqual(dataPassedToConsoleError, []);
5553

@@ -58,6 +56,25 @@ describe("preuninstall.js", () => {
5856
assert.deepEqual(dataPassedToConsoleError, [`Failed to complete all pre-uninstall steps. Error is ${errMsg}`]);
5957
});
6058

59+
it("passes --analyticsLogFile option when NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE is set", () => {
60+
const content = readFileSync(path.join(__dirname, "..", "preuninstall.js")).toString();
61+
const originalEnvValue = process.env.NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE;
62+
process.env.NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE = "value from env analyticsLog.txt";
63+
/* tslint:disable:no-eval */
64+
eval(content);
65+
/* tslint:enable:no-eval */
66+
process.env.NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE = originalEnvValue;
67+
assert.isTrue(isSpawnCalled, "child_process.spawn must be called from preuninstall.js");
68+
69+
// NOTE: As the script is eval'd, the `__dirname` in it is resolved to current file's location,
70+
// so the expectedPathToCliExecutable should be set as it is located in current dir.
71+
const expectedPathToCliExecutable = path.join(__dirname, "bin", "tns");
72+
73+
assert.deepEqual(argsPassedToSpawn, [expectedPathToCliExecutable, "dev-preuninstall", "--analyticsLogFile", "value from env analyticsLog.txt"]);
74+
assert.deepEqual(optionsPassedToSpawn, [{ stdio: "inherit" }], "The stdio must be inherit as this way CLI's command can determine correctly if terminal is in interactive mode.");
75+
assert.deepEqual(dataPassedToConsoleError, []);
76+
});
77+
6178
it("ensure package.json has correct preuninstall script", () => {
6279
const packageJsonData = require("../package.json");
6380
assert.equal(packageJsonData.scripts.preuninstall, "node preuninstall.js");

0 commit comments

Comments
 (0)