-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdatastore-wipe.js
157 lines (135 loc) · 4.49 KB
/
datastore-wipe.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const prompts = require('prompts');
module.exports = {
friendlyName: 'Datastore Wipe',
description: 'Wipe all data on all the datastores. WILL NOT run on PRODUCTION. VERY DANGEROUS!!!',
inputs: {},
exits: {
canceled: {
description: 'Datastore wipe was canceled.'
},
success: {
description: 'Success! THIS DATASTORE IS CLEAN.'
},
production: {
description: 'You CAN NOT run this program on a PRODUCTION environment.'
}
},
fn: async (inputs, exits) => {
if ((process.env.NODE_ENV && process.env.NODE_ENV.toUpperCase() === 'PRODUCTION') || sails.config.models.migrate.toLowerCase() === 'safe') {
return exits.production();
}
// A word for every letter.
const randomWords = [
'Vanilla',
'Fluffy',
'Ultimate',
'Longitude',
'Zenith',
'Inquisitive',
'Dimple',
'Spectrum',
'Gargoyle',
'Bobcat',
'Official',
'Winning',
'Ephemeral',
'Young',
'Train',
'Mingle',
'Joint',
'Cascade',
'Rabbit',
'Prelude',
'Quiet',
'Halo',
'Nickel',
'Kilometer',
'Xenon',
'Apple',
];
const chosenOne = Math.floor(Math.random() * randomWords.length);
let notChosenOne;
do {
notChosenOne = Math.floor(Math.random() * randomWords.length);
} while (notChosenOne === chosenOne);
const answers = await prompts([
{
message: 'Do you wish to wipe all of the data from the connected datastore(s)?',
name: 'sureConfirm',
type: 'confirm',
initial: false
}, {
message: 'ARE YOU ABSOLUTELY SURE? THIS CAN NOT BE UNDONE! YOU WILL LOSE EVERYTHING! ALL DATA WILL BE ERASED!',
name: 'sureToggle',
type: (prev) => {
if (prev) {
console.log('');
return 'toggle';
}
return null;
},
initial: false,
inactive: 'NO, CANCEL',
active: 'YES, I\'M SURE'
}, {
message: 'FINAL FAILSAFE VERIFICATION. In order to erase everything, you must select the word "' + randomWords[chosenOne] + '":',
name: 'sureSelect',
type: (prev) => {
if (prev) {
console.log('');
return 'select';
}
return null;
},
initial: notChosenOne,
choices: randomWords
}
]);
if (!answers.sureConfirm || !answers.sureToggle || answers.sureSelect !== chosenOne) {
console.log('');
return exits.canceled();
}
// It's a bit more than 10 seconds...
console.log('\nYOU NOW HAVE 10 SECONDS TO CHANGE YOUR MIND, BEFORE ALL DATA IS DESTROYED...\n');
await sleep(2500);
console.log('10');
await sleep();
console.log('9');
await sleep();
console.log('8');
await sleep();
console.log('7');
await sleep();
console.log('6');
await sleep();
console.log('\nI REALLY HOPE YOU KNOW WHAT YOU ARE DOING!!!\n');
await sleep(2500);
console.log('5');
await sleep();
console.log('4');
await sleep();
console.log('3');
await sleep();
console.log('2');
await sleep();
console.log('1');
await sleep();
console.log('\nNO GOING BACK NOW!!!');
await sleep();
console.log('ALL DATA IS BEING ERASED!!!\n');
await sleep();
const modelsToDestroy = [];
_.forEach(sails.models, (model, modelName) => {
modelsToDestroy.push(modelName);
modelsToDestroy.push(model.destroy({}));
});
for(let i = 0; i < modelsToDestroy.length; i = i + 2) {
console.log(modelsToDestroy[i]);
await modelsToDestroy[i + 1];
}
return exits.success('\nThe datastore(s) are clean.\n');
}
};
async function sleep(napTime = 1000) {
await new Promise(resolve => setTimeout(resolve, napTime));
}