Skip to content

Started using new code style. #111

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 113 additions & 67 deletions adventure.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const inherits = require('util').inherits

/* jshint -W079 */
const Core = require('./index')
const WorkshopperAdventure = require('./index')
const error = require('./lib/print').error
/* jshint +W079 */

function legacyCommands (item) {
if (!item.aliases) {
Expand All @@ -15,96 +11,146 @@ function legacyCommands (item) {
return item
}

function LegacyAdventure (options) {
if (!(this instanceof LegacyAdventure)) {
return new LegacyAdventure(options)
class LegacyAdventure extends WorkshopperAdventure {
constructor (options = {}) {
if (typeof options === 'string') {
options = { name: options }
}
if (typeof options !== 'object') {
return error('You need to provide an options object')
}
if (!options.commands) {
options.commands = options.menuItems
}
if (options.commands) {
options.commands = options.commands.map(legacyCommands)
}
if (options.modifiers) {
options.modifiers = options.modifiers.map(legacyCommands)
}

if (options.helpFile) {
options.help = { file: options.helpFile }
}

if (!options.footer) {
if (options.footerFile) {
options.footer = { file: options.footerFile }
}
}

if (!options.defaultOutputType) {
options.defaultOutputType = 'txt'
}

if (options.hideSolutions === undefined) {
options.hideSolutions = true
}

if (options.hideRemaining === undefined) {
options.hideRemaining = true
}

super(options)
// an `onComplete` hook function *must* call the callback given to it when it's finished, async or not
if (typeof options.onComplete === 'function') {
this.onComplete = options.onComplete
}

if (options.execute === 'now') {
this.execute(process.argv.slice(2))
} else if (options.execute === 'immediatly') {
setImmediate(this.execute.bind(this, process.argv.slice(2)))
}
}

// backwards compatibility support
get title () {
return this.__('title')
}
if (typeof options === 'string') {
options = { name: options }

get subtitle () {
return this.__('subtitle')
}
if (!options) {
options = {}

get name () {
return this.__('name')
}
if (typeof options !== 'object') {
return error('You need to provide an options object')

get appName () {
return this.__('name')
}
if (!options.commands) {
options.commands = options.menuItems

get appname () {
return this.__('name')
}
if (options.commands) {
options.commands = options.commands.map(legacyCommands)

get lang () {
return this.i18n.lang('lang')
}
if (options.modifiers) {
options.modifiers = options.modifiers.map(legacyCommands)

get width () {
return this.menuFactory.options.width
}

if (options.helpFile) {
options.help = { file: options.helpFile }
get helpFile () {
return this.options.helpFile
}

if (!options.footer) {
if (options.footerFile) {
options.footer = { file: options.footerFile }
}
get footer () {
return this.options.footer
}

if (!options.defaultOutputType) {
options.defaultOutputType = 'txt'
get defaultLang () {
return this.options.defaultLang
}

if (options.hideSolutions === undefined) {
options.hideSolutions = true
get languages () {
return this.options.languages
}

if (options.hideRemaining === undefined) {
options.hideRemaining = true
get globalDataDir () {
return this.globalStorage.dir
}

// an `onComplete` hook function *must* call the callback given to it when it's finished, async or not
if (typeof options.onComplete === 'function') {
this.onComplete = options.onComplete
get dataDir () {
return this.appStorage.dir
}
Core.call(this, options)

if (options.execute === 'now') {
this.execute(process.argv.slice(2))
} else if (options.execute === 'immediatly') {
setImmediate(this.execute.bind(this, process.argv.slice(2)))
// adventure
get datadir () {
return this.appStorage.dir
}

// backwards compatibility support
this.__defineGetter__('title', this.__.bind(this, 'title'))
this.__defineGetter__('subtitle', this.__.bind(this, 'subtitle'))
this.__defineGetter__('name', this.__.bind(this, 'name'))
this.__defineGetter__('appName', this.__.bind(this, 'name'))
this.__defineGetter__('appname', this.__.bind(this, 'name'))
this.__defineGetter__('lang', this.i18n.lang.bind(this.i18n, 'lang'))
this.__defineGetter__('width', function () { return this.menuFactory.options.width }.bind(this))
this.__defineGetter__('helpFile', function () { return this.options.helpFile }.bind(this))
this.__defineGetter__('footer', function () { return this.options.footer }.bind(this))
this.__defineGetter__('defaultLang', function () { return this.options.defaultLang }.bind(this))
this.__defineGetter__('languages', function () { return this.options.languages }.bind(this))
this.__defineGetter__('globalDataDir', function () { return this.globalStorage.dir }.bind(this))
this.__defineGetter__('dataDir', function () { return this.appStorage.dir }.bind(this))
this.__defineGetter__('datadir', function () { return this.appStorage.dir }.bind(this)) // adventure
this.__defineGetter__('appDir', function () { return this.options.appDir }.bind(this))
this.__defineGetter__('exerciseDir', function () { return this.options.exerciseDir }.bind(this))
this.__defineGetter__('current', function () { return this.appStorage.get('current') }.bind(this))
this.__defineGetter__('_adventures', function () { return this.exercises }.bind(this))
this.__defineGetter__('state', function () {
get appDir () {
return this.options.appDir
}

get exerciseDir () {
return this.options.exerciseDir
}

get current () {
return this.appStorage.get('current')
}

get _adventures () {
return this.exercises
}

get state () {
return {
completed: this.appStorage.get('completed'),
current: this.appStorage.get('current')
}
})
}
}

inherits(LegacyAdventure, Core)
LegacyAdventure.prototype.processResult = function (result, stream) {
if (result) {
stream.append(['```', result, '```'])
processResult (result, stream) {
if (result) {
stream.append(['```', result, '```'])
}
return stream
}
return stream
}

module.exports = LegacyAdventure
6 changes: 4 additions & 2 deletions default/fail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
text: '\n{bold}{red}# {solution.fail.title}{/red}{/bold}\n' +
'{solution.fail.message}\n'
text: `
{bold}{red}# {solution.fail.title}{/red}{/bold}
{solution.fail.message}
`
}
4 changes: 2 additions & 2 deletions default/footer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var path = require('path')
const { join } = require('path')

module.exports = [
'---',
{
file: path.join(__dirname, '../i18n/footer/{lang}.md')
file: join(__dirname, '../i18n/footer/{lang}.md')
}
]
7 changes: 4 additions & 3 deletions default/header.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
text: '# {title}' +
'\n## __{currentExercise.name}__ (_{progress.state_resolved}_)' +
'\n\n',
text: `# {title}
## __{currentExercise.name}__ (_{progress.state_resolved}_)

`,
type: 'md'
}
6 changes: 3 additions & 3 deletions default/help.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var path = require('path')
const { join } = require('path')

module.exports = {
file: [
path.join(__dirname, '../i18n/usage/{lang}.md'),
path.join(__dirname, '../i18n/usage/en.md')
join(__dirname, '../i18n/usage/{lang}.md'),
join(__dirname, '../i18n/usage/en.md')
]
}
6 changes: 4 additions & 2 deletions default/pass.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
text: '\n{bold}{green}# {solution.pass.title}{/green}{/bold}\n' +
'{bold}{solution.pass.message}{/bold}\n'
text: `
{bold}{green}# {solution.pass.title}{/green}{/bold}
{bold}{solution.pass.message}{/bold}
`
}
Loading