Skip to content
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

Add theme creation command #33

Open
wants to merge 5 commits 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
8 changes: 8 additions & 0 deletions lib/console/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ module.exports = function(ctx) {
]
}, require('./init'));

console.register('theme', 'Create a new theme folder structure.', {
desc: 'Create a new Hexo theme folder structure at the current directory.',
usage: '[name]',
arguments: [
{name: 'name', desc: 'The name of your new theme'}
]
}, require('./theme'));

console.register('version', 'Display version information.', {}, require('./version'));
};
54 changes: 54 additions & 0 deletions lib/console/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

var Promise = require('bluebird');
var fs = require('fs');
var path = require('path');

var themeStructureFolders = [
'languages',
'layout',
'scripts',
'source'
];

function parseArguments(args) {
return { name: args._[0] };
}

function addFolder(pathName) {
fs.mkdirSync(pathName);
}

function addFile(directoryPath, fileName) {
fs.writeFileSync(path.join(directoryPath, fileName), '');
}

function themeConsole(args) {
var name = parseArguments(args).name;

if (!name) {
return Promise.reject(new Error('A theme name must be provided'));
}

var baseDirectory = this.base_dir;
var themeDirectory = path.join(baseDirectory, name);

if (fs.existsSync(themeDirectory)) {
return Promise.reject(
new Error('Seems you already have `' + name + '` in your directory')
);
}

addFolder(themeDirectory);
addFile(themeDirectory, 'config.yml');

themeStructureFolders.forEach(folderName => {
var folderPath = path.join(themeDirectory, folderName);
addFolder(folderPath);
addFile(folderPath, '.gitkeep');
});

return Promise.resolve();
}

module.exports = themeConsole;
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ describe('hexo-cli', function() {
require('./scripts/hexo');
require('./scripts/init');
require('./scripts/help');
require('./scripts/theme');
require('./scripts/version');
});
62 changes: 62 additions & 0 deletions test/scripts/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

var should = require('chai').should(); // eslint-disable-line
var fs = require('fs');
var path = require('path');
var rewire = require('rewire');
var sinon = require('sinon');
var Context = require('../../lib/context');

describe('theme', function() {
var context, themeModule;

beforeEach(function() {
context = new Context();
themeModule = require('../../lib/console/theme');
});

it('throw error when folder already exists', function() {
var params = { _: [] };

return themeModule
.call(context, params)
.catch(function(error) {
error.message.should.contain('A theme name must be provided');
});
});

it('create theme initial structure', function() {
var params = { _: ['my-hexo-theme'] };
var themeModule = rewire('../../lib/console/theme');
var mkdirSpy = sinon.spy();
var writeFileSpy = sinon.spy();
var fsMock = {
mkdirSync: mkdirSpy,
writeFileSync: writeFileSpy
};

return themeModule
.__with__({
fs: Object.assign(fs, fsMock)
})(function() {
return themeModule.call(context, params);
})
.then(function() {
mkdirSpy.args.should.eql([
[path.join(context.base_dir, 'my-hexo-theme')],
[path.join(context.base_dir, 'my-hexo-theme/languages')],
[path.join(context.base_dir, 'my-hexo-theme/layout')],
[path.join(context.base_dir, 'my-hexo-theme/scripts')],
[path.join(context.base_dir, 'my-hexo-theme/source')]
]);

writeFileSpy.args.should.eql([
[path.join(context.base_dir, 'my-hexo-theme/config.yml'), ''],
[path.join(context.base_dir, 'my-hexo-theme/languages/.gitkeep'), ''],
[path.join(context.base_dir, 'my-hexo-theme/layout/.gitkeep'), ''],
[path.join(context.base_dir, 'my-hexo-theme/scripts/.gitkeep'), ''],
[path.join(context.base_dir, 'my-hexo-theme/source/.gitkeep'), '']
]);
});
});
});