|
| 1 | +const fs = require('fs'); |
| 2 | +const faker = require('faker'); |
| 3 | + |
| 4 | +/* |
| 5 | + Welcome to the fake file generator! The Linia Team threw this together so you |
| 6 | + could spend less time making fake data and more time building! In order to get |
| 7 | + this working for you, you should change three things: |
| 8 | +
|
| 9 | + 1. Change `numberOfFiles` to equal the amount of files you want to generate |
| 10 | +
|
| 11 | + 2. Change the `fileSchema` to make that of the data you want to produce. More explantion |
| 12 | + on how to do that in the comment above it. |
| 13 | +
|
| 14 | + 3. Change the fileName function to product the name for the file that you want. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | + The number of files that the script will generator. Pretty self explanatory. |
| 19 | +*/ |
| 20 | + |
| 21 | +const numberOfFiles = 10; |
| 22 | + |
| 23 | +/* |
| 24 | + The schema for the files you want to generate. Keys added to the object will be |
| 25 | + the name of the property that appears in the JSON, and the value will be a function |
| 26 | + that produces the fake data. |
| 27 | +
|
| 28 | + For instance: |
| 29 | +
|
| 30 | + const fileSchema = { |
| 31 | + name: () => 'Sammy' |
| 32 | + } |
| 33 | +
|
| 34 | + Will set translate to: |
| 35 | +
|
| 36 | + { |
| 37 | + "name": "Sammy" |
| 38 | + } |
| 39 | +
|
| 40 | + The included `faker` library provides great methods to produce fake data. Documentation |
| 41 | + can be found here: https://github.com/marak/Faker.js |
| 42 | +*/ |
| 43 | + |
| 44 | +const fileSchema = { |
| 45 | + // will provide a fake name provided by the library |
| 46 | + name: faker.name.findName, |
| 47 | + // will increment age by 1 per file |
| 48 | + age: (() => { |
| 49 | + let age = 0; |
| 50 | + return () => { |
| 51 | + age++; |
| 52 | + return age; |
| 53 | + }; |
| 54 | + })(), |
| 55 | + // will always set favoritePet to cats |
| 56 | + favoritePet: () => 'cats' |
| 57 | +}; |
| 58 | + |
| 59 | +/* |
| 60 | + A function that produces the name for each file. It will receive the generated data |
| 61 | + for the file as an argument |
| 62 | +*/ |
| 63 | + |
| 64 | +const generateFileName = (fileData) => fileData.name; |
| 65 | + |
| 66 | +/* |
| 67 | + *********************************************************** |
| 68 | + YOU PROBABLY DONT NEED TO CHANGE ANYTHING BELOW THESE LINES |
| 69 | + *********************************************************** |
| 70 | +*/ |
| 71 | + |
| 72 | +const generateFileData = () => { |
| 73 | + const fileData = {}; |
| 74 | + |
| 75 | + Object.entries(fileSchema).forEach((pair) => { |
| 76 | + const [ key, value ] = pair; |
| 77 | + fileData[key] = value(); |
| 78 | + }); |
| 79 | + |
| 80 | + return fileData; |
| 81 | +}; |
| 82 | + |
| 83 | +const generateFiles = async () => { |
| 84 | + for (let i = 0; i < numberOfFiles; i++) { |
| 85 | + const fileData = generateFileData(); |
| 86 | + const fileName = generateFileName(fileData); |
| 87 | + const json = JSON.stringify(fileData); |
| 88 | + fs.writeFileSync(`./files/${fileName}.json`, json); |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +generateFiles(); |
| 93 | + |
0 commit comments