Skip to content

Commit e41a0a4

Browse files
authored
Merge pull request #49 from strapi/chore/prefill-script-fix
chore: fix prefill login script
2 parents 7edd8d4 + 386a808 commit e41a0a4

File tree

2 files changed

+43
-38
lines changed

2 files changed

+43
-38
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ Simply make sure the whole Strapi application doesn't crash and the connected Ne
1313
Some additional things to check:
1414

1515
- [ ] Strapi project uuid is "LAUNCHPAD". `strapi/packages.json`.
16-
- [ ] If you updated content, make sure to create a new export in the `strapi/data` folder and update the `strapi/packages.json` seed command if necessary.
1716
- [ ] Strapi version is the latest possible.
17+
- [ ] If the Strapi version has been changed, make sure that the `strapi/scripts/prefillLoginFields.js` works.
18+
- [ ] If you updated content, make sure to create a new export in the `strapi/data` folder and update the `strapi/packages.json` seed command if necessary.
1819

1920
### Related issue(s)/PR(s)
2021

strapi/scripts/prefillLoginFields.js

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,66 @@ const fs = require("fs");
22
const path = require("path");
33
const glob = require("glob");
44

5-
// Specify the directory path and the pattern to find the file
6-
const directoryPath = "./node_modules/@strapi/admin/dist/admin/";
7-
const filePattern = "index-*.mjs";
5+
// Base directory and target file patterns
6+
const directoryPath =
7+
"./node_modules/@strapi/admin/dist/admin/admin/src/pages/Auth/components";
8+
const targetFiles = ["Login.js", "Login.mjs"]; // You can add more file patterns here
89

9-
// Function to find and replace the specific content in the file
10+
// Content to replace
11+
const originalContent = `initialValues: {
12+
email: '',
13+
password: '',
14+
rememberMe: false
15+
},`;
16+
17+
const newContent = `initialValues: {
18+
19+
password: "welcomeToStrapi123",
20+
rememberMe: false
21+
},`;
22+
23+
// Function to update a given file
1024
const updateFile = (filePath) => {
11-
// Read the file content
1225
fs.readFile(filePath, "utf8", (err, data) => {
1326
if (err) {
14-
console.error(`Error reading file ${filePath}:`, err);
27+
console.error(`Error reading file ${filePath}:`, err);
1528
return;
1629
}
1730

18-
// Define the original and replacement content
19-
const originalContent = `initialValues: {
20-
email: "",
21-
password: "",
22-
rememberMe: false
23-
},`;
24-
25-
const newContent = `initialValues: {
26-
27-
password: "welcomeToStrapi123",
28-
rememberMe: false
29-
},`;
31+
if (data.includes(newContent)) {
32+
console.log(`✅ File already modified with demo credentials: ${filePath}`);
33+
return;
34+
}
3035

31-
// Check if the content exists and replace it
3236
if (data.includes(originalContent)) {
3337
const updatedData = data.replace(originalContent, newContent);
3438

35-
// Write the updated content back to the file
3639
fs.writeFile(filePath, updatedData, "utf8", (err) => {
3740
if (err) {
38-
console.error(`Error writing to file ${filePath}:`, err);
41+
console.error(`Error writing to file ${filePath}:`, err);
3942
return;
4043
}
41-
console.log(`Successfully updated the content in file: ${filePath}`);
44+
console.log(`Successfully updated: ${filePath}`);
4245
});
4346
} else {
44-
console.log(`Original content not found in file: ${filePath}`);
47+
console.log(`⚠️ Original content not found in: ${filePath}`);
4548
}
4649
});
4750
};
4851

49-
// Find the file using glob pattern matching
50-
glob(path.join(directoryPath, filePattern), (err, files) => {
51-
if (err) {
52-
console.error("Error finding files:", err);
53-
return;
54-
}
55-
56-
// Process the first file found (since index-*.mjs may match multiple files)
57-
if (files.length > 0) {
58-
const filePath = files[0];
59-
updateFile(filePath);
60-
} else {
61-
console.log("No matching files found.");
62-
}
52+
// Iterate over each file pattern
53+
targetFiles.forEach((pattern) => {
54+
glob(path.join(directoryPath, pattern), (err, files) => {
55+
if (err) {
56+
console.error("❌ Error finding files:", err);
57+
return;
58+
}
59+
60+
if (files.length > 0) {
61+
files.forEach(updateFile);
62+
} else {
63+
console.log(`⚠️ No matching files found for: ${pattern}`);
64+
process.exit(1);
65+
}
66+
});
6367
});

0 commit comments

Comments
 (0)