esm.mjs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict'
  2. import { notStrictEqual, strictEqual } from 'assert'
  3. import cliui from 'cliui'
  4. import escalade from 'escalade/sync'
  5. import { inspect } from 'util'
  6. import { readFileSync } from 'fs'
  7. import { fileURLToPath } from 'url';
  8. import Parser from 'yargs-parser'
  9. import { basename, dirname, extname, relative, resolve } from 'path'
  10. import { getProcessArgvBin } from '../../build/lib/utils/process-argv.js'
  11. import { YError } from '../../build/lib/yerror.js'
  12. import y18n from 'y18n'
  13. const REQUIRE_ERROR = 'require is not supported by ESM'
  14. const REQUIRE_DIRECTORY_ERROR = 'loading a directory of commands is not supported yet for ESM'
  15. let __dirname;
  16. try {
  17. __dirname = fileURLToPath(import.meta.url);
  18. } catch (e) {
  19. __dirname = process.cwd();
  20. }
  21. const mainFilename = __dirname.substring(0, __dirname.lastIndexOf('node_modules'));
  22. export default {
  23. assert: {
  24. notStrictEqual,
  25. strictEqual
  26. },
  27. cliui,
  28. findUp: escalade,
  29. getEnv: (key) => {
  30. return process.env[key]
  31. },
  32. inspect,
  33. getCallerFile: () => {
  34. throw new YError(REQUIRE_DIRECTORY_ERROR)
  35. },
  36. getProcessArgvBin,
  37. mainFilename: mainFilename || process.cwd(),
  38. Parser,
  39. path: {
  40. basename,
  41. dirname,
  42. extname,
  43. relative,
  44. resolve
  45. },
  46. process: {
  47. argv: () => process.argv,
  48. cwd: process.cwd,
  49. emitWarning: (warning, type) => process.emitWarning(warning, type),
  50. execPath: () => process.execPath,
  51. exit: process.exit,
  52. nextTick: process.nextTick,
  53. stdColumns: typeof process.stdout.columns !== 'undefined' ? process.stdout.columns : null
  54. },
  55. readFileSync,
  56. require: () => {
  57. throw new YError(REQUIRE_ERROR)
  58. },
  59. requireDirectory: () => {
  60. throw new YError(REQUIRE_DIRECTORY_ERROR)
  61. },
  62. stringWidth: (str) => {
  63. return [...str].length
  64. },
  65. y18n: y18n({
  66. directory: resolve(__dirname, '../../../locales'),
  67. updateFiles: false
  68. })
  69. }