@@ -7,31 +7,64 @@ const latestVersion = require('latest-version')
77const semver = require ( 'semver' )
88
99const hook : Hook < 'init' > = async function ( opts ) {
10- // Look for updates
11- try {
12- const latest = await latestVersion ( opts . config . name , { version : `>${ opts . config . version } ` } )
13- if ( semver . gt ( latest , opts . config . version ) ) {
14- this . log ( 'Update available ' )
15- this . log ( ' Run ' )
16- this . log ( `npm i -g ${ opts . config . name } ` )
17- }
18- /* tslint:disable:no-unused */
19- } catch ( err ) {
20- // swallow the exception; we don't want to crash the app
21- // on a failed attempt to check version
10+ // get config settings
11+ let userConfig : any
12+ const curDateTime = new Date ( )
13+ const configFileExists = fs . existsSync ( path . join ( this . config . configDir , 'config.json' ) )
14+
15+ const writeUserConfig = async ( userconfig : any ) => {
16+ await fs . mkdirp ( this . config . configDir )
17+ await fs . writeFile ( path . join ( this . config . configDir , 'config.json' ) , JSON . stringify ( userconfig , null , 2 ) )
2218 }
2319
24- // Ensure telemetry is set
2520 try {
26- let userConfig : any = ''
27- if ( fs . existsSync ( path . join ( this . config . configDir , 'config.json' ) ) ) {
21+ // if config file exists, load settings
22+ if ( configFileExists ) {
2823 userConfig = await fs . readJSON ( path . join ( this . config . configDir , 'config.json' ) )
2924 } else {
25+ // otherwise create in-memory config
3026 userConfig = {
3127 telemetry : null ,
28+ lastVersionCheck : null
29+ }
30+ }
31+
32+ const checkForUpdate = async ( ) => {
33+ const latest = await latestVersion ( opts . config . name , { version : `>${ opts . config . version } ` } )
34+ if ( semver . gt ( latest , opts . config . version ) ) {
35+ this . log ( 'Update available ' )
36+ this . log ( ' Run ' )
37+ this . log ( `npm i -g ${ opts . config . name } ` )
3238 }
3339 }
3440
41+ const updateUserConfig = async ( curVersionCheck : Date ) => {
42+ userConfig . lastVersionCheck = curVersionCheck
43+ await writeUserConfig ( userConfig )
44+ }
45+
46+ const isToday = ( dateObj : Date | null , today : Date ) => {
47+ return dateObj && dateObj . getDate ( ) === today . getDate ( ) &&
48+ dateObj . getMonth ( ) === today . getMonth ( ) &&
49+ dateObj . getFullYear ( ) === today . getFullYear ( )
50+ }
51+
52+ // if there's no timestamp in config, create one and check for updates
53+ // if there is a timestamp in config and it's not from today, check for updates
54+ const lastCheck = userConfig . lastVersionCheck ? new Date ( userConfig . lastVersionCheck ) : null
55+ if ( ! isToday ( lastCheck , curDateTime ) ) {
56+ await checkForUpdate ( )
57+ await updateUserConfig ( curDateTime )
58+ }
59+
60+ /* tslint:disable:no-unused */
61+ } catch ( err ) {
62+ // swallow the exception; we don't want to crash the app
63+ // on a failed attempt to check version
64+ }
65+
66+ // Ensure telemetry is set
67+ try {
3568 if ( userConfig . telemetry === null ) {
3669 const disableTelemetry = await cli . prompt ( chalk . red ( 'Telemetry is disabled. Would you like to opt in?. Only command and flags usage will be sent. (Y/N)' ) )
3770 if ( disableTelemetry === 'Y' || disableTelemetry === 'y' ) {
@@ -48,9 +81,7 @@ const hook: Hook<'init'> = async function (opts) {
4881 this . log ( chalk . blue ( 'bf config:telemetry:enable' ) )
4982 }
5083
51- await fs . mkdirp ( this . config . configDir )
52-
53- await fs . writeFileSync ( path . join ( this . config . configDir , 'config.json' ) , JSON . stringify ( userConfig , null , 2 ) )
84+ await writeUserConfig ( userConfig )
5485 }
5586
5687 this . config . pjson . telemetry = userConfig . telemetry
0 commit comments