Skip to content

Commit

Permalink
Chore: fill test entry with template
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro committed Apr 24, 2021
1 parent b986009 commit 9177859
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
69 changes: 69 additions & 0 deletions bin/pack-tests.js
@@ -0,0 +1,69 @@
#!./node_modules/.bin/babel-node
import os from 'os';
import path from 'path';
import { fill } from 'myrmidon';
import { rollup } from 'rollup';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';
import multi from '@rollup/plugin-multi-entry';
import babel from '@rollup/plugin-babel';
import fs from 'fs-extra';
import uuid from 'uuid';

const isMain = !module.parent;

async function replaceWithTemplate(templatePath, context, destPath) {
const content = await fs.readFile(templatePath);
const filled = fill(content.toString(), context);
const backupPath = path.resolve(os.tmpdir(), uuid.v4());

await fs.move(destPath, backupPath);
await fs.writeFile(destPath, filled);

return backupPath;
}

async function run() {
try {
const backup = await replaceWithTemplate(path.resolve('tests/entry.static.js'), { entry: '../tmp/package/lib' }, path.resolve('tests/entry.js'));

try {
const bundle = await rollup({
input : 'tests/**/*test.js',
plugins : [
babel({ exclude: 'node_modules/**' }),
resolve({ preferBuiltins: true }),
commonjs({
include : [ /node_modules/ ],
sourceMap : false
}),
json({
include : 'node_modules/**',
compact : true
}),
multi()
]
});

console.log(bundle.watchFiles);
await bundle.write({
file : 'tmp/tests.js',
format : 'cjs'
});
} catch (error) {
console.error('ROLLUP ERROR');
throw error;
} finally {
await fs.move(backup, path.resolve('tests/entry.js'), { overwrite: true });
}
console.log('Done');
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
}


if (isMain) run(process.argv.slice(2));
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -71,6 +71,7 @@
"jscpd": "^3.3.25",
"lockfile-lint": "^4.6.2",
"mocha": "^6.2.3",
"myrmidon": "^1.4.5",
"nyc": "^14.1.1",
"rollup": "^2.45.2",
"semantic-release": "^17.4.2",
Expand Down
9 changes: 7 additions & 2 deletions tests/entry.js
@@ -1,7 +1,12 @@
import path from 'path';

const isBuild = process.env.BUILD && [ '1', 'true' ].includes(process.env.BUILD);
const entry = process.env.ENTRY && path.resolve(process.env.ENTRY);
const entry = process.env.ENTRY && path.resolve(process.env.ENTRY)
|| isBuild && path.resolve(__dirname, '../lib')
|| path.resolve(__dirname, '../src');

module.exports = entry && require(entry) || isBuild && require('../lib') || require('../src');
module.exports = require(entry);

module.exports._load = function (relPath) {
return require(path.join(entry, relPath));
};
7 changes: 7 additions & 0 deletions tests/entry.static.js
@@ -0,0 +1,7 @@
import path from 'path';

export default require('{entry}');

export function _load(relPath) {
return require(path.join('{entry}', relPath));
}

0 comments on commit 9177859

Please sign in to comment.