-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: user consent prompt and random seed instead of machine-id (#4)
- Loading branch information
pooya parsa
committed
May 29, 2020
1 parent
9f35a2f
commit 4910922
Showing
15 changed files
with
1,049 additions
and
618 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ node_modules | |
.DS_Store | ||
coverage | ||
dist | ||
.nuxtrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
require('../dist/cli.js').run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
import _module from '../src/module' | ||
|
||
export default { | ||
modules: [_module], | ||
telemetry: { | ||
debug: true | ||
// url: 'http://localhost:8888' | ||
} | ||
modules: [_module] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { resolve } from 'path' | ||
import { existsSync } from 'fs' | ||
import arg from 'arg' | ||
import * as rc from 'rc9' | ||
import consola from 'consola' | ||
import { usage, consentVersion } from './meta' | ||
|
||
function _run () { | ||
const args = arg({ | ||
'--global': Boolean, | ||
'-g': '--global' | ||
}) | ||
|
||
const [command, _dir] = args._ | ||
const dir = resolve(process.cwd(), _dir || '.') | ||
const global = args['--global'] | ||
|
||
if (!global && !existsSync(resolve(dir, 'nuxt.config.js')) && | ||
!existsSync(resolve(dir, 'nuxt.config.ts'))) { | ||
consola.error(`It seems you are not in a nuxt project (no nuxt.config found at ${dir})`) | ||
showUsage() | ||
} | ||
|
||
switch (command) { | ||
case 'enable': | ||
setRC('telemetry.consent', consentVersion) | ||
consola.success('Nuxt telemetry enabled for', global ? 'user' : dir) | ||
consola.info('You can disable telemetry with `nuxt telemetry disable ' + (global ? '-g' : _dir)) | ||
return | ||
case 'disable': | ||
setRC('telemetry.consent', false) | ||
consola.success('Nuxt telemetry disabled for', global ? 'user' : dir) | ||
consola.info('You can enable telemetry with `nuxt telemetry enable ' + (global ? '-g' : _dir) + '`') | ||
return | ||
default: | ||
showUsage() | ||
} | ||
|
||
function showUsage () { | ||
consola.info(`Usage: ${usage}\n`) | ||
process.exit(1) | ||
} | ||
|
||
function setRC (key, val) { | ||
const update = { [key]: val } | ||
if (global) { | ||
rc.updateUser(update, '.nuxtrc') | ||
} else { | ||
rc.update(update, { name: '.nuxtrc', dir }) | ||
} | ||
} | ||
} | ||
|
||
export function run () { | ||
try { | ||
_run() | ||
} catch (err) { | ||
consola.fatal(err) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
if (!module.parent) { | ||
run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import consola from 'consola' | ||
import inquirer from 'inquirer' | ||
import c from 'chalk' | ||
import { updateUserNuxtRc } from './utils/nuxtrc' | ||
import { TelemetryOptions } from './types' | ||
import { usage, consentVersion } from './meta' | ||
|
||
export async function ensureUserConsent (options: TelemetryOptions): Promise<boolean> { | ||
if (options.consent >= consentVersion) { | ||
return true | ||
} | ||
|
||
if (options.consent === false || !process.stdout.isTTY) { | ||
return false | ||
} | ||
|
||
consola.info(`${c.green('NuxtJS')} collects completely anonymous data about usage. | ||
This will help us improving developer experience over the time. | ||
Read more: ${c.cyan.underline('https://git.io/nuxt-telemetry')}`) | ||
|
||
const manualInstructions = `by setting ${c.cyan('telemetry: true|false')} in ${c.cyan('nuxt.config')} or\n Using ${c.cyan(usage)} or\n Setting ${c.cyan('NUXT_TELEMETRY_DISABLED')} environment variable` | ||
|
||
const { accept } = await inquirer.prompt({ | ||
type: 'confirm', | ||
name: 'accept', | ||
message: 'Are you interested in participation?' | ||
}) | ||
process.stdout.write('\n') | ||
|
||
if (accept) { | ||
consola.success(`Thanks for participating!\n You can always change your mind ${manualInstructions}`) | ||
updateUserNuxtRc('telemetry.consent', consentVersion) | ||
return true | ||
} | ||
|
||
consola.success(`Telemetry disabled for you machine.\n You can always change your mind ${manualInstructions}`) | ||
updateUserNuxtRc('telemetry.consent', false) | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { name, version } from '../package.json' | ||
|
||
export const usage = 'nuxt telemetry enable|disable [-g,--global] [dir]' | ||
|
||
export const consentVersion = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { updateUser } from 'rc9' | ||
|
||
export function updateUserNuxtRc (key, val) { | ||
updateUser({ [key]: val }, '.nuxtrc') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.