forked from developit/microbundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-fixture.js
74 lines (60 loc) · 2.11 KB
/
build-fixture.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { resolve } from 'path';
import { promisify } from 'util';
import shellQuote from 'shell-quote';
import _rimraf from 'rimraf';
import { readFile } from '../src/utils';
import createProg from '../src/prog';
import microbundle from '../src/index';
const rimraf = promisify(_rimraf);
const FIXTURES_DIR = resolve(`${__dirname}/../test/fixtures`);
const DEFAULT_SCRIPT = 'microbundle';
const parseScript = (() => {
return script => {
let parsed;
const prog = createProg(_parsed => (parsed = _parsed));
const argv = shellQuote.parse(`node ${script}`);
// default to non-modern formats
let hasFormats = argv.some(arg => /^(--format|-f)$/.test(arg));
if (!hasFormats) argv.push('-f', 'es,cjs,umd');
// assuming {op: 'glob', pattern} for non-string args
prog(argv.map(arg => (typeof arg === 'string' ? arg : arg.pattern)));
return parsed;
};
})();
export const getBuildScript = async (fixturePath, defaultScript) => {
let pkg = {};
try {
pkg = JSON.parse(
await readFile(resolve(fixturePath, 'package.json'), 'utf8'),
);
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
return (pkg && pkg.scripts && pkg.scripts.build) || defaultScript;
};
export const buildDirectory = async fixtureDir => {
let fixturePath = resolve(FIXTURES_DIR, fixtureDir);
if (fixtureDir.endsWith('-with-cwd')) {
fixturePath = resolve(fixturePath, fixtureDir.replace('-with-cwd', ''));
}
const dist = resolve(`${fixturePath}/dist`);
// clean up
await rimraf(dist);
if (!fixturePath.endsWith('ts-custom-declaration')) {
await rimraf(resolve(`${fixturePath}/types`));
}
await rimraf(resolve(`${fixturePath}/.rts2_cache_cjs`));
await rimraf(resolve(`${fixturePath}/.rts2_cache_es`));
await rimraf(resolve(`${fixturePath}/.rts2_cache_umd`));
const script = await getBuildScript(fixturePath, DEFAULT_SCRIPT);
const prevDir = process.cwd();
process.chdir(resolve(fixturePath));
const parsedOpts = parseScript(script);
let { output } = await microbundle({
...parsedOpts,
cwd: parsedOpts.cwd !== '.' ? parsedOpts.cwd : resolve(fixturePath),
});
output = output || '';
process.chdir(prevDir);
return output;
};