eleventy-generate-posts 0.0.4 → 0.0.5
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 +6 -0
- package/eleventy-generate-posts.js +175 -167
- package/package.json +3 -2
- package/readme.md +5 -0
package/changelog.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 20240109 (v0.0.5)
|
|
4
|
+
|
|
5
|
+
Added `-g` parameter used to append generator content to the end of generated posts:
|
|
6
|
+
|
|
7
|
+
> Post content generated by [Eleventy Generate Posts](https://www.npmjs.com/package/eleventy-generate-posts).
|
|
8
|
+
|
|
3
9
|
## 20230709 - version 0.0.4
|
|
4
10
|
|
|
5
11
|
Made number of posts the first prompt, it seemed better this way.
|
|
@@ -1,167 +1,175 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import fs from 'fs-extra';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import boxen from 'boxen';
|
|
5
|
-
import chalk from 'chalk';
|
|
6
|
-
import prompts from 'prompts';
|
|
7
|
-
import YAML from 'yaml';
|
|
8
|
-
import logger from 'cli-logger';
|
|
9
|
-
var log = logger();
|
|
10
|
-
var HighlightType;
|
|
11
|
-
(function (HighlightType) {
|
|
12
|
-
HighlightType[HighlightType["Red"] = 0] = "Red";
|
|
13
|
-
HighlightType[HighlightType["Yellow"] = 1] = "Yellow";
|
|
14
|
-
HighlightType[HighlightType["Green"] = 2] = "Green";
|
|
15
|
-
})(HighlightType || (HighlightType = {}));
|
|
16
|
-
const APP_NAME = '11ty Generate Posts';
|
|
17
|
-
const APP_AUTHOR = 'by John M. Wargo (https://johnwargo.com)\n';
|
|
18
|
-
const ELEVENTY_FILES = ['.eleventy.js', 'eleventy.config.js'];
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
var
|
|
26
|
-
var
|
|
27
|
-
var
|
|
28
|
-
var
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
console.log(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
initial: '
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
message: '
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
writeConsole(yellow, '
|
|
124
|
-
if (!
|
|
125
|
-
writeConsole(red, 'Error', '
|
|
126
|
-
process.exit(1);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
log.debug(
|
|
142
|
-
|
|
143
|
-
let
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
log.debug(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import boxen from 'boxen';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
import prompts from 'prompts';
|
|
7
|
+
import YAML from 'yaml';
|
|
8
|
+
import logger from 'cli-logger';
|
|
9
|
+
var log = logger();
|
|
10
|
+
var HighlightType;
|
|
11
|
+
(function (HighlightType) {
|
|
12
|
+
HighlightType[HighlightType["Red"] = 0] = "Red";
|
|
13
|
+
HighlightType[HighlightType["Yellow"] = 1] = "Yellow";
|
|
14
|
+
HighlightType[HighlightType["Green"] = 2] = "Green";
|
|
15
|
+
})(HighlightType || (HighlightType = {}));
|
|
16
|
+
const APP_NAME = '11ty Generate Posts';
|
|
17
|
+
const APP_AUTHOR = 'by John M. Wargo (https://johnwargo.com)\n';
|
|
18
|
+
const ELEVENTY_FILES = ['.eleventy.js', 'eleventy.config.js'];
|
|
19
|
+
const GENERATOR_CONTENT = '***\n\nPost content generated by [Eleventy Generate Posts](https://www.npmjs.com/package/eleventy-generate-posts)';
|
|
20
|
+
const NEW_LINE = "\n";
|
|
21
|
+
const spaces40 = '-'.repeat(40);
|
|
22
|
+
const red = HighlightType.Red;
|
|
23
|
+
const yellow = HighlightType.Yellow;
|
|
24
|
+
const green = HighlightType.Green;
|
|
25
|
+
var numPosts;
|
|
26
|
+
var startYear;
|
|
27
|
+
var tag;
|
|
28
|
+
var targetFolder;
|
|
29
|
+
var yearMode;
|
|
30
|
+
function zeroPad(tmpVal, numChars = 2) {
|
|
31
|
+
return tmpVal.toString().padStart(numChars, '0');
|
|
32
|
+
}
|
|
33
|
+
function directoryExists(filePath) {
|
|
34
|
+
if (fs.existsSync(filePath)) {
|
|
35
|
+
try {
|
|
36
|
+
return fs.lstatSync(filePath).isDirectory();
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
log.error(`checkDirectory error: ${err}`);
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
function writeConsole(color, highlightText, msg) {
|
|
46
|
+
if (color == HighlightType.Red)
|
|
47
|
+
console.log(NEW_LINE + chalk.red(`${highlightText}: `) + msg + NEW_LINE);
|
|
48
|
+
if (color == HighlightType.Yellow)
|
|
49
|
+
console.log(chalk.yellow(`${highlightText}: `) + msg);
|
|
50
|
+
if (color == HighlightType.Green)
|
|
51
|
+
console.log(chalk.green(`${highlightText}: `) + msg);
|
|
52
|
+
}
|
|
53
|
+
function getRandomInt(max) {
|
|
54
|
+
return Math.floor(Math.random() * max) + 1;
|
|
55
|
+
}
|
|
56
|
+
function checkEleventyProject() {
|
|
57
|
+
log.debug('Validating project folder');
|
|
58
|
+
let result = false;
|
|
59
|
+
ELEVENTY_FILES.forEach((file) => {
|
|
60
|
+
let tmpFile = path.join(process.cwd(), file);
|
|
61
|
+
if (fs.existsSync(tmpFile)) {
|
|
62
|
+
result = true;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
if (!checkEleventyProject()) {
|
|
68
|
+
log.error('Current folder is not an Eleventy project folder.');
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
console.log(boxen(APP_NAME, { padding: 1 }));
|
|
72
|
+
console.log(APP_AUTHOR);
|
|
73
|
+
console.log('Add -d to command to enable debug mode, -g to append generator info to post files.\n');
|
|
74
|
+
const debugMode = process.argv.includes('-d');
|
|
75
|
+
if (debugMode) {
|
|
76
|
+
writeConsole(green, 'Debug mode', 'enabled\n');
|
|
77
|
+
}
|
|
78
|
+
log.level(debugMode ? log.DEBUG : log.INFO);
|
|
79
|
+
const generatorInfo = process.argv.includes('-g');
|
|
80
|
+
if (generatorInfo) {
|
|
81
|
+
writeConsole(green, 'Generator info', 'will be added to post files\n');
|
|
82
|
+
}
|
|
83
|
+
const questions = [
|
|
84
|
+
{
|
|
85
|
+
type: 'number',
|
|
86
|
+
name: 'numPosts',
|
|
87
|
+
initial: 10,
|
|
88
|
+
message: 'Number of posts to generate?'
|
|
89
|
+
}, {
|
|
90
|
+
type: 'text',
|
|
91
|
+
name: 'targetFolder',
|
|
92
|
+
initial: 'src/posts',
|
|
93
|
+
message: 'Target folder for generated posts?'
|
|
94
|
+
}, {
|
|
95
|
+
type: 'text',
|
|
96
|
+
name: 'tag',
|
|
97
|
+
message: 'Post tag?',
|
|
98
|
+
initial: 'post'
|
|
99
|
+
}, {
|
|
100
|
+
type: 'number',
|
|
101
|
+
name: 'startYear',
|
|
102
|
+
initial: new Date().getFullYear(),
|
|
103
|
+
message: 'Start year for generated posts?'
|
|
104
|
+
}, {
|
|
105
|
+
type: 'confirm',
|
|
106
|
+
name: 'yearMode',
|
|
107
|
+
initial: true,
|
|
108
|
+
message: 'Use year folder for posts?'
|
|
109
|
+
},
|
|
110
|
+
];
|
|
111
|
+
const response = await prompts(questions);
|
|
112
|
+
targetFolder = response.targetFolder;
|
|
113
|
+
numPosts = response.numPosts;
|
|
114
|
+
startYear = response.startYear;
|
|
115
|
+
tag = response.tag;
|
|
116
|
+
yearMode = response.yearMode;
|
|
117
|
+
console.log('\nSettings Summary:');
|
|
118
|
+
console.log(spaces40);
|
|
119
|
+
writeConsole(yellow, 'Number of posts', numPosts.toString());
|
|
120
|
+
writeConsole(yellow, 'Target Folder', targetFolder);
|
|
121
|
+
writeConsole(yellow, 'Start Year', startYear.toString());
|
|
122
|
+
writeConsole(yellow, 'Tag', tag);
|
|
123
|
+
writeConsole(yellow, 'Year mode', yearMode ? 'enabled' : 'disabled');
|
|
124
|
+
if (!(numPosts > 0 && numPosts < 101)) {
|
|
125
|
+
writeConsole(red, 'Error', 'Number of posts must be between 1 and 100');
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
var outputFilePath = path.join(process.cwd(), targetFolder);
|
|
129
|
+
writeConsole(yellow, 'Output folder', outputFilePath);
|
|
130
|
+
if (!directoryExists(outputFilePath)) {
|
|
131
|
+
writeConsole(red, 'Error', 'Output folder does not exist');
|
|
132
|
+
process.exit(1);
|
|
133
|
+
}
|
|
134
|
+
console.log('\nGenerating posts...');
|
|
135
|
+
console.log(spaces40);
|
|
136
|
+
var currentDate = new Date();
|
|
137
|
+
if (startYear)
|
|
138
|
+
currentDate.setFullYear(startYear);
|
|
139
|
+
numPosts++;
|
|
140
|
+
for (let i = 1; i < numPosts; i++) {
|
|
141
|
+
log.debug('\nGetting random words (this may take a few seconds)');
|
|
142
|
+
let wordCount = getRandomInt(4) + 3;
|
|
143
|
+
let letTitleRes = await fetch(`https://random-word-api.vercel.app/api?words=${wordCount}`);
|
|
144
|
+
let titleWords = await letTitleRes.json();
|
|
145
|
+
titleWords = titleWords.map((a) => a.charAt(0).toUpperCase() + a.substr(1));
|
|
146
|
+
let postTitle = titleWords.join(' ');
|
|
147
|
+
log.debug(`Post title: ${postTitle}`);
|
|
148
|
+
currentDate.setDate(currentDate.getDate() - getRandomInt(20));
|
|
149
|
+
let postDate = `${currentDate.getFullYear()}-${zeroPad(currentDate.getMonth() + 1)}-${zeroPad(currentDate.getDate())}`;
|
|
150
|
+
log.debug(`Post date: ${postDate}`);
|
|
151
|
+
var postFm = {
|
|
152
|
+
title: postTitle,
|
|
153
|
+
date: postDate,
|
|
154
|
+
tags: tag
|
|
155
|
+
};
|
|
156
|
+
log.debug('Getting bacon ipsum text (this may take a few seconds)...');
|
|
157
|
+
let response = await fetch(`https://baconipsum.com/api/?type=all-meat¶s=${getRandomInt(10)}&start-with-lorem=1`);
|
|
158
|
+
let postContent = await response.json();
|
|
159
|
+
log.debug(`Post content: ${postContent}`);
|
|
160
|
+
if (generatorInfo)
|
|
161
|
+
postContent.push(GENERATOR_CONTENT);
|
|
162
|
+
var thePost = '---\n';
|
|
163
|
+
thePost += YAML.stringify(postFm, { logLevel: 'silent' });
|
|
164
|
+
thePost += '---\n\n';
|
|
165
|
+
thePost += postContent.join('\n\n');
|
|
166
|
+
var outputFilePath = path.join(process.cwd(), targetFolder);
|
|
167
|
+
if (yearMode) {
|
|
168
|
+
outputFilePath = path.join(outputFilePath, currentDate.getFullYear().toString());
|
|
169
|
+
if (!fs.existsSync(outputFilePath))
|
|
170
|
+
fs.mkdirSync(outputFilePath, { recursive: true });
|
|
171
|
+
}
|
|
172
|
+
var outputFilePath = path.join(outputFilePath, postTitle.toLowerCase().replaceAll(' ', '-') + '.md');
|
|
173
|
+
writeConsole(green, 'Writing', outputFilePath);
|
|
174
|
+
fs.writeFileSync(outputFilePath, thePost, 'utf8');
|
|
175
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eleventy-generate-posts",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Generates batches of Eleventy posts",
|
|
5
5
|
"author": "John M. Wargo",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,7 +19,8 @@
|
|
|
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
|
+
"startg": "tsc && node eleventy-generate-posts.js -g"
|
|
23
24
|
},
|
|
24
25
|
"dependencies": {
|
|
25
26
|
"boxen": "^7.0.2",
|
package/readme.md
CHANGED
|
@@ -79,6 +79,11 @@ Obviously if you generate enough posts to push into the previous year, the posts
|
|
|
79
79
|
|
|
80
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
81
|
|
|
82
|
+
Add a reference to this module at the bottom of all generated posts using the `-g` command-line parameter. I added this feature primarily for my personal use so I can advertise the module when I use it. Here's an example of the added content:
|
|
83
|
+
|
|
84
|
+
> ***
|
|
85
|
+
> Post content generated by [Eleventy Generate Posts](https://www.npmjs.com/package/eleventy-generate-posts)
|
|
86
|
+
|
|
82
87
|
## Example Post
|
|
83
88
|
|
|
84
89
|
A sample generated post looks like the following:
|