env.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getAsBooleanFromENV = getAsBooleanFromENV;
  6. exports.getFromENV = getFromENV;
  7. exports.getPackageManager = getPackageManager;
  8. exports.getPackageManagerExecCommand = getPackageManagerExecCommand;
  9. /**
  10. * Copyright (c) Microsoft Corporation.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. function getFromENV(name) {
  25. let value = process.env[name];
  26. value = value === undefined ? process.env[`npm_config_${name.toLowerCase()}`] : value;
  27. value = value === undefined ? process.env[`npm_package_config_${name.toLowerCase()}`] : value;
  28. return value;
  29. }
  30. function getAsBooleanFromENV(name) {
  31. const value = getFromENV(name);
  32. return !!value && value !== 'false' && value !== '0';
  33. }
  34. function getPackageManager() {
  35. const env = process.env.npm_config_user_agent || '';
  36. if (env.includes('yarn')) return 'yarn';
  37. if (env.includes('pnpm')) return 'pnpm';
  38. return 'npm';
  39. }
  40. function getPackageManagerExecCommand() {
  41. const packageManager = getPackageManager();
  42. if (packageManager === 'yarn') return 'yarn';
  43. if (packageManager === 'pnpm') return 'pnpm exec';
  44. return 'npx';
  45. }