-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fdb218
commit 9183541
Showing
11 changed files
with
296 additions
and
277 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,188 +1,7 @@ | ||
'use strict'; | ||
const {spawn} = require('child_process'); | ||
const path = require('path'); | ||
const {format} = require('util'); | ||
const importLazy = require('import-lazy')(require); | ||
import UpdateNotifier from './update-notifier.js'; | ||
|
||
const configstore = importLazy('configstore'); | ||
const chalk = importLazy('chalk'); | ||
const semver = importLazy('semver'); | ||
const semverDiff = importLazy('semver-diff'); | ||
const latestVersion = importLazy('latest-version'); | ||
const isNpm = importLazy('is-npm'); | ||
const isInstalledGlobally = importLazy('is-installed-globally'); | ||
const isYarnGlobal = importLazy('is-yarn-global'); | ||
const hasYarn = importLazy('has-yarn'); | ||
const boxen = importLazy('boxen'); | ||
const xdgBasedir = importLazy('xdg-basedir'); | ||
const isCi = importLazy('is-ci'); | ||
const pupa = importLazy('pupa'); | ||
|
||
const ONE_DAY = 1000 * 60 * 60 * 24; | ||
|
||
class UpdateNotifier { | ||
constructor(options = {}) { | ||
this.options = options; | ||
options.pkg = options.pkg || {}; | ||
options.distTag = options.distTag || 'latest'; | ||
|
||
// Reduce pkg to the essential keys. with fallback to deprecated options | ||
// TODO: Remove deprecated options at some point far into the future | ||
options.pkg = { | ||
name: options.pkg.name || options.packageName, | ||
version: options.pkg.version || options.packageVersion | ||
}; | ||
|
||
if (!options.pkg.name || !options.pkg.version) { | ||
throw new Error('pkg.name and pkg.version required'); | ||
} | ||
|
||
this.packageName = options.pkg.name; | ||
this.packageVersion = options.pkg.version; | ||
this.updateCheckInterval = typeof options.updateCheckInterval === 'number' ? options.updateCheckInterval : ONE_DAY; | ||
this.disabled = 'NO_UPDATE_NOTIFIER' in process.env || | ||
process.env.NODE_ENV === 'test' || | ||
process.argv.includes('--no-update-notifier') || | ||
isCi(); | ||
this.shouldNotifyInNpmScript = options.shouldNotifyInNpmScript; | ||
|
||
if (!this.disabled) { | ||
try { | ||
const ConfigStore = configstore(); | ||
this.config = new ConfigStore(`update-notifier-${this.packageName}`, { | ||
optOut: false, | ||
// Init with the current time so the first check is only | ||
// after the set interval, so not to bother users right away | ||
lastUpdateCheck: Date.now() | ||
}); | ||
} catch { | ||
// Expecting error code EACCES or EPERM | ||
const message = | ||
chalk().yellow(format(' %s update check failed ', options.pkg.name)) + | ||
format('\n Try running with %s or get access ', chalk().cyan('sudo')) + | ||
'\n to the local update config store via \n' + | ||
chalk().cyan(format(' sudo chown -R $USER:$(id -gn $USER) %s ', xdgBasedir().config)); | ||
|
||
process.on('exit', () => { | ||
console.error(boxen()(message, {align: 'center'})); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
check() { | ||
if ( | ||
!this.config || | ||
this.config.get('optOut') || | ||
this.disabled | ||
) { | ||
return; | ||
} | ||
|
||
this.update = this.config.get('update'); | ||
|
||
if (this.update) { | ||
// Use the real latest version instead of the cached one | ||
this.update.current = this.packageVersion; | ||
|
||
// Clear cached information | ||
this.config.delete('update'); | ||
} | ||
|
||
// Only check for updates on a set interval | ||
if (Date.now() - this.config.get('lastUpdateCheck') < this.updateCheckInterval) { | ||
return; | ||
} | ||
|
||
// Spawn a detached process, passing the options as an environment property | ||
spawn(process.execPath, [path.join(__dirname, 'check.js'), JSON.stringify(this.options)], { | ||
detached: true, | ||
stdio: 'ignore' | ||
}).unref(); | ||
} | ||
|
||
async fetchInfo() { | ||
const {distTag} = this.options; | ||
const latest = await latestVersion()(this.packageName, {version: distTag}); | ||
|
||
return { | ||
latest, | ||
current: this.packageVersion, | ||
type: semverDiff()(this.packageVersion, latest) || distTag, | ||
name: this.packageName | ||
}; | ||
} | ||
|
||
notify(options) { | ||
const suppressForNpm = !this.shouldNotifyInNpmScript && isNpm().isNpmOrYarn; | ||
if (!process.stdout.isTTY || suppressForNpm || !this.update || !semver().gt(this.update.latest, this.update.current)) { | ||
return this; | ||
} | ||
|
||
options = { | ||
isGlobal: isInstalledGlobally(), | ||
isYarnGlobal: isYarnGlobal()(), | ||
...options | ||
}; | ||
|
||
let installCommand; | ||
if (options.isYarnGlobal) { | ||
installCommand = `yarn global add ${this.packageName}`; | ||
} else if (options.isGlobal) { | ||
installCommand = `npm i -g ${this.packageName}`; | ||
} else if (hasYarn()()) { | ||
installCommand = `yarn add ${this.packageName}`; | ||
} else { | ||
installCommand = `npm i ${this.packageName}`; | ||
} | ||
|
||
const defaultTemplate = 'Update available ' + | ||
chalk().dim('{currentVersion}') + | ||
chalk().reset(' → ') + | ||
chalk().green('{latestVersion}') + | ||
' \nRun ' + chalk().cyan('{updateCommand}') + ' to update'; | ||
|
||
const template = options.message || defaultTemplate; | ||
|
||
options.boxenOptions = options.boxenOptions || { | ||
padding: 1, | ||
margin: 1, | ||
align: 'center', | ||
borderColor: 'yellow', | ||
borderStyle: 'round' | ||
}; | ||
|
||
const message = boxen()( | ||
pupa()(template, { | ||
packageName: this.packageName, | ||
currentVersion: this.update.current, | ||
latestVersion: this.update.latest, | ||
updateCommand: installCommand | ||
}), | ||
options.boxenOptions | ||
); | ||
|
||
if (options.defer === false) { | ||
console.error(message); | ||
} else { | ||
process.on('exit', () => { | ||
console.error(message); | ||
}); | ||
|
||
process.on('SIGINT', () => { | ||
console.error(''); | ||
process.exit(); | ||
}); | ||
} | ||
|
||
return this; | ||
} | ||
} | ||
|
||
module.exports = options => { | ||
export default function updateNotifier(options) { | ||
const updateNotifier = new UpdateNotifier(options); | ||
updateNotifier.check(); | ||
return updateNotifier; | ||
}; | ||
|
||
module.exports.UpdateNotifier = UpdateNotifier; | ||
} |
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,9 @@ | ||
Copyright Google | ||
MIT License | ||
|
||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com) | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
Oops, something went wrong.