eleventy-generate-posts 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +8 -0
- package/eleventy-generate-posts.js +104 -81
- package/package.json +3 -2
- package/pub.mjs +5 -0
- package/readme.md +50 -34
package/changelog.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 20230709 - version 0.0.4
|
|
4
|
+
|
|
5
|
+
Made number of posts the first prompt, it seemed better this way.
|
|
6
|
+
|
|
7
|
+
## 20230701 - version 0.0.3
|
|
8
|
+
|
|
9
|
+
Replaced command line options with interactive prompts
|
|
10
|
+
|
|
3
11
|
## 20230516
|
|
4
12
|
|
|
5
13
|
Added support for the `-y` flag which saves generated posts to a subfolder for the current year.
|
|
@@ -3,7 +3,7 @@ import fs from 'fs-extra';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import boxen from 'boxen';
|
|
5
5
|
import chalk from 'chalk';
|
|
6
|
-
import
|
|
6
|
+
import prompts from 'prompts';
|
|
7
7
|
import YAML from 'yaml';
|
|
8
8
|
import logger from 'cli-logger';
|
|
9
9
|
var log = logger();
|
|
@@ -14,12 +14,18 @@ var HighlightType;
|
|
|
14
14
|
HighlightType[HighlightType["Green"] = 2] = "Green";
|
|
15
15
|
})(HighlightType || (HighlightType = {}));
|
|
16
16
|
const APP_NAME = '11ty Generate Posts';
|
|
17
|
-
const APP_AUTHOR = 'by John M. Wargo (https://johnwargo.com)';
|
|
17
|
+
const APP_AUTHOR = 'by John M. Wargo (https://johnwargo.com)\n';
|
|
18
18
|
const ELEVENTY_FILES = ['.eleventy.js', 'eleventy.config.js'];
|
|
19
19
|
const NEW_LINE = "\n";
|
|
20
|
+
const spaces40 = '-'.repeat(40);
|
|
20
21
|
const red = HighlightType.Red;
|
|
21
22
|
const yellow = HighlightType.Yellow;
|
|
22
23
|
const green = HighlightType.Green;
|
|
24
|
+
var numPosts;
|
|
25
|
+
var startYear;
|
|
26
|
+
var tag;
|
|
27
|
+
var targetFolder;
|
|
28
|
+
var yearMode;
|
|
23
29
|
function zeroPad(tmpVal, numChars = 2) {
|
|
24
30
|
return tmpVal.toString().padStart(numChars, '0');
|
|
25
31
|
}
|
|
@@ -61,84 +67,101 @@ if (!checkEleventyProject()) {
|
|
|
61
67
|
log.error('Current folder is not an Eleventy project folder.');
|
|
62
68
|
process.exit(1);
|
|
63
69
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
70
|
+
console.log(boxen(APP_NAME, { padding: 1 }));
|
|
71
|
+
console.log(APP_AUTHOR);
|
|
72
|
+
const debugMode = process.argv.includes('-d');
|
|
73
|
+
if (debugMode) {
|
|
74
|
+
writeConsole(green, 'Debug mode', 'enabled\n');
|
|
75
|
+
}
|
|
76
|
+
log.level(debugMode ? log.DEBUG : log.INFO);
|
|
77
|
+
const questions = [
|
|
78
|
+
{
|
|
79
|
+
type: 'number',
|
|
80
|
+
name: 'numPosts',
|
|
81
|
+
initial: 10,
|
|
82
|
+
message: 'Number of posts to generate?'
|
|
83
|
+
}, {
|
|
84
|
+
type: 'text',
|
|
85
|
+
name: 'targetFolder',
|
|
86
|
+
initial: 'src/posts',
|
|
87
|
+
message: 'Target folder for generated posts?'
|
|
88
|
+
}, {
|
|
89
|
+
type: 'text',
|
|
90
|
+
name: 'tag',
|
|
91
|
+
message: 'Post tag?',
|
|
92
|
+
initial: 'post'
|
|
93
|
+
}, {
|
|
94
|
+
type: 'number',
|
|
95
|
+
name: 'startYear',
|
|
96
|
+
initial: new Date().getFullYear(),
|
|
97
|
+
message: 'Start year for generated posts?'
|
|
98
|
+
}, {
|
|
99
|
+
type: 'confirm',
|
|
100
|
+
name: 'yearMode',
|
|
101
|
+
initial: true,
|
|
102
|
+
message: 'Use year folder for posts?'
|
|
103
|
+
},
|
|
104
|
+
];
|
|
105
|
+
const response = await prompts(questions);
|
|
106
|
+
targetFolder = response.targetFolder;
|
|
107
|
+
numPosts = response.numPosts;
|
|
108
|
+
startYear = response.startYear;
|
|
109
|
+
tag = response.tag;
|
|
110
|
+
yearMode = response.yearMode;
|
|
111
|
+
console.log('\nSettings Summary:');
|
|
112
|
+
console.log(spaces40);
|
|
113
|
+
writeConsole(yellow, 'Number of posts', numPosts.toString());
|
|
114
|
+
writeConsole(yellow, 'Target Folder', targetFolder);
|
|
115
|
+
writeConsole(yellow, 'Start Year', startYear.toString());
|
|
116
|
+
writeConsole(yellow, 'Tag', tag);
|
|
117
|
+
writeConsole(yellow, 'Year mode', yearMode ? 'enabled' : 'disabled');
|
|
118
|
+
if (!(numPosts > 0 && numPosts < 101)) {
|
|
119
|
+
writeConsole(red, 'Error', 'Number of posts must be between 1 and 100');
|
|
120
|
+
process.exit(1);
|
|
121
|
+
}
|
|
122
|
+
var outputFilePath = path.join(process.cwd(), targetFolder);
|
|
123
|
+
writeConsole(yellow, 'Output folder', outputFilePath);
|
|
124
|
+
if (!directoryExists(outputFilePath)) {
|
|
125
|
+
writeConsole(red, 'Error', 'Output folder does not exist');
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
console.log('\nGenerating posts...');
|
|
129
|
+
console.log(spaces40);
|
|
130
|
+
var currentDate = new Date();
|
|
131
|
+
if (startYear)
|
|
132
|
+
currentDate.setFullYear(startYear);
|
|
133
|
+
numPosts++;
|
|
134
|
+
for (let i = 1; i < numPosts; i++) {
|
|
135
|
+
log.debug('\nGetting random words (this may take a few seconds)');
|
|
136
|
+
let wordCount = getRandomInt(4) + 3;
|
|
137
|
+
let letTitleRes = await fetch(`https://random-word-api.vercel.app/api?words=${wordCount}`);
|
|
138
|
+
let titleWords = await letTitleRes.json();
|
|
139
|
+
titleWords = titleWords.map((a) => a.charAt(0).toUpperCase() + a.substr(1));
|
|
140
|
+
let postTitle = titleWords.join(' ');
|
|
141
|
+
log.debug(`Post title: ${postTitle}`);
|
|
142
|
+
currentDate.setDate(currentDate.getDate() - getRandomInt(20));
|
|
143
|
+
let postDate = `${currentDate.getFullYear()}-${zeroPad(currentDate.getMonth() + 1)}-${zeroPad(currentDate.getDate())}`;
|
|
144
|
+
log.debug(`Post date: ${postDate}`);
|
|
145
|
+
var postFm = {
|
|
146
|
+
title: postTitle,
|
|
147
|
+
date: postDate,
|
|
148
|
+
tags: tag
|
|
149
|
+
};
|
|
150
|
+
log.debug('Getting bacon ipsum text (this may take a few seconds)...');
|
|
151
|
+
let response = await fetch(`https://baconipsum.com/api/?type=all-meat¶s=${getRandomInt(10)}&start-with-lorem=1`);
|
|
152
|
+
let postContent = await response.json();
|
|
153
|
+
log.debug(`Post content: ${postContent}`);
|
|
154
|
+
var thePost = '---\n';
|
|
155
|
+
thePost += YAML.stringify(postFm, { logLevel: 'silent' });
|
|
156
|
+
thePost += '---\n\n';
|
|
157
|
+
thePost += postContent.join('\n\n');
|
|
98
158
|
var outputFilePath = path.join(process.cwd(), targetFolder);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
159
|
+
if (yearMode) {
|
|
160
|
+
outputFilePath = path.join(outputFilePath, currentDate.getFullYear().toString());
|
|
161
|
+
if (!fs.existsSync(outputFilePath))
|
|
162
|
+
fs.mkdirSync(outputFilePath, { recursive: true });
|
|
103
163
|
}
|
|
104
|
-
var
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
for (let i = 1; i < numPosts; i++) {
|
|
109
|
-
log.debug('\nGetting random words (this may take a few seconds)');
|
|
110
|
-
let wordCount = getRandomInt(4) + 3;
|
|
111
|
-
let letTitleRes = await fetch(`https://random-word-api.vercel.app/api?words=${wordCount}`);
|
|
112
|
-
let titleWords = await letTitleRes.json();
|
|
113
|
-
titleWords = titleWords.map((a) => a.charAt(0).toUpperCase() + a.substr(1));
|
|
114
|
-
let postTitle = titleWords.join(' ');
|
|
115
|
-
log.debug(`Post title: ${postTitle}`);
|
|
116
|
-
currentDate.setDate(currentDate.getDate() - getRandomInt(20));
|
|
117
|
-
let postDate = `${currentDate.getFullYear()}-${zeroPad(currentDate.getMonth() + 1)}-${zeroPad(currentDate.getDate())}`;
|
|
118
|
-
log.debug(`Post date: ${postDate}`);
|
|
119
|
-
var postFm = {
|
|
120
|
-
title: postTitle,
|
|
121
|
-
date: postDate,
|
|
122
|
-
tags: tag
|
|
123
|
-
};
|
|
124
|
-
log.debug('Getting bacon ipsum text (this may take a few seconds)...');
|
|
125
|
-
let response = await fetch(`https://baconipsum.com/api/?type=all-meat¶s=${getRandomInt(10)}&start-with-lorem=1`);
|
|
126
|
-
let postContent = await response.json();
|
|
127
|
-
log.debug(`Post content: ${postContent}`);
|
|
128
|
-
var thePost = '---\n';
|
|
129
|
-
thePost += YAML.stringify(postFm, { logLevel: 'silent' });
|
|
130
|
-
thePost += '---\n\n';
|
|
131
|
-
thePost += postContent.join('\n\n');
|
|
132
|
-
var outputFilePath = path.join(process.cwd(), targetFolder);
|
|
133
|
-
if (yearMode) {
|
|
134
|
-
outputFilePath = path.join(outputFilePath, currentDate.getFullYear().toString());
|
|
135
|
-
if (!fs.existsSync(outputFilePath))
|
|
136
|
-
fs.mkdirSync(outputFilePath, { recursive: true });
|
|
137
|
-
}
|
|
138
|
-
var outputFilePath = path.join(outputFilePath, postTitle.toLowerCase().replaceAll(' ', '-') + '.md');
|
|
139
|
-
writeConsole(green, 'Writing', outputFilePath);
|
|
140
|
-
fs.writeFileSync(outputFilePath, thePost, 'utf8');
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
console.log();
|
|
144
|
-
program.parse();
|
|
164
|
+
var outputFilePath = path.join(outputFilePath, postTitle.toLowerCase().replaceAll(' ', '-') + '.md');
|
|
165
|
+
writeConsole(green, 'Writing', outputFilePath);
|
|
166
|
+
fs.writeFileSync(outputFilePath, thePost, 'utf8');
|
|
167
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eleventy-generate-posts",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Generates batches of Eleventy posts",
|
|
5
5
|
"author": "John M. Wargo",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
21
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
22
|
-
"start": "tsc && node eleventy-generate-posts.js
|
|
22
|
+
"start": "tsc && node eleventy-generate-posts.js"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"boxen": "^7.0.2",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"cli-logger": "^0.5.40",
|
|
28
28
|
"commander": "^10.0.1",
|
|
29
29
|
"fs-extra": "^11.1.1",
|
|
30
|
+
"prompts": "^2.4.2",
|
|
30
31
|
"yaml": "^2.2.2"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
package/pub.mjs
ADDED
package/readme.md
CHANGED
|
@@ -6,6 +6,8 @@ When you're testing out some aspect of an Eleventy site or doing a demo, you oft
|
|
|
6
6
|
|
|
7
7
|
The utility generates batches of Eleventy site posts, populating the post title with a random quantity of random words using the [Rando Free Random Word Generator API](https://random-word-api.vercel.app/) and populating the post body with a random number of paragraphs of [Bacon Ipsum](https://baconipsum.com/) text.
|
|
8
8
|
|
|
9
|
+
The original version of this utility used command-line arguments, but this version replaces them with prompts.
|
|
10
|
+
|
|
9
11
|
## Installation
|
|
10
12
|
|
|
11
13
|
To install the command globally on the system, open a terminal window or command prompt, then execute the following command:
|
|
@@ -16,53 +18,67 @@ npm install -g eleventy-generate-posts
|
|
|
16
18
|
|
|
17
19
|
This adds a `11ty-gp` command to the system.
|
|
18
20
|
|
|
19
|
-
You don't have to install the package to use it; simply open a terminal window or command prompt to your Eleventy project folder and execute the command using `npx eleventy-generate-posts` and
|
|
21
|
+
You don't have to install the package to use it; simply open a terminal window or command prompt to your Eleventy project folder and execute the command using `npx eleventy-generate-posts` and it will execute without being installed.
|
|
20
22
|
|
|
21
23
|
## Usage
|
|
22
24
|
|
|
23
|
-
Execute the command using the following command
|
|
24
|
-
|
|
25
|
-
```shell
|
|
26
|
-
11ty-gp [options] <numPosts> <targetFolder> <tag> [startYear]
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
Supported command-line parameters are:
|
|
30
|
-
|
|
31
|
-
* `numPosts` (required) An integer value representing the number of posts generated.
|
|
32
|
-
* `targetFolder` (required) Relative path pointing to the Eleventy project's posts folder; use `.` for the current folder.
|
|
33
|
-
* `tag` (required) The post tag applied to the generated posts
|
|
34
|
-
* `startYear` (optional) The starting year used for post date in the generated posts. The command uses the current date or the current date with the specified year (when provided) to for the post date for the first generated post. For subsequent post dates, the command randomly decrements the day.
|
|
35
|
-
|
|
36
|
-
Supported command-line options (flags) are:
|
|
37
|
-
|
|
38
|
-
* `-d` or `--debug`: Enables debug mode which generates additional content to the terminal during execution
|
|
39
|
-
* `-h` or `--help`: Displays usage information in the terminal
|
|
40
|
-
* `-y` or `--year`: instructs the command to store posts to a subfolder for the post date year.
|
|
41
|
-
|
|
42
|
-
As an example, to generate 10 posts in the project's `posts` folder using a `tags` value of `post`, use:
|
|
25
|
+
Execute the command using the following command:
|
|
43
26
|
|
|
44
27
|
```shell
|
|
45
|
-
11ty-gp
|
|
28
|
+
11ty-gp
|
|
46
29
|
```
|
|
47
30
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
11ty
|
|
31
|
+
The module will prompt you for all the required options:
|
|
32
|
+
|
|
33
|
+
```text
|
|
34
|
+
┌─────────────────────────┐
|
|
35
|
+
│ │
|
|
36
|
+
│ 11ty Generate Posts │
|
|
37
|
+
│ │
|
|
38
|
+
└─────────────────────────┘
|
|
39
|
+
by John M. Wargo (https://johnwargo.com)
|
|
40
|
+
|
|
41
|
+
√ Number of posts to generate? ... 10
|
|
42
|
+
√ Target folder for generated posts? ... posts
|
|
43
|
+
√ Post tag? ... post
|
|
44
|
+
√ Start year for generated posts? ... 2023
|
|
45
|
+
√ Use year folder for posts? ... yes
|
|
46
|
+
|
|
47
|
+
Settings Summary:
|
|
48
|
+
----------------------------------------
|
|
49
|
+
Number of posts: 10
|
|
50
|
+
Target Folder: posts
|
|
51
|
+
Start Year: 2023
|
|
52
|
+
Tag: post
|
|
53
|
+
Year mode: enabled
|
|
54
|
+
Output folder: D:\dev\node\11ty-generate-posts\posts
|
|
55
|
+
|
|
56
|
+
Generating posts...
|
|
57
|
+
----------------------------------------
|
|
58
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\navigator-washbasin-dramatize-landside-sensation.md
|
|
59
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\amusing-peculiar-surgical-borough-impotency-surround-tubular.md
|
|
60
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\maturely-convene-squishier-verify.md
|
|
61
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\matchless-overlaid-expend-oxidation-tribesman.md
|
|
62
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\basis-brunt-swaddling-ladylike-support-epidemic-graded.md
|
|
63
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\certify-unsheathe-undress-obstacle-tweak-tray-ridden.md
|
|
64
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\matching-enjoying-contact-atlas.md
|
|
65
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\android-gecko-penalize-possum.md
|
|
66
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\contempt-acquire-filtrate-defense-ergonomic-acts.md
|
|
67
|
+
Writing: D:\dev\node\11ty-generate-posts\posts\2023\splinter-ecology-computer-nearby-shorts-feminize.md
|
|
54
68
|
```
|
|
55
69
|
|
|
56
|
-
|
|
70
|
+
Configuration options are:
|
|
57
71
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
72
|
+
* **Number of Posts:** An integer value between 1 and 100 representing the number of posts generated.
|
|
73
|
+
* **Posts Folder:** Relative path pointing to the Eleventy project's posts folder; use `.` for the current folder.
|
|
74
|
+
* **Post Tag:** The front matter `tag` property applied to the generated posts
|
|
75
|
+
* **Start Year:** The starting year used for post date in the generated posts. The command uses the current date or the current date with the specified year (when provided) to for the post date for the first generated post. For subsequent post dates, the command randomly decrements the day.
|
|
76
|
+
* **Use Year Folder:** specifies whether the module writes generated posts to the Posts folder (`N`) or into a separate folder for each year (`Y`).
|
|
63
77
|
|
|
64
78
|
Obviously if you generate enough posts to push into the previous year, the posts will save into a folder for the previous year.
|
|
65
79
|
|
|
80
|
+
To enable debug mode, pass a `-d` flag on the command-line; in this mode, the module writes additional information to the console as it executes.
|
|
81
|
+
|
|
66
82
|
## Example Post
|
|
67
83
|
|
|
68
84
|
A sample generated post looks like the following:
|