Skip to content
Permalink
Browse files

fix: respect base on target output (#356)

  • Loading branch information...
bezoerb committed Jan 30, 2019
1 parent b262f47 commit 89265db45e065a0bed816a4651fb3eb0ad5231d3
Showing with 57 additions and 4 deletions.
  1. +4 −4 index.js
  2. +53 −0 test/index.test.js
@@ -17,21 +17,21 @@ const {getOptions} = require('./src/config');
async function generate(params, cb) {
try {
const options = getOptions(params);
const {target = {}} = options;
const {target = {}, base = process.cwd()} = options;
const result = await create(options);
// Store generated css
if (target.css) {
await fs.outputFile(path.resolve(target.css), result.css);
await fs.outputFile(path.resolve(base, target.css), result.css);
}

// Store generated html
if (target.html) {
await fs.outputFile(path.resolve(target.html), result.html);
await fs.outputFile(path.resolve(base, target.html), result.html);
}

// Store extracted css
if (target.uncritical) {
await fs.outputFile(path.resolve(target.uncritical), result.uncritical);
await fs.outputFile(path.resolve(base, target.uncritical), result.uncritical);
}

if (typeof cb === 'function') {
@@ -5,6 +5,7 @@ const Vinyl = require('vinyl');
const PluginError = require('plugin-error');
const nn = require('normalize-newline');
const streamAssert = require('stream-assert');
const tempy = require('tempy');
const {ConfigError, FileNotFoundError, NoCssError} = require('../src/errors');
const {getVinyl, readAndRemove, read} = require('./helper');
const {generate, stream} = require('..');
@@ -77,6 +78,58 @@ test('Write all targets', async () => {
expect(css).toBe(data.css);
});

test('Write all targets relative to base', async () => {
const base = tempy.directory();
const getFile = f => path.join(base, f);
const data = await generate({
base,
src: path.join(__dirname, 'fixtures/generate-default.html'),
target: {html: '.test.html', css: '.test.css', uncritical: '.uncritical.css'},
});
expect(data).toHaveProperty('css');
expect(data).toHaveProperty('html');
expect(data).toHaveProperty('uncritical');
expect(fs.existsSync(getFile('.test.css'))).toBeTruthy();
expect(fs.existsSync(getFile('.uncritical.css'))).toBeTruthy();
expect(fs.existsSync(getFile('.test.html'))).toBeTruthy();

const html = readAndRemove(getFile('.test.html'));
const css = readAndRemove(getFile('.test.css'));
const uncritical = readAndRemove(getFile('.uncritical.css'));
expect(uncritical).toBe(data.uncritical);
expect(html).toBe(data.html);
expect(css).toBe(data.css);

await fs.remove(base);
});

test('Write all targets respecting absolute paths', async () => {
const base = tempy.directory();
const fileBase = tempy.directory();
const getFile = f => path.join(fileBase, f);
const data = await generate({
base,
src: path.join(__dirname, 'fixtures/generate-default.html'),
target: {html: getFile('.test.html'), css: getFile('.test.css'), uncritical: getFile('.uncritical.css')},
});
expect(data).toHaveProperty('css');
expect(data).toHaveProperty('html');
expect(data).toHaveProperty('uncritical');
expect(fs.existsSync(getFile('.test.css'))).toBeTruthy();
expect(fs.existsSync(getFile('.uncritical.css'))).toBeTruthy();
expect(fs.existsSync(getFile('.test.html'))).toBeTruthy();

const html = readAndRemove(getFile('.test.html'));
const css = readAndRemove(getFile('.test.css'));
const uncritical = readAndRemove(getFile('.uncritical.css'));
expect(uncritical).toBe(data.uncritical);
expect(html).toBe(data.html);
expect(css).toBe(data.css);

await fs.remove(base);
await fs.remove(fileBase);
});

test('Reject with ConfigError on invalid config', () => {
expect(generate({})).rejects.toThrow(ConfigError);
});

0 comments on commit 89265db

Please sign in to comment.
You can’t perform that action at this time.