MCPcopy
hub / github.com/ampproject/amphtml / createTask

Function createTask

build-system/task-runner/amp-task-runner.js:211–242  ·  view source on GitHub ↗

* Helper that creates the tasks in AMP's toolchain based on the invocation: * - For `amp --help`, load all task descriptions so a list can be printed. * - For `amp --help`, load and print just the task description + flags. * - When a task is actually run, update root packages, load the ent

(
  taskName,
  taskFuncName = taskName,
  taskSourceFileName = taskName
)

Source from the content-addressed store, hash-verified

209 * @param {string=} taskSourceFileName
210 */
211function createTask(
212 taskName,
213 taskFuncName = taskName,
214 taskSourceFileName = taskName
215) {
216 const isInvokedTask = argv._.includes(taskName); // `amp <task>`
217 const isDefaultTask =
218 argv._.length === 0 && taskName == 'default' && !isHelpTask; // `amp`
219
220 if (isHelpTask) {
221 const task = program.command(cyan(taskName));
222 const description = getTaskDescription(taskSourceFileName, taskFuncName);
223 task.description(description);
224 }
225 if (isInvokedTask || isDefaultTask) {
226 startAtRepoRoot();
227 ensureUpdatedPackages(taskSourceFileName);
228 const taskFunc = getTaskFunc(taskSourceFileName, taskFuncName);
229 const task = program.command(taskName, {isDefault: isDefaultTask});
230 task.description(green(taskFunc.description));
231 task.allowUnknownOption(); // Fall through to validateUsage()
232 task.helpOption('--help', 'Print this list of flags');
233 task.usage('<flags>');
234 for (const [flag, description] of Object.entries(taskFunc.flags ?? {})) {
235 task.option(`--${cyan(flag)}`, description);
236 }
237 task.action(async () => {
238 validateUsage(task, taskName, taskFunc);
239 await runTask(taskName, taskFunc);
240 });
241 }
242}
243
244/**
245 * Validates usage by examining task and flag invocation.

Callers 1

amp.jsFile · 0.85

Calls 9

cyanFunction · 0.85
getTaskDescriptionFunction · 0.85
startAtRepoRootFunction · 0.85
ensureUpdatedPackagesFunction · 0.85
getTaskFuncFunction · 0.85
greenFunction · 0.85
validateUsageFunction · 0.85
runTaskFunction · 0.70
entriesMethod · 0.45

Tested by

no test coverage detected