* 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 )
| 209 | * @param {string=} taskSourceFileName |
| 210 | */ |
| 211 | function 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. |
no test coverage detected