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