From 4a135bafb16facaa2856fb171d62f549cc6185b1 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 15 Jan 2015 17:17:23 -0700 Subject: [PATCH] create post from command line --- README.md | 8 +-- bin/deardesi.js | 123 +++++++++++++++++++++++++++++++++++++---- package.json | 7 ++- views/about/about.html | 3 +- views/post/post.js | 12 ++-- 5 files changed, 129 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 4c62f07..d7d4735 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ git submodule update And now fire up Dear Desi to get started ``` -desi serve -b ~/my-desirae-blog +desi serve -d ~/my-desirae-blog ``` Now open up your evergreen browser to @@ -53,7 +53,7 @@ Commandline Once you've done the initial setup in the browser, you can run `desi` from the commandline -**NOTE:** You can omit the `-b` if you are already in your blog directory. +**NOTE:** You can omit the `-d` if you are already in your blog directory. Build Production Site --------------------- @@ -61,7 +61,7 @@ Build Production Site You must set `base_path` and `base_url` in `site.yml` before attempting to build. ``` -desi build -b ~/my-desirae-blog +desi build -d ~/my-desirae-blog ``` Outputs to `~/my-desirae-blog/compiled` @@ -70,7 +70,7 @@ Create a new Post ----------------- ``` -desi post "My First Post" -b ~/my-desirae-blog +desi post "My First Post" -d ~/my-desirae-blog ``` Outputs to `~/my-desirae-blog/posts/my-first-post.md` diff --git a/bin/deardesi.js b/bin/deardesi.js index 2bcfa13..7550824 100644 --- a/bin/deardesi.js +++ b/bin/deardesi.js @@ -4,6 +4,7 @@ var PromiseA = require('bluebird') , fs = PromiseA.promisifyAll(require('fs')) , path = require('path') , cli = require('cli') + , UUID = require('node-uuid') ; cli.parse({ @@ -19,8 +20,8 @@ function serve(blogdir) { ; server = http.createServer(app).listen(65080, function () { - console.log("Listening from " + blogdir); - console.log("Listening on http://local.dear.desi:" + server.address().port); + console.info("Listening from " + blogdir); + console.info("Listening on http://local.dear.desi:" + server.address().port); }); //secureServer = https.createServer(app).listen(65043); } @@ -41,25 +42,128 @@ function build(blogdir) { Desi.buildAll(desi, env).then(function () { Desi.write(desi, env).then(function () { - console.log('Build Success!'); + console.info('Built and saved to ' + path.join(env.wolking_path, env.compiled_path)); }); }); }); } +function createPost(originalDir, blogdir, title, extra) { + if (!title) { + console.error("Usage desi post \"My First Post\""); + console.error("(you didn't specify a title)"); + process.exit(1); + } + if (extra) { + console.error("Usage desi post \"My First Post\""); + console.error("(too many arguments - maybe you didn't put your title in quotes?)"); + process.exit(1); + } + + var Desi = require('desirae').Desirae + //, desi = {} + , env = {} + , post = {} + , slug + , filepath + , displaypath + ; + + env.working_path = env.blogdir = blogdir; + + Desi._initFileAdapter(env).then(function () { + /* + Desi.init(desi, env).then(function () { + env.url = desi.site.base_url + desi.site.base_path.replace(/^\/$/, ''); + env.base_url = desi.site.base_url; + env.base_path = desi.site.base_path; + env.compiled_path = 'compiled'; + //env.since = 0; + */ + + // TODO move this logic to desirae + post.title = title; + post.description = ""; + post.date = Desi.toLocaleDate(new Date()); + // TODO use site.permalink or collection.permalink or something like that + + slug = post.title.toLowerCase() + .replace(/["']/g, '') + .replace(/\W/g, '-') + .replace(/^-+/g, '') + .replace(/-+$/g, '') + .replace(/--/g, '-') + ; + + // TODO as per config + post.permalink = path.join('/', 'articles', slug + '.html'); + post.uuid = UUID.v4(); + // TODO as per config for default collection and default format (jade, md, etc) + filepath = path.join(blogdir, (/*config.collection ||*/ 'posts'), slug + '.md'); + displaypath = path.join(originalDir, 'posts', slug + '.md').replace(/^\/(Users|home)\/[^\/]+\//, '~/').replace(/ /g, '\\ '); + + ['updated', 'theme', 'layout', 'swatch'].forEach(function (key) { + if (!post[key]) { + delete post[key]; + } + }); + + return Desi.fsapi.putFiles([{ + path: filepath + , contents: + '---\n' + + Desi.YAML.stringify(post).trim() + + '\n' + + '---\n' + + '\n' + + '\n' + }], { overwrite: false }).then(function (r) { + var err + ; + + if (r.error || r.errors.length) { + err = r.error || r.errors[0]; + if (/exists/i.test(err.message)) { + console.error(''); + console.error("Looks like that post already exists. Try a different name?"); + console.error(''); + console.error(''); + } else { + throw err; + } + + return; + } + + console.log(''); + console.log(displaypath); + console.log(''); + console.log('vim ' + displaypath); + console.log('(or emacs ' + displaypath + ', if you swing that way)'); + console.log(''); + console.log(''); + }); + /* + }); + */ + }); +} + cli.main(function (args, options) { var command = args[0] - , blogdir = options.blog + , blogdir = options.blogdir + , originalDir = blogdir ; if (!blogdir) { blogdir = path.resolve('./'); + originalDir = './'; } - if (!fs.existsSync(path.join(options.blog, 'site.yml'))) { - console.error("Usage: deardesi [serve|init|post] -b ~/path/to/blog"); + if (!fs.existsSync(path.join(options.blogdir, 'site.yml'))) { + console.error("Usage: desi [serve|init|post] -d ~/path/to/blog"); console.error("(if ~/path/to/blog doesn't yet exist or doesn't have config.yml, site.yml, etc, " - + "try `deardesi init -b ~/path/to/blog'"); + + "try `deardesi init -d ~/path/to/blog'"); process.exit(1); return; } @@ -74,8 +178,7 @@ cli.main(function (args, options) { return; } else if ('post' === command) { - console.error("`post' not yet implemented"); - process.exit(1); + createPost(originalDir, blogdir, args[1], args[2]); return; } else if ('serve' === command) { @@ -83,7 +186,7 @@ cli.main(function (args, options) { return; } else { - console.error("Usage: deardesi [serve|init|post] -b ~/path/to/blog"); + console.error("Usage: desi [serve|init|post] -d ~/path/to/blog"); return; } }); diff --git a/package.json b/package.json index 7318a14..c808610 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "bin": { - "deardesi": "bin/deardesi.js" - , "desi": "bin/deardesi.js" + "deardesi": "bin/deardesi.js", + "desi": "bin/deardesi.js" }, "repository": { "type": "git", @@ -44,8 +44,9 @@ "connect": "^3.3.4", "connect-query": "^0.2.0", "connect-send-json": "^1.0.0", - "desirae": "^0.9.1", + "desirae": "^0.9.2", "fs.extra": "^1.3.0", + "node-uuid": "^1.4.2", "require-yaml": "0.0.1", "serve-static": "^1.8.0" } diff --git a/views/about/about.html b/views/about/about.html index caf5ac4..dc3b7c9 100644 --- a/views/about/about.html +++ b/views/about/about.html @@ -17,7 +17,8 @@

