From e4cc61b696151c9cf4941632a296824374a08955 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sat, 29 Sep 2018 23:05:14 +0200 Subject: [PATCH] new post script --- README.md | 2 +- package.json | 1 + scripts/new.js | 18 ++++++++++-------- scripts/new.md | 4 +--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1ea33f9c..2eca06a0 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ npm run format:css ### 🎈 Add a new project ```bash -npm run new -- "Hello" +npm run new "Hello" ``` ... diff --git a/package.json b/package.json index 7ed28edd..7c3d235f 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "eslint-plugin-react": "^7.11.1", "markdownlint": "^0.11.0", "markdownlint-cli": "^0.13.0", + "ora": "^3.0.0", "prettier": "^1.14.3", "prettier-stylelint": "^0.4.2", "stylelint": "^9.6.0", diff --git a/scripts/new.js b/scripts/new.js index 3bd4079c..e8014a7f 100644 --- a/scripts/new.js +++ b/scripts/new.js @@ -1,6 +1,5 @@ import fs from 'fs' import path from 'path' -import prepend from 'prepend' import slugify from 'slugify' import ora from 'ora' @@ -10,21 +9,24 @@ const template = fs.readFileSync(templatePath).toString() const spinner = ora('Adding new project').start() if (!process.argv[2]) { - spinner.fail('Use the format `npm run new -- Title of post`') + spinner.fail('Use the format `npm run new "Title of post"`') } const title = process.argv[2] spinner.text = `Adding '${title}'.` const titleSlug = slugify(title, { lower: true }) -const postsPath = path.join(__dirname, 'content', 'posts') +const postsPath = path.join('.', 'content', 'posts') +const date = new Date().toISOString() const newContents = template .split('TITLE') .join(title) .split('DATE') - .join(Date.now()) + .join(date) -// prepend(projects, newContents, error => { -// if (error) spinner.fail(error) -// spinner.succeed(`Added '${title}' to top of projects.yml file.`) -// }) +const datePath = date.slice(0, 10) + +fs.appendFile(`${postsPath}/${datePath}-${titleSlug}.md`, newContents, err => { + if (err) spinner.fail(`Error creating post: ${err}`) + spinner.succeed(`New post '${title}' created.`) +}) diff --git a/scripts/new.md b/scripts/new.md index c4d993ed..6cd4a9d8 100644 --- a/scripts/new.md +++ b/scripts/new.md @@ -1,10 +1,8 @@ --- type: post +date: DATE title: TITLE -author: Matthias Kretschmann - -date: DATE tags: - tag