forked from ahmedkamalio/migration-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (123 loc) · 3.09 KB
/
index.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
import Listr from "listr";
import commandLineArgs from "command-line-args";
import * as fs from "fs";
import { migrateSchema } from "./tasks/schema.js";
import { migrateFiles } from "./tasks/files.js";
import { migrateUsers } from "./tasks/users.js";
import { migrateData } from "./tasks/data.js";
import { fetchRelations, migrateRelations } from "./tasks/relations.js";
import { writeFile } from "fs/promises";
const commandLineOptions = commandLineArgs([
{
name: "skipCollections",
alias: "s",
type: String,
multiple: true,
defaultValue: [],
},
{
name: "useContext",
alias: "c",
type: String,
multiple: false,
defaultValue: "./context/start.json",
},
{
name: "allowFailures",
alias: "l",
type: Boolean,
multiple: false,
defaultValue: false,
},
]);
const tasks = new Listr([
{
title: "Loading context",
task: setupContext,
},
{
title: "Fetch Relations",
skipt: (context) =>
context.completedSteps.relationsv8 === true &&
context.completedSteps.schema === true &&
context.completedSteps.relations === true,
task: fetchRelations,
},
{
title: "Migrating Schema",
skip: (context) =>
context.completedSteps.schema === true &&
context.completedSteps.collections === true,
task: (context) => {
context.skipCollections = commandLineOptions.skipCollections;
return migrateSchema(context);
},
},
{
title: "Migrating Users",
skip: (context) =>
context.completedSteps.roles === true &&
context.completedSteps.users === true,
task: migrateUsers,
},
{
title: "Migrating Relations",
skip: (context) =>
context.completedSteps.relationsv8 === true &&
context.completedSteps.relations === true,
task: migrateRelations,
},
{
title: "Migration Files",
skip: (context) => context.completedSteps.files === true,
task: migrateFiles,
},
{
title: "Migrating Data",
skip: (context) => context.completedSteps.data === true,
task: migrateData,
},
{
title: "Save final context",
task: (context) => {
context.section = "completed";
writeContext(context);
},
},
]);
let ctx = {};
export async function writeContext(context, completed = true) {
context.completedSteps[context.section] = completed;
await fs.promises.writeFile(
`./context/state/${context.section}.json`,
JSON.stringify(context)
);
}
async function setupContext(context) {
ctx = context;
context.allowFailures = commandLineOptions.allowFailures;
const contextJSON = await fs.promises.readFile(
commandLineOptions.useContext,
"utf8"
);
console.log("Loading context");
const fetchedContext = JSON.parse(contextJSON);
Object.entries(fetchedContext).forEach(([key, value]) => {
context[key] = value;
});
console.log(`✨ Loaded context succesfully`);
}
console.log(
`✨ Migrating ${process.env.V8_URL} (v8) to ${process.env.V9_URL} (v9)...`
);
try {
await tasks.run();
console.log("✨ All set! Migration successful.");
} catch (err) {
delete err.context;
const errorFilename = `./error-${new Date()
.toISOString()
.replace(/[^\w]/g, "_")}.log`;
await writeFile(errorFilename, `\n${err.message}\n${err.stack}\n`);
await writeContext(ctx, false);
}