react-update-notification
Advanced tools
Comparing version 0.1.0-alpha.1 to 0.1.0-alpha.2
@@ -9,5 +9,16 @@ #!/usr/bin/env node | ||
const version = process.env.npm_package_version; | ||
function getVersion(strategy) { | ||
if (strategy === 'latest-commit') { | ||
return require('child_process') | ||
.execSync('git rev-parse --short HEAD') | ||
.toString() | ||
.trim(); | ||
} | ||
return process.env.npm_package_version; | ||
} | ||
function generate(argv) { | ||
const appVersion = getVersion(argv.s); | ||
const versionPath = path.resolve(process.cwd(), argv.v); | ||
@@ -17,3 +28,3 @@ const indexPath = path.resolve(process.cwd(), argv.i); | ||
try { | ||
fs.writeFileSync(versionPath, JSON.stringify({ version })); | ||
fs.writeFileSync(versionPath, JSON.stringify({ version: appVersion })); | ||
} catch (e) { | ||
@@ -47,3 +58,3 @@ if (e.code === 'ENOENT') { | ||
loadedIndex('head').append( | ||
`<script>window.__APP_VERSION__ = "${version}"; window.__APP_VERSION_FILE__ = "${argv.v}"</script>` | ||
`<script>window.__APP_VERSION__ = "${appVersion}"; window.__APP_VERSION_FILE__ = "${argv.v}"</script>` | ||
); | ||
@@ -55,2 +66,9 @@ fs.writeFileSync(indexPath, loadedIndex.html()); | ||
.usage('Usage: $0 [options]') | ||
.option('strategy', { | ||
alias: 's', | ||
description: 'What source to use to generate version', | ||
choices: ['latest-commit', 'package'], | ||
nargs: 1, | ||
default: 'package' | ||
}) | ||
.option('indexFile', { | ||
@@ -57,0 +75,0 @@ alias: 'i', |
@@ -6,4 +6,8 @@ declare global { | ||
} | ||
interface UpdateHookParams { | ||
type: 'mount' | 'interval'; | ||
interval?: number; | ||
} | ||
declare type Status = 'checking' | 'current' | 'available'; | ||
export declare const useUpdateCheck: () => { | ||
export declare const useUpdateCheck: (params: UpdateHookParams) => { | ||
status: Status; | ||
@@ -10,0 +14,0 @@ reloadPage: () => void; |
import { useState, useEffect } from 'react'; | ||
var reloadPage = function () { return window.location.reload(true); }; | ||
var currentVersion = window.__APP_VERSION__; | ||
export var useUpdateCheck = function () { | ||
export var useUpdateCheck = function (params) { | ||
var _a = useState('checking'), status = _a[0], setStatus = _a[1]; | ||
useEffect(function () { | ||
var checkUpdate = function () { | ||
if (typeof currentVersion === 'undefined') { | ||
@@ -11,2 +11,3 @@ setStatus('current'); | ||
} | ||
setStatus('checking'); | ||
fetch('/version.json') | ||
@@ -21,5 +22,19 @@ .then(function (res) { return res.json(); }) | ||
} | ||
}) | ||
.catch(function (err) { | ||
//TODO Define behavior when version file is broken / does not exist | ||
}); | ||
}; | ||
useEffect(function () { | ||
checkUpdate(); | ||
}, []); | ||
useEffect(function () { | ||
if (params.type === 'interval') { | ||
var interval_1 = setInterval(function () { return checkUpdate(); }, params.interval || 10000); | ||
return function () { | ||
clearInterval(interval_1); | ||
}; | ||
} | ||
}, [params.type, params.interval]); | ||
return { status: status, reloadPage: reloadPage }; | ||
}; |
@@ -6,4 +6,8 @@ declare global { | ||
} | ||
interface UpdateHookParams { | ||
type: 'mount' | 'interval'; | ||
interval?: number; | ||
} | ||
declare type Status = 'checking' | 'current' | 'available'; | ||
export declare const useUpdateCheck: () => { | ||
export declare const useUpdateCheck: (params: UpdateHookParams) => { | ||
status: Status; | ||
@@ -10,0 +14,0 @@ reloadPage: () => void; |
@@ -6,5 +6,5 @@ "use strict"; | ||
var currentVersion = window.__APP_VERSION__; | ||
exports.useUpdateCheck = function () { | ||
exports.useUpdateCheck = function (params) { | ||
var _a = react_1.useState('checking'), status = _a[0], setStatus = _a[1]; | ||
react_1.useEffect(function () { | ||
var checkUpdate = function () { | ||
if (typeof currentVersion === 'undefined') { | ||
@@ -14,2 +14,3 @@ setStatus('current'); | ||
} | ||
setStatus('checking'); | ||
fetch('/version.json') | ||
@@ -24,5 +25,19 @@ .then(function (res) { return res.json(); }) | ||
} | ||
}) | ||
.catch(function (err) { | ||
//TODO Define behavior when version file is broken / does not exist | ||
}); | ||
}; | ||
react_1.useEffect(function () { | ||
checkUpdate(); | ||
}, []); | ||
react_1.useEffect(function () { | ||
if (params.type === 'interval') { | ||
var interval_1 = setInterval(function () { return checkUpdate(); }, params.interval || 10000); | ||
return function () { | ||
clearInterval(interval_1); | ||
}; | ||
} | ||
}, [params.type, params.interval]); | ||
return { status: status, reloadPage: reloadPage }; | ||
}; |
{ | ||
"name": "react-update-notification", | ||
"version": "0.1.0-alpha.1", | ||
"version": "0.1.0-alpha.2", | ||
"description": "New version notification for your React application.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -31,3 +31,3 @@ # react-update-notification | ||
"scripts": { | ||
"build": "react-scripts build && generate-version" | ||
"build": "react-scripts build && generate-version -s package" | ||
} | ||
@@ -37,2 +37,7 @@ } | ||
`generate-version` accepts several types of strategies to differentiate versions and trigger the update notification: | ||
- `latest-commit` uses current commit hash. | ||
- `package` uses package.json `version` field. | ||
<!-- `generate-version` accepts custom paths to `index.html` and to target `version.json` like this: | ||
@@ -51,3 +56,6 @@ | ||
const NotificationContainer = () => { | ||
const { status, reloadPage } = useUpdateCheck(); | ||
const { status, reloadPage } = useUpdateCheck({ | ||
type: 'interval', | ||
interval: 10000 | ||
}); | ||
@@ -70,4 +78,6 @@ if (status === 'checking' || status === 'current') { | ||
- [ ] Define behavior when version file is broken / does not exist | ||
- [x] Custom strategies for version generation (package.json, commit id) | ||
- [ ] Custom paths to `generate-version` | ||
- [ ] Hook options (checking update using interval) | ||
- [x] Hook options (checking update using interval) | ||
- [ ] Proper documentation, tests and build |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8521
184
80
3