Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit fe3b112

Browse files
authored
Silent ci install (#551)
* Fixing CI telemetry prompting * Adding lock file changes * Fixing tests to detect CI * Modifying logic to favor existing config file * Removing condition on function
1 parent a58fa27 commit fe3b112

5 files changed

Lines changed: 53 additions & 16 deletions

File tree

common/config/rush/pnpm-lock.yaml

Lines changed: 25 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"chalk": "2.4.1",
7171
"cli-ux": "^5.3.0",
7272
"fs-extra": "^7.0.1",
73+
"is-ci":"2.0.0",
7374
"latest-version": "^4.0.0",
7475
"semver": "^5.5.1",
7576
"tslib": "^1.9.3"

packages/cli/scripts/postinstall.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {cli} = require('cli-ux')
33
const chalk = require('chalk')
44
const path = require('path')
55
const fs = require('fs-extra')
6+
const isCI = require('is-ci')
67
const pjson = require('../package.json');
78

89
const windowsHomedriveHome = () => process.env.HOMEDRIVE && process.env.HOMEPATH && path.join(process.env.HOMEDRIVE, process.env.HOMEPATH)
@@ -37,7 +38,10 @@ const getUserConfig = async () => {
3738
try {
3839
const userConfig = await getUserConfig()
3940
userConfig.lastVersionCheck = new Date()
40-
if (userConfig.telemetry === null) {
41+
42+
if (!process.env.BF_CLI_TELEMETRY &&
43+
!isCI &&
44+
userConfig.telemetry === null) {
4145
const disableTelemetry = await cli.prompt(chalk.red('Help us improve products by allowing Microsoft to collect anonymous command and flags usage: (Y/N)'))
4246
if (disableTelemetry === 'Y' || disableTelemetry === 'y') {
4347
userConfig.telemetry = true

packages/cli/src/hooks/init/inithook.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const chalk = require('chalk')
1010
const path = require('path')
1111
const latestVersion = require('latest-version')
1212
const semver = require('semver')
13+
const isCI = require('is-ci')
1314

1415
const hook: Hook<'init'> = async function (opts) {
1516
// get config settings
@@ -70,7 +71,9 @@ const hook: Hook<'init'> = async function (opts) {
7071

7172
// Ensure telemetry is set
7273
try {
73-
if (userConfig.telemetry === null) {
74+
if (process.env.BF_CLI_TELEMETRY) {
75+
userConfig.telemetry = process.env.BF_CLI_TELEMETRY.toLowerCase() === 'true' ? true : false
76+
} else if (userConfig.telemetry === null && !isCI) {
7477
const disableTelemetry = await cli.prompt(chalk.red('Help us improve products by allowing Microsoft to collect anonymous command and flags usage: (Y/N)'))
7578
if (disableTelemetry === 'Y' || disableTelemetry === 'y') {
7679
userConfig.telemetry = true
@@ -89,7 +92,7 @@ const hook: Hook<'init'> = async function (opts) {
8992
await writeUserConfig(userConfig)
9093
}
9194

92-
this.config.pjson.telemetry = userConfig.telemetry
95+
this.config.pjson.telemetry = userConfig.telemetry === null ? false : userConfig.telemetry
9396
/* tslint:disable:no-unused */
9497

9598
} catch (err) {

packages/cli/test/commands/inithook.test.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from 'fs-extra'
44
const path = require('path')
55
const semver = require('semver')
66
const os = require('os')
7+
const isCI = require('is-ci')
78

89
const lastversioncheck = path.join(__dirname, '../fixtures/lastversioncheck')
910
const upgradeavailable = path.join(__dirname, '../fixtures/upgradeavailable')
@@ -46,7 +47,11 @@ describe('Check if telemetry is set if config is null', () => {
4647
.stdout()
4748
.hook('init', {argv: ['arg']}, {root: rootTelemetryNull})
4849
.do(output => {
49-
expect(output.stdout).to.contain('Telemetry will remain disabled')
50+
if (!isCI) {
51+
expect(output.stdout).to.contain('Telemetry will remain disabled')
52+
} else {
53+
expect(output.stdout).to.be.empty
54+
}
5055
})
5156
.it('it should disable telemetry when a user opts out')
5257

@@ -56,7 +61,11 @@ describe('Check if telemetry is set if config is null', () => {
5661
.stdout()
5762
.hook('init', {argv: ['arg']}, {root: rootTelemetryNull})
5863
.do(output => {
59-
expect(output.stdout).to.contain('Telemetry has been enabled')
64+
if (!isCI) {
65+
expect(output.stdout).to.contain('Telemetry has been enabled')
66+
} else {
67+
expect(output.stdout).to.be.empty
68+
}
6069
})
6170
.it('it should enable telemetry when a user opts in')
6271
})
@@ -132,19 +141,24 @@ describe('Update available to stdout', () => {
132141
})
133142

134143
describe('bypass version update if it\'s already been checked today', async () => {
144+
let originalDate
135145
beforeEach(async () => {
136146
// runs before all tests in this block
137147
const today = new Date()
138148
let userConfig = await fs.readJSON(path.join(lastversioncheck, 'package.json'))
149+
originalDate = userConfig.lastversioncheck
139150
userConfig.lastversioncheck = today
140151
fs.writeFileSync(path.join(lastversioncheck, 'package.json'), JSON.stringify(userConfig, null, 2))
141152
fs.mkdirSync(pathToConfigJsonUpdate)
142153
fs.writeFileSync(path.join(pathToConfigJsonUpdate, 'config.json'), JSON.stringify({telemetry: true}, null, 2))
143154
});
144155

145-
afterEach(function() {
156+
afterEach(async function() {
146157
// runs after all tests in this block
147158
fs.removeSync(pathToConfigJsonUpdate)
159+
let userConfig = await fs.readJSON(path.join(lastversioncheck, 'package.json'))
160+
userConfig.lastversioncheck = originalDate
161+
fs.writeFileSync(path.join(lastversioncheck, 'package.json'), JSON.stringify(userConfig, null, 2))
148162
});
149163

150164
test

0 commit comments

Comments
 (0)