Skip to content

Commit

Permalink
feat: enable to override log messages and theme with a config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Feb 12, 2024
1 parent be0955c commit 9034164
Show file tree
Hide file tree
Showing 79 changed files with 1,289 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.log
node_modules
!test/fixtures/errors/bad-gulp-version/node_modules/
!test/fixtures/config/theming/error/badGulpVersion/node_modules/
build
*.node
components
Expand Down
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var theme = require('./lib/shared/log/theme');
var msgs = require('./lib/shared/log/messages');

var mergeProjectAndUserHomeConfigs = require('./lib/shared/config/merge-configs');
var overrideEnvFlagsByConfigAndCliOpts = require('./lib/shared/config/env-flags');
var overrideEnvByConfigAndCliOpts = require('./lib/shared/config/env-config');

// Get supported ranges
var ranges = fs.readdirSync(path.join(__dirname, '/lib/versioned/'));
Expand Down Expand Up @@ -100,7 +100,7 @@ module.exports = run;

function onPrepare(env) {
var cfg = mergeProjectAndUserHomeConfigs(env);
env = overrideEnvFlagsByConfigAndCliOpts(env, cfg, opts);
env = overrideEnvByConfigAndCliOpts(env, cfg, opts);

// Set up event listeners for logging again after configuring.
toConsole(log, env.config.flags);
Expand Down Expand Up @@ -131,15 +131,13 @@ function onExecute(env) {
}

if (!env.modulePath) {
/* istanbul ignore next */
var missingNodeModules =
fs.existsSync(path.join(env.cwd, 'package.json'))
&& !fs.existsSync(path.join(env.cwd, 'node_modules'));

var hasYarn = fs.existsSync(path.join(env.cwd, 'yarn.lock'));
var hasNpm = !hasYarn;

/* istanbul ignore if */
if (missingNodeModules) {
log.error(msgs.error.nodeModulesNotFound, tildify(env.cwd), hasYarn, hasNpm);
} else {
Expand Down
15 changes: 13 additions & 2 deletions lib/shared/config/env-flags.js → lib/shared/config/env-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ var copyProps = require('copy-props');

var mergeCliOpts = require('./cli-flags');

var theme = require('../log/theme');
var msgs = require('../log/messages');

var toEnvFromConfig = {
configPath: 'flags.gulpfile',
configBase: 'flags.gulpfile',
preload: 'flags.preload',
nodeFlags: 'flags.nodeFlags',
};

function overrideEnvFlags(env, config, cliOpts) {
function overrideEnvConfig(env, config, cliOpts) {
cliOpts = mergeCliOpts(cliOpts, config);

// This must reverse because `flags.gulpfile` determines 2 different properties
Expand All @@ -21,10 +24,18 @@ function overrideEnvFlags(env, config, cliOpts) {

env.config = {
flags: cliOpts,
theme: theme,
msgs: msgs,
};
if (config.description) {
env.config.description = config.description;
}
if (config.theme) {
copyProps(config.theme, env.config.theme);
}
if (config.msgs) {
copyProps(config.msgs, env.config.msgs);
}
return env

function convert(configInfo, envInfo) {
Expand Down Expand Up @@ -53,4 +64,4 @@ function overrideEnvFlags(env, config, cliOpts) {
}
}

module.exports = overrideEnvFlags;
module.exports = overrideEnvConfig;
1 change: 1 addition & 0 deletions lib/shared/config/merge-configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var path = require('path');

function mergeConfigs(env) {
var cfg = {};
/* istanbul ignore if */
if (env.configFiles.userHome) {
copyConfig(env.config.userHome, cfg, env.configFiles.userHome);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/log/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ module.exports = {
'{IF:{3:has cause}?\n{TIMESTAMP}{ERROR: {4:cause}}}',

taskNotFound:
'{TIMESTAMP}{ERROR: Task never defined: {1:task}}\n' +
'{TIMESTAMP}{ERROR: Task never defined: {1:target task}{IF:{2:has similar tasks}? - did you mean? {3:similar tasks}}\n' +
'{TIMESTAMP}{ERROR: To list available tasks, try running: gulp --tasks}',

failToRun:
Expand Down
4 changes: 2 additions & 2 deletions lib/shared/log/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function logTasks(tree, opts, getTask) {
var maxDepth = opts.tasksDepth;
if (typeof maxDepth !== 'number') {
maxDepth = 50;
} else if (maxDepth < 1) {
} else /* istanbul ignore if */ if (maxDepth < 1) {
maxDepth = 1;
}

Expand All @@ -38,7 +38,7 @@ function printTaskTree(tree, opts) {
tree.nodes.forEach(function(node, idx, arr) {
var isLast = idx === arr.length - 1;
var w = createTreeLines(node, lines, opts, 1, '', isLast);
maxLabelWidth = Math.max(maxLabelWidth, w || 0);
maxLabelWidth = Math.max(maxLabelWidth, w || /* istanbul ignore next */ 0);
});

lines.forEach(function(line) {
Expand Down
2 changes: 2 additions & 0 deletions lib/shared/log/timestamp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

function timestamp(format) {
/* istanbul ignore if */
if (typeof format !== 'string') {
return noop;
}
Expand Down Expand Up @@ -42,6 +43,7 @@ function timestamp(format) {
return result;
}

/* istanbul ignore next */
function noop() {
return "";
}
Expand Down
5 changes: 3 additions & 2 deletions lib/shared/log/to-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ var levels = [
];

function consoleLog(s) {
if (s != null) console.log(s);
if (s) console.log(s);
}

function consoleError(s) {
if (s != null) console.error(s);
/* istanbul ignore else */
if (s) console.error(s);
}

function cleanup(log) {
Expand Down
4 changes: 2 additions & 2 deletions lib/versioned/^3.7.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ function execute(env) {
}
if (opts.tasks) {
tree = taskTree(gulpInst.tasks);
tree.label = getTasksDescription(env);
tree.label = getTasksDescription(env, true);
return logTasks(tree, opts, function(task) {
return gulpInst.tasks[task].fn;
});
}
if (opts.tasksJson) {
tree = taskTree(gulpInst.tasks);
tree.label = getTasksDescription(env);
tree.label = getTasksDescription(env, false);

var output = JSON.stringify(copyTree(tree, opts));

Expand Down
12 changes: 10 additions & 2 deletions lib/versioned/^3.7.0/log/tasks-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ var tildify = require('../../../shared/tildify');
var theme = require('../../../shared/log/theme');
var msgs = require('../../../shared/log/messages');

function getTasksDescription(env) {
function getTasksDescription(env, isBackslashEscaping) {
var desc;
if (env.config.description && typeof env.config.description === 'string') {
desc = env.config.description;
} else {
desc = msgs.tasks.gulpfile;
}
return format(theme, desc, tildify(env.configPath));
var gulpfile = tildify(env.configPath);
if (isBackslashEscaping) {
gulpfile = escapeBackslash(gulpfile);
}
return format(theme, desc, gulpfile);
}

function escapeBackslash(s) {
return s.replace(/\\/g, '\\\\');
}

module.exports = getTasksDescription;
10 changes: 5 additions & 5 deletions lib/versioned/^4.0.0-alpha.1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function execute(env) {
}
if (opts.tasks) {
tree = {};
tree.label = getTasksDescription(env);
tree.label = getTasksDescription(env, true);
tree.nodes = gulpInst.tree({ deep: true });

return logTasks(tree, opts, function(taskname) {
Expand All @@ -64,7 +64,7 @@ function execute(env) {
}
if (opts.tasksJson) {
tree = {};
tree.label = getTasksDescription(env);
tree.label = getTasksDescription(env, false);
tree.nodes = gulpInst.tree({ deep: true });

var output = JSON.stringify(copyTree(tree, opts));
Expand All @@ -82,9 +82,9 @@ function execute(env) {
}
});
} catch (err) {
var taskName = checkTaskNotFound(err);
if (taskName) {
log.error(msgs.error.taskNotFound, taskName);
var task = checkTaskNotFound(err);
if (task) {
log.error(msgs.error.taskNotFound, task.target, Boolean(task.similar), task.similar);
} else {
log.error(msgs.error.failToRun, err.message);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/versioned/^4.0.0-alpha.2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ function execute(env) {
}
if (opts.tasks) {
tree = gulpInst.tree({ deep: true });
tree.label = getTasksDescription(env);
tree.label = getTasksDescription(env, true);
return logTasks(tree, opts, getTask(gulpInst));
}
if (opts.tasksJson) {
tree = gulpInst.tree({ deep: true });
tree.label = getTasksDescription(env);
tree.label = getTasksDescription(env, false);

var output = JSON.stringify(copyTree(tree, opts));

Expand All @@ -80,9 +80,9 @@ function execute(env) {
}
});
} catch (err) {
var taskName = checkTaskNotFound(err);
if (taskName) {
log.error(msgs.error.taskNotFound, taskName);
var task = checkTaskNotFound(err);
if (task) {
log.error(msgs.error.taskNotFound, task.target, Boolean(task.similar), task.similar);
} else {
log.error(msgs.error.failToRun, err.message);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/versioned/^4.0.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ function execute(env) {
}
if (opts.tasks) {
tree = gulpInst.tree({ deep: true });
tree.label = getTasksDescription(env);
tree.label = getTasksDescription(env, true);
return logTasks(tree, opts, getTask(gulpInst));
}
if (opts.tasksJson) {
tree = gulpInst.tree({ deep: true });
tree.label = getTasksDescription(env);
tree.label = getTasksDescription(env, false);

var output = JSON.stringify(copyTree(tree, opts));

Expand All @@ -80,9 +80,9 @@ function execute(env) {
}
});
} catch (err) {
var taskName = checkTaskNotFound(err);
if (taskName) {
log.error(msgs.error.taskNotFound, taskName);
var task = checkTaskNotFound(err);
if (task) {
log.error(msgs.error.taskNotFound, task.target, Boolean(task.similar), task.similar);
} else {
log.error(msgs.error.failToRun, err.message);
}
Expand Down
22 changes: 18 additions & 4 deletions lib/versioned/^4.0.0/log/check-task-not-found.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
'use strict';

function checkTaskNotFound(err) {
var result = /^Task never defined: +(.*)$/.exec(err.message);
/* istanbul ignore else */
if (result) {
return result[1];
/* istanbul ignore if */
if (!err || !err.message) {
return undefined;
}
var fixed0 = 'Task never defined: ';
var fixed1 = ' - did you mean? ';

if (err.message.startsWith(fixed0)) {
var target = err.message.slice(fixed0.length);
var similar = undefined;

var index = target.indexOf(fixed1);
if (index >= 0) {
similar = target.slice(index + fixed1.length);
target = target.slice(0, index);
}

return { target: target, similar: similar };
}
}

Expand Down
12 changes: 10 additions & 2 deletions lib/versioned/^4.0.0/log/tasks-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ var tildify = require('../../../shared/tildify');
var theme = require('../../../shared/log/theme');
var msgs = require('../../../shared/log/messages');

function getTasksDescription(env) {
function getTasksDescription(env, isBackslashEscaping) {
var desc;
if (env.config.description && typeof env.config.description === 'string') {
desc = env.config.description;
} else {
desc = msgs.tasks.gulpfile;
}
return format(theme, desc, tildify(env.configPath));
var gulpfile = tildify(env.configPath);
if (isBackslashEscaping) {
gulpfile = escapeBackslash(gulpfile);
}
return format(theme, desc, gulpfile);
}

function escapeBackslash(s) {
return s.replace(/\\/g, '\\\\');
}

module.exports = getTasksDescription;

0 comments on commit 9034164

Please sign in to comment.