Stephan Herzog 01855bd2ed Added sub-generator setup-env #278 (#308)
This subgen creates a new Webpack config environment by

- creating conf/webpack/<EnvName>.js
- creating src/config/<env_name>.js
- requiring and exporting the new env in conf/webpack/index.js

The commit introduces a basic config template that is supposed to be populated by the generator's users. Various more fine grained subgen options can be added at a later time (e.g. prompting the user if new run scripts should be added to package.json).

The subgen's basic functionality is backed up by unit tests that check

- if files are created
- if conf/webpack/index.js contains correct import/export
2016-11-22 19:38:13 +01:00

78 lines
2.0 KiB
JavaScript

'use strict';
const acorn = require('acorn');
const assert = require('yeoman-assert');
const fs = require('fs-extra');
const helpers = require('yeoman-test');
const path = require('path');
const walk = require('acorn/dist/walk');
/**
* Returns absolute path to (sub-)generator with {@code name}.
* @param {string} name
*/
const genpath = (name) =>
path.join(__dirname, '../../../generators', name);
/**
* A mocked generator config object.
* @type {{appName: string, style: string, cssmodules: boolean, postcss: boolean, generatedWithVersion: number}}
*/
const cfg = {
appName: 'testCfg',
style: 'css',
cssmodules: false,
postcss: false,
generatedWithVersion: 4
};
describe('react-webpack:setup-env', function () {
describe('react-webpack:setup-env foobar', function () {
before(function () {
return helpers
.run(genpath('setup-env'))
.withArguments(['foobar'])
.withLocalConfig(cfg)
.inTmpDir(function (dir) {
fs.copySync(
path.join(__dirname, 'assets/moduleIndex.js'),
path.join(dir, 'conf/webpack/index.js')
);
})
.toPromise();
});
it('creates env files', function () {
assert.file(['conf/webpack/Foobar.js']);
assert.file(['src/config/foobar.js']);
});
it('requires the new env in conf/webpack/index.js', function () {
assert.fileContent(
'conf/webpack/index.js',
/const foobar = require\('\.\/Foobar'\)/
);
});
it('exports the new env from conf/webpack/index.js', function () {
const fileStr = fs.readFileSync('conf/webpack/index.js').toString();
const ast = acorn.parse(fileStr);
let found = false;
walk.simple(ast, {
'Property': (node) => {
if (node.key.name === 'foobar' && node.value.name === 'foobar') {
found = true;
}
}
});
assert(found, 'Did not find a key and value of `foobar` on the module.exports AST node');
});
});
});