Skip to content

Commit

Permalink
Merge pull request #365 from oclif/mdonnalley/esm-friendly
Browse files Browse the repository at this point in the history
feat: remove deprecated module.parent
  • Loading branch information
mdonnalley committed Jul 26, 2023
2 parents dc0eb84 + ca06eef commit d43c626
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path'

import {Config} from '@oclif/core'
import {expect, fancy, FancyTypes} from 'fancy-test'

Expand All @@ -6,7 +8,18 @@ import exit from './exit'
import hook from './hook'
import {loadConfig} from './load-config'

loadConfig.root = module.parent!.filename
function traverseFilePathUntil(filename: string, predicate: (filename: string) => boolean): string {
let current = filename
while (!predicate(current)) {
current = path.dirname(current)
}

return current
}

// Update to path.dirname(url.fileURLToPath(import.meta.url)) whenever we update tsconfig target to ES2020
// eslint-disable-next-line unicorn/prefer-module
loadConfig.root = traverseFilePathUntil(require.main?.path ?? module.path, p => !p.includes('node_modules'))

export const test = fancy
.register('loadConfig', loadConfig)
Expand Down
15 changes: 9 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// tslint:disable no-console

import * as OS from 'os'

import {expect, test} from '../src'

describe('stdout', () => {
Expand Down Expand Up @@ -34,12 +30,19 @@ describe('stdout + stderr', () => {
})
})

// eslint-disable-next-line unicorn/no-static-only-class
class MockOs {
static platform() {
return 'not-a-platform'
}
}

for (const os of ['darwin', 'win32', 'linux']) {
describe(os, () => {
test
.stub(OS, 'platform', () => os)
.stub(MockOs, 'platform', () => os)
.end('sets os', () => {
expect(OS.platform()).to.equal(os)
expect(MockOs.platform()).to.equal(os)
})
})
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"./src"
],
"strict": true,
"target": "es2017"
"target": "es2017",
"esModuleInterop": true
},
"include": [
"./src/**/*"
Expand Down

0 comments on commit d43c626

Please sign in to comment.