Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit f7672e8

Browse files
author
Walker
committed
Update to improve writer interface
Add support for a single string file path instead of array Throw useful error if relative path given to file
1 parent 0012a0a commit f7672e8

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

lib/writer.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33
const { Readable } = require('stream');
44
const { identifyCssModule, bundleCssModule } = require('./util');
55
const { existsSync } = require('fs');
6+
const { isAbsolute } = require('path');
67
const assert = require('assert');
78

89
module.exports = class Writer extends Readable {
910
constructor (files = []) {
10-
assert(Array.isArray(files), `Expected 'files' to be of type 'Array', instead got '${typeof files}'`);
11-
for (const file of files) {
11+
super({ objectMode: true });
12+
13+
assert(
14+
Array.isArray(files) || typeof files === 'string',
15+
`Expected 'files' to be of type 'Array' or a 'string', instead got '${typeof this.files}'`
16+
);
17+
this.files = Array.isArray(files) ? files : [files];
18+
for (const file of this.files) {
1219
assert(typeof file === 'string', `Expected 'file' (${file}) to be of type 'string', instead got '${typeof file}'`);
20+
assert(isAbsolute(file), `Expected 'file' (${file}) to be an absolute path to a file but it was not`);
1321
assert(existsSync(file), `Expected 'file' (${file}) to exist on file system but it did not`);
1422
}
15-
16-
super({ objectMode: true });
17-
this.files = files;
1823
}
1924

2025
_read () {

test/writer.test.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,34 @@ test('bundleCssModule(filePath)', async () => {
4545
expect(result).toMatch('dep/main.css');
4646
});
4747

48+
test('new Writer(filePath)', () => {
49+
const filePath = path.join(__dirname, 'test-assets/my-module-1/main.css');
50+
const fileRef = 'my-module-1/main.css';
51+
52+
const writer = new Writer(filePath);
53+
const items = [];
54+
55+
writer.on('data', item => {
56+
items.push(item);
57+
});
58+
59+
writer.on('end', () => {
60+
const result1 = items[0];
61+
const result2 = items[1];
62+
63+
expect(result1.id).toBe(hasher(`my-module-1|1.0.1|${fileRef}`));
64+
expect(result2).toBeFalsy();
65+
});
66+
});
67+
68+
test('new Writer(filePath) relative paths throw error', () => {
69+
const filePath = './test-assets/my-module-1/main.css';
70+
71+
const result = () => new Writer(filePath);
72+
73+
expect(result).toThrow();
74+
});
75+
4876
test('new Writer([filePath])', () => {
4977
const filePath = path.join(__dirname, 'test-assets/my-module-1/main.css');
5078
const fileRef = 'my-module-1/main.css';
@@ -112,14 +140,30 @@ test('new Writer([filePath1, filePath2]) ensures correct order', () => {
112140
});
113141
});
114142

115-
test('new Writer([filePath]) ensures filePath[] is an array', () => {
143+
test('new Writer([filePath]) ensures filePath[] is not null', () => {
116144
const filePath = null;
117145

118146
const result = () => new Writer(filePath);
119147

120148
expect(result).toThrow();
121149
});
122150

151+
test('new Writer([filePath]) ensures filePath[] is not a number', () => {
152+
const filePath = 2;
153+
154+
const result = () => new Writer(filePath);
155+
156+
expect(result).toThrow();
157+
});
158+
159+
test('new Writer([filePath]) ensures filePath is not an object', () => {
160+
const filePath = {};
161+
162+
const result = () => new Writer(filePath);
163+
164+
expect(result).toThrow();
165+
});
166+
123167
test('new Writer([filePath]) ensures filePath[] is an array of strings', () => {
124168
const filePath = null;
125169

@@ -128,7 +172,15 @@ test('new Writer([filePath]) ensures filePath[] is an array of strings', () => {
128172
expect(result).toThrow();
129173
});
130174

131-
test('new Writer([filePath]) ensures valid filePaths provided', () => {
175+
test('new Writer([filePath]) ensures valid filePath provided', () => {
176+
const filePath = 'fake.css';
177+
178+
const result = () => new Writer(filePath);
179+
180+
expect(result).toThrow();
181+
});
182+
183+
test('new Writer([filePath]) ensures valid filePaths provided in array', () => {
132184
const filePath = 'fake.css';
133185

134186
const result = () => new Writer([filePath]);

0 commit comments

Comments
 (0)