-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin.mjs
62 lines (49 loc) · 1.28 KB
/
bin.mjs
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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import { readPackageUpSync } from 'read-pkg-up';
const isESM = (function () {
if (typeof require === 'undefined') {
return true;
}
if (readPackageUpSync()?.packageJson.type === 'module') {
return true;
}
return false;
})();
const files = ['./src/console', './lib/console', './console'];
const mjsFile = js('.mjs');
const cjsFile = js('.cjs');
const jsFile = js('.js');
const tsFile = ts();
(() => {
const loadESM = mjsFile || (isESM && jsFile) || (isESM && tsFile);
if (loadESM) {
import('./es/bin.js').then((bin) => bin.loadESM(loadESM));
return;
}
const loadCJS = cjsFile || (!isESM && jsFile) || (!isESM && tsFile);
if (loadCJS) {
import('./lib/bin.js').then((bin) => bin.loadCJS(loadCJS));
return;
}
console.error(chalk.red('Command entry file console.{ts|js|mjs|cjs} is not found.'));
process.exit(127);
})();
function js(ext) {
for (let i = 0; i < files.length; ++i) {
const file = path.resolve(files[i] + ext);
if (fs.existsSync(file)) {
return file;
}
}
}
function ts() {
for (let i = 0; i < files.length; ++i) {
const file = path.resolve(files[i] + '.ts');
if (fs.existsSync(file)) {
return file;
}
}
}