Repository navigation
Expand file tree
/
Copy pathpostinstall.js
More file actions
63 lines (55 loc) · 1.74 KB
/
Copy pathpostinstall.js
File metadata and controls
63 lines (55 loc) · 1.74 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
#!/bin/node
const fs = require('fs');
const cwd = process.env.INIT_CWD
const getTemplateEnv = () => {
const envFile = cwd + "/node_modules/@myelastic/indexer/.env.example";
return envFile;
}
const getMainFile = () => {
const main = cwd + "/node_modules/@myelastic/indexer/lib/indexer.js";
return main;
}
const getCliFile = () => {
const cli = cwd + "/node_modules/@myelastic/indexer/lib/cli/myelastic.js";
return cli;
}
const copyFile = function (source, target, overwrite=false, executable=false) {
if (fs.existsSync(target) && overwrite) {
fs.copyFileSync(source, target);
if (executable) {
fs.chmodSync(target, "755");
}
} else {
// do nothing since we don't want to overwrite a file
}
}
const requireDotEnv = function (file, envReference) {
const source = cwd + '/.env';
const requireDotEnv = `require('dotenv').config({ path: ${envReference} });`;
if (fs.existsSync(source) && fs.existsSync(file) && !fs.readFileSync(file).toString().match(/require\('dotenv'\)/gm)) {
var fileLineByLine = fs.readFileSync(file).toString().match(/^.+$/gm);
fileLineByLine[1] = fileLineByLine[1].concat(`\n${requireDotEnv}\n`);
fs.writeFileSync(file, fileLineByLine.join("\n"));
}
};
const copyEnvAndSetLocationInMain = () => {
const source = getTemplateEnv();
const target = cwd + "/.env";
copyFile(source, target, false);
requireDotEnv(getMainFile(), "__dirname + '/../../../../.env'");
}
const setCliEnvLocation = () => {
const target = getCliFile();
requireDotEnv(target, "__dirname + '/../../../../../.env'");
}
const createBinDir = () => {
if(!fs.existsSync(cwd + '/bin')) {
fs.mkdirSync(cwd + '/bin');
}
}
try {
copyEnvAndSetLocationInMain();
setCliEnvLocation();
} catch (e) {
console.log(e);
}