* 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)
| 171 | * @return {string} |
| 172 | */ |
| 173 | function 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: |
no test coverage detected