@onereach/get-version-data
Version:
CLI tool to get version data for deploy
67 lines (57 loc) • 1.69 kB
JavaScript
;
const fs = require('fs/promises');
const git = require('./git');
const {getOneReachDependencies} = require('./lock/parse');
const {resolve} = require('path');
module.exports = {
async getData (rootPath, options = {}) {
const addGit = options.git !== false;
const addDp = options.dp !== false;
const [packages, packageJson, gitData, dpData] = await Promise.all([
getOneReachDependencies(rootPath, options),
getPackageJson(rootPath),
addGit ? getGitData() : null,
addDp ? getDeployPlatformData() : null,
]);
return {
service: packageJson.name,
version: packageJson.version,
feature: options.feature ?? process.env.FEATURE ?? 'master',
tools: {
...addDp ? {'deploy-platform': dpData} : null,
},
deploymentTime: getDeploymentTime(),
...addGit ? {git: gitData} : null,
packages,
};
}
}
async function getGitData () {
const [branch, commitHash, bundleTags, isDirty] = await Promise.all([
git.getBranch(),
git.getCommitHash(),
git.getAllBundleTags(),
git.getIsDirty(),
]);
return {
branch,
commitHash,
bundleTags,
isDirty,
};
}
function getDeploymentTime () {
return new Date().toISOString();
}
async function getPackageJson (rootPath) {
return await readJsonFile(resolve(rootPath, 'package.json'));
}
async function getDeployPlatformData() {
const orRoot = process.env.OR_ROOT;
if (!orRoot) return;
const {version} = await readJsonFile(resolve(orRoot, 'deploy-platform', 'package.json'))
return {version};
}
async function readJsonFile(filePath) {
return JSON.parse(await fs.readFile(filePath, 'utf8'));
}