-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgardener.js
124 lines (110 loc) · 3.32 KB
/
gardener.js
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
const sh = require('shelljs'); // https://documentup.com/shelljs/shelljs
const KNEX_SEED_FOLDER = 'data/seeds';
/**
* NOTE:
*
* this script was written with brevity in mind,
* to seed your database from the command line.
*
*/
/**
* knex seed file names; shorthand
*/
const changes = {
reset: "000_cleanup",
cleanup: "000_cleanup",
parents: "001_Parents",
avatars: "002_Avatars",
grades: "003_GradeLevels",
stories: "004_Stories",
cohorts: "005_Cohorts",
children: "006_Children",
submissions: "007_Submissions",
art: "008_Writings_Drawings",
writings: "008_Writings_Drawings",
drawings: "008_Writings_Drawings",
squads: "009_Squads",
teams: "010_Teams",
members: "011_Members",
points: "012_Points",
achievements: "013_Achievements",
faceoffs: "014_Faceoffs",
}
/**
* list of file names within the knex seed folder
*/
const seedFiles = sh.ls(KNEX_SEED_FOLDER).map(function (file) {
return file
});
/**
* searches a list of file names
* @param {string} searchstring
* @returns array - matches
*/
const searchName = (searchstring) => seedFiles.filter(function (fname, _, __) {
return fname.toLowerCase().search(searchstring.toLowerCase()) > 0
});
seedFiles.forEach(function (fname) {
changes[fname] = fname;
});
/**
* Plant a seed of change
* @param {string} seed
* @returns Returns an object containing the return code and output as string.
*/
const plant = (seed) => sh.exec(`npx knex seed:run --specific="${seed}"`);
const [_, __, seed, matches] = process.argv;
const optionSwitches = ["all", "search", "--help"];
const availableOptions = Object.keys(changes) + optionSwitches + Object.keys(changes).map((s) => s + '.js'); // concatenate 3 lists
/**
*
* @returns Returns an object containing the return code and output as string.
*/
function dataFarm({ option }) {
const change = option.endsWith('.js') ? option : changes[option] + '.js';
if (!availableOptions.includes(option)) {
return sh.echo('do you need --help with that ?');
}
return plant(change);
}
function cli(virtualSeed) {
if (!virtualSeed) {
const fname = __filename.split('/').pop();
sh.echo(`usage: node ${fname} {option}`)
sh.echo('for a list of available {options}, type');
sh.echo(` node ${fname} --help`);
return false;
}
switch (virtualSeed) {
case "all":
sh.echo("Running ALL seeds...");
sh.exec('npx knex seed:run');
break;
case "search":
sh.echo(searchName(matches));
break;
case "--help":
sh.echo('available options');
sh.echo(` --help -> this!`);
sh.echo(` all -> 'npx knex seed:run'`);
Object.keys(changes).map((option) => sh.echo(` ${option} -> ${changes[option]}.js`));
break;
// TODO: create cases to seed the database for each stage of the game.
case "zeroday":
sh.echo('factory-reset the database');
sh.exec('npx knex migrate:rollback');
sh.exec('npx knex migrate:latest');
break;
default:
dataFarm({ option: virtualSeed });
break;
}
return true;
}
cli(seed);
/**
* const { gardener } = require('./gardener');
*/
module.exports = {
gardener: (s) => cli(s)
}