// @flow

import { writeFile } from 'fs';
import { join } from 'path';
import { sample, sampleSize } from 'lodash';
import { name, internet, lorem, date, random, image } from 'faker';
import { v4 as uuid } from 'node-uuid';
import { promisify } from 'bluebird';
import ora from 'ora';

const write = promisify(writeFile);

class User {
  id: string;
  email: string;
  firstName: string;
  lastName: string;
  password: string;
  username: string;
  profilePicture: string;
  constructor() {
    this.email = internet.email();
    this.firstName = name.firstName();
    this.id = uuid();
    this.profilePicture = image.people();
    this.lastName = name.lastName();
    this.password = internet.password();
    this.username = internet.userName();
  }
}

class Post {
  comments: Array<Comment>;
  content: string;
  id: string;
  image: ?string;
  link: ?string;
  user: ?string;
  likes: number;
  constructor(user) {
    this.id = uuid();
    this.comments = [];
    this.content = lorem.paragraph(sample([1, 2, 3]));
    this.image = random.boolean() ? null : image.image();
    this.likes = random.number(1, 100);
    this.link = random.boolean() ? null : internet.url();
    this.user = user.id;
  }
}

class Comment {
  content: string;
  date: Date;
  id: string;
  likes: number;
  user: ?string;
  constructor(user) {
    this.id = uuid();
    this.content = lorem.paragraph(sample([1, 2, 3]));
    this.date = date.recent(sample([1, 2, 3, 4, 5, 10, 15]));
    this.likes = random.number(1, 100);
    this.user = user.id;
  }
}

function generateUsers(n) {
  const users = [];
  for (let i = 0; i < n; i++) {
    users.push(new User());
  }
  return users;
}

function generateComments(n: number, users: Array<any>) {
  const comments = [];
  for (let i = 0; i < n; i++) {
    comments.push(new Comment(sample(users)));
  }
  return comments;
}

function generatePosts(n: number, users: Array<User>, comments: Array<Comment>) {
  const posts = [];
  for (let i = 0; i < n; i++) {
    const newPost = new Post(sample(users));
    newPost.comments = sampleSize(comments, random.number(7));
    posts.push(newPost);
  }
  return posts;
}

export function seed(nUsers: number = 100, nPosts: number = 500, nComments: number = 500) {
  const spinner = ora('Generating sample data...').start();
  const users = generateUsers(nUsers);
  const comments = generateComments(nComments, users);
  const posts = generatePosts(nPosts, users, comments);
  Promise.all([
    write(join(__dirname, '..', 'db/seed/', 'db.json'), JSON.stringify({
      users,
      posts,
      comments,
    })),
  ])
  .then(() => {
    spinner.text = '🎉 🎉 🎉 🎉  Done writing sample data 🎉 🎉 🎉 🎉';
    setTimeout(() => spinner.stop(), 2000);
  })
  .catch(err => console.error(err));
}

seed();

@@ -12,25 +12,20 @@
 mport %7B 
-writeFile
+join
  %7D from 
@@ -29,10 +29,12 @@
 om '
-fs
+path
 ';%0Ai
@@ -33,36 +33,74 @@
 path';%0Aimport %7B 
-join
+name, internet, lorem, date, random, image
  %7D from 'path';%0A
@@ -84,36 +84,37 @@
 , image %7D from '
-path
+faker
 ';%0Aimport %7B samp
@@ -113,26 +113,17 @@
 t %7B 
-sample, sampleSize
+promisify
  %7D f
@@ -131,14 +131,16 @@
 om '
-lodash
+bluebird
 ';%0Ai
@@ -151,49 +151,25 @@
 t %7B 
-name, internet, lorem, date, random, imag
+sample, sampleSiz
 e %7D 
@@ -166,37 +166,38 @@
 pleSize %7D from '
-faker
+lodash
 ';%0Aimport %7B v4 a
@@ -228,33 +228,33 @@
 ';%0Aimport %7B 
-promisify
+writeFile
  %7D from 'blu
@@ -246,32 +246,26 @@
 ile %7D from '
-bluebird
+fs
 ';%0Aimport or
@@ -316,16 +316,334 @@
 File);%0A%0A
+function generateProfilePicture() %7B%0A  const pics = %5B%5D;%0A  for (let i = 0; i %3C 15; i++) %7B%0A    pics.push(%60https://newsfeed.learnreactjs.io/assets/profile-pictures/$%7Bi + 1%7D.png%60);%0A  %7D%0A  return function selectRandomProfilePicture() %7B%0A    return sample(pics);%0A  %7D;%0A%7D%0A%0Aconst createProfilePicture = generateProfilePicture();%0A%0A
 class Us
@@ -929,19 +929,27 @@
 e = 
-image.peopl
+createProfilePictur
 e();

Use self-hosted profile picture images to add variety/speed