Skip to content

Commit

Permalink
Lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Aug 14, 2019
1 parent 0825d31 commit 1c2e7ba
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 46 deletions.
10 changes: 7 additions & 3 deletions cli.js
Expand Up @@ -129,20 +129,23 @@ cli.flags = reduce(cli.flags, (res, val, key) => {
if (isString(val)) {
val = [val];
}

res.assetPaths = val;
break;
case 'include':
case 'ignore':
if (isString(val) || isRegExp(val)) {
val = [val];
}

res[key] = map(val || [], entry => {
// Check regex
const match = entry.match(/^\/(.*)\/([igmy]+)?$/);

if (match) {
return new RegExp(escapeRegExp(match[1]), match[2]);
}

return entry;
});
break;
Expand All @@ -168,7 +171,7 @@ function run(data) {
if (data) {
opts.html = data;
} else {
opts.src = cli.input[0]; // eslint-disable-line prefer-destructuring
opts.src = cli.input[0];
if (opts.src && !file.isExternal(opts.src)) {
opts.src = path.resolve(cli.input[0]);
}
Expand All @@ -182,8 +185,8 @@ function run(data) {
process.stdout.write(val, process.exit);
}
});
} catch (err) {
error(err);
} catch (error) {
error(error);
}
}

Expand All @@ -196,6 +199,7 @@ if (cli.input[0]) {
if (ok) {
return;
}

run();
}, 100);
}
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -112,9 +112,9 @@ exports.generate = function (opts, cb) {

// Return promise if callback is not defined
if (isFunction(cb)) {
corePromise.catch(err => { // eslint-disable-line promise/valid-params
corePromise.catch(error => { // eslint-disable-line promise/valid-params
cleanup();
cb(err);
cb(error);
throw new Bluebird.CancellationError();
}).then(output => {
cleanup();
Expand Down
9 changes: 6 additions & 3 deletions lib/core.js
Expand Up @@ -98,6 +98,7 @@ function inlineImages(opts) {
assetPaths.push(path.dirname(vinyl.path));
// Add domain as asset source for external domains
if (file.isExternal(opts.src)) {
// eslint-disable-next-line node/no-deprecated-api
const urlObj = url.parse(opts.src);
const domain = urlObj.protocol + '//' + urlObj.host;
assetPaths.push(domain, domain + path.dirname(urlObj.pathname));
Expand Down Expand Up @@ -135,6 +136,7 @@ function vinylize(opts) {
if (filepath._isVinyl) {
return filepath;
}

debug('vinylize', path.resolve(filepath));
return file.getVinylPromise({
src: path.resolve(filepath),
Expand Down Expand Up @@ -233,11 +235,12 @@ function generate(opts) {

debug('generate', 'Done');
return criticalCSS;
}).catch(err => {
if (err.message.startsWith('PAGE_UNLOADED_DURING_EXECUTION')) {
}).catch(error => {
if (error.message.startsWith('PAGE_UNLOADED_DURING_EXECUTION')) {
return '';
}
return Promise.reject(err);

return Promise.reject(error);
});
}

Expand Down
16 changes: 12 additions & 4 deletions lib/file-helper.js
Expand Up @@ -134,6 +134,7 @@ function requestAsync(uri, secure = true, opts = {}) {
let resourceUrl = uri;
// Consider protocol-relative urls
if (/^\/\//.test(uri)) {
// eslint-disable-next-line node/no-deprecated-api
resourceUrl = url.resolve(`http${secure ? 's' : ''}://te.st`, uri);
}

Expand All @@ -142,17 +143,19 @@ function requestAsync(uri, secure = true, opts = {}) {
if (user && pass) {
options.headers.Authorization = 'Basic ' + token(user, pass);
}

if (userAgent) {
options.headers['User-Agent'] = userAgent;
}
return got(resourceUrl, options).catch(err => {

return got(resourceUrl, options).catch(error => {
if (secure) {
debug(`${err.message} - trying again over http`);
debug(`${error.message} - trying again over http`);
return requestAsync(uri, false, opts);
}

debug(`${resourceUrl} failed: ${err.message}`);
return Promise.resolve(err);
debug(`${resourceUrl} failed: ${error.message}`);
return Promise.resolve(error); // eslint-disable-line promise/no-return-wrap
});
}

Expand All @@ -165,6 +168,7 @@ function guessBasePath(opts) {
if (opts.src && !isExternal(opts.src) && !isVinyl(opts.src)) {
return path.dirname(opts.src);
}

if (opts.src && isVinyl(opts.src)) {
return opts.src.dirname;
}
Expand Down Expand Up @@ -195,6 +199,7 @@ function assertLocal(filePath, opts = {}) {
resolve(filePath);
});
}

return requestAsync(filePath, true, opts)
.then(response => temp(response));
}
Expand All @@ -214,16 +219,19 @@ function resourcePath(htmlfile, opts) {

if (isExternal(htmlfile.history[0])) {
debug('resourcePath - remote', htmlfile.history[0]);
// eslint-disable-next-line node/no-deprecated-api
return url.resolve(htmlfile.history[0], filepath);
}

if (/(?:^\/)/.test(filepath)) {
return path.join(opts.base, filepath.split('?')[0]);
}

const folder = path.relative(opts.base, path.dirname(htmlfile.path));
if (folder) {
debug('resourcePath - folder', folder);
}

return path.join(path.dirname(htmlfile.path), filepath.split('?')[0]);
};
}
Expand Down
1 change: 1 addition & 0 deletions lib/vinyl-remote.js
Expand Up @@ -23,6 +23,7 @@ Object.defineProperty(File.prototype, 'remotePath', {
return;
}

// eslint-disable-next-line node/no-deprecated-api
const urlObj = url.parse(remotePath);
remotePath = urlObj.protocol + '//' + urlObj.host + urlObj.pathname;

Expand Down
4 changes: 2 additions & 2 deletions test/01-module.js
Expand Up @@ -17,8 +17,8 @@ describe('Module', () => {
it('should return rejected Promise for generating without callback if src and dest not specified', done => {
const tmp = critical.generate({}).then(data => {
assert.fail(data, undefined, 'Should not be called');
}).catch(err => {
assert.instanceOf(err, Error);
}).catch(error => {
assert.instanceOf(error, Error);
done();
});

Expand Down
3 changes: 2 additions & 1 deletion test/02-generate.js
Expand Up @@ -10,8 +10,8 @@ const getPort = require('get-port');
const finalhandler = require('finalhandler');
const serveStatic = require('serve-static');
const Vinyl = require('vinyl');
const critical = require('..');
const {read, assertCritical} = require('./helper/testhelper');
const critical = require('..');

process.chdir(path.resolve(__dirname));

Expand Down Expand Up @@ -683,6 +683,7 @@ describe('Module - generate (remote)', () => {
if (req.headers['user-agent'] === 'custom agent') {
return serveUserAgent(req, res, finalhandler(req, res));
}

serve(req, res, finalhandler(req, res));
});

Expand Down
88 changes: 60 additions & 28 deletions test/03-cli.js
Expand Up @@ -40,16 +40,20 @@ describe('CLI', () => {
const cp = execFile('node', [
path.join(__dirname, '../', this.pkg.bin.critical),
'fixtures/generate-default.html',
'--base', 'fixtures',
'--width', '1300',
'--height', '900'
'--base',
'fixtures',
'--width',
'1300',
'--height',
'900'
]);

const expected = fs.readFileSync(path.join(__dirname, 'expected/generate-default.css'), 'utf8');
cp.stdout.on('data', data => {
if (data instanceof Buffer) {
data = data.toString('utf8');
}

assert.strictEqual(nn(data), nn(expected));
done();
});
Expand All @@ -73,6 +77,7 @@ describe('CLI', () => {
if (data instanceof Buffer) {
data = data.toString('utf8');
}

assert.strictEqual(nn(data), nn(expected));
done();
});
Expand Down Expand Up @@ -159,16 +164,20 @@ describe('CLI', () => {
const cp = execFile('node', [
path.join(__dirname, '../', this.pkg.bin.critical),
`http://localhost:${serverport}`,
'--base', 'fixtures',
'--width', '1300',
'--height', '900'
'--base',
'fixtures',
'--width',
'1300',
'--height',
'900'
]);

const expected = fs.readFileSync(path.join(__dirname, 'expected/generate-default.css'), 'utf8');
cp.stdout.on('data', data => {
if (data instanceof Buffer) {
data = data.toString('utf8');
}

assert.strictEqual(nn(data), nn(expected));
done();
});
Expand All @@ -178,18 +187,24 @@ describe('CLI', () => {
const cp = execFile('node', [
path.join(__dirname, '../', this.pkg.bin.critical),
`http://localhost:${serverport}`,
'--css', `http://localhost:${serverport}/styles/main.css`,
'--css', `http://localhost:${serverport}/styles/bootstrap.css`,
'--base', 'fixtures',
'--width', '1300',
'--height', '900'
'--css',
`http://localhost:${serverport}/styles/main.css`,
'--css',
`http://localhost:${serverport}/styles/bootstrap.css`,
'--base',
'fixtures',
'--width',
'1300',
'--height',
'900'
]);

const expected = fs.readFileSync(path.join(__dirname, 'expected/generate-default.css'), 'utf8');
cp.stdout.on('data', data => {
if (data instanceof Buffer) {
data = data.toString('utf8');
}

assert.strictEqual(nn(data), nn(expected));
done();
});
Expand Down Expand Up @@ -226,11 +241,16 @@ describe('CLI', () => {
'node',
path.join(__dirname, '../', this.pkg.bin.critical),
'fixtures/generate-default.html',
'-c', 'css',
'-w', '300',
'-h', '400',
'-f', 'folder',
'-p', 'pathPrefix',
'-c',
'css',
'-w',
'300',
'-h',
'400',
'-f',
'folder',
'-p',
'pathPrefix',
'-e',
'-i'
];
Expand All @@ -251,19 +271,29 @@ describe('CLI', () => {
'node',
path.join(__dirname, '../', this.pkg.bin.critical),
'fixtures/generate-default.html',
'--css', 'css',
'--width', '300',
'--height', '400',
'--ignore', 'ignore',
'--include', '/include/',
'--folder', 'folder',
'--pathPrefix', 'pathPrefix',
'--css',
'css',
'--width',
'300',
'--height',
'400',
'--ignore',
'ignore',
'--include',
'/include/',
'--folder',
'folder',
'--pathPrefix',
'pathPrefix',
'--inline',
'--extract',
'--inlineImages',
'--maxFileSize', '1024',
'--assetPaths', 'assetPath1',
'--assetPaths', 'assetPath2'
'--maxFileSize',
'1024',
'--assetPaths',
'assetPath1',
'--assetPaths',
'assetPath2'
];

require('../cli'); // eslint-disable-line import/no-unassigned-import
Expand Down Expand Up @@ -305,8 +335,10 @@ describe('CLI', () => {
path.join(__dirname, '../', this.pkg.bin.critical),
'fixtures/generate-default.html',
'--penthouse-strict',
'--penthouse-timeout', '50000',
'--penthouse-renderWaitTime', '300'
'--penthouse-timeout',
'50000',
'--penthouse-renderWaitTime',
'300'
];

require('../cli'); // eslint-disable-line import/no-unassigned-import
Expand Down
2 changes: 1 addition & 1 deletion test/04-streams.js
Expand Up @@ -12,8 +12,8 @@ const Vinyl = require('vinyl');
const array = require('stream-array');
const nn = require('normalize-newline');

const critical = require('..');
const {read} = require('./helper/testhelper');
const critical = require('..');

process.chdir(path.resolve(__dirname));

Expand Down
7 changes: 5 additions & 2 deletions test/helper/testhelper.js
Expand Up @@ -44,18 +44,21 @@ function assertCritical(target, expected, done, skipTarget) {
console.log(err);
done(err);
}

try {
assert.isNull(err, Boolean(err) && err);
assert.isDefined(output, 'Should produce output');
if (!skipTarget) {
const dest = readAndRemove(target, true);
assert.strictEqual(nn(dest), nn(expected));
}

assert.strictEqual(nn(output), nn(expected));
} catch (err) {
done(err);
} catch (error) {
done(error);
return;
}

done();
};
}
Expand Down

0 comments on commit 1c2e7ba

Please sign in to comment.