|
| 1 | +/** |
| 2 | + * Adds N number of posts (10 by default) |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * |
| 6 | + * node add-random-posts.js https://blah.ghost.io ADMIN_API_KEY number_of_posts |
| 7 | + */ |
| 8 | +if (process.argv.length < 4) { |
| 9 | + console.error('Missing an argument'); |
| 10 | + process.exit(1); |
| 11 | +} |
| 12 | + |
| 13 | +const url = process.argv[2]; |
| 14 | +const key = process.argv[3]; |
| 15 | +const count = process.argv[4] || 10; |
| 16 | + |
| 17 | +const Promise = require('bluebird'); |
| 18 | +const loremIpsum = require('lorem-ipsum').loremIpsum; |
| 19 | +const _ = require('lodash'); |
| 20 | +const GhostAdminAPI = require('@tryghost/admin-api'); |
| 21 | + |
| 22 | +console.log(url); |
| 23 | + |
| 24 | +const api = new GhostAdminAPI({ |
| 25 | + url, |
| 26 | + key, |
| 27 | + version: 'v2' |
| 28 | + }); |
| 29 | + |
| 30 | +(async function main() { |
| 31 | + try { |
| 32 | + const posts = []; |
| 33 | + |
| 34 | + _.times(count, () => { |
| 35 | + let post = { |
| 36 | + status: 'published', |
| 37 | + title: loremIpsum({ |
| 38 | + count: 2, |
| 39 | + units: 'words', |
| 40 | + }), |
| 41 | + excerpt: loremIpsum({ |
| 42 | + count: 2, |
| 43 | + units: 'sentences' |
| 44 | + }), |
| 45 | + html: loremIpsum({ |
| 46 | + count: 400, // Number of "words", "sentences", or "paragraphs" |
| 47 | + format: 'html', // "plain" or "html" |
| 48 | + paragraphLowerBound: 3, // Min. number of sentences per paragraph. |
| 49 | + paragraphUpperBound: 7, // Max. number of sentences per paragarph. |
| 50 | + random: Math.random, // A PRNG function |
| 51 | + sentenceLowerBound: 3, // Min. number of words per sentence. |
| 52 | + sentenceUpperBound: 15, // Max. number of words per sentence. |
| 53 | + suffix: '\n', // Line ending, defaults to "\n" or "\r\n" (win32) |
| 54 | + units: 'paragraphs', // paragraph(s), "sentence(s)", or "word(s)" |
| 55 | + words: undefined // Array of words to draw from |
| 56 | + }), |
| 57 | + |
| 58 | + }; |
| 59 | + |
| 60 | + posts.push(post); |
| 61 | + }); |
| 62 | + |
| 63 | + console.log(`Adding ${posts.length} posts to ${url}`); |
| 64 | + |
| 65 | + // convert our list of posts, to a list of promises for requests to the api |
| 66 | + const result = await Promise.mapSeries(posts, async (post) => { |
| 67 | + |
| 68 | + console.log('Adding', post.title); |
| 69 | + // Call the API |
| 70 | + let result = await api.posts.add(post, {source: 'html'}); |
| 71 | + |
| 72 | + // Add a delay but return the original result |
| 73 | + return Promise.delay(5).return(result); |
| 74 | + }); |
| 75 | + |
| 76 | + console.log(`Added ${result.length} posts`); |
| 77 | + |
| 78 | + } catch (err) { |
| 79 | + console.error('There was an error', require('util').inspect(err, false, null)); |
| 80 | + process.exit(1); |
| 81 | + } |
| 82 | +}()); |
0 commit comments