Skip to content

Commit

Permalink
feat(globals): adds globals
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 6, 2019
1 parent 05fce07 commit b1e8f44
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 12 deletions.
58 changes: 46 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@types/object-hash": "^1.2.0",
"@types/pify": "^3.0.2",
"@types/prompts": "^2.4.0",
"@types/read-pkg-up": "^3.0.1",
"@types/semver": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^1.7.0",
"@typescript-eslint/parser": "^1.7.0",
Expand Down Expand Up @@ -131,6 +132,7 @@
"pify": "^4.0.1",
"promist": "^0.5.3",
"prompts": "^2.0.4",
"read-pkg-up": "^5.0.0",
"semver": "^6.0.0",
"slimconf": "^0.9.0",
"string-argv": "^0.3.0"
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ export const CONCURRENTLY_PATH = require.resolve(
);
// Environment variable name for core state storage
export const KPO_ENV_STATE = 'KPO_STATE';
/* Shared between instances: changes might imply a major version release */
export const LOG_ENV_KEY = 'kpo_log';
export const GLOBALS_KEY = 'kpo_globals';
export type TGlobal = 'version' | 'core' | 'options' | 'processses';
4 changes: 4 additions & 0 deletions src/globals/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { GLOBALS_KEY, TGlobal } from '~/constants';

export default ((global as any)[GLOBALS_KEY] ||
((global as any)[GLOBALS_KEY] = {})) as { [key in TGlobal]: any };
17 changes: 17 additions & 0 deletions src/globals/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import inVersionRange from './version-range';
import _ from './globals';
import { TGlobal } from '~/constants';

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default function globals<T>(key: TGlobal, initial: T) {
inVersionRange();

return {
get(): T {
return _[key] || (_[key] = initial);
},
set(value: T): void {
_[key] = value;
}
};
}
15 changes: 15 additions & 0 deletions src/globals/pkg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import read from 'read-pkg-up';
import cache from '~/utils/cache';
import { IOfType } from '~/types';
import errors from '~/utils/errors';

export default cache(
null,
(): IOfType<any> => {
try {
return read.sync({ cwd: __dirname }).pkg;
} catch (e) {
throw new errors.CustomError(`Package couldn't be retrieved`, null, e);
}
}
);
31 changes: 31 additions & 0 deletions src/globals/version-range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { clean, diff } from 'semver';
import pkg from './pkg';
import globals from './globals';

export default function inVersionRange(): void {
if (!globals.version) {
globals.version = clean(pkg().version);
return;
}

const executable = globals.version && clean(globals.version);
const local = pkg().version && clean(pkg().version);

if (!executable || !local) throw Error(`Version could not be parsed`);

const release = diff(executable, local);

// `false` if difference is a major version or we're on v0.x.x
if (
release !== 'major' &&
release !== 'premajor' &&
(!release || (local as string)[0] !== '0')
) {
return;
}

throw Error(
`Local kpo version (${local})` +
` doesn't match executing version (${executable})`
);
}

0 comments on commit b1e8f44

Please sign in to comment.