File size: 1,967 Bytes
b76416f
 
 
 
 
 
 
 
 
 
 
cbdd38a
 
 
 
 
 
 
 
 
 
 
 
b76416f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbdd38a
 
 
 
 
b76416f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// mongoose seeder script
// takes an optional cli argument "--reset" to reset the database
//
// Usage:
// npm run seed
// npm run seed:reset
// node src/seeder/seed.ts
// node src/seeder/seed.ts --reset

import * as glob from "glob";
import path from "path";
import { loadExercisesDataset } from "./helpers/load-exercises-dataset";
import { dbStore } from "./helpers/db-store";

const loadDatasets = async() => {
  const exercisesDataset = await loadExercisesDataset();
  const musclesDataset = Array.from(new Set(exercisesDataset.map((exercise) => exercise.target)));
  const equipmentsDataset = Array.from(new Set(exercisesDataset.map((exercise) => exercise.equipment)))
  
  dbStore.excerisesDataset = exercisesDataset;
  dbStore.musclesDataset = musclesDataset;
  dbStore.equipmentsDataset = equipmentsDataset;
}

const main = async () => {
  // get cli arguments
  const args = process.argv.slice(2);

  // get cli names that don't start with "--"
  const seederNames = args.filter((arg) => !arg.startsWith("--"));

  // list all files under ./seeders
  const seedersPath = path
    .relative(process.cwd(), path.join(__dirname, "**/*.seeder.{ts,js}"))
    .replace(/\\/g, "/");

  const seedersFiles = glob
    .sync(seedersPath, {})
    .map((file) => {
      return path.resolve(file);
    })
    .filter((file) => {
      if (seederNames.length === 0) {
        return true;
      }

      return seederNames.includes(path.basename(file));
    })
    .sort();

  // load datasets
  await loadDatasets();

  // run all seeders
  let count = 0;
  for await (const file of seedersFiles) {
    const baseName = path.basename(file);
    console.log(`Running ${baseName} ...`);
    const seeder = await import(file);
    await seeder.default();
    console.log(`${baseName} completed! (${++count}/${seedersFiles.length})`);
  }

  console.log("All seeders completed!");
  process.exit(0);
};

main().catch((err) => {
  console.error(err);
  process.exit(1);
});