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

Function getTaskDescription

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

* Uses esprima to extract the description from a task file without require-ing * it, by tokenizing the JS file and finding the sequence that matches the text * "fooTask.description = ' '". * @param {string} taskSourceFileName * @param {string} taskFuncName * @return {string}

(taskSourceFileName, taskFuncName)

Source from the content-addressed store, hash-verified

171 * @return {string}
172 */
173function getTaskDescription(taskSourceFileName, taskFuncName) {
174 const taskSourceFilePath = getTaskSourceFilePath(taskSourceFileName);
175 const taskSourceFile = fs.existsSync(`${taskSourceFilePath}.js`)
176 ? `${taskSourceFilePath}.js`
177 : path.join(taskSourceFilePath, 'index.js');
178 const contents = fs.readFileSync(taskSourceFile, 'utf-8').trim();
179 const tokens = Array.from(esprima.tokenize(contents));
180 let description = '';
181 for (let i = 0; i < tokens.length; ++i) {
182 if (
183 tokens[i]?.type == 'Identifier' &&
184 tokens[i]?.value == taskFuncName &&
185 tokens[i + 1]?.type == 'Punctuator' &&
186 tokens[i + 1]?.value == '.' &&
187 tokens[i + 2]?.type == 'Identifier' &&
188 tokens[i + 2]?.value == 'description' &&
189 tokens[i + 3]?.type == 'Punctuator' &&
190 tokens[i + 3]?.value == '=' &&
191 tokens[i + 4]?.type == 'String'
192 ) {
193 description = tokens[i + 4]?.value
194 ?.replace(/^['"]/, '')
195 ?.replace(/['"]$/, '');
196 }
197 }
198 return description;
199}
200
201/**
202 * Helper that creates the tasks in AMP's toolchain based on the invocation:

Callers 1

createTaskFunction · 0.85

Calls 2

getTaskSourceFilePathFunction · 0.85
replaceMethod · 0.45

Tested by

no test coverage detected