From 1256f7f3f5c763278d7fa3da3b0b93943fb1a2b5 Mon Sep 17 00:00:00 2001 From: Hariadi Hinta Date: Thu, 13 Mar 2014 15:11:38 +0800 Subject: [PATCH 1/6] only show when user want --- app/index.js | 195 +++++++++++++++++++++++++-------------------------- 1 file changed, 97 insertions(+), 98 deletions(-) diff --git a/app/index.js b/app/index.js index 2adff68..53fc791 100644 --- a/app/index.js +++ b/app/index.js @@ -1,98 +1,97 @@ -'use strict'; - -var yeoman = require('yeoman-generator'); -var chalk = require('chalk'); - -var MetalsmithGenerator = yeoman.generators.Base.extend({ - - init: function () { - this.pkg = require('../package.json'); - - this.on('end', function () { - this.installDependencies({ - skipInstall: this.options['skip-install'] || this.options['s'], - callback: function () { - this.spawnCommand('make', ['build']); - }.bind(this) - }); - }); - - }, - - askFor: function () { - var done = this.async(); - - if (!this.options['skip-welcome-message'] || !this.options['w']) { - this.log(this.yeoman); - } - - this.log(chalk.magenta('You\'re using the fantastic Metalsmith generator.')); - - /* - TODO: - 1. build type - 2. metalsmith-templates engine - */ - - var prompts = [{ - type : 'input', - name : 'msTitle', - message : 'Site Title', - default : this.appname - }, { - type : 'input', - name : 'msDesc', - message : 'Site Description', - default : 'My Metalsmith-Powered Site' - }, { - type : "input", - name : "msAuthor", - message : "Author name", - default : this.user.git.username || 'Metal Smith' - }, { - type : "input", - name : "msGithubUser", - message : "Would you mind telling me your username on Github?", - default : process.env.username || 'metalsmith' - }]; - - this.prompt(prompts, function (answers) { - - for (var key in answers) { - if (answers.hasOwnProperty(key)) { - this[key] = answers[key]; - } - } - - done(); - }.bind(this)); - }, - - site: function () { - this.mkdir('_site'); - }, - - layouts: function () { - this.mkdir('_layouts'); - this.directory('_layouts', '_layouts'); - }, - - posts: function () { - this.mkdir('_posts'); - this.directory('_posts', '_posts'); - }, - - gitfiles: function () { - this.copy('gitignore', '.gitignore'); - }, - - app: function () { - this.copy('_package.json', 'package.json'); - this.copy('_metalsmith.json', 'metalsmith.json'); - this.copy('Makefile', 'Makefile'); - this.copy('README.md', 'README.md'); - }, - -}); - -module.exports = MetalsmithGenerator; +'use strict'; + +var yeoman = require('yeoman-generator'); +var chalk = require('chalk'); + +var MetalsmithGenerator = yeoman.generators.Base.extend({ + + init: function () { + this.pkg = require('../package.json'); + + this.on('end', function () { + this.installDependencies({ + skipInstall: this.options['skip-install'] || this.options['s'], + callback: function () { + this.spawnCommand('make', ['build']); + }.bind(this) + }); + }); + + }, + + askFor: function () { + var done = this.async(); + + if (!this.options['skip-welcome-message'] || !this.options['w']) { + this.log(this.yeoman); + this.log(chalk.magenta('You\'re using the fantastic Metalsmith generator.')); + } + + /* + TODO: + 1. build type + 2. metalsmith-templates engine + */ + + var prompts = [{ + type : 'input', + name : 'msTitle', + message : 'Site Title', + default : this.appname + }, { + type : 'input', + name : 'msDesc', + message : 'Site Description', + default : 'My Metalsmith-Powered Site' + }, { + type : "input", + name : "msAuthor", + message : "Author name", + default : this.user.git.username || 'Metal Smith' + }, { + type : "input", + name : "msGithubUser", + message : "Would you mind telling me your username on Github?", + default : process.env.username || 'metalsmith' + }]; + + this.prompt(prompts, function (answers) { + + for (var key in answers) { + if (answers.hasOwnProperty(key)) { + this[key] = answers[key]; + } + } + + done(); + }.bind(this)); + }, + + site: function () { + this.mkdir('_site'); + }, + + layouts: function () { + this.mkdir('_layouts'); + this.directory('_layouts', '_layouts'); + }, + + posts: function () { + this.mkdir('_posts'); + this.directory('_posts', '_posts'); + }, + + gitfiles: function () { + this.copy('gitignore', '.gitignore'); + }, + + app: function () { + this.copy('_package.json', 'package.json'); + this.copy('_metalsmith.json', 'metalsmith.json'); + this.copy('Makefile', 'Makefile'); + this.copy('README.md', 'README.md'); + }, + +}); + +module.exports = MetalsmithGenerator; From a3432ae8b089c0212a1b7119e7f7fd01b918928a Mon Sep 17 00:00:00 2001 From: Hariadi Hinta Date: Fri, 14 Mar 2014 14:46:43 +0800 Subject: [PATCH 2/6] ability to choose engine template and permalink patern --- app/index.js | 79 ++++++++++++++++--- .../_layouts/handlebars-default.html | 16 ++++ app/templates/_layouts/handlebars-post.html | 12 +++ app/templates/_layouts/handlebars.html | 12 +++ app/templates/_layouts/post.html | 2 + app/templates/_metalsmith.json | 18 ++--- app/templates/_package.json | 14 ++-- metalsmith.json | 16 ++++ 8 files changed, 144 insertions(+), 25 deletions(-) create mode 100644 app/templates/_layouts/handlebars-default.html create mode 100644 app/templates/_layouts/handlebars-post.html create mode 100644 app/templates/_layouts/handlebars.html create mode 100644 metalsmith.json diff --git a/app/index.js b/app/index.js index 53fc791..8a41df9 100644 --- a/app/index.js +++ b/app/index.js @@ -17,6 +17,8 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ }); }); + this.metalsmith = require('../metalsmith.json'); + }, askFor: function () { @@ -27,11 +29,15 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ this.log(chalk.magenta('You\'re using the fantastic Metalsmith generator.')); } - /* - TODO: - 1. build type - 2. metalsmith-templates engine - */ + var plugins = this.metalsmith.plugins; + var choices = []; + for (var plugin in plugins) { + if(plugins.hasOwnProperty(plugin)){ + choices.push({ name: plugin, checked: true }); + } + } + + var prompts = [{ type : 'input', @@ -53,6 +59,31 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ name : "msGithubUser", message : "Would you mind telling me your username on Github?", default : process.env.username || 'metalsmith' + }, { + type: 'checkbox', + name: 'msPlugins', + message: 'Which plugins do you want to use?', + choices: choices + }, { + type: 'list', + message: 'Which template engine do you want to use?', + name: 'templateEngine', + //TODO: https://github.com/visionmedia/consolidate.js#supported-template-engines + choices: [{ + name: 'swig', + checked: true + }, 'handlebars'], + when : function( answers ){ + return answers.msPlugins.indexOf('metalsmith-templates') > -1; + } + }, { + type: 'input', + message: 'What should a permalink look like?', + name: 'permalinksPattern', + default : ':title', + when : function( answers ){ + return answers.msPlugins.indexOf('metalsmith-permalinks') > -1; + } }]; this.prompt(prompts, function (answers) { @@ -63,6 +94,23 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ } } + var deps = this._.object(answers.msPlugins.map(function(plugin) { + return plugin.replace('metalsmith-', '') + }), answers.msPlugins.map(function() { + return true + })); + + for (var key in deps) { + if (deps.hasOwnProperty(key)) { + this[key] = deps[key]; + } + } + + //this.deps = this.metalsmith.plugins; + + console.log(this); + + done(); }.bind(this)); }, @@ -72,8 +120,12 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ }, layouts: function () { + + var prefix = (this.templateEngine === 'swig') ? '' : this.templateEngine + '-'; + this.mkdir('_layouts'); - this.directory('_layouts', '_layouts'); + this.template('_layouts/' + prefix + 'default.html', '_layouts/default.html'); + this.template('_layouts/' + prefix + 'post.html', '_layouts/post.html'); }, posts: function () { @@ -85,10 +137,19 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ this.copy('gitignore', '.gitignore'); }, - app: function () { - this.copy('_package.json', 'package.json'); - this.copy('_metalsmith.json', 'metalsmith.json'); + package: function () { + this.template('_package.json', 'package.json'); + }, + + metalsmith: function () { + this.template('_metalsmith.json', 'metalsmith.json'); + }, + + makefile: function () { this.copy('Makefile', 'Makefile'); + }, + + readme: function () { this.copy('README.md', 'README.md'); }, diff --git a/app/templates/_layouts/handlebars-default.html b/app/templates/_layouts/handlebars-default.html new file mode 100644 index 0000000..7c35986 --- /dev/null +++ b/app/templates/_layouts/handlebars-default.html @@ -0,0 +1,16 @@ + + + <%= _.capitalize(msTitle) %> - {{ title }} + + +

Blog Posts

+ + {{#if articles }} + {{#each articles }} +

{{ article.title }}

+ + {{/each}} + {{/if}} + + + diff --git a/app/templates/_layouts/handlebars-post.html b/app/templates/_layouts/handlebars-post.html new file mode 100644 index 0000000..b39d581 --- /dev/null +++ b/app/templates/_layouts/handlebars-post.html @@ -0,0 +1,12 @@ + + + <%= _.capitalize(msTitle) %> - {{ title }} + + +

{{ title }}

+ + + {{{ contents }}} + + + diff --git a/app/templates/_layouts/handlebars.html b/app/templates/_layouts/handlebars.html new file mode 100644 index 0000000..b39d581 --- /dev/null +++ b/app/templates/_layouts/handlebars.html @@ -0,0 +1,12 @@ + + + <%= _.capitalize(msTitle) %> - {{ title }} + + +

{{ title }}

+ + + {{{ contents }}} + + + diff --git a/app/templates/_layouts/post.html b/app/templates/_layouts/post.html index 3b21a41..93dd881 100644 --- a/app/templates/_layouts/post.html +++ b/app/templates/_layouts/post.html @@ -4,7 +4,9 @@

{{ title }}

+ {{ contents | safe }} + diff --git a/app/templates/_metalsmith.json b/app/templates/_metalsmith.json index 524fb1c..893898d 100644 --- a/app/templates/_metalsmith.json +++ b/app/templates/_metalsmith.json @@ -5,26 +5,26 @@ "title": "<%= _.capitalize(msTitle) %>", "description": "<%= msDesc %>" }, - "plugins": { + "plugins": {<% if(ignore){ %> "metalsmith-ignore": [ "_drafts/*", "_posts/index.md" - ], - "metalsmith-drafts": {}, - "metalsmith-markdown": {}, + ],<% } %><% if(drafts){ %> + "metalsmith-drafts": {},<% } %><% if(markdown){ %> + "metalsmith-markdown": {},<% } %><% if(permalinks){ %> "metalsmith-permalinks": { - "pattern": ":title" - }, + "pattern": "<%= permalinksPattern %>" + },<% } %><% if(collections){ %> "metalsmith-collections": { "articles": { "pattern": "*.md", "sortBy": "date", "reverse": true } - }, + },<% } %><% if(templates){ %> "metalsmith-templates": { - "engine": "swig", + "engine": "<%= templateEngine %>", "directory": "_layouts" - } + }<% } %> } } diff --git a/app/templates/_package.json b/app/templates/_package.json index 110555b..89e19df 100644 --- a/app/templates/_package.json +++ b/app/templates/_package.json @@ -10,14 +10,14 @@ "type": "git", "url": "https://github.com/<%= msGithubUser %>/<%= _.slugify(msTitle) %>" }, - "dependencies": { - "metalsmith-ignore": "^0.1.2", - "metalsmith-drafts": "0.0.1", + "dependencies": {<% if(ignore){ %> + "metalsmith-ignore": "^0.1.2",<% } %><% if(drafts){ %> + "metalsmith-drafts": "0.0.1",<% } %><% if(templates){ %> "metalsmith-templates": "^0.3.0", - "metalsmith-markdown": "^0.2.1", - "metalsmith-permalinks": "^0.2.0", - "metalsmith-collections": "^0.1.0", - "swig": "^1.3.2", + "<%= templateEngine %>": "*",<% } %><% if(markdown){ %> + "metalsmith-markdown": "^0.2.1",<% } %><% if(permalinks){ %> + "metalsmith-permalinks": "^0.2.0",<% } %><% if(collections){ %> + "metalsmith-collections": "^0.1.0",<% } %> "metalsmith": "^0.3.0" } } diff --git a/metalsmith.json b/metalsmith.json new file mode 100644 index 0000000..e55ced9 --- /dev/null +++ b/metalsmith.json @@ -0,0 +1,16 @@ +{ + "title": "Metalsmith Blog", + "description": "My Metalsmith-Powered Site", + "plugins": { + "metalsmith-ignore": "^0.1.2", + "metalsmith-drafts": "^0.0.1", + "metalsmith-templates": "^0.3.0", + "metalsmith-markdown": "^0.2.1", + "metalsmith-permalinks": "^0.2.0", + "metalsmith-collections": "^0.1.0" + }, + "engine": { + "swig": "^1.3.2", + "handlebars": "^2.0.0-alpha.2" + } +} From 81d69537a66ae4910982bb42bb215b58f31aa225 Mon Sep 17 00:00:00 2001 From: Hariadi Hinta Date: Fri, 14 Mar 2014 16:07:09 +0800 Subject: [PATCH 3/6] update test --- app/index.js | 7 +------ test/test-creation.js | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/index.js b/app/index.js index 8a41df9..1cea5b9 100644 --- a/app/index.js +++ b/app/index.js @@ -68,7 +68,7 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ type: 'list', message: 'Which template engine do you want to use?', name: 'templateEngine', - //TODO: https://github.com/visionmedia/consolidate.js#supported-template-engines + //TODO: support https://github.com/visionmedia/consolidate.js#supported-template-engines choices: [{ name: 'swig', checked: true @@ -106,11 +106,6 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ } } - //this.deps = this.metalsmith.plugins; - - console.log(this); - - done(); }.bind(this)); }, diff --git a/test/test-creation.js b/test/test-creation.js index 4faaaa9..1dd4b6d 100644 --- a/test/test-creation.js +++ b/test/test-creation.js @@ -32,10 +32,20 @@ describe('metalsmith generator', function () { ]; helpers.mockPrompt(this.app, { - 'msTitle': 'Metalsmith Blog', - 'msDesc': 'My Metalsmith-Powered Site', - 'msAuthor': 'Metal Smith', - 'msGithubUser': 'metalsmith', + msTitle: 'Metalsmith Blog', + msDesc: 'My Metalsmith-Powered Site', + msAuthor: 'Metal Smith', + msGithubUser: 'metalsmith', + msPlugins: [ + 'metalsmith-ignore', + 'metalsmith-drafts', + 'metalsmith-templates', + 'metalsmith-markdown', + 'metalsmith-permalinks', + 'metalsmith-collections' + ], + templateEngine: 'swig', + permalinksPattern: ':title', }); this.app.options['skip-install'] = true; this.app.run({}, function () { From 51f888569f862def0aa2b69e2c6ee73891723879 Mon Sep 17 00:00:00 2001 From: Hariadi Hinta Date: Sat, 15 Mar 2014 01:50:22 +0800 Subject: [PATCH 4/6] cleanup --- app/index.js | 50 ++++++++++++++++++++++--------------------- test/test-creation.js | 2 +- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/app/index.js b/app/index.js index 1cea5b9..5f982a3 100644 --- a/app/index.js +++ b/app/index.js @@ -10,7 +10,7 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ this.on('end', function () { this.installDependencies({ - skipInstall: this.options['skip-install'] || this.options['s'], + skipInstall: this.options['skip-install'] || this.options.s, callback: function () { this.spawnCommand('make', ['build']); }.bind(this) @@ -24,7 +24,7 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ askFor: function () { var done = this.async(); - if (!this.options['skip-welcome-message'] || !this.options['w']) { + if (!this.options['skip-welcome-message'] || !this.options.w) { this.log(this.yeoman); this.log(chalk.magenta('You\'re using the fantastic Metalsmith generator.')); } @@ -32,13 +32,11 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ var plugins = this.metalsmith.plugins; var choices = []; for (var plugin in plugins) { - if(plugins.hasOwnProperty(plugin)){ + if (plugins.hasOwnProperty(plugin)) { choices.push({ name: plugin, checked: true }); } } - - var prompts = [{ type : 'input', name : 'msTitle', @@ -50,14 +48,14 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ message : 'Site Description', default : 'My Metalsmith-Powered Site' }, { - type : "input", - name : "msAuthor", - message : "Author name", + type : 'input', + name : 'msAuthor', + message : 'Author name', default : this.user.git.username || 'Metal Smith' }, { - type : "input", - name : "msGithubUser", - message : "Would you mind telling me your username on Github?", + type : 'input', + name : 'msGithubUser', + message : 'Would you mind telling me your username on Github?', default : process.env.username || 'metalsmith' }, { type: 'checkbox', @@ -73,7 +71,7 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ name: 'swig', checked: true }, 'handlebars'], - when : function( answers ){ + when : function (answers) { return answers.msPlugins.indexOf('metalsmith-templates') > -1; } }, { @@ -81,28 +79,32 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ message: 'What should a permalink look like?', name: 'permalinksPattern', default : ':title', - when : function( answers ){ + when : function (answers) { return answers.msPlugins.indexOf('metalsmith-permalinks') > -1; } }]; this.prompt(prompts, function (answers) { + var deps = this._.object(answers.msPlugins.map(function (plugin) { + return plugin.replace('metalsmith-', ''); + }), answers.msPlugins.map(function () { + return true; + })); + for (var key in answers) { if (answers.hasOwnProperty(key)) { - this[key] = answers[key]; - } - } + if (key === 'msPlugins') { - var deps = this._.object(answers.msPlugins.map(function(plugin) { - return plugin.replace('metalsmith-', '') - }), answers.msPlugins.map(function() { - return true - })); + for (var pkg in deps) { + if (deps.hasOwnProperty(pkg)) { + this[pkg] = deps[pkg]; + } + } - for (var key in deps) { - if (deps.hasOwnProperty(key)) { - this[key] = deps[key]; + } else { + this[key] = answers[key]; + } } } diff --git a/test/test-creation.js b/test/test-creation.js index 1dd4b6d..686dcb6 100644 --- a/test/test-creation.js +++ b/test/test-creation.js @@ -43,7 +43,7 @@ describe('metalsmith generator', function () { 'metalsmith-markdown', 'metalsmith-permalinks', 'metalsmith-collections' - ], + ], templateEngine: 'swig', permalinksPattern: ':title', }); From e4ceb0cde115645cd0b4bd1afaaf4127e42d1e47 Mon Sep 17 00:00:00 2001 From: Hariadi Hinta Date: Sat, 15 Mar 2014 01:59:49 +0800 Subject: [PATCH 5/6] v0.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index faae2c5..68f8285 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "generator-metalsmith", - "version": "0.1.1", + "version": "0.2.0", "homepage": "https://github.com/hariadi/generator-metalsmith", "description": "Yeoman generator for Metalsmith", "author": { From f05301ee147c79c75613396a38814bdc79df0425 Mon Sep 17 00:00:00 2001 From: Hariadi Hinta Date: Sat, 15 Mar 2014 19:47:29 +0800 Subject: [PATCH 6/6] we don't need at this time --- app/index.js | 2 -- package.json | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/index.js b/app/index.js index 5f982a3..117d74a 100644 --- a/app/index.js +++ b/app/index.js @@ -1,7 +1,6 @@ 'use strict'; var yeoman = require('yeoman-generator'); -var chalk = require('chalk'); var MetalsmithGenerator = yeoman.generators.Base.extend({ @@ -26,7 +25,6 @@ var MetalsmithGenerator = yeoman.generators.Base.extend({ if (!this.options['skip-welcome-message'] || !this.options.w) { this.log(this.yeoman); - this.log(chalk.magenta('You\'re using the fantastic Metalsmith generator.')); } var plugins = this.metalsmith.plugins; diff --git a/package.json b/package.json index 68f8285..8843bde 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,7 @@ "app" ], "dependencies": { - "yeoman-generator": "~0.16.0", - "chalk": "~0.4.0" + "yeoman-generator": "~0.16.0" }, "devDependencies": { "mocha": "*"