@speedy-js/mono
Version:
Monorepo development & continuous integration tooling.
110 lines • 3.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolvePackageJson = exports.cleanLernaConfig = exports.ensureLernaConfig = exports.resolveLernaConfig = exports.safelyRequireJson = exports.requireJson = exports.gitPush = exports.execa = void 0;
/**
* Module dependencies
*/
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const execa_1 = __importDefault(require("execa"));
exports.execa = execa_1.default;
const LERNA_CONFIG = 'lerna.json';
const MONO_CONFIG = 'mono.json';
/**
* Git push
*
* @returns {Promise<void>}
*/
async function gitPush() {
await (0, execa_1.default)('git', ['push'], { stdio: 'inherit' });
}
exports.gitPush = gitPush;
/**
* Require json file with no cache
*
* @param {String} jsonPath
* @returns {Promise<Object>}
*/
function requireJson(jsonPath) {
delete require.cache[jsonPath];
const json = require(jsonPath);
return json;
}
exports.requireJson = requireJson;
/**
* Safely require json file with no cache
*
* @param {String} jsonPath
* @returns {Promise<Object>}
*/
function safelyRequireJson(jsonPath) {
if ((0, fs_extra_1.existsSync)(jsonPath)) {
return requireJson(jsonPath);
}
return {};
}
exports.safelyRequireJson = safelyRequireJson;
/**
* Resolve content of `lerna.json` or `mono.json`
*
* @param {String} cwd
* @returns {Object}
*/
function resolveLernaConfig(cwd = process.cwd()) {
const lernaConfigPath = (0, path_1.join)(cwd, LERNA_CONFIG);
if ((0, fs_extra_1.existsSync)(lernaConfigPath)) {
return {
path: lernaConfigPath,
data: requireJson(lernaConfigPath),
};
}
const monoConfigPath = (0, path_1.join)(cwd, MONO_CONFIG);
if ((0, fs_extra_1.existsSync)(monoConfigPath)) {
return {
path: monoConfigPath,
data: requireJson(monoConfigPath),
};
}
}
exports.resolveLernaConfig = resolveLernaConfig;
/**
* Ensure lerna.json exists
*/
function ensureLernaConfig(cwd) {
const lernaConfig = resolveLernaConfig(cwd);
if (lernaConfig.path.endsWith(MONO_CONFIG)) {
(0, fs_extra_1.writeFileSync)((0, path_1.join)(cwd, LERNA_CONFIG), JSON.stringify(lernaConfig.data, null, 2));
}
}
exports.ensureLernaConfig = ensureLernaConfig;
/**
* Ensure that lerna.json does not exists when 'mono.json' exists.
*/
function cleanLernaConfig(cwd) {
const monoConfigPath = (0, path_1.join)(cwd, MONO_CONFIG);
const lernaConfigPath = (0, path_1.join)(cwd, LERNA_CONFIG);
if ((0, fs_extra_1.existsSync)(monoConfigPath)) {
const lernaConfig = resolveLernaConfig(cwd);
if (lernaConfig.path.endsWith(LERNA_CONFIG)) {
(0, fs_extra_1.writeFileSync)(monoConfigPath, JSON.stringify(lernaConfig.data, null, 2));
(0, fs_extra_1.unlinkSync)(lernaConfigPath);
}
}
}
exports.cleanLernaConfig = cleanLernaConfig;
/**
*
* Resolve content of `lerna.json`
*
* @param {String} cwd
* @returns {Object}
*/
function resolvePackageJson(cwd = process.cwd()) {
const pkgJsonPath = (0, path_1.join)(cwd, 'package.json');
return safelyRequireJson(pkgJsonPath);
}
exports.resolvePackageJson = resolvePackageJson;
//# sourceMappingURL=helpers.js.map