Skip to content

Instantly share code, notes, and snippets.

@k1sul1
Created November 30, 2016 15:27
Show Gist options
  • Save k1sul1/808e0b60638472138713e393a939b861 to your computer and use it in GitHub Desktop.
Save k1sul1/808e0b60638472138713e393a939b861 to your computer and use it in GitHub Desktop.
Script to run after npm install, sets variables to .env file, and the variables can be later utilized.
const fs = require('fs');
const nl = require('os').EOL;
const LineByLineReader = require('line-by-line');
const cp = require('child_process');
afterInstall('.env', 'utf8');
function afterInstall(filename, encoding) {
const envVars = {
'GIT_COMMIT_HASH': {
exists: false,
value: undefined
}
};
getHash().then(value => {
envVars.GIT_COMMIT_HASH.value = value;
parseFile(filename, encoding, envVars);
// If you add more values that need to be set later on, refactor this bit
// so that parseFile is triggered after they are all complete.
// I'm too lazy to do it now. Just setInterval and check that none
// of the values are undefined and then continue.
});
}
function parseFile(filename, encoding, envVars) {
const variables = envVars;
const keys = Object.keys(variables);
const lr = new LineByLineReader(filename, { encoding: encoding });
const step1 = () => {
// First, read the file, and check if any of the envvars exist already.
readFile(filename, encoding).then(data => {
keys.forEach(key => {
if (data.indexOf(key) > -1) {
variables[key].exists = true;
}
});
step2();
}).catch(err => console.error(err));
};
const step2 = () => {
// Then, read the file line-by-line, and if the value exists, replace it.
lr.on('error', err => console.error('Line reader failed: ', err));
lr.on('line', line => {
lr.pause();
keys.forEach(key => {
if (line.indexOf(key) > -1) {
replaceInFile(line, `${key}=${variables[key].value}`, filename, encoding).then((response) => {
console.log(`Replaced ${key} value with ${variables[key].value}`);
variables[key].exists = true;
lr.resume();
}).catch(err => {
console.error(err);
lr.resume();
});
} else {
/* Line didn't match our keys. Ignore it */
lr.resume();
}
});
});
lr.on('end', step3);
};
const step3 = () => {
// Loop through the object once more and check for anything that has not been set.
keys.forEach(key => {
if (variables[key].exists === false) {
addToFile(key, variables[key].value, filename, encoding)
.then(response => {
variables[key].exists = true;
console.log(`Added ${key}=${variables[key].value} to the file`);
})
.catch(err => {
console.error(err);
});
}
});
};
step1();
}
function getHash() {
return new Promise((resolve, reject) => {
cp.exec('git rev-parse HEAD', (err, stdout, stderr) => {
if (err) {
reject(err);
} else if (stderr) {
reject(err);
}
resolve(stdout);
});
});
}
function replaceInFile(line, new_line, filename, encoding) {
return new Promise((resolve, reject) => {
readFile(filename, encoding).then(data => {
const newdata = data.replace(line, new_line);
fs.writeFile(filename, newdata, encoding, err => {
if (err) {
reject(err);
}
resolve('replaced');
});
}).catch(err => {
reject(err);
});
});
}
function addToFile(key, value, filename, encoding) {
return new Promise((resolve, reject) => {
readFile(filename, encoding).then(data => {
const newdata = `${data}${key}=${value}${nl}`;
fs.writeFile(filename, newdata, encoding, err => {
if (err) {
reject(err);
}
resolve('added');
});
}).catch(err => {
if (err) {
reject(err);
}
})
});
}
function readFile(filename, encoding) {
return new Promise((resolve, reject) => {
fs.readFile(filename, encoding, (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment