@coat/cli
Version:
TODO: See #3
141 lines (134 loc) • 6.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.setup = setup;
var _getContext = require("../util/get-context");
var _getAllTemplates = require("../util/get-all-templates");
var _gatherAllTasks = require("./gather-all-tasks");
var _updateLockfile = require("../lockfiles/update-lockfile");
var _getTasksToRun = require("./get-tasks-to-run");
var _coatManifestTasks = require("../types/coat-manifest-tasks");
var _removeUnmanagedTasksFromLockfile = require("./remove-unmanaged-tasks-from-lockfile");
var _writeLockfiles = require("../lockfiles/write-lockfiles");
var _lodash = require("lodash");
var _immer = _interopRequireDefault(require("immer"));
var _chalk = _interopRequireDefault(require("chalk"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Gathers and runs all tasks of a coat project.
*
* When running setup with the force = true option, all tasks will be
* run regardless of whether they have been run before.
*
* @param options.cwd The working directory of the current coat project
* @param options.check Whether a dry-run should be performed that checks
* and exits if the coat project is out of sync and has pending global tasks that need to be run
* @param options.force Whether to run tasks although they have been run before (e.g. when running the `coat setup` command directly)
*/
async function setup({
cwd,
check,
force
}) {
const checkFlag = !!check;
const forceFlag = !!force;
// Get context from cwd
let context = await (0, _getContext.getContext)(cwd);
// Gather all extended templates
const allTemplates = (0, _getAllTemplates.getAllTemplates)(context);
const allTasks = (0, _gatherAllTasks.gatherAllTasks)(allTemplates);
// TODO: See #38
// Let user interactively select which tasks should be run
const tasksToRun = await (0, _getTasksToRun.getTasksToRun)(allTasks, context, forceFlag);
// --check flag
if (checkFlag) {
// If any global task has to be run, the coat project is out of sync
const globalTasksToRun = tasksToRun.filter(task => task.type === _coatManifestTasks.CoatTaskType.Global);
if (globalTasksToRun.length) {
const messages = ["", (0, _chalk.default)`The {cyan coat} project is not in sync.`, "There are global tasks pending that need to be run to setup this coat project.", "", (0, _chalk.default)`Run {cyan coat sync} to bring the project back in sync.`];
console.error(messages.join("\n"));
process.exit(1);
}
} else {
// Run tasks sequentially to provide earlier task results
// to following tasks
for (const task of tasksToRun) {
const taskResult = await task.run({
context,
previousResults: {
global: context.coatGlobalLockfile.setup,
local: context.coatLocalLockfile.setup
}
});
const partialLockfileUpdate = {
setup: {
[task.id]: taskResult || {}
}
};
// Update the lockfile in between task runs
// to save task results in case a following task
// throws an error
switch (task.type) {
case _coatManifestTasks.CoatTaskType.Global:
{
const newGlobalLockfile = (0, _updateLockfile.updateGlobalLockfile)(context.coatGlobalLockfile, partialLockfileUpdate);
context = (0, _immer.default)(context, draft => {
draft.coatGlobalLockfile = newGlobalLockfile;
});
await (0, _writeLockfiles.writeGlobalLockfile)(newGlobalLockfile, context);
break;
}
case _coatManifestTasks.CoatTaskType.Local:
{
const newLocalLockfile = (0, _updateLockfile.updateLocalLockfile)(context.coatLocalLockfile, partialLockfileUpdate);
context = (0, _immer.default)(context, draft => {
draft.coatLocalLockfile = newLocalLockfile;
});
await (0, _writeLockfiles.writeLocalLockfile)(newLocalLockfile, context);
break;
}
// The default case is only required to let TypeScript throw
// compiler errors if a new task type is added
/* istanbul ignore next */
default:
{
const unhandledTask = task;
throw new Error(`Unhandled task type for task: ${unhandledTask}`);
}
}
}
}
// Remove task results that are from tasks that are
// no longer managed by coat
//
// global lockfile
const newGlobalLockfile = (0, _removeUnmanagedTasksFromLockfile.removeUnmanagedTasksFromLockfile)(context.coatGlobalLockfile, allTasks.filter(task => task.type === _coatManifestTasks.CoatTaskType.Global));
if (!(0, _lodash.isEqual)(context.coatGlobalLockfile, newGlobalLockfile)) {
//
// --check flag
if (checkFlag) {
// If the global lockfile needs to be updated,
// the coat project is out of sync
const messages = ["", (0, _chalk.default)`The {cyan coat} project is not in sync.`, (0, _chalk.default)`The global lockfile ({green coat.lock}) needs to be updated.`, "", (0, _chalk.default)`Run {cyan coat sync} to bring the project back in sync.`];
console.error(messages.join("\n"));
process.exit(1);
}
context = (0, _immer.default)(context, draft => {
draft.coatGlobalLockfile = newGlobalLockfile;
});
await (0, _writeLockfiles.writeGlobalLockfile)(newGlobalLockfile, context);
}
// Local lockfile should only be updated if the setup method is not invoked with --check
if (!checkFlag) {
// local lockfile
const newLocalLockfile = (0, _removeUnmanagedTasksFromLockfile.removeUnmanagedTasksFromLockfile)(context.coatLocalLockfile, allTasks.filter(task => task.type === _coatManifestTasks.CoatTaskType.Local));
if (!(0, _lodash.isEqual)(context.coatLocalLockfile, newLocalLockfile)) {
context = (0, _immer.default)(context, draft => {
draft.coatLocalLockfile = newLocalLockfile;
});
await (0, _writeLockfiles.writeLocalLockfile)(newLocalLockfile, context);
}
}
return context;
}