Skip to content

Commit

Permalink
Merge pull request #6 from thiamsantos/node
Browse files Browse the repository at this point in the history
CLI
  • Loading branch information
gustavoquinalha authored Apr 3, 2017
2 parents 72da7a1 + 12d1feb commit a447440
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 72 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Thiago Santos
Copyright (c) 2017 Gustavo Quinalha

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
198 changes: 198 additions & 0 deletions bin/seotopper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#!/usr/bin/env node

const inquirer = require('inquirer')
const seotopper = require('../lib/seotopper')

const questions = [
{
type: 'input',
name: 'title',
message: 'What\'s the title of your page',
validate: value => {
if (value === '') {
return 'Please enter a title'
}
if (value.length > 57) {
return 'Please enter a title with less than 57 characters'
}
return true
}
},
{
type: 'input',
name: 'description',
message: 'What\'s the description of your page',
validate: value => {
if (value === '') {
return 'Please enter a description'
}
if (value.length > 160) {
return 'Please enter a description with less than 160 characters'
}
return true
}
},
{
type: 'input',
name: 'author',
message: 'Who\'s the author',
validate: value => {
if (value === '') {
return 'Please enter an author\'s name'
}
return true
}
},
{
type: 'input',
name: 'image',
message: 'What\'s the image of your page',
validate: value => {
if (value === '') {
return 'Please enter an image'
}
return true
}
},
{
type: 'input',
name: 'canonical',
message: 'What\'s the canonical url of your page',
validate: value => {
if (value === '') {
return 'Please enter a canonical url'
}
return true
}
},
{
type: 'list',
name: 'robots',
message: 'What you wanna tell to the robots',
choices: [
'index/follow',
'noindex/follow',
'index/nofollow',
'noarchive',
'nosnippet',
'noodp',
'notranslate',
'noimageindex',
'none'
]
},
{
type: 'input',
name: 'base',
message: 'What\'s the base url of your page'
},
{
type: 'input',
name: 'sitemap',
message: 'What\'s the sitemap of your page'
},
{
type: 'input',
name: 'themeColor',
message: 'What\'s the theme-color of your page'
},
{
type: 'confirm',
name: 'facebook',
message: 'Do you wanna seo for facebook'
},
{
type: 'list',
name: 'facebookType',
message: 'What\'s the type of your page',
choices: [
'website',
'blog',
'article',
'activity',
'sport',
'company',
'restaurant',
'hotel',
'cause',
'band',
'government',
'non_profit',
'school',
'university',
'actor',
'athlete',
'city',
'country',
'album',
'book',
'drink',
'game',
'product',
'song',
'movie'
],
when: answers => {
return answers.facebook
}
},
{
type: 'input',
name: 'facebookSiteName',
message: 'What\'s the name of your page on facebook',
when: answers => {
return answers.facebook
}
},
{
type: 'input',
name: 'facebookLocale',
message: 'What\'s the locale of your page on facebook',
when: answers => {
return answers.facebook
}
},
{
type: 'input',
name: 'facebookId',
message: 'What\'s the id of your page on facebook',
when: answers => {
return answers.facebook
}
},
{
type: 'input',
name: 'facebookAdmins',
message: 'What are the admins of your page on facebook',
when: answers => {
return answers.facebook
}
},
{
type: 'confirm',
name: 'twitter',
message: 'Do you wanna seo for twitter'
},
{
type: 'list',
name: 'twitterCard',
message: 'Which twitter do you want',
choices: [
'Summary',
'Product',
'Photo',
'Summary Large Image',
'Player',
'App',
'Gallery'
],
when: answers => {
return answers.twitter
}
}
]

inquirer.prompt(questions).then(answers => {
const generatedSEO = seotopper(answers)
process.stdout.write(generatedSEO)
})
40 changes: 30 additions & 10 deletions lib/seotopper.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
const utils = require('./utils')

