-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.js
203 lines (182 loc) · 4.78 KB
/
bootstrap.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"use strict";
const fs = require("fs");
const path = require("path");
const mime = require("mime-types");
const set = require("lodash.set");
const {
categories,
homepage,
writers,
articles,
global,
} = require("../data/data.json");
async function isFirstRun() {
const pluginStore = strapi.store({
environment: strapi.config.environment,
type: "type",
name: "setup",
});
const initHasRun = await pluginStore.get({ key: "initHasRun" });
await pluginStore.set({ key: "initHasRun", value: true });
return !initHasRun;
}
async function setPublicPermissions(newPermissions) {
// Find the ID of the public role
const publicRole = await strapi
.query("plugin::users-permissions.role")
.findOne({
where: {
type: "public",
},
});
// Create the new permissions and link them to the public role
const allPermissionsToCreate = [];
Object.keys(newPermissions).map(controller => {
const actions = newPermissions[controller];
const permissionsToCreate = actions.map(action => {
return strapi.query("plugin::users-permissions.permission").create({
data: {
action: `api::${controller}.${controller}.${action}`,
role: publicRole.id,
},
});
});
allPermissionsToCreate.push(...permissionsToCreate);
});
await Promise.all(allPermissionsToCreate);
}
function getFileSizeInBytes(filePath) {
const stats = fs.statSync(filePath);
const fileSizeInBytes = stats["size"];
return fileSizeInBytes;
}
function getFileData(fileName) {
const filePath = `./data/uploads/${fileName}`;
// Parse the file metadata
const size = getFileSizeInBytes(filePath);
const ext = fileName.split(".").pop();
const mimeType = mime.lookup(ext);
return {
path: filePath,
name: fileName,
size,
type: mimeType,
};
}
// Create an entry and attach files if there are any
async function createEntry({ model, entry, files }) {
try {
if (files) {
for (const [key, file] of Object.entries(files)) {
// Get file name without the extension
const [fileName] = file.name.split('.');
// Upload each individual file
const uploadedFile = await strapi
.plugin("upload")
.service("upload")
.upload({
files: file,
data: {
fileInfo: {
alternativeText: fileName,
caption: fileName,
name: fileName,
},
},
});
// Attach each file to its entry
set(entry, key, uploadedFile[0].id);
}
}
// Actually create the entry in Strapi
const createdEntry = await strapi.entityService.create(
`api::${model}.${model}`,
{
data: entry,
}
);
} catch (e) {
console.log("model", entry, e);
}
}
async function importCategories() {
return Promise.all(
categories.map((category) => {
return createEntry({ model: "category", entry: category });
})
);
}
async function importHomepage() {
const files = {
"seo.shareImage": getFileData("default-image.png"),
};
await createEntry({ model: "homepage", entry: homepage, files });
}
async function importWriters() {
return Promise.all(
writers.map(async (writer) => {
const files = {
picture: getFileData(`${writer.email}.jpg`),
};
return createEntry({
model: "writer",
entry: writer,
files,
});
})
);
}
async function importArticles() {
return Promise.all(
articles.map((article) => {
const files = {
image: getFileData(`${article.slug}.jpg`),
};
return createEntry({
model: "article",
entry: {
...article,
// Make sure it's not a draft
publishedAt: Date.now(),
},
files,
});
})
);
}
async function importGlobal() {
const files = {
favicon: getFileData("favicon.png"),
"defaultSeo.shareImage": getFileData("default-image.png"),
};
return createEntry({ model: "global", entry: global, files });
}
async function importSeedData() {
// Allow read of application content types
await setPublicPermissions({
global: ["find"],
homepage: ["find"],
article: ["find", "findOne"],
category: ["find", "findOne"],
writer: ["find", "findOne"],
});
// Create all entries
await importCategories();
await importHomepage();
await importWriters();
await importArticles();
await importGlobal();
}
module.exports = async () => {
const shouldImportSeedData = await isFirstRun();
if (shouldImportSeedData) {
try {
console.log("Setting up the template...");
await importSeedData();
console.log("Ready to go");
} catch (error) {
console.log("Could not import seed data");
console.error(error);
}
}
};