@coat/cli
Version:
TODO: See #3
110 lines (106 loc) • 4.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.writeGlobalLockfile = writeGlobalLockfile;
exports.writeLocalLockfile = writeLocalLockfile;
var _fsExtra = _interopRequireDefault(require("fs-extra"));
var _immer = require("immer");
var _path = _interopRequireDefault(require("path"));
var _constants = require("../constants");
var _yaml = require("../file-types/yaml");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function stripEmptyPropertiesGlobal(lockfile) {
return (0, _immer.produce)(lockfile, draft => {
if (!lockfile.files.length) {
delete draft.files;
} else {
var _draft$files;
// Only place the "once" property if it is true
(_draft$files = draft.files) === null || _draft$files === void 0 ? void 0 : _draft$files.forEach(file => {
if ("once" in file && !file.once) {
// eslint-disable-next-line no-param-reassign
delete file.once;
}
});
}
if (!Object.keys(lockfile.setup).length) {
draft === null || draft === void 0 ? true : delete draft.setup;
}
if (!lockfile.scripts.length) {
delete draft.scripts;
}
if (!lockfile.dependencies.dependencies.length) {
var _draft$dependencies;
(_draft$dependencies = draft.dependencies) === null || _draft$dependencies === void 0 ? true : delete _draft$dependencies.dependencies;
}
if (!lockfile.dependencies.devDependencies.length) {
var _draft$dependencies2;
(_draft$dependencies2 = draft.dependencies) === null || _draft$dependencies2 === void 0 ? true : delete _draft$dependencies2.devDependencies;
}
if (!lockfile.dependencies.peerDependencies.length) {
var _draft$dependencies3;
(_draft$dependencies3 = draft.dependencies) === null || _draft$dependencies3 === void 0 ? true : delete _draft$dependencies3.peerDependencies;
}
if (!lockfile.dependencies.optionalDependencies.length) {
var _draft$dependencies4;
(_draft$dependencies4 = draft.dependencies) === null || _draft$dependencies4 === void 0 ? true : delete _draft$dependencies4.optionalDependencies;
}
// If no dependency key exists, the dependencies
// property should also be deleted
if (!Object.keys(draft.dependencies).length) {
delete draft.dependencies;
}
});
}
function stripEmptyPropertiesLocal(lockfile) {
return (0, _immer.produce)(lockfile, draft => {
if (!lockfile.files.length) {
delete draft.files;
} else {
var _draft$files2;
// Only place the "once" property if it is true
(_draft$files2 = draft.files) === null || _draft$files2 === void 0 ? void 0 : _draft$files2.forEach(file => {
if ("once" in file && !file.once) {
// eslint-disable-next-line no-param-reassign
delete file.once;
}
});
}
if (draft.setup && !Object.keys(draft.setup).length) {
delete draft.setup;
}
});
}
async function writeLockfile(coatLockfile, lockfilePath, context) {
const lockfileContent = _yaml.yamlFileFunctions.polish(coatLockfile, lockfilePath, context);
await _fsExtra.default.outputFile(lockfilePath, lockfileContent);
}
/**
* Strips all unnecessary properties from the provided lockfile and
* writes the result to the global lockfile location:
* project-dir/coat.lock
*
* @param coatLockfile The new global lockfile that should be written to the disk
* @param context The context of the current coat project
*/
async function writeGlobalLockfile(coatLockfile, context) {
const lockfilePath = _path.default.join(context.cwd, _constants.COAT_GLOBAL_LOCKFILE_PATH);
// Strip properties to not save empty properties onto the disk
const leanLockfile = stripEmptyPropertiesGlobal(coatLockfile);
await writeLockfile(leanLockfile, lockfilePath, context);
}
/**
* Strips all unnecessary properties from the provided lockfile and
* writes the result to the local lockfile location:
* project-dir/.coat/coat.lock
*
* @param coatLockfile The new local lockfile that should be written to the disk
* @param context The context of the current coat project
*/
async function writeLocalLockfile(coatLockfile, context) {
const lockfilePath = _path.default.join(context.cwd, _constants.COAT_LOCAL_LOCKFILE_PATH);
// Strip properties to not save empty properties onto the disk
const leanLockfile = stripEmptyPropertiesLocal(coatLockfile);
await writeLockfile(leanLockfile, lockfilePath, context);
}