2018-12-18 12:51:30 +01:00
|
|
|
/* eslint-env jest */
|
2019-08-08 18:10:41 +02:00
|
|
|
|
|
|
|
'use strict';
|
2018-12-18 12:51:30 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const readPkgUp = require('read-pkg-up');
|
|
|
|
const execa = require('execa');
|
|
|
|
const nn = require('normalize-newline');
|
|
|
|
|
2020-05-23 00:36:56 +02:00
|
|
|
const read = async (file) => {
|
2018-12-18 12:51:30 +01:00
|
|
|
const filepath = path.isAbsolute(file) ? file : path.join(__dirname, '..', file);
|
|
|
|
const content = await fs.readFile(filepath, 'utf8');
|
|
|
|
return nn(content);
|
|
|
|
};
|
|
|
|
|
2020-05-23 00:36:56 +02:00
|
|
|
const checkAndDelete = (file) => {
|
2018-12-18 12:51:30 +01:00
|
|
|
const filepath = path.isAbsolute(file) ? file : path.join(__dirname, '..', file);
|
|
|
|
if (fs.existsSync(filepath)) {
|
|
|
|
fs.removeSync(filepath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2020-05-23 00:36:56 +02:00
|
|
|
const strip = (string) => nn(string.replace(/[\r\n]+/gm, ' ').replace(/\s+/gm, ''));
|
2018-12-18 12:51:30 +01:00
|
|
|
|
|
|
|
const getBin = async () => {
|
2019-10-07 16:12:10 +02:00
|
|
|
const {packageJson} = await readPkgUp();
|
|
|
|
return path.join(__dirname, '../../', packageJson.bin['inline-critical']);
|
2018-12-18 12:51:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const run = async (args = []) => {
|
|
|
|
const bin = await getBin();
|
|
|
|
return execa('node', [bin, ...args]);
|
|
|
|
};
|
|
|
|
|
2020-05-23 00:36:56 +02:00
|
|
|
const getArgs = async (parameters = []) => {
|
2018-12-18 12:51:30 +01:00
|
|
|
const bin = await getBin();
|
|
|
|
const origArgv = process.argv;
|
|
|
|
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
|
|
|
|
|
|
|
|
jest.mock('../../index', () => jest.fn(() => ''));
|
|
|
|
|
2020-05-23 00:36:56 +02:00
|
|
|
process.argv = ['node', bin, ...parameters];
|
2018-12-18 12:51:30 +01:00
|
|
|
const inline = require('../..');
|
|
|
|
|
|
|
|
require('../../cli'); // eslint-disable-line import/no-unassigned-import
|
|
|
|
|
|
|
|
// wait for cli to run
|
2020-05-23 00:36:56 +02:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 200));
|
2018-12-18 12:51:30 +01:00
|
|
|
const [args] = inline.mock.calls;
|
|
|
|
const [html, styles, options] = args || ['', '', {}];
|
|
|
|
expect(inline).toHaveBeenCalledTimes(1);
|
|
|
|
inline.mockRestore();
|
|
|
|
mockExit.mockRestore();
|
2019-10-24 13:36:10 +02:00
|
|
|
process.argv = origArgv;
|
2018-12-18 12:51:30 +01:00
|
|
|
return [html, styles, options];
|
|
|
|
};
|
|
|
|
|
|
|
|
const pipe = async (file, args = []) => {
|
|
|
|
const filepath = path.isAbsolute(file) ? file : path.join(__dirname, '..', file);
|
|
|
|
const cat = process.platform === 'win32' ? 'type' : 'cat';
|
|
|
|
const bin = await getBin();
|
|
|
|
const cmd = `${cat} ${path.normalize(filepath)} | node ${bin} ${args.join(' ')}`;
|
2020-05-23 00:36:56 +02:00
|
|
|
return execa(cmd, {shell: true});
|
2018-12-18 12:51:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
read,
|
|
|
|
checkAndDelete,
|
|
|
|
strip,
|
|
|
|
getBin,
|
|
|
|
run,
|
|
|
|
getArgs,
|
|
|
|
pipe,
|
|
|
|
};
|