Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
move tasks to tasks subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
hennessyevan committed Sep 7, 2019
1 parent 3acd238 commit 4069167
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 147 deletions.
107 changes: 19 additions & 88 deletions src/commands/seed.ts
@@ -1,21 +1,8 @@
import { Command, flags } from "@oclif/command"
import Listr from "listr"
import input from "listr-input"
import mongoose, { Mongoose } from "mongoose"
import pEachSeries from "p-each-series"
import { Observable } from "rxjs"
import { register } from "ts-node"
import {
CacheFile,
createDocsFromData,
generateData,
getModels,
getRefPaths,
log,
Options,
parseOptions,
populateSmartIds
} from "../utils"
import { Mongoose } from "mongoose"
import { generate, init, loadModels, populate, createDocsFromData } from "../tasks"
import { CacheFile, log, Options } from "../utils"
import chalk from "chalk"

export type SmartMapType = {
Expand All @@ -26,6 +13,14 @@ export type SmartMapType = {
timestamp: number
}

export type ListrContext = {
config: Options
models: string[]
mongoose: Mongoose
cache?: CacheFile
smartMap: SmartMapType
}

export default class Seed extends Command {
static description = "describe the command here"

Expand All @@ -43,93 +38,29 @@ export default class Seed extends Command {
// const { collection } = args
log(`Seeding all collections`)

const tasks = new Listr<{
config: Options
models: string[]
mongoose: Mongoose
cache?: CacheFile
smartMap: SmartMapType
}>([
const tasks = new Listr<ListrContext>([
{
title: "Initializing",
task: async (ctx, task) => {
// Parse config from package.json
ctx.config = await parseOptions()
ctx.mongoose = mongoose

if (ctx.config && !ctx.config.dropDatabase) {
task.title = "Drop Database?"
input("Y/n", {
validate: (value: string) => ["Y", "y", "N", "n", ""].includes(value),
done: (drop: string) => {
switch (drop.toUpperCase()) {
case "N":
ctx.config.dropDatabase = false
break
default:
case "Y":
ctx.config.dropDatabase = true
break
}
}
})
}
}
task: init
},
{
title: "Loading models",
task: async (ctx, task) => {
// Enumerate models
ctx.models = await getModels(ctx.config.modelDir)
// Connect mongoose
await mongoose.connect(ctx.config.mongoURI, { useNewUrlParser: true })
await mongoose.connection.dropDatabase()
// Register models with mongoose
ctx.models.map(async model => {
register({ transpileOnly: true })
await require(model)
task.output = model
})
}
task: loadModels
},
{
title: "Generating Primary Data",
task: async (ctx, task) => {
const promises = Object.keys(mongoose.models).map(async model => {
// Get model
const mongooseModel = mongoose.model(model)
// Get ref paths
const refPaths = await getRefPaths(mongooseModel)
// Here we insert whatever data is available and swap out ids later
return { data: await generateData(mongooseModel, refPaths, ctx.config), model }
})
await pEachSeries(promises, async ({ data, model }) => {
task.output = model
ctx.cache = { ...ctx.cache, ...data }
})
}
title: "Transforming Seed Data",
task: generate
},
{
title: "Populating Smart References",
task: async ctx => {
ctx.cache = await populateSmartIds(ctx.cache)
}
task: populate
},
{
title: `Feeding ${chalk.green("Gooseberries")} to Mongoose`,
task: async (ctx, task) => {
const count = await createDocsFromData(ctx.cache!, task)
task.output = `🚀 Successfully wrote ${count} documents`
setTimeout(async () => {
task.output = "Closing"
await mongoose.disconnect().then(() => {
process.exit()
})
}, 1500)
}
task: createDocsFromData
}
])

await tasks.run()
tasks.run()
}
}
30 changes: 30 additions & 0 deletions src/tasks/createDocsFromData.ts
@@ -0,0 +1,30 @@
import { ListrTaskWrapper } from "listr"
import mongoose from "mongoose"
import { from } from "rxjs"
import { concatMap, endWith, map } from "rxjs/operators"
import { ListrContext } from "../commands/seed"
import { log, LogType } from "../utils/log"

export function createDocsFromData(ctx: ListrContext, task: ListrTaskWrapper) {
if (ctx.cache) {
return from(Object.entries(ctx.cache)).pipe(
concatMap(([modelName, { data }]) => {
const model = mongoose.model(modelName)
task.output = modelName

return from(Object.values(data)).pipe(
map(doc =>
new model(doc).save().catch(error => {
log(error.message, LogType.error)
})
)
)
}),
endWith(() => {
mongoose.connection.close().then(() => {
process.exit()
})
})
)
}
}
25 changes: 25 additions & 0 deletions src/tasks/generate.ts
@@ -0,0 +1,25 @@
import { ListrContext, ListrTaskWrapper } from "listr"
import { from } from "rxjs"
import { map, delay, concatAll } from "rxjs/operators"
import mongoose from "mongoose"
import { getRefPaths, generateData } from "../utils"

export function generate(ctx: ListrContext, task: ListrTaskWrapper) {
return from(Object.keys(mongoose.models)).pipe(
map(async model => {
delay(1000)
// Get model
const mongooseModel = mongoose.model(model)
// Get ref paths
const refPaths = await getRefPaths(mongooseModel)
// Here we insert whatever data is available and swap out ids later
const data = await generateData(mongooseModel, refPaths, ctx.config)

task.output = model

ctx.cache = { ...ctx.cache, ...data }
return model
}),
concatAll()
)
}
5 changes: 5 additions & 0 deletions src/tasks/index.ts
@@ -0,0 +1,5 @@
export * from "./init"
export * from "./loadModels"
export * from "./generate"
export * from "./populate"
export * from "./createDocsFromData"
29 changes: 29 additions & 0 deletions src/tasks/init.ts
@@ -0,0 +1,29 @@
import { ListrContext } from "../commands/seed"
import { ListrTaskWrapper } from "listr"
import { parseOptions } from "../utils"
import mongoose from "mongoose"
import input from "listr-input"
import { Observable } from "rxjs"

export async function init(ctx: ListrContext, task: ListrTaskWrapper) {
ctx.config = await parseOptions()
ctx.mongoose = mongoose

if (ctx.config && !ctx.config.dropDatabase) {
task.title = "Drop Database?"
input("Y/n", {
validate: (value: string) => ["Y", "y", "N", "n", ""].includes(value),
done: (drop: string) => {
switch (drop.toUpperCase()) {
case "N":
ctx.config.dropDatabase = false
break
default:
case "Y":
ctx.config.dropDatabase = true
break
}
}
})
}
}
24 changes: 24 additions & 0 deletions src/tasks/loadModels.ts
@@ -0,0 +1,24 @@
import { ListrTaskWrapper } from "listr"
import mongoose from "mongoose"
import { from } from "rxjs"
import { concatAll, map } from "rxjs/operators"
import { register } from "ts-node"
import { getModels } from "../utils"
import { ListrContext } from "../commands/seed"

export async function loadModels(ctx: ListrContext, task: ListrTaskWrapper) {
ctx.models = await getModels(ctx.config.modelDir)
// Connect mongoose
await mongoose.connect(ctx.config.mongoURI, { useNewUrlParser: true })
await mongoose.connection.dropDatabase()

return from(ctx.models).pipe(
map(model => {
task.output = model
register({ transpileOnly: true })
require(model)
return model
}),
concatAll()
)
}
28 changes: 28 additions & 0 deletions src/tasks/populate.ts
@@ -0,0 +1,28 @@
import { Observable } from "rxjs"
import { ListrContext } from "../commands/seed"
import { ListrTaskWrapper } from "listr"
import set from "lodash.set"
import get from "lodash.get"

export function populate(ctx: ListrContext, task: ListrTaskWrapper) {
if (!ctx.cache) return

return new Observable(subscriber => {
Object.values(ctx.cache!).forEach(data => {
data.$gooseberry.refEntries!.forEach(refEntry => {
if (Array.isArray(refEntry.pathToRef)) {
const idsAtRef = refEntry.pathToRef.map(pathToRef => get(ctx.cache!, pathToRef + "_id"))
set(ctx.cache!, refEntry.pathToEntry, idsAtRef)
return
}

const idAtRef = get(ctx.cache, refEntry.pathToRef + "_id") as any

if (idAtRef) {
set(ctx.cache!, refEntry.pathToEntry, idAtRef)
}
})
})
subscriber.complete()
})
}
22 changes: 0 additions & 22 deletions src/utils/createDocsFromData.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/index.ts
Expand Up @@ -4,7 +4,6 @@ export * from "./log"
export * from "./getRefPaths"
export * from "./generateData"
export * from "./populateSmartIds"
export * from "./createDocsFromData"
export * from "./sanitizeCache"
export * from "./removeDuplicateData"
export * from "./loadFile"
36 changes: 0 additions & 36 deletions src/utils/populateSmartIds.ts

This file was deleted.

0 comments on commit 4069167

Please sign in to comment.