-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(globals; utils): globals manages environment variables; places v…
…ersion range validation in utils
- Loading branch information
Showing
10 changed files
with
138 additions
and
54 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* eslint-disable @typescript-eslint/explicit-function-return-type */ | ||
import inVersionRange from '~/utils/version-range'; | ||
import read from 'read-pkg-up'; | ||
import cache from '~/utils/cache'; | ||
import errors from '~/utils/errors'; | ||
import { GLOBALS_KEY, TGlobal, TEnvironmental } from '~/constants'; | ||
import { IOfType } from '~/types'; | ||
|
||
const locals = { | ||
globals: {} as { [key in TGlobal]: any }, | ||
environmentals: {} as IOfType<string> | ||
}; | ||
|
||
export const pkg = cache( | ||
null, | ||
(): IOfType<any> => { | ||
try { | ||
return read.sync({ cwd: __dirname }).pkg; | ||
} catch (e) { | ||
throw new errors.CustomError(`Package couldn't be retrieved`, null, e); | ||
} | ||
} | ||
); | ||
|
||
/** | ||
* Used for state preservation for different kpo instances (installations) | ||
* on the same process. Will only pollute `global` if the process is kpo owned, | ||
* that is, if `process.env[OWNED_ENV_KEY]` is set. | ||
*/ | ||
export function globals<T>(key: TGlobal, initial: T) { | ||
const vars = ((global as any)[GLOBALS_KEY] || | ||
((global as any)[GLOBALS_KEY] = {})) as { [key in TGlobal]: any }; | ||
|
||
if (!vars.version) vars.version = pkg().version; | ||
else inVersionRange(pkg().version, vars.version); | ||
|
||
return { | ||
get(): T { | ||
return vars[key] || (vars[key] = initial); | ||
}, | ||
set(value: T): void { | ||
vars[key] = value; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Used for state preservation for different kpo instances AND children | ||
* processes. Will only pollute `process.env` if the process is kpo owned, | ||
* that is, if `process.env[OWNED_ENV_KEY]` is set. | ||
*/ | ||
export function environmentals(key: TEnvironmental) { | ||
const vars = process.env; | ||
|
||
return { | ||
get(): string | void { | ||
return vars[key] || undefined; | ||
}, | ||
set(value?: string): void { | ||
vars[key] = value || undefined; | ||
} | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
import inVersionRange from '~/utils/version-range'; | ||
|
||
describe(`all`, () => { | ||
test(`fails for undefined`, () => { | ||
expect(() => inVersionRange('1.0.1')).toThrowErrorMatchingInlineSnapshot( | ||
`"Version could not be parsed"` | ||
); | ||
expect(() => inVersionRange(undefined, '1.0.1')).toThrowError(); | ||
expect(() => inVersionRange()).toThrowError(); | ||
}); | ||
}); | ||
describe(`stable`, () => { | ||
test(`succeeds for equal`, () => { | ||
expect(() => inVersionRange('1.0.1', '1.0.1')).not.toThrow(); | ||
}); | ||
test(`succeeds for patch`, () => { | ||
expect(() => inVersionRange('1.0.1', '1.0.2')).not.toThrow(); | ||
}); | ||
test(`succeeds for minor`, () => { | ||
expect(() => inVersionRange('1.1.1', '1.2.1')).not.toThrow(); | ||
expect(() => inVersionRange('1.2.9', '1.1.1')).not.toThrow(); | ||
}); | ||
test(`fails for major`, () => { | ||
expect(() => | ||
inVersionRange('1.1.1', '2.1.1') | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Local kpo version (1.1.1) doesn't match executing version (2.1.1)"` | ||
); | ||
}); | ||
}); | ||
describe(`unstable`, () => { | ||
test(`succeedsfor equal`, () => { | ||
expect(() => inVersionRange('0.1.1', '0.1.1')).not.toThrow(); | ||
}); | ||
test(`fails for patch`, () => { | ||
expect(() => | ||
inVersionRange('0.1.1', '0.1.2') | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Local kpo version (0.1.1) doesn't match executing version (0.1.2)"` | ||
); | ||
}); | ||
test(`fails for minor`, () => { | ||
expect(() => | ||
inVersionRange('0.1.1', '0.2.1') | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Local kpo version (0.1.1) doesn't match executing version (0.2.1)"` | ||
); | ||
expect(() => | ||
inVersionRange('0.2.9', '0.1.1') | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Local kpo version (0.2.9) doesn't match executing version (0.1.1)"` | ||
); | ||
}); | ||
test(`fails for major`, () => { | ||
expect(() => | ||
inVersionRange('0.1.1', '1.1.1') | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Local kpo version (0.1.1) doesn't match executing version (1.1.1)"` | ||
); | ||
}); | ||
}); |