Skip to content

Commit

Permalink
core: add option to use custom encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Feb 20, 2017
1 parent 67726e0 commit 90c5c1e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ class LicensePlugin {
const exists = fs.existsSync(filePath);

if (exists) {
const content = fs.readFileSync(filePath, 'utf-8');
const encoding = banner.encoding || 'utf-8';
this.debug(`use encoding: ${encoding}`);

const content = fs.readFileSync(filePath, encoding);

// Create the template function with lodash.
const tmpl = _.template(content);
Expand Down Expand Up @@ -227,7 +230,12 @@ class LicensePlugin {
.trim()
.value();

fs.writeFileSync(output, text || 'No third parties dependencies');
const encoding = thirdParty.encoding || 'utf-8';
this.debug(`use encoding: ${encoding}`);

fs.writeFileSync(output, text || 'No third parties dependencies', {
encoding,
});
}
}

Expand Down
97 changes: 97 additions & 0 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,35 @@ describe('LicensePlugin', () => {
);
});

it('should prepend banner to bundle with custom encoding', () => {
spyOn(fs, 'readFileSync').and.callThrough();

const encoding = 'ascii';
const instance = new LicensePlugin({
banner: {
file: path.join(__dirname, 'fixtures', 'banner.js'),
encoding,
},
});

const code = 'var foo = 0;';

const result = instance.prependBanner(code);

expect(fs.readFileSync).toHaveBeenCalledWith(jasmine.any(String), encoding);
expect(result).toBeDefined();
expect(result.map).not.toBeDefined();
expect(result.code).toEqual(
`/**\n` +
` * Test banner.\n` +
` *\n` +
` * With a second line.\n` +
` */\n` +
`\n` +
`${code}`
);
});

it('should prepend banner to bundle with source map', () => {
const instance = new LicensePlugin({
banner: {
Expand Down Expand Up @@ -670,6 +699,74 @@ describe('LicensePlugin', () => {
});
});

it('should display list of dependencies with custom encoding', (done) => {
const file = path.join(tmpDir.name, 'third-party.txt');
const encoding = 'ascii';
const instance = new LicensePlugin({
thirdParty: {
output: file,
encoding,
},
});

instance.addDependency({
name: 'foo',
version: '1.0.0',
description: 'Foo Package',
license: 'MIT',
private: false,
author: {
name: 'Mickael Jeanroy',
email: 'mickael.jeanroy@gmail.com',
},
});

instance.addDependency({
name: 'bar',
version: '2.0.0',
description: 'Bar Package',
license: 'Apache 2.0',
private: false,
});

spyOn(fs, 'writeFileSync').and.callThrough();

const result = instance.exportThirdParties();

expect(result).not.toBeDefined();
expect(fs.writeFileSync).toHaveBeenCalledWith(jasmine.any(String), jasmine.any(String), {
encoding,
});

fs.readFile(file, encoding, (err, content) => {
if (err) {
done.fail(err);
return;
}

const txt = content.toString();
expect(txt).toBeDefined();
expect(txt).toEqual(
`Name: foo\n` +
`Version: 1.0.0\n` +
`License: MIT\n` +
`Private: false\n` +
`Description: Foo Package\n` +
`Author: Mickael Jeanroy <mickael.jeanroy@gmail.com>\n` +
`\n` +
`---\n` +
`\n` +
`Name: bar\n` +
`Version: 2.0.0\n` +
`License: Apache 2.0\n` +
`Private: false\n` +
`Description: Bar Package`
);

done();
});
});

it('should display default message without dependencies', (done) => {
const file = path.join(tmpDir.name, 'third-party.txt');
const instance = new LicensePlugin({
Expand Down

0 comments on commit 90c5c1e

Please sign in to comment.