|
1 | 1 | const path = require('path'); |
| 2 | +const fs = require('fs'); |
2 | 3 |
|
3 | 4 | const { parse: parseHbs, print: printHbs } = require('ember-template-recast'); |
4 | 5 | const { determineThisUsage } = require('./helpers/determine-this-usage'); |
5 | | -const { getOptions } = require('codemod-cli'); |
| 6 | +const { getOptions: getCLIOptions } = require('codemod-cli'); |
6 | 7 | const DEFAULT_OPTIONS = { |
7 | 8 | dontAssumeThis: false, |
8 | 9 | }; |
9 | 10 |
|
| 11 | +/** |
| 12 | + * Accepts the config path for custom helpers and returns the array of helpers |
| 13 | + * if the file path is resolved. |
| 14 | + * Context: This will allow the users to specify their custom list of helpers |
| 15 | + * along with the known helpers, this would give them more flexibility for handling |
| 16 | + * special usecases. |
| 17 | + * @param {string} configPath |
| 18 | + */ |
| 19 | +function _getCustomHelpersFromConfig(configPath) { |
| 20 | + let customHelpers = []; |
| 21 | + if (configPath) { |
| 22 | + let filePath = path.join(process.cwd(), configPath); |
| 23 | + let config = JSON.parse(fs.readFileSync(filePath)); |
| 24 | + if (config.helpers) { |
| 25 | + customHelpers = config.helpers; |
| 26 | + } |
| 27 | + } |
| 28 | + return customHelpers; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Returns custom options object to support the custom helpers config path passed |
| 33 | + * by the user. |
| 34 | + */ |
| 35 | +function getOptions() { |
| 36 | + let cliOptions = getCLIOptions(); |
| 37 | + let options = { |
| 38 | + dontAssumeThis: cliOptions.dontAssumeThis, |
| 39 | + customHelpers: _getCustomHelpersFromConfig(cliOptions.config), |
| 40 | + }; |
| 41 | + return options; |
| 42 | +} |
| 43 | + |
10 | 44 | module.exports = function transformer(file /*, api */) { |
11 | 45 | let extension = path.extname(file.path); |
12 | 46 | let options = Object.assign({}, DEFAULT_OPTIONS, getOptions()); |
|
0 commit comments