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 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 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&paras=${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
- }
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&paras=${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.4",
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: