Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Scribunto console/remoting examples #166

Merged
merged 14 commits into from Jun 18, 2020
23 changes: 23 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# `nodemw` examples
The `examples` directory contains `nodemw` script examples that communicate with the MediaWiki API.

To run each example, use the following in your console:
```console
cd examples
node pagesInCategory.js
```

## Scribunto examples
There are two scripts - `scribuntoConsole.js` and `scribuntoRemoteDebug.js` - that provide Scribunto debugging in your IDE using Node.js.

The `scribuntoConsole.js` script will query the Scribunto console for a wiki module.

The `scribuntoRemoteDebug.js` script will query the console for staging or debugging of a local file. A simple Scribunto module is provided at `'helloworld.lua'` for convenience and configuration testing.

Script usage:
1. Set the `title` value in the API query parameters (the `params` variable) to the module's current or future page name.
2. When using `scribuntoRemoteDebug.js`, the local module is placed in the same folder. The script would be edited to replace the `'helloworld.lua'` file (the first `fs.readFile` argument) with your staged module's filename.
3. When the script is run, the Scribunto console introduction is shown and a console session begins.
4. Write a single line of Lua code into the console (for example, `p "help"`), and press <kbd>ENTER</kbd> to see the output.

Note that for both scripts, the module content remains the same throughout the session. You will need to close the current session (<kbd>CTRL-C</kbd>) and rerun the script if there are changes to the module you wish to test.
21 changes: 21 additions & 0 deletions examples/helloworld.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--- Hello world module for Scribunto remote debugging on Node.js.
local title = mw.title
local currentTitle = title.getCurrentTitle()

local CommandLine = require('Dev:CLI')
local commands = {}

function commands.hello(args)
mw.log('Hello, world! Max didn\'t make me put this here.')
end

return CommandLine:new{
description = '"Hello world" command line script for Scribunto.',
commands = commands,
words = {
hello = {
description = 'Outputs a "hello world" string to the console.'
}
},
options = {}
}
43 changes: 43 additions & 0 deletions examples/scribuntoConsole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';
const Bot = require( '..' ),
readline = require( 'readline' ),
c = require( 'ansicolors' ),
rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
} ),
client = new Bot( {
protocol: 'https',
server: 'dev.fandom.com',
path: ''
} ),
params = {
action: 'scribunto-console',
title: 'Module:CLI/testcases/title',
clear: true
};

function call( err, info, next, data ) {
if ( err ) {
console.error( err );
} else if ( data.type === 'error' ) {
console.error( data.message );
} else {
console.log( data.print );
}
}

function cli( input ) {
params.question = input;
client.api.call( params, call );
}

function session( err, content ) {
params.content = content;
console.log( c.green( '* The module exports are available as the variable "p", including unsaved modifications.' ) );
console.log( c.green( '* Precede a line with "=" to evaluate it as an expression, or use print().' ) );
console.log( c.green( '* Use mw.log() in module code to send messages to this console.' ) );
rl.on( 'line', cli );
}

client.getArticle( params.title, session );
44 changes: 44 additions & 0 deletions examples/scribuntoRemoteDebug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
const Bot = require( '..' ),
readline = require( 'readline' ),
fs = require( 'fs' ),
c = require( 'ansicolors' ),
rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
} ),
client = new Bot( {
protocol: 'https',
server: 'dev.fandom.com',
path: ''
} ),
params = {
action: 'scribunto-console',
title: 'Module:Sandbox/CLI',
clear: true
};

function call( err, info, next, data ) {
if ( err ) {
console.error( err );
} else if ( data.type === 'error' ) {
console.error( data.message );
} else {
console.log( data.print );
}
}

function cli( input ) {
params.question = input;
client.api.call( params, call );
}

function session( err, data ) {
params.content = data;
console.log( c.green( '* The module exports are available as the variable "p", including unsaved modifications.' ) );
console.log( c.green( '* Precede a line with "=" to evaluate it as an expression, or use print().' ) );
console.log( c.green( '* Use mw.log() in module code to send messages to this console.' ) );
rl.on( 'line', cli );
}

fs.readFile( 'helloworld.lua', 'utf8', session );