Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
feat(generator): update base-module template (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephanie Coates authored Mar 6, 2020
1 parent e1e0c7d commit 1a0af74
Show file tree
Hide file tree
Showing 34 changed files with 1,051 additions and 89 deletions.
33 changes: 32 additions & 1 deletion packages/generator-one-app-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,43 @@ Assuming you have [npx installed](https://medium.com/@maybekatz/introducing-npx-
npx -p yo -p @americanexpress/generator-one-app-module -- yo @americanexpress/one-app-module
```

The generator will start up and prompt you with the following questions:
- What is the name of your module?
- Is this a [root module or child module](https://github.com/americanexpress/one-app/blob/master/docs/api/API.md#modules)?
- Generate with [Parrot Middleware](https://github.com/americanexpress/parrot)?
- Setup with [internationalization](https://github.com/americanexpress/one-app/blob/master/docs/api/modules/Internationalization.md)?

#### Optional Flags
##### --setupInternationalizationByDefault
If you'd like to include internationalization in your module without being prompted, you can pass in the `--setupInternationalizationByDefault` flag. Your command will look as follows:
```
yo @americanexpress/one-app-module --setupInternationalizationByDefault
```

Alternatively, if you choose to compose `generator-one-app-module` with another generator, you can pass the `setupInternationalizationByDefault` value in an object as the second argument to the Yeoman `ComposeWith` function, like so:

```
const CustomExtension = require.resolve('./path-to-custom-extension');
module.exports = class extends Generator {
initializing() {
this.composeWith(require.resolve('@americanexpress/generator-one-app-module/generators/app'),
{ setupInternationalizationByDefault: true });
this.composeWith(CustomExtension);
}
};
```

Doing this, you're able to extend the `generator-one-app-module` generator, enable internationalization by default, and add additional prompts and logic in your custom extension.

[More on composing generators here](https://yeoman.io/authoring/composability.html).

## 🏆 Contributing

After making changes to the generator, test your changes locally:
1. Install yeoman globally: `npm install --global yo`
2. Link your local generator module so it is used instead of the repo version: `npm link`
3. Switch to a temp directory and run: `yo one-app-module`
3. Switch to a temp directory and run: `yo @americanexpress/one-app-module`
4. When you are done, unlink the local module : `npm unlink`

Please see our [contributing guide](../../CONTRIBUTING.md) for more details.
301 changes: 273 additions & 28 deletions packages/generator-one-app-module/__tests__/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,283 @@ const assert = require('yeoman-assert');
const helpers = require('yeoman-test');

describe('generator-one-app-module', () => {
describe('module creation', () => {
describe('providing module name when prompted', () => {
beforeAll(() => helpers.run(path.join(__dirname, '../generators/app'))
.withPrompts({ moduleName: 'my-module' })
.toPromise()
);
describe('basic child module creation', () => {
beforeAll(() => helpers.run(path.join(__dirname, '../generators/app'))
.withPrompts({
moduleName: 'my-basic-child-module',
moduleType: 'child module',
setupInternationalization: 'No',
setupParrotMiddleware: 'No',
})
.toPromise()
);
it('creates files', () => {
assert.file([
'__tests__/components/MyBasicChildModule.spec.jsx',
'__tests__/.eslintrc.json',
'__tests__/index.spec.js',
'src/components/MyBasicChildModule.jsx',
'src/index.js',
'.babelrc',
'.eslintrc.json',
'.gitignore',
'package.json',
'README.md',
]);
});
it('provides src/components/DefaultModule.jsx as default when no prompt is provided', () => helpers
.run(path.join(__dirname, '../generators/app')).then(() => {
assert.file(['src/components/DefaultModule.jsx']);
}));
});

describe('basic root module creation', () => {
beforeAll(() => helpers.run(path.join(__dirname, '../generators/app'))
.withPrompts({
moduleName: 'my-basic-root-module',
moduleType: 'root module',
setupInternationalization: 'No',
setupParrotMiddleware: 'No',
})
.toPromise()
);
it('creates files', () => {
assert.file([
'__tests__/components/MyBasicRootModule.spec.jsx',
'__tests__/appConfig.spec.js',
'__tests__/index.spec.js',
'__tests__/.eslintrc.json',
'src/components/MyBasicRootModule.jsx',
'src/index.js',
'src/csp.js',
'src/childRoutes.jsx',
'src/appConfig.js',
'.babelrc',
'.eslintrc.json',
'.gitignore',
'package.json',
'README.md',
]);
});
it('installs csp dependencies', () => {
assert.jsonFileContent('package.json', {
dependencies: {
'content-security-policy-builder': '^2.1.0',
},
});
});
});

describe('intl child module creation', () => {
beforeAll(() => helpers.run(path.join(__dirname, '../generators/app'))
.withPrompts({
moduleName: 'my-intl-child-module',
moduleType: 'child module',
setupInternationalization: 'Yes',
setupParrotMiddleware: 'No',
})
.toPromise()
);
it('creates files', () => {
assert.file([
'__tests__/components/MyIntlChildModule.spec.jsx',
'__tests__/.eslintrc.json',
'__tests__/index.spec.js',
'__tests__/locale.spec.js',
'locale/en-CA.json',
'locale/en-US.json',
'locale/es-MX.json',
'src/components/MyIntlChildModule.jsx',
'src/index.js',
'test-setup.js',
'.babelrc',
'.eslintrc.json',
'.gitignore',
'package.json',
'README.md',
]);
});
it('installs intl dependencies and test scripts', () => {
assert.jsonFileContent('package.json', {
dependencies: {
'@americanexpress/one-app-ducks': '^4.0.0',
immutable: '^3.8.2',
'prop-types': '^15.5.9',
'react-intl': '^3.6.0',
'react-redux': '^7.1.3',
redux: '^4.0.4',
},
devDependencies: {
glob: '^7.1.6',
'@babel/polyfill': '^7.8.3',
},
jest: {
setupFilesAfterEnv: './test-setup.js',
},
});
});
it('adds react-intl as an external', () => {
assert.jsonFileContent('package.json', {
'one-amex': {
bundler: {
externals: 'react-intl',
},
},
});
});
});

it('creates files', () => {
assert.file([
'src/components/MyModule.jsx',
'src/index.js',
'src/csp.js',
'.babelrc',
'.eslintrc.json',
'.gitignore',
'package.json',
'README.md',
]);
describe('intl root module creation', () => {
beforeAll(() => helpers.run(path.join(__dirname, '../generators/app'))
.withPrompts({
moduleName: 'my-intl-root-module',
moduleType: 'root module',
setupInternationalization: 'Yes',
setupParrotMiddleware: 'No',
})
.toPromise()
);
it('creates files', () => {
assert.file([
'__tests__/components/MyIntlRootModule.spec.jsx',
'__tests__/appConfig.spec.js',
'__tests__/.eslintrc.json',
'__tests__/index.spec.js',
'__tests__/locale.spec.js',
'locale/en-CA.json',
'locale/en-US.json',
'locale/es-MX.json',
'src/components/MyIntlRootModule.jsx',
'src/index.js',
'src/csp.js',
'src/childRoutes.jsx',
'src/appConfig.js',
'test-setup.js',
'.babelrc',
'.eslintrc.json',
'.gitignore',
'package.json',
'README.md',
]);
});
it('installs intl dependencies and test scripts', () => {
assert.jsonFileContent('package.json', {
dependencies: {
'@americanexpress/one-app-ducks': '^4.0.0',
immutable: '^3.8.2',
'prop-types': '^15.5.9',
'react-intl': '^3.6.0',
'react-redux': '^7.1.3',
redux: '^4.0.4',
},
devDependencies: {
glob: '^7.1.6',
'@babel/polyfill': '^7.8.3',
},
jest: {
setupFilesAfterEnv: './test-setup.js',
},
});
});
it('provides react-intl as an external for child modules', () => {
assert.jsonFileContent('package.json', {
'one-amex': {
bundler: {
providesExternals: 'react-intl',
},
},
});
});
});

describe('creating src/components file', () => {
it('creates src/components/<ModuleName>.jsx file', () => helpers
.run(path.join(__dirname, '../generators/app'))
.withPrompts({ moduleName: 'my-special-module' })
.then(() => {
assert.file(['src/components/MySpecialModule.jsx']);
}));
it('provides src/components/DefaultModule.jsx as default when no prompt is provided', () => helpers
.run(path.join(__dirname, '../generators/app')).then(() => {
assert.file(['src/components/DefaultModule.jsx']);
}));
describe('intl child with parrot module creation', () => {
beforeAll(() => helpers.run(path.join(__dirname, '../generators/app'))
.withPrompts({
moduleName: 'my-intl-child-parrot-module',
moduleType: 'child module',
setupInternationalization: 'Yes',
setupParrotMiddleware: 'Yes',
})
.toPromise()
);
it('creates files', () => {
assert.file([
'__tests__/components/MyIntlChildParrotModule.spec.jsx',
'__tests__/.eslintrc.json',
'__tests__/index.spec.js',
'__tests__/locale.spec.js',
'mock/scenarios.js',
'locale/en-CA.json',
'locale/en-US.json',
'locale/es-MX.json',
'src/components/MyIntlChildParrotModule.jsx',
'src/index.js',
'test-setup.js',
'.babelrc',
'.eslintrc.json',
'dev.middleware.js',
'.gitignore',
'package.json',
'README.md',
]);
});
it('adds parrot-middleware dependency and runner script', () => {
assert.jsonFileContent('package.json', {
'one-amex': {
runner: {
parrotMiddleware: './dev.middleware.js',
},
},
devDependencies: {
'parrot-middleware': '^3.1.0',
},
});
});
});
describe('child with setupInternationalizationByDefault option module creation', () => {
beforeAll(() => helpers.run(path.join(__dirname, '../generators/app'))
.withOptions({
setupInternationalizationByDefault: 'my-intl-by-default-child',
})
.withPrompts({
moduleName: 'my-intl-by-default-child',
moduleType: 'child module',
setupParrotMiddleware: 'Yes',
})
.toPromise()
);
it('creates files', () => {
assert.file([
'__tests__/components/MyIntlByDefaultChild.spec.jsx',
'__tests__/.eslintrc.json',
'__tests__/index.spec.js',
'__tests__/locale.spec.js',
'mock/scenarios.js',
'locale/en-CA.json',
'locale/en-US.json',
'locale/es-MX.json',
'src/components/MyIntlByDefaultChild.jsx',
'src/index.js',
'test-setup.js',
'.babelrc',
'.eslintrc.json',
'dev.middleware.js',
'.gitignore',
'package.json',
'README.md',
]);
});
it('adds parrot-middleware dependency and runner script', () => {
assert.jsonFileContent('package.json', {
'one-amex': {
runner: {
parrotMiddleware: './dev.middleware.js',
},
},
devDependencies: {
'parrot-middleware': '^3.1.0',
},
});
});
});
});
Loading

0 comments on commit 1a0af74

Please sign in to comment.