eleventy-generate-posts 0.0.3 → 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 +5 -1
- package/eleventy-generate-posts.js +167 -171
- package/package.json +1 -1
- package/readme.md +3 -3
package/changelog.md
CHANGED
|
@@ -1,171 +1,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 NEW_LINE = "\n";
|
|
20
|
-
const spaces40 = '-'.repeat(40);
|
|
21
|
-
const red = HighlightType.Red;
|
|
22
|
-
const yellow = HighlightType.Yellow;
|
|
23
|
-
const green = HighlightType.Green;
|
|
24
|
-
var numPosts;
|
|
25
|
-
var startYear;
|
|
26
|
-
var tag;
|
|
27
|
-
var targetFolder;
|
|
28
|
-
var yearMode;
|
|
29
|
-
function zeroPad(tmpVal, numChars = 2) {
|
|
30
|
-
return tmpVal.toString().padStart(numChars, '0');
|
|
31
|
-
}
|
|
32
|
-
function directoryExists(filePath) {
|
|
33
|
-
if (fs.existsSync(filePath)) {
|
|
34
|
-
try {
|
|
35
|
-
return fs.lstatSync(filePath).isDirectory();
|
|
36
|
-
}
|
|
37
|
-
catch (err) {
|
|
38
|
-
log.error(`checkDirectory error: ${err}`);
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
function writeConsole(color, highlightText, msg) {
|
|
45
|
-
if (color == HighlightType.Red)
|
|
46
|
-
console.log(NEW_LINE + chalk.red(`${highlightText}: `) + msg + NEW_LINE);
|
|
47
|
-
if (color == HighlightType.Yellow)
|
|
48
|
-
console.log(chalk.yellow(`${highlightText}: `) + msg);
|
|
49
|
-
if (color == HighlightType.Green)
|
|
50
|
-
console.log(chalk.green(`${highlightText}: `) + msg);
|
|
51
|
-
}
|
|
52
|
-
function getRandomInt(max) {
|
|
53
|
-
return Math.floor(Math.random() * max) + 1;
|
|
54
|
-
}
|
|
55
|
-
function checkEleventyProject() {
|
|
56
|
-
log.debug('Validating project folder');
|
|
57
|
-
let result = false;
|
|
58
|
-
ELEVENTY_FILES.forEach((file) => {
|
|
59
|
-
let tmpFile = path.join(process.cwd(), file);
|
|
60
|
-
if (fs.existsSync(tmpFile)) {
|
|
61
|
-
result = true;
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
return result;
|
|
65
|
-
}
|
|
66
|
-
if (!checkEleventyProject()) {
|
|
67
|
-
log.error('Current folder is not an Eleventy project folder.');
|
|
68
|
-
process.exit(1);
|
|
69
|
-
}
|
|
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: '
|
|
80
|
-
name: '
|
|
81
|
-
initial:
|
|
82
|
-
message: '
|
|
83
|
-
},
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
writeConsole(yellow, '
|
|
118
|
-
|
|
119
|
-
writeConsole(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
let
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
var
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
var outputFilePath = path.join(outputFilePath, postTitle.toLowerCase().replaceAll(' ', '-') + '.md');
|
|
169
|
-
writeConsole(green, 'Writing', outputFilePath);
|
|
170
|
-
fs.writeFileSync(outputFilePath, thePost, 'utf8');
|
|
171
|
-
}
|
|
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 NEW_LINE = "\n";
|
|
20
|
+
const spaces40 = '-'.repeat(40);
|
|
21
|
+
const red = HighlightType.Red;
|
|
22
|
+
const yellow = HighlightType.Yellow;
|
|
23
|
+
const green = HighlightType.Green;
|
|
24
|
+
var numPosts;
|
|
25
|
+
var startYear;
|
|
26
|
+
var tag;
|
|
27
|
+
var targetFolder;
|
|
28
|
+
var yearMode;
|
|
29
|
+
function zeroPad(tmpVal, numChars = 2) {
|
|
30
|
+
return tmpVal.toString().padStart(numChars, '0');
|
|
31
|
+
}
|
|
32
|
+
function directoryExists(filePath) {
|
|
33
|
+
if (fs.existsSync(filePath)) {
|
|
34
|
+
try {
|
|
35
|
+
return fs.lstatSync(filePath).isDirectory();
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
log.error(`checkDirectory error: ${err}`);
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
function writeConsole(color, highlightText, msg) {
|
|
45
|
+
if (color == HighlightType.Red)
|
|
46
|
+
console.log(NEW_LINE + chalk.red(`${highlightText}: `) + msg + NEW_LINE);
|
|
47
|
+
if (color == HighlightType.Yellow)
|
|
48
|
+
console.log(chalk.yellow(`${highlightText}: `) + msg);
|
|
49
|
+
if (color == HighlightType.Green)
|
|
50
|
+
console.log(chalk.green(`${highlightText}: `) + msg);
|
|
51
|
+
}
|
|
52
|
+
function getRandomInt(max) {
|
|
53
|
+
return Math.floor(Math.random() * max) + 1;
|
|
54
|
+
}
|
|
55
|
+
function checkEleventyProject() {
|
|
56
|
+
log.debug('Validating project folder');
|
|
57
|
+
let result = false;
|
|
58
|
+
ELEVENTY_FILES.forEach((file) => {
|
|
59
|
+
let tmpFile = path.join(process.cwd(), file);
|
|
60
|
+
if (fs.existsSync(tmpFile)) {
|
|
61
|
+
result = true;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
if (!checkEleventyProject()) {
|
|
67
|
+
log.error('Current folder is not an Eleventy project folder.');
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
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');
|
|
158
|
+
var outputFilePath = path.join(process.cwd(), targetFolder);
|
|
159
|
+
if (yearMode) {
|
|
160
|
+
outputFilePath = path.join(outputFilePath, currentDate.getFullYear().toString());
|
|
161
|
+
if (!fs.existsSync(outputFilePath))
|
|
162
|
+
fs.mkdirSync(outputFilePath, { recursive: true });
|
|
163
|
+
}
|
|
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
package/readme.md
CHANGED
|
@@ -18,7 +18,7 @@ npm install -g eleventy-generate-posts
|
|
|
18
18
|
|
|
19
19
|
This adds a `11ty-gp` command to the system.
|
|
20
20
|
|
|
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
|
|
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.
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
@@ -38,8 +38,8 @@ The module will prompt you for all the required options:
|
|
|
38
38
|
└─────────────────────────┘
|
|
39
39
|
by John M. Wargo (https://johnwargo.com)
|
|
40
40
|
|
|
41
|
-
√ Target folder for generated posts? ... posts
|
|
42
41
|
√ Number of posts to generate? ... 10
|
|
42
|
+
√ Target folder for generated posts? ... posts
|
|
43
43
|
√ Post tag? ... post
|
|
44
44
|
√ Start year for generated posts? ... 2023
|
|
45
45
|
√ Use year folder for posts? ... yes
|
|
@@ -69,8 +69,8 @@ Writing: D:\dev\node\11ty-generate-posts\posts\2023\splinter-ecology-computer-ne
|
|
|
69
69
|
|
|
70
70
|
Configuration options are:
|
|
71
71
|
|
|
72
|
-
* **Posts Folder:** Relative path pointing to the Eleventy project's posts folder; use `.` for the current folder.
|
|
73
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
74
|
* **Post Tag:** The front matter `tag` property applied to the generated posts
|
|
75
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
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`).
|