Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/generate-loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ generateLoader();
### CLI (via `webpack-cli`)

```bash
npx webpack-cli generate-loader
npx webpack-cli loader
```

> Optionally specify a path for generating the loader template.

```bash
npx webpack-cli loader [path]
```

[downloads]: https://img.shields.io/npm/dm/@webpack-cli/generate-loader.svg
Expand Down
5 changes: 3 additions & 2 deletions packages/generate-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const { logger } = utils;
* @returns {void}
*/

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

env.registerStub(loaderGenerator, generatorName);
Expand Down
72 changes: 65 additions & 7 deletions test/loader/loader.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { existsSync } = require('fs');
const { existsSync, mkdirSync } = require('fs');
const { join, resolve } = require('path');
const rimraf = require('rimraf');
const stripAnsi = require('strip-ansi');
Expand All @@ -10,13 +10,16 @@ const firstPrompt = '? Loader name (my-loader)';
const ENTER = '\x0D';
const loaderName = 'test-loader';
const loaderPath = join(__dirname, loaderName);
const genPath = join(__dirname, 'test-assets');
const customLoaderPath = join(genPath, loaderName);

describe('loader command', () => {
beforeAll(() => {
beforeEach(() => {
rimraf.sync(loaderPath);
rimraf.sync(genPath);
});

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

// check if the output directory exists with the appropriate loader name
expect(existsSync(join(__dirname, loaderName))).toBeTruthy();
// Check if the output directory exists with the appropriate loader name
expect(existsSync(loaderPath)).toBeTruthy();

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

files.forEach((file) => {
expect(existsSync(join(__dirname, `${loaderName}/${file}`))).toBeTruthy();
expect(existsSync(loaderPath, file)).toBeTruthy();
});

//check if the the generated plugin works successfully
// Check if the the generated loader works successfully
const path = resolve(__dirname, './test-loader/examples/simple/');
({ stdout } = run(path, [], false));
expect(stdout).toContain('test-loader');
});

it('should scaffold loader template in the specified path', async () => {
let { stdout } = await runPromptWithAnswers(__dirname, ['loader', 'test-assets'], [`${loaderName}${ENTER}`]);

expect(stripAnsi(stdout)).toContain(firstPrompt);

// Skip test in case installation fails
if (!existsSync(resolve(customLoaderPath, './yarn.lock'))) {
return;
}

// Check if the output directory exists with the appropriate loader name
expect(existsSync(customLoaderPath)).toBeTruthy();

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

files.forEach((file) => {
expect(existsSync(customLoaderPath, file)).toBeTruthy();
});

// Check if the the generated loader works successfully
const path = resolve(customLoaderPath, './examples/simple/');
({ stdout } = run(path, [], false));
expect(stdout).toContain('test-loader');
});

it('should scaffold loader template in the current directory', async () => {
// Create test-assets directory
mkdirSync(genPath);

let { stdout } = await runPromptWithAnswers(genPath, ['loader', './'], [`${loaderName}${ENTER}`]);

expect(stripAnsi(stdout)).toContain(firstPrompt);

// Skip test in case installation fails
if (!existsSync(resolve(customLoaderPath, './yarn.lock'))) {
return;
}

// Check if the output directory exists with the appropriate loader name
expect(existsSync(customLoaderPath)).toBeTruthy();

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

files.forEach((file) => {
expect(existsSync(customLoaderPath, file)).toBeTruthy();
});

// Check if the the generated loader works successfully
const path = resolve(customLoaderPath, './examples/simple/');
({ stdout } = run(path, [], false));
expect(stdout).toContain('test-loader');
});
});