-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.js
More file actions
127 lines (108 loc) · 4.21 KB
/
gulpfile.js
File metadata and controls
127 lines (108 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const gulp = require('gulp')
const server = require('gulp-develop-server')
const browserSync = require('browser-sync').create()
const historyApiFallback = require('connect-history-api-fallback')
const browserify = require('browserify')
const babelify = require('babelify')
const source = require('vinyl-source-stream')
const cuid = require('cuid')
const factory = require('factory-girl').factory
const Chance = require('chance')
const shuffle = require('lodash/shuffle')
const data = require('vientos-data')
const MONGO_URL = process.env.MONGO_URL || 'mongodb://localhost:27017/vientos-dev'
const mongoose = require('mongoose')
mongoose.Promise = global.Promise
const MongooseAdapter = require('factory-girl').MongooseAdapter
const schemas = require('./vientos-service/src/models/schemas')
const chance = new Chance()
// service
gulp.task('service:start', () => {
server.listen({ path: './server.js' })
})
gulp.task('service:restart', () => {
gulp.watch([ 'vientos-service/src/**/*' ], server.restart)
})
gulp.task('service', ['service:start', 'service:restart'])
// APP
gulp.task('app:browsersync', () => {
browserSync.init({
server: './vientos-app',
middleware: [ historyApiFallback() ],
port: 8080,
open: false,
notify: false
})
gulp.watch(['vientos-app/app/**/*', 'vientos-app/index.html', 'vientos-app/bundle.js']).on('change', browserSync.reload)
})
function bundle () {
console.log('bundling app')
return browserify({
entries: ['vientos-app/src/main.js'],
debug: true
}).transform(babelify.configure({
presets: ['es2015'],
plugins: ['transform-object-rest-spread']
})).bundle().pipe(source('bundle.js')).pipe(gulp.dest('vientos-app/'))
}
gulp.task('app:bundle', bundle)
gulp.task('app:bundle:watch', () => {
gulp.watch(['vientos-app/src/**/*', 'vientos-app/config.json']).on('change', bundle)
})
gulp.task('app', ['app:browsersync', 'app:bundle', 'app:bundle:watch'])
// stack
gulp.task('stack', ['service', 'app'])
// factories
function generateLocation () {
return {
type: 'Place',
latitude: chance.latitude({ min: 19.26, max: 19.56 }),
longitude: chance.longitude({ min: -99.21, max: -99 }),
address: chance.address()
}
}
function getRandomId (collection) {
return shuffle(collection)[0]._id
}
function getRandomIds (collection, quantity = 3) {
return shuffle(collection).slice(-quantity).map(doc => doc._id)
}
gulp.task('factory', (done) => {
mongoose.connect(MONGO_URL, { promiseLibrary: global.Promise })
factory.setAdapter(new MongooseAdapter())
mongoose.model('Project', schemas.project, 'projects')
mongoose.model('Intent', schemas.intent, 'intents')
let collaborationTypes = data.collaborationTypes.map(ct => ct.id)
factory.define('intent', mongoose.models.Intent, {
_id: () => { return process.env.OAUTH_CLIENT_DOMAIN + '/intents/' + cuid() },
type: 'Intent',
title: () => { return chance.sentence({ words: 6 }).replace('.', '') },
collaborationType: () => { return chance.pickone(collaborationTypes) },
direction: () => { return chance.pickone(['offer', 'request']) },
locations: () => { return chance.d8() > 4 ? [] : [generateLocation()] }
})
let categories = data.categories.map(cat => cat.id)
factory.define('project', mongoose.models.Project, {
_id: () => { return process.env.OAUTH_CLIENT_DOMAIN + '/projects/' + cuid() },
type: 'Project',
name: () => { return chance.sentence({ words: 2 }).replace('.', '') },
description: () => { return chance.paragraph() },
logo: () => { return 'https://robohash.org/' + chance.hash() + '?set=set3' },
categories: () => { return chance.pickset(categories, chance.d4()) },
locations: () => { return Array.from(Array(chance.d4()), () => generateLocation()) }
})
let createdProjects
factory.createMany('project', 50)
.then((projects) => {
createdProjects = projects
return factory.createMany('intent', 600)
}).then((intents) => {
return Promise.all(intents.map(intent => {
intent.projects = chance.d8() > 6 ? getRandomIds(createdProjects, chance.d4()) : [getRandomId(createdProjects)]
return intent.save()
}))
}).then((intents) => {
mongoose.disconnect()
return done()
}).catch(err => console.log(err))
})