Skip to content

Commit

Permalink
CLI: Add flag to disable version checks (#8488)
Browse files Browse the repository at this point in the history
CLI: Add flag to disable version checks
  • Loading branch information
shilman authored Oct 24, 2019
2 parents 4dad6b3 + d580f30 commit d53b13d
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 206 deletions.
82 changes: 42 additions & 40 deletions lib/api/src/modules/versions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fetch } from 'global';
import { VERSIONCHECK } from 'global';
import semver from 'semver';
import { logger } from '@storybook/client-logger';
import memoize from 'memoizerific';

import { version as currentVersion } from '../version';

Expand Down Expand Up @@ -30,13 +30,15 @@ export interface SubState {
dismissedVersionNotification: undefined | string;
}

const checkInterval = 24 * 60 * 60 * 1000;
const versionsUrl = 'https://storybook.js.org/versions.json';

async function fetchLatestVersion(v: string) {
const fromFetch = await fetch(`${versionsUrl}?current=${v}`);
return fromFetch.json();
}
const getVersionCheckData = memoize(1)(
(): Versions => {
try {
return JSON.parse(VERSIONCHECK).data as Versions;
} catch (e) {
return {};
}
}
);

export interface SubAPI {
getCurrentVersion: () => Version;
Expand All @@ -45,25 +47,15 @@ export interface SubAPI {
}

export default function({ store, mode }: Module) {
const {
versions: persistedVersions = {},
lastVersionCheck = 0,
dismissedVersionNotification,
} = store.getState();
const { dismissedVersionNotification } = store.getState();

// Check to see if we have info about the current version persisted
const persistedCurrentVersion = Object.values(persistedVersions).find(
v => v.version === currentVersion
);
const state = {
versions: {
...persistedVersions,
current: {
version: currentVersion,
...(persistedCurrentVersion && { info: persistedCurrentVersion.info }),
},
...getVersionCheckData(),
},
lastVersionCheck,
dismissedVersionNotification,
};

Expand All @@ -87,39 +79,49 @@ export default function({ store, mode }: Module) {
const latest = api.getLatestVersion();
const current = api.getCurrentVersion();

if (!latest || !latest.version) {
return true;
if (latest) {
if (!latest.version) {
return true;
}
if (!current.version) {
return true;
}

const onPrerelease = !!semver.prerelease(current.version);

const actualCurrent = onPrerelease
? `${semver.major(current.version)}.${semver.minor(current.version)}.${semver.patch(
current.version
)}`
: current.version;

const diff = semver.diff(actualCurrent, latest.version);

return (
semver.gt(latest.version, actualCurrent) && diff !== 'patch' && !diff.includes('pre')
);
}
return latest && semver.gt(latest.version, current.version);
return false;
},
};

// Grab versions from the server/local storage right away
async function init({ api: fullApi }: API) {
const { versions = {} } = store.getState();

const now = Date.now();
if (!lastVersionCheck || now - lastVersionCheck > checkInterval) {
try {
const { latest, next } = await fetchLatestVersion(currentVersion);
await store.setState(
{
versions: { ...versions, latest, next },
lastVersionCheck: now,
},
{ persistence: 'permanent' }
);
} catch (error) {
logger.warn(`Failed to fetch latest version from server: ${error}`);
}
}
const { latest, next } = getVersionCheckData();
await store.setState({
versions: { ...versions, latest, next },
});

if (api.versionUpdateAvailable()) {
const latestVersion = api.getLatestVersion().version;

const diff = semver.diff(versions.current.version, versions.latest.version);

if (
latestVersion !== dismissedVersionNotification &&
!semver.patch(latestVersion) &&
diff !== 'patch' &&
!semver.prerelease(latestVersion) &&
mode !== 'production'
) {
Expand Down
Loading

1 comment on commit d53b13d

@vercel
Copy link

@vercel vercel bot commented on d53b13d Oct 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.