Skip to content

Commit

Permalink
Improved installer, making itemsPerPage a configuration variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Boës committed Aug 15, 2016
1 parent bf42eee commit 372853f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
56 changes: 29 additions & 27 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,7 @@ var fs = require('fs-extra');
var inquirer = require('inquirer');
var configFilename = 'user/config.json';

var defaultValues = {};
try {
defaultValues = JSON.parse(fs.readFileSync(configFilename));
} catch (e) {
defaultValues = {
"baseUrl": "http://example.com",
"basePath": "/",
"language": "en",
"themeColor": "#ffffff",
"deployCmd":"# rsync -az --exclude=node_modules --delete * HOST:PATH && echo \"Pubslished\"",
"imageSizes": [
[180,180],
[400,225],
[800,450]
]
};
}
var defaultValues = require('./src/config');

defaultValues.imageSizes = defaultValues.imageSizes.map(function(i){
return i.join('x');
Expand All @@ -34,14 +18,17 @@ var questions = [
type: 'input',
name: 'name',
message: 'The name of your site',
default: defaultValues.name
default: defaultValues.name,
validate: function(v) {
return v ? true : 'Please supply at least a short name for your site.'
}
},{
type: 'input',
name: 'baseUrl',
message: 'Domain of your site, starting with `http`',
default: defaultValues.baseUrl,
validate: function(v) {
return v.match(/^http(s)?:\/\/\S+$/) ? true : 'Please add a valid url, starting with `http://`';
return v.match(/^http(s)?:\/\/\S+$/) ? true : 'Please supply a valid url, starting with `http://`.';
},
filter: function(v) {
return v.replace(/\/$/,'');
Expand All @@ -52,63 +39,77 @@ var questions = [
message: 'Base path like `/`',
default: defaultValues.basePath,
validate: function(v) {
return v.match(/^[a-zA-Z0-9\.\/_-]+$/) ? true : 'Please add a valid path, at least `/`';
return v.match(/^[a-zA-Z0-9\.\/_-]+$/) ? true : 'Please supply a valid path, at least `/`.';
},
filter: function(v) {
return v.replace(/^([^\/])/,'/$1').replace(/([^\/])$/,'$1/');
}
},{
type: 'input',
name: 'description',
message: 'A short description of your blog',
message: 'A short description of your blog (optional)',
default: defaultValues.description
},{
type: 'input',
name: 'language',
message: 'Language code',
default: defaultValues.language,
validate: function(v) {
return v.match(/^[a-zA-Z]+$/) ? true : 'Please add a valid language code like `en` or `es`';
return v.match(/^[a-zA-Z]+$/) ? true : 'Please supply a valid language code like `en` or `es`.';
},
filter: function(v) {
return v.toLowerCase();
}
},{
type: 'input',
name: 'itemsPerPage',
message: 'Items per page',
default: defaultValues.itemsPerPage,
validate: function(v) {
return Number(v)> 0 ? true : 'Please supply a positive number.';
},
filter: function(v) {
return Number(v);
}
},{
type: 'input',
name: 'defaultAuthor',
message: 'Default author name',
default: defaultValues.defaultAuthor.name
default: defaultValues.defaultAuthor.name,
validate: function(v) {
return v ? true : 'Please supply at least a short name for your site.'
}
},{
type: 'input',
name: 'defaultAuthorEmail',
message: 'Default email address of author',
default: defaultValues.defaultAuthor.email,
validate: function(v) {
return v.match(/^\S+@\S+$/) ? true : 'Please add a valid email address';
return (v.match(/^\S+@\S+$/)) ? true : 'Please supply a valid email address.';
}
},{
type: 'input',
name: 'ogImage',
message: 'URL of standard teaser image (optional)',
default: defaultValues.ogImage,
validate: function(v) {
return (!v || v.match(/^http(s)?:\/\/\S+$/)) ? true : 'Please add a valid url, starting with `http://` or leave field empty';
return (!v || v.match(/^http(s)?:\/\/\S+$/)) ? true : 'Please supply a valid url, starting with `http://` or leave field empty.';
}
},{
type: 'input',
name: 'twitterAccount',
message: 'Twitter account name (optional)',
default: defaultValues.twitterAccount,
validate: function(v) {
return (!v || v.match(/^[a-zA-z0-9_-]+$/)) ? true : 'Please add a Twitter account name or leave field empty';
return (!v || v.match(/^[a-zA-z0-9_-]+$/)) ? true : 'Please supply a Twitter account name or leave field empty.';
}
},{
type: 'input',
name: 'themeColor',
message: 'Basic color of your theme as hexcode (optional)',
default: defaultValues.themeColor,
validate: function(v) {
return (!v || v.match(/^#[a-zA-z0-9]{3,6}$/)) ? true : 'Please add a Thex color code like `#fa647a`';
return (!v || v.match(/^#[a-zA-z0-9]{3,6}$/)) ? true : 'Please supply a Thex color code like `#fa647a`.';
},
filter: function(v) {
return v.toLowerCase();
Expand Down Expand Up @@ -142,6 +143,7 @@ inquirer.prompt(questions).then(
"email": answers.defaultAuthorEmail,
"name": answers.defaultAuthor
};
delete(answers.defaultAuthorEmail);
answers.imageSizes = answers.imageSizes.map(function(i) {
return i.split(/x/);
});
Expand Down
21 changes: 20 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
'use strict';

var fs = require('fs-extra');
var config = JSON.parse(fs.readFileSync('./user/config.json'));
var config = {};
var pkg = JSON.parse(fs.readFileSync('./package.json'));

try {
config = JSON.parse(fs.readFileSync('./user/config.json'));
} catch (e) {
config = {
"baseUrl": "http://example.com",
"basePath": "/",
"language": "en",
"itemsPerPage": 5,
"themeColor": "#ffffff",
"defaultAuthor": {},
"deployCmd":"# rsync -az --exclude=node_modules --delete * HOST:PATH && echo \"Pubslished\"",
"imageSizes": [
[180,180],
[400,225],
[800,450]
]
};
}

config.directories = pkg.directories;

if (config.deployCmd.match(/^#/)) {
Expand Down
2 changes: 1 addition & 1 deletion src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Generator.buildAllArticles = function ( force ) {
* @return {Promise} with first parameter of `resolve` being the number of files converted.
*/
Generator.buildSpecialPages = function () {
var pagedPosts = index.getPagedPosts(5),
var pagedPosts = index.getPagedPosts(config.itemsPerPage),
page,
indexFilename,
i,
Expand Down

0 comments on commit 372853f

Please sign in to comment.