import { Factory, faker, trait } from 'ember-cli-mirage'; import { pickOne } from '../utils'; const REF_TIME = new Date(); const TROUBLESOME_CHARACTERS = '🏆 💃 🤩 🙌🏿 🖨 ? ; %'.split(' '); const makeWord = () => Math.round(Math.random() * 10000000 + 50000).toString(36); const makeSentence = (count = 10) => new Array(count) .fill(null) .map(makeWord) .join(' '); const fileTypeMapping = { svg: 'image/svg', txt: 'text/plain', json: 'application/json', app: 'application/octet-stream', exe: 'application/octet-stream', }; const fileBodyMapping = { svg: () => ` `, txt: () => new Array(3000) .fill(null) .map((_, i) => { const date = new Date(2019, 6, 23); date.setSeconds(i * 5); return `${date.toISOString()} ${makeSentence(Math.round(Math.random() * 5 + 7))}`; }) .join('\n'), json: () => JSON.stringify({ key: 'value', array: [1, 'two', [3]], deep: { ly: { nest: 'ed', }, }, }), }; export default Factory.extend({ id: i => i, isDir: faker.random.boolean, // Depth is used to recursively create nested directories. depth: 0, parent: null, fileType() { if (this.isDir) return 'dir'; return pickOne(['svg', 'txt', 'json', 'app', 'exe']); }, contentType() { return fileTypeMapping[this.fileType] || null; }, path() { if (this.parent) { return `${this.parent.path}/${this.name}`; } return this.name; }, name() { return `${faker.hacker.noun().dasherize()}-${pickOne(TROUBLESOME_CHARACTERS)}${ this.isDir ? '' : `.${this.fileType}` }`; }, body() { const strategy = fileBodyMapping[this.fileType]; return strategy ? strategy() : ''; }, size() { return this.body.length; }, modTime: () => faker.date.past(2 / 365, REF_TIME), dir: trait({ isDir: true, afterCreate(allocFile, server) { // create files for the directory if (allocFile.depth > 0) { server.create('allocFile', 'dir', { parent: allocFile, depth: allocFile.depth - 1 }); } server.createList('allocFile', faker.random.number({ min: 1, max: 3 }), 'file', { parent: allocFile, }); }, }), file: trait({ isDir: false, }), });