Skip to content

Commit

Permalink
fix: use EOL character based on current platform
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Oct 24, 2019
1 parent 51cb5b3 commit fd3f4ae
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const {EOL} = require('os');
const path = require('path');
const fs = require('fs-extra');
const pify = require('pify');
Expand Down Expand Up @@ -25,14 +26,14 @@ const getDefaultBanner = packageJson =>
packageJson.name ? packageJson.name.charAt(0).toUpperCase() + packageJson.name.slice(1) : 'unknown'
} v${packageJson.version || '0.0.0'}${
packageJson.homepage || packageJson.name
? `\n * ${packageJson.homepage || `https://npm.com/${packageJson.name}`}`
? `${EOL} * ${packageJson.homepage || `https://npm.com/${packageJson.name}`}`
: ''
}
*
* Copyright (c) ${new Date().getFullYear()}${
packageJson.author && packageJson.author.name ? ` ${packageJson.author.name}` : ''
}
*${packageJson.license ? ` Licensed under the ${packageJson.license} license\n *` : ''}/\n`;
*${packageJson.license ? ` Licensed under the ${packageJson.license} license${EOL} *` : ''}/${EOL}`;
/**
* Log messages.
*
Expand Down Expand Up @@ -103,7 +104,7 @@ If -o is not passed, the sourcemap is disabled and it writes to stdout.`
const concat = new Concat(
argv.output !== undefined && argv.output !== null && argv.map,
argv.output ? path.basename(argv.output) : '',
'\n'
EOL
);
/**
* Cache the content of stdin the first it's retrieve.
Expand Down Expand Up @@ -171,7 +172,7 @@ async function concatFiles() {
(files.length === 0 && (typeof argv.banner === 'undefined' || !argv.footer))
) {
throw new Error(
chalk.bold.red('Require at least 2 file, banner or footer to concatenate. ("ncat --help" for help)\n')
chalk.bold.red(`Require at least 2 file, banner or footer to concatenate. ("ncat --help" for help)${EOL}`)
);
}

Expand Down Expand Up @@ -309,10 +310,10 @@ function output() {
*/
function getSourceMappingURL() {
if (path.extname(argv.output) === '.css') {
return `\n/*# sourceMappingURL=${path.basename(argv.output)}.map */`;
return `${EOL}/*# sourceMappingURL=${path.basename(argv.output)}.map */`;
}

return `\n//# sourceMappingURL=${path.basename(argv.output)}.map`;
return `${EOL}//# sourceMappingURL=${path.basename(argv.output)}.map`;
}

/**
Expand All @@ -322,7 +323,7 @@ function getSourceMappingURL() {
* @return {String} the modified code.
*/
function removeMapURL(code) {
return sourceMappingURL.removeFrom(code.toString()).replace(/\n\n$/, '\n');
return sourceMappingURL.removeFrom(code.toString()).replace(new RegExp(`${EOL}${EOL}$`), EOL);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/misc.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {EOL} from 'os';
import test from 'ava';
import cli from './helpers/cli';

Expand All @@ -18,5 +19,5 @@ test('--version', async t => {

t.falsy(error, stderr);
t.is(exitCode, 0, 'expected zero return code');
t.is(stdout, `${version}\n`);
t.is(stdout, `${version}${EOL}`);
});
5 changes: 3 additions & 2 deletions test/output.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {EOL} from 'os';
import path from 'path';
import test from 'ava';
import randomstring from 'randomstring';
Expand All @@ -22,7 +23,7 @@ test('preserve order with large number of files', async t => {
const filepaths = [];
const expected = [];

for (let i = 0; i < 100; i++) {
for (let i = 0; i < 50; i++) {
const filepath = path.join(tmp, `file-${i}`);
const content = randomstring.generate();

Expand All @@ -36,5 +37,5 @@ test('preserve order with large number of files', async t => {
const {error, stderr} = await cli(filepaths.concat(['-o', outputFile]));

t.falsy(error, stderr);
t.is(expected.join('\n'), await read(outputFile));
t.is(expected.join(EOL), await read(outputFile));
});
5 changes: 3 additions & 2 deletions test/sourcemap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import tempDir from 'temp-dir';
import unixify from 'unixify';
import cli from './helpers/cli';
import read from './helpers/read';
import eol from './helpers/eol';

test('--map generate an external map (css)', async t => {
const output = tempy.file({extension: 'css'});
Expand Down Expand Up @@ -148,8 +149,8 @@ test('--map generate an external map and preserve embeded code', async t => {
t.truthy(await fs.pathExists(output.replace('.css', '.css.map')));
const sourceMap = JSON.parse(await read(output.replace('.css', '.css.map')));

t.is(await read('test/fixtures/a.css'), sourceMap.sourcesContent[0]);
t.is(await read('test/fixtures/b.css'), sourceMap.sourcesContent[1]);
t.is(await read('test/fixtures/a.css'), eol(sourceMap.sourcesContent[0]));
t.is(await read('test/fixtures/b.css'), eol(sourceMap.sourcesContent[1]));
});

test("--map generate an external map and do not embed code if it wasn't embeded in original map", async t => {
Expand Down

0 comments on commit fd3f4ae

Please sign in to comment.