feat: add makeJSOnlyValue to generator API (#3568)

Provides convenience method for passing JS into config files.
Closes issue #3535.
This commit is contained in:
Steve Workman
2019-04-09 12:54:17 +01:00
committed by Haoqun Jiang
parent cb113971e2
commit f69339e816
3 changed files with 38 additions and 0 deletions
+8
View File
@@ -97,6 +97,14 @@ Add a message to be printed when the generator exits (after any other standard m
- **Usage**:
Convenience method for generating a JS config file from JSON
## makeJSOnlyValue
- **Arguments**
- `{any} str` - JS expression as a string
- **Usage**:
Turns a string expression into executable JS for .js config files
## injectImports
- **Arguments**
@@ -656,3 +656,23 @@ test('extract config files', async () => {
expect(fs.readFileSync('/jest.config.js', 'utf-8')).toMatch(js(configs.jest))
expect(fs.readFileSync('/.browserslistrc', 'utf-8')).toMatch('> 1%\nnot <= IE8')
})
test('generate a JS-Only value from a string', async () => {
const jsAsString = 'true ? "alice" : "bob"'
const generator = new Generator('/', { plugins: [
{
id: 'test',
apply: api => {
api.extendPackage({
testScript: api.makeJSOnlyValue(jsAsString)
})
}
}
] })
await generator.generate({})
expect(generator.pkg).toHaveProperty('testScript')
expect(typeof generator.pkg.testScript).toBe('function')
})
+10
View File
@@ -240,6 +240,16 @@ class GeneratorAPI {
return `module.exports = ${stringifyJS(value, null, 2)}`
}
/**
* Turns a string expression into executable JS for JS configs.
* @param {*} str JS expression as a string
*/
makeJSOnlyValue (str) {
const fn = () => {}
fn.__expression = str
return fn
}
/**
* Add import statements to a file.
*/