Skip to content

Commit

Permalink
feat(best): very simple customizable test template
Browse files Browse the repository at this point in the history
enabling project and test level template overriding

fixing tests
  • Loading branch information
damien-quintal committed May 9, 2018
1 parent e29930e commit 9bab3a4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/best-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@best/messager": "0.5.0",
"@best/runtime": "0.5.0",
"rollup": "~0.53.0"
"rollup": "~0.53.0",
"ncp": "^2.0.0"
}
}
18 changes: 14 additions & 4 deletions packages/best-build/src/__tests__/best-build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,32 @@ import * as path from 'path';
import { buildBenchmark } from '../index';

const TEMP_DIR_PREFIX = 'best-test-';
const ROOT_DIR_PREFIX = 'best-root-test-';
const MOCK_MESSAGER = {
onBenchmarkBuildStart() {},
onBenchmarkBuildEnd() {},
logState() {}
};
const projectName = 'test';
const rootDir = roorDir();

function tempDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), TEMP_DIR_PREFIX));
}

function roorDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), ROOT_DIR_PREFIX));
}

describe('buildBenchmark', () => {
test('generating index.js and index.html', async () => {
const cacheDirectory = tempDir();
await buildBenchmark(
path.resolve(__dirname, 'fixtures', 'single-file', 'single-file.js'),
{
cacheDirectory,
projectName
projectName,
rootDir
},
{},
MOCK_MESSAGER,
Expand All @@ -39,7 +46,8 @@ describe('buildBenchmark', () => {
path.resolve(__dirname, 'fixtures', 'single-file', 'single-file.js'),
{
cacheDirectory: tempDir(),
projectName
projectName,
rootDir
},
{},
MOCK_MESSAGER,
Expand All @@ -62,8 +70,8 @@ describe('buildBenchmark', () => {
path.resolve(__dirname, 'fixtures', 'single-file', 'single-file.js'),
{
cacheDirectory: tempDir(),
projectName

projectName,
rootDir
},
{},
messager,
Expand Down Expand Up @@ -99,6 +107,7 @@ describe('buildBenchmark', () => {
cacheDirectory: tempDir(),
projectName,
plugins: [['build-plugin-opts', PLUGIN_OPTIONS]],
rootDir
},
{},
MOCK_MESSAGER,
Expand Down Expand Up @@ -135,6 +144,7 @@ describe('buildBenchmark', () => {
cacheDirectory: tempDir(),
projectName,
plugins: ['build-plugin-hooks'],
rootDir
},
{},
MOCK_MESSAGER,
Expand Down
6 changes: 5 additions & 1 deletion packages/best-build/src/html-templating.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ export function generateParametrizedHTML(html, options) {
}

export function generateDefaultHTML(options) {
return generateParametrizedHTML(DEFAULT_HTML, options);
let template = DEFAULT_HTML;
if (Object.keys(options).indexOf("customTemplate") > -1) {
template = options.customTemplate;
}
return generateParametrizedHTML(template, options);
}
40 changes: 37 additions & 3 deletions packages/best-build/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { rollup } from 'rollup';
import path from 'path';
import benchmarkRollup from './rollup-plugin-benchmark-import';
import { generateDefaultHTML } from './html-templating';
import { ncp } from 'ncp';
import fs from 'fs';
import crypto from 'crypto';

Expand Down Expand Up @@ -33,12 +34,33 @@ function addResolverPlugins({ plugins }) {
});
}

function copyPublicFolder(source, destination) {
return new Promise((resolve, reject) => {
ncp(source, destination, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

function overwriteDefaultTemplate(templatePath, rootDir, publicFolder) {
const template = fs.readFileSync(templatePath, 'utf8');
let templateOptions = {};
templateOptions["customTemplate"] = template;
templateOptions["publicFolder"] = publicFolder;
return templateOptions;
}

export async function buildBenchmark(entry, projectConfig, globalConfig, messager) {
const { projectName, cacheDirectory } = projectConfig;
messager.onBenchmarkBuildStart(entry, projectName);

const ext = path.extname(entry);
const benchmarkName = path.basename(entry, ext);
const publicFolder = path.join(cacheDirectory, projectName, "public");
const benchmarkFolder = path.join(cacheDirectory, projectName, benchmarkName);
const benchmarkJSFileName = benchmarkName + ext;
const inputOptions = Object.assign({}, BASE_ROLLUP_INPUT, {
Expand All @@ -59,10 +81,22 @@ export async function buildBenchmark(entry, projectConfig, globalConfig, message
await bundle.write(outputOptions);

const htmlPath = path.resolve(path.join(benchmarkFolder, benchmarkName + '.html'));
const html = generateDefaultHTML({
const projectTemplatePath = path.resolve(path.join(projectConfig.rootDir, 'src', 'template.benchmark.html'));
const benchmarkTemplatePath = path.resolve(path.join(entry, '..', 'template.benchmark.html'));
const generateHTMLOptions = {
benchmarkJS: `./${benchmarkJSFileName}`,
benchmarkName,
});
benchmarkName
};

if (fs.existsSync(benchmarkTemplatePath)) {
Object.assign(generateHTMLOptions, overwriteDefaultTemplate(benchmarkTemplatePath, projectConfig.rootDir, publicFolder));
await copyPublicFolder(path.resolve(path.join(projectConfig.rootDir, "public")), publicFolder);
} else if (fs.existsSync(projectTemplatePath)) {
Object.assign(generateHTMLOptions, overwriteDefaultTemplate(projectTemplatePath, projectConfig.rootDir, publicFolder));
await copyPublicFolder(path.resolve(path.join(projectConfig.rootDir, "public")), publicFolder);
}

const html = generateDefaultHTML(generateHTMLOptions);

messager.logState('Saving artifacts...');

Expand Down

0 comments on commit 9bab3a4

Please sign in to comment.