Skip to content

Commit cd09f85

Browse files
feat(generate-loader): scaffold in a specified path (#2072)
* feat: add the ability to specify a path for scaffolding * tests: add test case for scaffolding in a specified path * docs: update usage info * tests: add test case for supplying relative path
1 parent 2c159ad commit cd09f85

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

packages/generate-loader/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ generateLoader();
2727
### CLI (via `webpack-cli`)
2828

2929
```bash
30-
npx webpack-cli generate-loader
30+
npx webpack-cli loader
31+
```
32+
33+
> Optionally specify a path for generating the loader template.
34+
35+
```bash
36+
npx webpack-cli loader [path]
3137
```
3238

3339
[downloads]: https://img.shields.io/npm/dm/@webpack-cli/generate-loader.svg

packages/generate-loader/src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ const { logger } = utils;
99
* @returns {void}
1010
*/
1111

12-
export default function loaderCreator(): void {
13-
const env = yeoman.createEnv();
12+
export default function loaderCreator(...args: string[]): void {
13+
const generationPath = args[0];
14+
const env = yeoman.createEnv([], { cwd: generationPath });
1415
const generatorName = 'webpack-loader-generator';
1516

1617
env.registerStub(loaderGenerator, generatorName);

test/loader/loader.test.js

+65-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { existsSync } = require('fs');
3+
const { existsSync, mkdirSync } = require('fs');
44
const { join, resolve } = require('path');
55
const rimraf = require('rimraf');
66
const stripAnsi = require('strip-ansi');
@@ -10,13 +10,16 @@ const firstPrompt = '? Loader name (my-loader)';
1010
const ENTER = '\x0D';
1111
const loaderName = 'test-loader';
1212
const loaderPath = join(__dirname, loaderName);
13+
const genPath = join(__dirname, 'test-assets');
14+
const customLoaderPath = join(genPath, loaderName);
1315

1416
describe('loader command', () => {
15-
beforeAll(() => {
17+
beforeEach(() => {
1618
rimraf.sync(loaderPath);
19+
rimraf.sync(genPath);
1720
});
1821

19-
it('Should ask the loader name when invoked', () => {
22+
it('should ask the loader name when invoked', () => {
2023
const { stdout, stderr } = run(__dirname, ['loader'], false);
2124
expect(stdout).toBeTruthy();
2225
expect(stderr).toBeFalsy();
@@ -33,19 +36,74 @@ describe('loader command', () => {
3336
return;
3437
}
3538

36-
// check if the output directory exists with the appropriate loader name
37-
expect(existsSync(join(__dirname, loaderName))).toBeTruthy();
39+
// Check if the output directory exists with the appropriate loader name
40+
expect(existsSync(loaderPath)).toBeTruthy();
3841

3942
// All test files are scaffolded
4043
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
4144

4245
files.forEach((file) => {
43-
expect(existsSync(join(__dirname, `${loaderName}/${file}`))).toBeTruthy();
46+
expect(existsSync(loaderPath, file)).toBeTruthy();
4447
});
4548

46-
//check if the the generated plugin works successfully
49+
// Check if the the generated loader works successfully
4750
const path = resolve(__dirname, './test-loader/examples/simple/');
4851
({ stdout } = run(path, [], false));
4952
expect(stdout).toContain('test-loader');
5053
});
54+
55+
it('should scaffold loader template in the specified path', async () => {
56+
let { stdout } = await runPromptWithAnswers(__dirname, ['loader', 'test-assets'], [`${loaderName}${ENTER}`]);
57+
58+
expect(stripAnsi(stdout)).toContain(firstPrompt);
59+
60+
// Skip test in case installation fails
61+
if (!existsSync(resolve(customLoaderPath, './yarn.lock'))) {
62+
return;
63+
}
64+
65+
// Check if the output directory exists with the appropriate loader name
66+
expect(existsSync(customLoaderPath)).toBeTruthy();
67+
68+
// All test files are scaffolded
69+
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
70+
71+
files.forEach((file) => {
72+
expect(existsSync(customLoaderPath, file)).toBeTruthy();
73+
});
74+
75+
// Check if the the generated loader works successfully
76+
const path = resolve(customLoaderPath, './examples/simple/');
77+
({ stdout } = run(path, [], false));
78+
expect(stdout).toContain('test-loader');
79+
});
80+
81+
it('should scaffold loader template in the current directory', async () => {
82+
// Create test-assets directory
83+
mkdirSync(genPath);
84+
85+
let { stdout } = await runPromptWithAnswers(genPath, ['loader', './'], [`${loaderName}${ENTER}`]);
86+
87+
expect(stripAnsi(stdout)).toContain(firstPrompt);
88+
89+
// Skip test in case installation fails
90+
if (!existsSync(resolve(customLoaderPath, './yarn.lock'))) {
91+
return;
92+
}
93+
94+
// Check if the output directory exists with the appropriate loader name
95+
expect(existsSync(customLoaderPath)).toBeTruthy();
96+
97+
// All test files are scaffolded
98+
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
99+
100+
files.forEach((file) => {
101+
expect(existsSync(customLoaderPath, file)).toBeTruthy();
102+
});
103+
104+
// Check if the the generated loader works successfully
105+
const path = resolve(customLoaderPath, './examples/simple/');
106+
({ stdout } = run(path, [], false));
107+
expect(stdout).toContain('test-loader');
108+
});
51109
});

0 commit comments

Comments
 (0)