UNPKG

@coat/cli

Version:

TODO: See #3

81 lines (78 loc) 4.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCoatGlobalLockfile = getCoatGlobalLockfile; exports.getCoatLocalLockfile = getCoatLocalLockfile; var _fs = require("fs"); var _path = _interopRequireDefault(require("path")); var _jsYaml = _interopRequireDefault(require("js-yaml")); var _chalk = _interopRequireDefault(require("chalk")); var _constants = require("../constants"); var _getStrictCoatLockfiles = require("./get-strict-coat-lockfiles"); var _validators = require("../generated/validators"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const initialCoatGlobalLockfile = { version: _constants.COAT_GLOBAL_LOCKFILE_VERSION }; const initialCoatLocalLockfile = { version: _constants.COAT_LOCAL_LOCKFILE_VERSION }; /** * Retrieves the parsed version of the coat global lockfile * that is placed in project-dir/coat.lock * * If no lockfile exists (yet), an initial empty lockfile is returned * * @param cwd Working directory of the coat project */ async function getCoatGlobalLockfile(cwd) { let lockfile = initialCoatGlobalLockfile; try { const lockfileRaw = await _fs.promises.readFile(_path.default.join(cwd, _constants.COAT_GLOBAL_LOCKFILE_PATH), "utf-8"); lockfile = _jsYaml.default.load(lockfileRaw); } catch (error) { // Throw if error is anything other than "not found" if (error.code !== "ENOENT") { throw error; } } if (lockfile.version > _constants.COAT_GLOBAL_LOCKFILE_VERSION) { console.warn((0, _chalk.default)`Warning! The global lockfile {green ${_constants.COAT_GLOBAL_LOCKFILE_PATH}} version (${lockfile.version}) is higher than the expected version (${_constants.COAT_GLOBAL_LOCKFILE_VERSION}) by the currently running cli. Please ensure that you are running the newest version of the {cyan @coat/cli} since the current project might not be backwards compatible with the current cli version.`); // The lockfile is only validated if the version equals the current lockfile version, // since the schema is highly likely to have changed if a lockfile version bump has happened // and validation therefore would be pointless. } else if (!(0, _validators.validateCoatGlobalLockfile)(lockfile)) { console.warn((0, _chalk.default)`{yellow Warning!} The global lockfile {green ${_constants.COAT_GLOBAL_LOCKFILE_PATH}} does not conform to the expected schema! Consider deleting and regenerating the lockfile by running {cyan coat sync} in case you run into any issues.`); } return (0, _getStrictCoatLockfiles.getStrictCoatGlobalLockfile)(lockfile); } /** * Retrieves the parsed version of the coat local lockfile * that is placed in project-dir/.coat/coat.lock * * If no lockfile exists (yet), an initial empty lockfile is returned * * @param cwd Working directory of the coat project */ async function getCoatLocalLockfile(cwd) { let lockfile = initialCoatLocalLockfile; try { const lockfileRaw = await _fs.promises.readFile(_path.default.join(cwd, _constants.COAT_LOCAL_LOCKFILE_PATH), "utf-8"); lockfile = _jsYaml.default.load(lockfileRaw); } catch (error) { // Throw if error is anything other than "not found" if (error.code !== "ENOENT") { throw error; } } if (lockfile.version > _constants.COAT_LOCAL_LOCKFILE_VERSION) { console.warn((0, _chalk.default)`Warning! The local lockfile {green ${_constants.COAT_LOCAL_LOCKFILE_PATH}} version (${lockfile.version}) is higher than the expected version (${_constants.COAT_LOCAL_LOCKFILE_VERSION}) by the currently running cli. Please ensure that you are running the newest version of the {cyan @coat/cli} since the current project might not be backwards compatible with the current cli version.`); // The lockfile is only validated if the version equals the current lockfile version, // since the schema is highly likely to have changed if a lockfile version bump has happened // and validation therefore would be pointless. } else if (!(0, _validators.validateCoatLocalLockfile)(lockfile)) { console.warn((0, _chalk.default)`{yellow Warning!} The local lockfile {green ${_constants.COAT_LOCAL_LOCKFILE_PATH}} does not conform to the expected schema! Consider deleting and regenerating the lockfile by running {cyan coat sync} in case you run into any issues.`); } return (0, _getStrictCoatLockfiles.getStrictCoatLocalLockfile)(lockfile); }