const checkMissingKeys = utils.checkMissingKeys
const createErrorMessage = utils.createErrorMessage
const requiredProperties = utils.requiredProperties
const checkMissingKeys = utils.checkMissingKeys
const createMissingKeysErrorMessage = utils.createMissingKeysErrorMessage
const checkEmptyKeys = utils.checkEmptyKeys
const createEmptyKeysErrorMessage = utils.createEmptyKeysErrorMessage

function seotopper(args) {
const keys = Object.keys(args)
const missingKeys = checkMissingKeys(requiredProperties, keys)

if (missingKeys.length > 0) {
throw new Error(createErrorMessage(missingKeys))
throw new Error(createMissingKeysErrorMessage(missingKeys))
}

const emptyKeys = checkEmptyKeys(requiredProperties, keys, args)

if (emptyKeys.length > 0) {
throw new Error(createEmptyKeysErrorMessage(emptyKeys))
}

return `<title>${args.title}</title>
Expand All @@ -35,21 +43,33 @@ function seotopper(args) {
${args.facebook ?
`<!-- markup for facebook -->
<meta property="og:type" content="${args.facebook.type}"/>
<meta property="og:type" content="${args.facebookType}"/>
<meta property="og:title" content="${args.title}"/>
<meta property="og:url" content="http://meusite.com.br"/>
<meta property="og:site_name" content="${args.facebook.siteName}"/>
<meta property="og:url" content="${args.canonical}"/>
${args.facebookSiteName ?
`<meta property="og:site_name" content="${args.facebookSiteName}"/>` :
''
}
<meta property="og:image" content="${args.image}"/>
<meta property="og:description" content="${args.description}"/>
<meta property="og:locale" content="${args.facebook.locale}"/>
<meta property="fb:app_id" content="${args.facebook.id}"/>
<meta property="fb:admins" content="${args.facebook.admins}"/>` :
${args.facebookLocale ?
`<meta property="og:locale" content="${args.facebookLocale}"/>` :
''
}
${args.facebookId ?
`<meta property="fb:app_id" content="${args.facebookId}"/>` :
''
}
${args.facebookAdmins ?
`<meta property="fb:admins" content="${args.facebookAdmins}"/>` :
''
}` :
''
}
${args.twitter ?
`<!-- markup for twitter -->
<meta name="twitter:card" content="${args.twitter.card}"/>
<meta name="twitter:card" content="${args.twitterCard}"/>
<meta name="twitter:title" content="${args.title}"/>
<meta name="twitter:description" content="${args.description}"/>
<meta name="twitter:creator" content="${args.author}"/>
Expand Down
18 changes: 15 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@ const requiredProperties = [
const checkMissingKeys = (requiredProperties, keys) => requiredProperties
.filter(property => keys.indexOf(property) === -1)

const createErrorMessage = missingKeys => String(
const createMissingKeysErrorMessage = missingKeys => String(
'The following ' +
(missingKeys.length > 1 ? 'properties are' : 'property is') +
' required: ' +
missingKeys.join(', ')
missingKeys.sort().join(', ')
)

const checkEmptyKeys = (requiredProperties, keys, args) => keys
.filter(key => requiredProperties.indexOf(key) !== -1)
.filter(key => args[key] === '')

const createEmptyKeysErrorMessage = emptyKeys => String(
'The following ' +
(emptyKeys.length > 1 ? 'properties' : 'property') +
' cannot be empty: ' +
emptyKeys.sort().join(', ')
)
module.exports = {
checkMissingKeys,
checkEmptyKeys,
requiredProperties,
createErrorMessage
createMissingKeysErrorMessage,
createEmptyKeysErrorMessage
}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"author": "Gustavo Quinalha",
"keywords": [],
"license": "MIT",
"bin": {
"seotopper": "bin/seotopper.js"
},
"main": "lib/seotopper.js",
"scripts": {
"precommit": "npm test",
Expand All @@ -31,7 +34,10 @@
"husky": "^0.13.3",
"xo": "^0.18.1"
},
"engines" : {
"engines": {
"node": ">=4"
},
"dependencies": {
"inquirer": "^3.0.6"
}
}
Loading

0 comments on commit a447440

Please sign in to comment.