-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-content.js
More file actions
138 lines (118 loc) · 4.45 KB
/
migrate-content.js
File metadata and controls
138 lines (118 loc) · 4.45 KB
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
const fs = require("fs");
const path = require("path");
const archiver = require("archiver");
function migrateContent() {
// Create directories
fs.mkdirSync("./migrated/content"); // Will contain content.zip and links.zip
fs.mkdirSync("./migrated/content/files"); // Will contain migrated files before links migration
fs.mkdirSync("./migrated/content/linked_files"); // Will contain migrated files with links generated
// Get the data model changes logs
const logs = JSON.parse(fs.readFileSync("./migrated/logs.json"));
fs.readdir(
path.join(__dirname, "exports/legacy_docs"),
function (err, files) {
if (err) {
return console.log("Unable to scan directory: " + err);
}
// Listing all files
files.forEach(function (file, index) {
if (!file.startsWith(".")) {
let fileData = require(path.join(
__dirname,
"exports/legacy_docs",
file
));
// Handle slices variations using logs
logs.slice_zones.forEach((sz) => {
// Look for new slices created
const nSlice = logs.created_slices?.filter(
(slice) => slice.type === fileData.type
);
// Replace legacy values
let str = JSON.stringify(fileData);
str = str.replace('"body":', '"slices":'); // If removed from migrate-cts.mjs, remove this line
str = str.replaceAll('"repeat":', '"items":');
str = str.replaceAll('"non-repeat":', '"primary":');
if (logs.created_slices.length && nSlice.length) {
nSlice.forEach((item) => {
str = str.replaceAll(`"${item.legacy_id}$`, `"${item.new_id}$`);
});
}
fileData = JSON.parse(str);
// Add variation to all slice zones
if (sz.type === fileData.type && fileData[sz.id]) {
fileData[sz.id].forEach(function (slice) {
slice.value.variation = "default-slice";
});
}
});
// If locales exists, handle file name
if (fileData.grouplang) {
// Replace underscores if they exist in the grouplang or import will fail
if (fileData.grouplang.includes("_")){
console.log(fileData.grouplang.includes("_"), fileData.grouplang);
let lang = JSON.stringify(fileData.grouplang).replaceAll("_", "-");
fileData.grouplang = JSON.parse(lang);
console.log(lang, "NEW FILE", fileData.grouplang)
}
fs.writeFile(
path.join(
__dirname,
"migrated/content/files",
`new_${fileData.grouplang}_${fileData.lang}.json`
),
JSON.stringify(fileData, null, 2),
function writeJSON(err) {
if (err) return console.log(err);
console.log(
"writing to " +
path.join(
__dirname,
"migrated/content/files",
`new_${fileData.grouplang}_${fileData.lang}.json`
)
);
}
);
} else {
fs.writeFile(
path.join(
__dirname,
"migrated/content/files",
index + "_migrated.json"
),
JSON.stringify(fileData, null, 2),
function writeJSON(err) {
if (err) return console.log(err);
console.log(
"writing to " +
path.join(
__dirname,
"migrated/content/files",
index + "_migrated.json"
)
);
}
);
}
}
});
var output = fs.createWriteStream("migrated/content/content.zip");
var archive = archiver("zip");
output.on("close", function () {
console.log(archive.pointer() + " total bytes");
console.log(
"archiver has been finalized and the output file descriptor has closed."
);
});
archive.on("error", function (err) {
throw err;
});
archive.pipe(output);
// append files from a sub-directory, putting its contents at the root of archive
archive.directory("migrated/content/files", false);
archive.finalize();
}
);
}
migrateContent();