Skip to content

Commit 658e372

Browse files
committed
feat(cli): defined proper config for a cli project
1 parent 2e83a7e commit 658e372

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

src/config-scaffolder.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export default async function ({projectRoot, dialect, projectType}) {
1717
`${projectRoot}/rollup.config.${(determineExtensionFor({dialect}))}`,
1818
mustache.render(
1919
await fs.readFile(resolve(__dirname, '..', 'templates', 'rollup.config.mustache'), 'utf-8'),
20-
{dualMode: dialect !== dialects.ESM}
20+
{
21+
dualMode: dialect !== dialects.ESM,
22+
cli: projectTypes.CLI === projectType
23+
}
2124
)
2225
);
2326

src/config-scaffolder.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ describe('config scaffolder', () => {
2424

2525
it('should generate the file and determine additional plugins', async () => {
2626
const dialect = any.word();
27-
when(mustache.render).calledWith(template, {dualMode: true}).mockReturnValue(renderedTemplate);
27+
when(mustache.render).calledWith(template, {dualMode: true, cli: false}).mockReturnValue(renderedTemplate);
2828

2929
expect(await scaffoldConfig({projectRoot, dialect})).toEqual({});
3030

3131
expect(fs.writeFile).toHaveBeenCalledWith(`${projectRoot}/rollup.config.mjs`, renderedTemplate);
3232
});
3333

3434
it('should use a `.js` extension for the config when the dialect is ESM', async () => {
35-
when(mustache.render).calledWith(template, {dualMode: false}).mockReturnValue(renderedTemplate);
35+
when(mustache.render).calledWith(template, {dualMode: false, cli: false}).mockReturnValue(renderedTemplate);
3636

3737
await scaffoldConfig({projectRoot, dialect: dialects.ESM});
3838

@@ -41,9 +41,11 @@ describe('config scaffolder', () => {
4141

4242
it('should adjust for CLI projects', async () => {
4343
const dialect = any.word();
44+
when(mustache.render).calledWith(template, {dualMode: true, cli: true}).mockReturnValue(renderedTemplate);
4445

4546
expect(await scaffoldConfig({projectRoot, dialect, projectType: projectTypes.CLI})).toEqual({
4647
devDependencies: ['@rollup/plugin-json', 'rollup-plugin-executable']
4748
});
49+
expect(fs.writeFile).toHaveBeenCalledWith(`${projectRoot}/rollup.config.mjs`, renderedTemplate);
4850
});
4951
});

templates/rollup.config.mustache

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import autoExternal from 'rollup-plugin-auto-external';
1+
import autoExternal from 'rollup-plugin-auto-external';{{#cli}}
2+
import json from '@rollup/plugin-json';
3+
import executable from 'rollup-plugin-executable';{{/cli}}
24

35
export default {
46
input: 'src/index.js',
5-
plugins: [autoExternal()],
6-
output: [{{#dualMode}}
7+
plugins: [
8+
autoExternal(){{#cli}},
9+
json(),
10+
executable(){{/cli}}
11+
],
12+
output: [{{^cli}}{{#dualMode}}
713
{file: 'lib/index.js', format: 'cjs', sourcemap: true},{{/dualMode}}
8-
{file: 'lib/index.mjs', format: 'esm', sourcemap: true}
14+
{file: 'lib/index.mjs', format: 'esm', sourcemap: true}{{/cli}}{{#cli}}
15+
{file: 'bin/travi.js', format: 'esm', sourcemap: true, banner: '#!/usr/bin/env node'}{{/cli}}
916
]
1017
};

test/integration/features/cli.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Feature: Scaffold for a CLI project-type
55
When the project is scaffolded
66
Then dependencies are installed for a "CLI" project-type
77
And the config is generated with an "mjs" extension
8+
And an CLI bundle is generated

test/integration/features/step_definitions/config-steps.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {assert} from 'chai';
22
import {promises as fs} from 'fs';
33
import {Then} from '@cucumber/cucumber';
44

5-
import {autoExternal} from './dependencies-steps.js';
5+
import {autoExternal, pluginExecutable, pluginJson} from './dependencies-steps.js';
66

77
Then('the config is generated with a(n) {string} extension', async function (extension) {
88
this.config = await fs.readFile(`${process.cwd()}/rollup.config.${extension}`, 'utf-8');
@@ -15,7 +15,9 @@ Then('dual-mode bundles are generated', async function () {
1515
1616
export default {
1717
input: 'src/index.js',
18-
plugins: [autoExternal()],
18+
plugins: [
19+
autoExternal()
20+
],
1921
output: [
2022
{file: 'lib/index.js', format: 'cjs', sourcemap: true},
2123
{file: 'lib/index.mjs', format: 'esm', sourcemap: true}
@@ -32,11 +34,35 @@ Then('an ESM bundle is generated', async function () {
3234
3335
export default {
3436
input: 'src/index.js',
35-
plugins: [autoExternal()],
37+
plugins: [
38+
autoExternal()
39+
],
3640
output: [
3741
{file: 'lib/index.mjs', format: 'esm', sourcemap: true}
3842
]
3943
};
4044
`
4145
);
4246
});
47+
48+
Then('an CLI bundle is generated', async function () {
49+
assert.equal(
50+
this.config,
51+
`import autoExternal from '${autoExternal}';
52+
import json from '${pluginJson}';
53+
import executable from '${pluginExecutable}';
54+
55+
export default {
56+
input: 'src/index.js',
57+
plugins: [
58+
autoExternal(),
59+
json(),
60+
executable()
61+
],
62+
output: [
63+
{file: 'bin/travi.js', format: 'esm', sourcemap: true, banner: '#!/usr/bin/env node'}
64+
]
65+
};
66+
`
67+
);
68+
});

test/integration/features/step_definitions/dependencies-steps.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {Then} from '@cucumber/cucumber';
44
import {assert} from 'chai';
55

66
export const autoExternal = 'rollup-plugin-auto-external';
7+
export const pluginJson = '@rollup/plugin-json';
8+
export const pluginExecutable = 'rollup-plugin-executable';
79

810
Then('dependencies are installed', async function () {
911
const {devDependencies} = this.scaffoldResult;
@@ -16,8 +18,8 @@ Then('dependencies are installed for a {string} project-type', async function (p
1618
const {devDependencies} = this.scaffoldResult;
1719

1820
if (projectTypes.CLI === projectType) {
19-
assert.include(devDependencies, '@rollup/plugin-json');
20-
assert.include(devDependencies, 'rollup-plugin-executable');
21+
assert.include(devDependencies, pluginJson);
22+
assert.include(devDependencies, pluginExecutable);
2123
}
2224
});
2325

0 commit comments

Comments
 (0)