@ou-imdt/create
Version:
Command line tool to create team boilerplate.
50 lines (44 loc) • 1.52 kB
JavaScript
const os = require('os');
const fs = require('fs');
const path = require('path');
const execa = require('execa');
/**
* @typedef {Object} Credentials
* @property {string} developerName - Developer name.
* @property {string} developerEmail - Developer email.
*/
/**
* Check Credentials.
* @return {Object || null} Credentials
*/
module.exports = async() => {
/**
* Check cache credentials.
* @return {Object || null} Credentials
*/
function checkCacheCredentials() {
const cacheFolderPath = path.join(os.homedir(), `.cache/imdt`);
if(!fs.existsSync(cacheFolderPath)) return null;
const cacheNameFile = path.join(os.homedir(), `.cache/imdt/developerName.json`);
const cacheEmailFile = path.join(os.homedir(), `.cache/imdt/developerEmail.json`);
if (fs.existsSync(cacheNameFile) && fs.existsSync(cacheEmailFile)) {
const cacheNameData = JSON.parse(fs.readFileSync(cacheNameFile, 'utf-8'));
const cacheEmailData = JSON.parse(fs.readFileSync(cacheEmailFile, 'utf-8'));
return {
developerName: cacheNameData.values.past[0],
developerEmail: cacheEmailData.values.past[0]
};
}
}
// Check Git credentials
const {stdout: developerName} = await execa('git config user.name');
const {stdout: developerEmail} = await execa('git config user.email')
if(developerName !== '' && developerEmail !== '') {
return {
developerName,
developerEmail
}
}
// Check cache credentials and return
return checkCacheCredentials();
};