runtime-versions.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import checkNodeVersion from 'check-node-version';
  2. type RuntimeVersions = {
  3. node: string | undefined;
  4. npm: string | undefined;
  5. pnpm: string | undefined;
  6. };
  7. // define original types because the object returned is not according to the official type definition
  8. type SatisfiedVersionInfo = {
  9. isSatisfied: true;
  10. version: {
  11. version: string;
  12. };
  13. };
  14. type NotfoundVersionInfo = {
  15. isSatisfied: true;
  16. notfound: true;
  17. };
  18. type VersionInfo = SatisfiedVersionInfo | NotfoundVersionInfo;
  19. function isNotfoundVersionInfo(info: VersionInfo): info is NotfoundVersionInfo {
  20. return 'notfound' in info;
  21. }
  22. function isSatisfiedVersionInfo(
  23. info: VersionInfo,
  24. ): info is SatisfiedVersionInfo {
  25. return 'version' in info;
  26. }
  27. const getVersion = (versionInfo: VersionInfo): string | undefined => {
  28. if (isNotfoundVersionInfo(versionInfo)) {
  29. return undefined;
  30. }
  31. if (isSatisfiedVersionInfo(versionInfo)) {
  32. return versionInfo.version.version;
  33. }
  34. return undefined;
  35. };
  36. export function getRuntimeVersions(): Promise<RuntimeVersions> {
  37. return new Promise((resolve, reject) => {
  38. checkNodeVersion({}, (error, result) => {
  39. if (error) {
  40. reject(error);
  41. return;
  42. }
  43. resolve({
  44. node: getVersion(result.versions.node as unknown as VersionInfo),
  45. npm: getVersion(result.versions.npm as unknown as VersionInfo),
  46. pnpm: getVersion(result.versions.pnpm as unknown as VersionInfo),
  47. });
  48. });
  49. });
  50. }