Skip to content

Commit

Permalink
Refactor loading commands. Move check that project exists to middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Govorov committed May 5, 2019
1 parent 1c099fe commit 3b6c2c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
31 changes: 16 additions & 15 deletions source/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ async function applyCommand (name, env, options) {
}
}

const list = {
create: {
const list = [
{
command: 'create [<directory>]',
aliases: ['new'],
meta: '[<directory>]',
describe: 'Create a new project',
builder: yargs => yargs
.options({
Expand All @@ -48,13 +48,14 @@ const list = {
}
})
.positional('directory', {
default: 'slides'
default: 'slides',
type: 'string'
})
},

pdf: {
{
command: 'pdf',
describe: 'Converts the presentation to PDF',
usesExistingPresentation: true,
builder: yargs => yargs
.options({
output: {
Expand All @@ -66,9 +67,9 @@ const list = {
})
},

serve: {
{
command: 'serve',
describe: 'Serve the presentation in development mode',
usesExistingPresentation: true,
builder: yargs => yargs
.options({
open: {
Expand Down Expand Up @@ -96,9 +97,9 @@ const list = {
})
},

prepare: {
{
command: 'prepare',
describe: 'Gather the necessary files in a separate folder',
usesExistingPresentation: true,
builder: yargs => yargs
.options({
output: {
Expand All @@ -116,9 +117,9 @@ const list = {
})
},

archive: {
{
command: 'archive',
describe: 'Create an archive of the presentation',
usesExistingPresentation: true,
builder: yargs => yargs
.options({
output: {
Expand All @@ -136,9 +137,9 @@ const list = {
})
},

publish: {
{
command: 'publish',
describe: 'Publish your presentation to GitHub Pages',
usesExistingPresentation: true,
builder: yargs => yargs
.options({
files: {
Expand All @@ -149,6 +150,6 @@ const list = {
}
})
}
}
]

module.exports = { list, apply: applyCommand }
38 changes: 20 additions & 18 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,33 @@ app.alias('v', 'version')

const env = getEnv()

for (const name in list) {
if (!list.hasOwnProperty(name)) {
continue
const COMMANDS_REQUIRE_EXISTING_PRESENTATION = [
'pdf', 'serve', 'prepare', 'archive', 'publish'
]

app.middleware((argv, app) => {
const name = argv._[0]

if (COMMANDS_REQUIRE_EXISTING_PRESENTATION.includes(name) && !env.project) {
process.stdout.write(
chalk`{red Shower presentation not found}\n\n` +
chalk`Use {yellow shower create} to create a presentation\n` +
chalk`Run {yellow shower create --help} to learn more\n`
)

app.exit(1)
}
})

let command = list[name]
for (const command of list) {
const name = command.command.split(' ')[0]

app.command({
command: command.meta ? `${name} ${command.meta}` : name,
command: command.command,
aliases: command.aliases,
describe: chalk.yellow(command.describe),
builder: command.builder,
handler (options) {
if (command.usesExistingPresentation && !env.project) {
process.stdout.write(
chalk`{red Shower presentation not found}\n\n` +
chalk`Use {yellow shower create} to create a presentation\n` +
chalk`Run {yellow shower create --help} to learn more\n`
)

return Promise.resolve()
}

return apply(name, env, options)
}
handler: apply.bind(null, name, env)
})
}

Expand Down

0 comments on commit 3b6c2c1

Please sign in to comment.