-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: strengthened ModuleLoader & unit tests; now supports mixed ESM …
…/ CJS plugins (#163) * significantly strengthened ModuleLoader / Oclif can now load mixed CJS / ESM plugins. * More complete ModuleLoader unit tests. * Added complete ESM, mixed (CJS w/ ESM), and mixed (ESM w/ CJS) runtime tests. * test description update * removed unnecessary yarn.lock files * removed to-do comments * removed commented out old code.
- Loading branch information
Showing
31 changed files
with
416 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as path from 'path' | ||
|
||
import {Config} from '../../src/config' | ||
|
||
import {expect, fancy} from './test' | ||
|
||
const root = path.resolve(__dirname, 'fixtures/esm') | ||
const p = (p: string) => path.join(root, p) | ||
|
||
const withConfig = fancy | ||
.add('config', () => Config.load(root)) | ||
|
||
describe('esm', () => { | ||
withConfig | ||
.it('has commandsDir', ({config}) => { | ||
expect(config.plugins[0]).to.deep.include({ | ||
commandsDir: p('src/commands'), | ||
}) | ||
}) | ||
|
||
withConfig | ||
.stdout() | ||
.it('runs esm command and prerun & postrun hooks', async ctx => { | ||
await ctx.config.runCommand('foo:bar:baz') | ||
expect(ctx.stdout).to.equal('running esm prerun hook\nit works!\nrunning esm postrun hook\n') | ||
}) | ||
|
||
withConfig | ||
.stdout() | ||
.it('runs faulty command, only prerun hook triggers', async ctx => { | ||
try { | ||
await ctx.config.runCommand('foo:bar:fail') | ||
} catch { | ||
console.log('caught error') | ||
} | ||
expect(ctx.stdout).to.equal('running esm prerun hook\nit fails!\ncaught error\n') | ||
}) | ||
|
||
withConfig | ||
.stdout() | ||
.it('runs esm command, postrun hook captures command result', async ctx => { | ||
await ctx.config.runCommand('foo:bar:test-result') | ||
expect(ctx.stdout).to.equal('running esm prerun hook\nit works!\nrunning esm postrun hook\nreturned success!\n') | ||
}) | ||
|
||
withConfig | ||
.stdout() | ||
.it('runs init hook', async ctx => { | ||
await (ctx.config.runHook as any)('init', {id: 'myid', argv: ['foo']}) | ||
expect(ctx.stdout).to.equal('running esm init hook\n') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "esm-plugin", | ||
"description": "Module type ESM; All ESM source", | ||
"private": true, | ||
"type": "module", | ||
"files": [], | ||
"oclif": { | ||
"commands": "./src/commands", | ||
"hooks": { | ||
"init": "./src/hooks/init", | ||
"prerun": [ | ||
"./src/hooks/prerun" | ||
], | ||
"postrun": [ | ||
"./src/hooks/postrun" | ||
] | ||
} | ||
}, | ||
"devDependencies": { | ||
"globby": "^8.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class Command { | ||
static run() { | ||
console.log('it works!') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class Command { | ||
static run() { | ||
console.log('it fails!') | ||
throw new Error('random error') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class Command { | ||
static run() { | ||
console.log('it works!') | ||
return 'returned success!' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function init() { | ||
console.log('running esm init hook') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default function postrun(options) { | ||
console.log('running esm postrun hook') | ||
if (options.Command.id === 'foo:bar:test-result') { | ||
console.log(options.result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function prerun() { | ||
console.log('running esm prerun hook') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "mixed-cjs-esm-plugin", | ||
"description": "CommonJS module, but mixed w/ ESM by .mjs extension", | ||
"private": true, | ||
"files": [], | ||
"oclif": { | ||
"commands": "./src/commands", | ||
"hooks": { | ||
"init": "./src/hooks/init", | ||
"prerun": [ | ||
"./src/hooks/prerun" | ||
], | ||
"postrun": [ | ||
"./src/hooks/postrun" | ||
] | ||
} | ||
}, | ||
"devDependencies": { | ||
"globby": "^8.0.1" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test/config/fixtures/mixed-cjs-esm/src/commands/foo/bar/baz.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class Command { | ||
static run() { | ||
console.log('it works!') | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
test/config/fixtures/mixed-cjs-esm/src/commands/foo/bar/fail.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Command { | ||
static run() { | ||
console.log('it fails!') | ||
throw new Error('random error') | ||
} | ||
} | ||
|
||
module.exports = Command |
8 changes: 8 additions & 0 deletions
8
test/config/fixtures/mixed-cjs-esm/src/commands/foo/bar/test-result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Command { | ||
static run() { | ||
console.log('it works!') | ||
return 'returned success!' | ||
} | ||
} | ||
|
||
module.exports = Command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function init() { | ||
console.log('running mixed-cjs-esm init hook') | ||
} | ||
|
||
module.exports = init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default function postrun(options) { | ||
console.log('running mixed-cjs-esm postrun hook') | ||
if (options.Command.id === 'foo:bar:test-result') { | ||
console.log(options.result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function prerun() { | ||
console.log('running mixed-cjs-esm prerun hook') | ||
} | ||
|
||
module.exports = prerun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "mixed-esm-cjs-plugin", | ||
"description": "Module type ESM, but mixed w/ CommonJS by .cjs extension", | ||
"private": true, | ||
"type": "module", | ||
"files": [], | ||
"oclif": { | ||
"commands": "./src/commands", | ||
"hooks": { | ||
"init": "./src/hooks/init", | ||
"prerun": [ | ||
"./src/hooks/prerun" | ||
], | ||
"postrun": [ | ||
"./src/hooks/postrun" | ||
] | ||
} | ||
}, | ||
"devDependencies": { | ||
"globby": "^8.0.1" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test/config/fixtures/mixed-esm-cjs/src/commands/foo/bar/baz.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class Command { | ||
static run() { | ||
console.log('it works!') | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
test/config/fixtures/mixed-esm-cjs/src/commands/foo/bar/fail.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Command { | ||
static run() { | ||
console.log('it fails!') | ||
throw new Error('random error') | ||
} | ||
} | ||
|
||
module.exports = Command |
7 changes: 7 additions & 0 deletions
7
test/config/fixtures/mixed-esm-cjs/src/commands/foo/bar/test-result.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default class Command { | ||
static run() { | ||
console.log('it works!') | ||
return 'returned success!' | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function init() { | ||
console.log('running mixed-esm-cjs init hook') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default function postrun(options) { | ||
console.log('running mixed-esm-cjs postrun hook') | ||
if (options.Command.id === 'foo:bar:test-result') { | ||
console.log(options.result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function prerun() { | ||
console.log('running mixed-esm-cjs prerun hook') | ||
} | ||
|
||
module.exports = prerun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as path from 'path' | ||
|
||
import {Config} from '../../src/config' | ||
|
||
import {expect, fancy} from './test' | ||
|
||
const root = path.resolve(__dirname, 'fixtures/mixed-cjs-esm') | ||
const p = (p: string) => path.join(root, p) | ||
|
||
const withConfig = fancy | ||
.add('config', () => Config.load(root)) | ||
|
||
describe('mixed-cjs-esm', () => { | ||
withConfig | ||
.it('has commandsDir', ({config}) => { | ||
expect(config.plugins[0]).to.deep.include({ | ||
commandsDir: p('src/commands'), | ||
}) | ||
}) | ||
|
||
withConfig | ||
.stdout() | ||
.it('runs mixed-cjs-esm command and prerun & postrun hooks', async ctx => { | ||
await ctx.config.runCommand('foo:bar:baz') | ||
expect(ctx.stdout).to.equal('running mixed-cjs-esm prerun hook\nit works!\nrunning mixed-cjs-esm postrun hook\n') | ||
}) | ||
|
||
withConfig | ||
.stdout() | ||
.it('runs faulty command, only prerun hook triggers', async ctx => { | ||
try { | ||
await ctx.config.runCommand('foo:bar:fail') | ||
} catch { | ||
console.log('caught error') | ||
} | ||
expect(ctx.stdout).to.equal('running mixed-cjs-esm prerun hook\nit fails!\ncaught error\n') | ||
}) | ||
|
||
withConfig | ||
.stdout() | ||
.it('runs mixed-cjs-esm command, postrun hook captures command result', async ctx => { | ||
await ctx.config.runCommand('foo:bar:test-result') | ||
expect(ctx.stdout).to.equal('running mixed-cjs-esm prerun hook\nit works!\nrunning mixed-cjs-esm postrun hook\nreturned success!\n') | ||
}) | ||
|
||
withConfig | ||
.stdout() | ||
.it('runs init hook', async ctx => { | ||
await (ctx.config.runHook as any)('init', {id: 'myid', argv: ['foo']}) | ||
expect(ctx.stdout).to.equal('running mixed-cjs-esm init hook\n') | ||
}) | ||
}) |
Oops, something went wrong.