-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: slurps commands and events both bust node cache
- Loading branch information
AutoSponge
committed
Mar 31, 2020
1 parent
49b3008
commit e6b0350
Showing
12 changed files
with
205 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const test = require('ava'); | ||
const Scriptwriter = require('../scriptwriter'); | ||
|
||
test.serial('commands must bust cache on .clear', async (t) => { | ||
const scriptwriter = new Scriptwriter(); | ||
t.is(scriptwriter.replServer, null); | ||
let allReady = waitFor(scriptwriter, 'ready', ['client']); | ||
await scriptwriter.init(); | ||
await allReady; | ||
const replServer = scriptwriter.company.director; | ||
const playbill = replServer.commands.playbill.action; | ||
allReady = waitFor(scriptwriter, 'ready', ['client']); | ||
replServer.commands.clear.action.call(replServer); | ||
await allReady; | ||
t.isNot(playbill, replServer.commands.playbill.action); | ||
scriptwriter.company.director.close(); | ||
}); |
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,9 @@ | ||
exports.name = 'clearScreen'; | ||
exports.command = { | ||
help: 'Clears the screen', | ||
action() { | ||
const { scriptwriter, director } = this.context; | ||
scriptwriter.log(scriptwriter.escapes.clearScreen); | ||
director.displayPrompt(); | ||
}, | ||
}; |
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,26 @@ | ||
const test = require('ava'); | ||
const clearScreen = require('./clear-screen'); | ||
const Scriptwriter = require('../scriptwriter'); | ||
|
||
test.serial('uses the correct interface', (t) => { | ||
t.truthy(clearScreen.name); | ||
t.truthy(clearScreen.command.help); | ||
t.truthy(clearScreen.command.action); | ||
}); | ||
|
||
test.serial('action', (t) => { | ||
const scriptwriter = new Scriptwriter(); | ||
let displayPromptCalled = false; | ||
const mockReplServer = { | ||
context: { | ||
scriptwriter, | ||
director: { | ||
displayPrompt() { | ||
displayPromptCalled = true; | ||
}, | ||
}, | ||
}, | ||
}; | ||
clearScreen.command.action.call(mockReplServer); | ||
t.true(displayPromptCalled); | ||
}); |
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,9 @@ | ||
exports.name = 'clearTerminal'; | ||
exports.command = { | ||
help: 'Clears the screen and scroll buffer', | ||
action() { | ||
const { scriptwriter, director } = this.context; | ||
scriptwriter.log(scriptwriter.escapes.clearTerminal); | ||
director.displayPrompt(); | ||
}, | ||
}; |
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,26 @@ | ||
const test = require('ava'); | ||
const clearTerminal = require('./clear-terminal'); | ||
const Scriptwriter = require('../scriptwriter'); | ||
|
||
test.serial('uses the correct interface', (t) => { | ||
t.truthy(clearTerminal.name); | ||
t.truthy(clearTerminal.command.help); | ||
t.truthy(clearTerminal.command.action); | ||
}); | ||
|
||
test.serial('action', (t) => { | ||
const scriptwriter = new Scriptwriter(); | ||
let displayPromptCalled = false; | ||
const mockReplServer = { | ||
context: { | ||
scriptwriter, | ||
director: { | ||
displayPrompt() { | ||
displayPromptCalled = true; | ||
}, | ||
}, | ||
}, | ||
}; | ||
clearTerminal.command.action.call(mockReplServer); | ||
t.true(displayPromptCalled); | ||
}); |
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,65 @@ | ||
const link = require('terminal-link'); | ||
const PLAYBILL = 'playbill'; | ||
const command = { | ||
help: `List the scriptwriter's ${PLAYBILL}`, | ||
action() { | ||
const { scriptwriter, director } = this.context; | ||
const credits = Object.keys(scriptwriter.company).sort(); | ||
const maxLength = Math.max(...credits.map((c) => c.length)) + 2; | ||
scriptwriter.log( | ||
credits | ||
.map((c) => | ||
[ | ||
getLink(c), | ||
' '.repeat(maxLength - c.length), | ||
getDescription(c), | ||
].join('') | ||
) | ||
.join('\n') | ||
); | ||
director.displayPrompt(); | ||
}, | ||
}; | ||
exports.name = PLAYBILL; | ||
exports.command = command; | ||
|
||
function getLink(name) { | ||
const PLAYWRIGHT_API = | ||
'https://github.com/microsoft/playwright/blob/v0.12.1/docs/api.md'; | ||
switch (name) { | ||
case 'page': | ||
case 'browser': | ||
return link(name, `${PLAYWRIGHT_API}#class-${name}`); | ||
case 'client': | ||
return link(name, 'https://chromedevtools.github.io/devtools-protocol/'); | ||
case 'context': | ||
return link(name, `${PLAYWRIGHT_API}#class-browser${name}`); | ||
case 'director': | ||
return link(name, 'https://nodejs.org/api/repl.html'); | ||
case 'playwright': | ||
return link(name, PLAYWRIGHT_API); | ||
case 'scriptwriter': | ||
return link(name, 'https://github.com/AutoSponge/scriptwriter#readme'); | ||
default: | ||
} | ||
} | ||
|
||
function getDescription(name) { | ||
switch (name) { | ||
case 'page': | ||
return `Playwright's Page instance for the browser's context.`; | ||
case 'browser': | ||
return `Playwright's Browser instance.`; | ||
case 'client': | ||
return `Playwritght's CDPSession instance (Chrome only).`; | ||
case 'context': | ||
return `Playwright's BrowserContext instance for the browser.`; | ||
case 'director': | ||
return `Node's repl instance.`; | ||
case 'playwright': | ||
return `Playwright object.`; | ||
case 'scriptwriter': | ||
return `The instance object that controls the director.`; | ||
default: | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,11 @@ | ||
module.exports = async function (scriptwriter) { | ||
const { context } = scriptwriter.company; | ||
try { | ||
const page = await context.newPage(); | ||
scriptwriter.assign({ page }); | ||
if (context.newCDPSession) { | ||
const client = await context.newCDPSession(page); | ||
scriptwriter.assign({ client }); | ||
} | ||
} catch (err) { | ||
scriptwriter.log(err); | ||
const page = await context.newPage(); | ||
await scriptwriter.assign({ page }); | ||
/* istanbul ignore next */ | ||
if (context.newCDPSession) { | ||
const client = await context.newCDPSession(page); | ||
await scriptwriter.assign({ client }); | ||
} | ||
scriptwriter.emit('ready', 'context'); | ||
}; |
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 |
---|---|---|
@@ -1,78 +1,9 @@ | ||
const dlv = require('dlv'); | ||
const link = require('terminal-link'); | ||
const PLAYBILL = 'playbill'; | ||
const COMMAND_PLAYBILL = `.${PLAYBILL}`; | ||
|
||
module.exports = function (scriptwriter) { | ||
module.exports = async function (scriptwriter) { | ||
const { director } = scriptwriter.company; | ||
const { magenta } = scriptwriter.color; | ||
scriptwriter.assign({ scriptwriter }); | ||
director.displayPrompt(); | ||
/* istanbul ignore if */ | ||
if (dlv(scriptwriter, `director.commands.${PLAYBILL}`)) return; | ||
scriptwriter.log(magenta('.help for help. Tab twice for hints.')); | ||
scriptwriter.completion = COMMAND_PLAYBILL; | ||
director.defineCommand(PLAYBILL, { | ||
help: `List the scriptwriter's ${PLAYBILL}`, | ||
action() { | ||
const credits = Object.keys(scriptwriter.company).sort(); | ||
const maxLength = Math.max(...credits.map((c) => c.length)) + 2; | ||
scriptwriter.log( | ||
credits | ||
.map((c) => | ||
[ | ||
getLink(c), | ||
' '.repeat(maxLength - c.length), | ||
getDescription(c), | ||
].join('') | ||
) | ||
.join('\n') | ||
); | ||
director.displayPrompt(); | ||
}, | ||
}); | ||
|
||
function getLink(name) { | ||
const PLAYWRIGHT_API = | ||
'https://github.com/microsoft/playwright/blob/v0.12.1/docs/api.md'; | ||
switch (name) { | ||
case 'page': | ||
case 'browser': | ||
return link(name, `${PLAYWRIGHT_API}#class-${name}`); | ||
case 'client': | ||
return link( | ||
name, | ||
'https://chromedevtools.github.io/devtools-protocol/' | ||
); | ||
case 'context': | ||
return link(name, `${PLAYWRIGHT_API}#class-browser${name}`); | ||
case 'director': | ||
return link(name, 'https://nodejs.org/api/repl.html'); | ||
case 'playwright': | ||
return link(name, PLAYWRIGHT_API); | ||
case 'scriptwriter': | ||
return link(name, 'https://github.com/AutoSponge/scriptwriter#readme'); | ||
default: | ||
} | ||
} | ||
|
||
function getDescription(name) { | ||
switch (name) { | ||
case 'page': | ||
return `Playwright's Page instance for the browser's context.`; | ||
case 'browser': | ||
return `Playwright's Browser instance.`; | ||
case 'client': | ||
return `Playwritght's CDPSession instance (Chrome only).`; | ||
case 'context': | ||
return `Playwright's BrowserContext instance for the browser.`; | ||
case 'director': | ||
return `Node's repl instance.`; | ||
case 'playwright': | ||
return `Playwright object.`; | ||
case 'scriptwriter': | ||
return `The instance object that controls the director.`; | ||
default: | ||
} | ||
if (!director.commands.playbill) { | ||
scriptwriter.log(magenta('.help for help. Tab twice for hints.')); | ||
} | ||
await scriptwriter.defineCommands(); | ||
director.displayPrompt(); | ||
}; |
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
Oops, something went wrong.