-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd:update) added the update command
- Loading branch information
Showing
11 changed files
with
168 additions
and
12 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
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,26 @@ | ||
/* | ||
* File: version.ts | ||
* Created: 10/11/2020 13:03:39 | ||
* ---- | ||
* Copyright: 2020 Nix² Technologies | ||
* Author: Max Koon (maxk@nix2.io) | ||
*/ | ||
|
||
import * as ora from 'ora'; | ||
|
||
import { CommanderStatic } from 'commander'; | ||
import { PACKAGE_NAME } from '../constants'; | ||
import { asyncExec } from '../util'; | ||
|
||
export default (program: CommanderStatic): void => { | ||
program | ||
.command('update') | ||
.description('update the cli') | ||
.action(async () => { | ||
const spinner = ora('Updating CLI').start(); | ||
await asyncExec(`yarn add global ${PACKAGE_NAME}`).catch((err) => { | ||
spinner.fail(err.message); | ||
}); | ||
spinner.succeed('Updated the CLI'); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as semver from 'semver'; | ||
import * as colors from 'colors'; | ||
|
||
import { | ||
PACKAGE_URL, | ||
REFRESH_VERSION_INTERVAL, | ||
SYMBOLS, | ||
VERSION, | ||
} from './constants'; | ||
|
||
import Axios from 'axios'; | ||
import cache from './cache'; | ||
|
||
interface VersionCacheType { | ||
refreshed: number; | ||
version: string; | ||
} | ||
|
||
const updateCachedVersion = async (): Promise<void> => { | ||
console.log('checking for updates'); | ||
await Axios.get(PACKAGE_URL).then((resp) => { | ||
const version = <string>resp.data.version; | ||
cache.set('version', { refreshed: new Date().getTime(), version }); | ||
}); | ||
return; | ||
}; | ||
|
||
export const checkForUpdates = (): void => { | ||
const versionCacheInfo: VersionCacheType = <VersionCacheType>( | ||
(<unknown>cache.get('version', { refreshed: -1, version: VERSION })) | ||
); | ||
if ( | ||
new Date().getTime() - versionCacheInfo.refreshed >= | ||
REFRESH_VERSION_INTERVAL | ||
) | ||
updateCachedVersion(); | ||
|
||
if (semver.lte(versionCacheInfo.version, VERSION)) return; | ||
console.warn( | ||
colors.yellow( | ||
`\n${ | ||
SYMBOLS.WARNING | ||
} You are using an out of date CLI: ${colors.cyan( | ||
VERSION, | ||
)} ${colors.white('→')} ${colors.cyan( | ||
versionCacheInfo.version, | ||
)}\n https://github.com/nix2io/cli/releases/tag/v${ | ||
versionCacheInfo.version | ||
}\n`, | ||
), | ||
); | ||
}; |
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