- + +
diff --git a/views/post/post.js b/views/post/post.js index 8262b98..f5b3b69 100644 --- a/views/post/post.js +++ b/views/post/post.js @@ -9,13 +9,13 @@ angular.module('myApp.post', ['ngRoute']) }]) .controller('PostCtrl' - , ['$scope', '$location', '$timeout', 'Desirae' - , function ($scope, $location, $timeout, Desirae) { + , ['$scope', '$location', '$timeout', 'DesiraeService' + , function ($scope, $location, $timeout, DesiraeService) { var scope = this ; function init() { - Desirae.meta().then(function (desi) { + DesiraeService.meta().then(function (desi) { console.warn(desi); scope.blogdir = desi.blogdir.path.replace(/^\/(Users|home)\/[^\/]+\//, '~/'); scope.site = desi.site; @@ -41,7 +41,7 @@ angular.module('myApp.post', ['ngRoute']) yml: { title: "" , permalink: "/article/new.html" - , date: Desirae.toDesiDate(new Date())// "YYYY-MM-DD HH:MM pm" // TODO desirae + , date: DesiraeService.toDesiDate(new Date())// "YYYY-MM-DD HH:MM pm" // TODO desirae , updated: null , description: "" , categories: [] @@ -128,7 +128,7 @@ angular.module('myApp.post', ['ngRoute']) $timeout.cancel(scope.dtlock); scope.dtlock = $timeout(function () { if (scope.selected && scope.selected.date === scope.selected.post.yml.date) { - scope.selected.date = scope.selected.post.yml.date = Desirae.toDesiDate(new Date()); + scope.selected.date = scope.selected.post.yml.date = DesiraeService.toDesiDate(new Date()); } scope.onChange(); updateDate(); @@ -163,7 +163,7 @@ angular.module('myApp.post', ['ngRoute']) + scope.selected.post.body.trim() }); - Desirae.putFiles(files).then(function (results) { + DesiraeService.putFiles(files).then(function (results) { console.log('TODO check for error'); console.log(files); console.log(results);