-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-env.cjs
53 lines (47 loc) · 2.1 KB
/
create-env.cjs
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
const crypto = require('crypto');
const fs = require('fs');
const { argv } = require('node:process');
const os = require('os');
const path = require('path');
const strapiEnvPath = path.join(__dirname, 'packages', 'strapi', '.env');
const clientEnvPath = path.join(__dirname, 'packages', 'client', '.env.development.local');
if (fs.existsSync(strapiEnvPath) && fs.existsSync(clientEnvPath)) {
console.error('The .env files already exist. Please delete them and run the script again.');
} else {
const modifyEnvFile = (filePath, fieldsToModify) => {
/* fieldsToModify = [[searchString, wantedValue]], e.g: [['[PORT]', 5000], ['[change-me]', 'randomValue']] */
const currentValues = fs.readFileSync(filePath).toString().split('\n');
let modifiedValues = currentValues;
fieldsToModify.forEach(([searchString, wantedValue]) => {
modifiedValues = modifiedValues.map((line) =>
line.includes(searchString) ? line.replaceAll(searchString, wantedValue) : line,
);
});
return modifiedValues.join('\n');
};
// taking care of the '.env' file related to the strapi package
const strapiEnvValues = modifyEnvFile(
path.join(__dirname, 'packages', 'strapi', '.env.example'),
[
['[change-me]', () => crypto.randomBytes(16).toString('base64')],
['[PORT]', argv[2] || 5000],
],
);
fs.writeFileSync(strapiEnvPath, strapiEnvValues);
const pkgJson = fs.readFileSync(
path.join(__dirname, 'packages', 'strapi', 'package.json'),
'utf8',
);
const pkgJsonObject = JSON.parse(pkgJson);
pkgJsonObject.strapi.uuid = crypto.randomUUID();
fs.writeFileSync(
path.join(__dirname, 'packages', 'strapi', 'package.json'),
JSON.stringify(pkgJsonObject, null, 2) + os.EOL,
);
// taking care of the '.env.local' file related to the client package
const clientEnvValues = modifyEnvFile(
path.join(__dirname, 'packages', 'client', '.env.example'),
[['[PORT]', argv[2] || 5000]],
);
fs.writeFileSync(clientEnvPath, clientEnvValues);
}