axios-miniprogram/scripts/utils.ts

41 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2023-05-05 10:59:37 +08:00
import { isPromise } from 'node:util/types';
import path from 'node:path';
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { ExecSyncOptions, execSync } from 'node:child_process';
import consola from 'consola';
2023-03-23 20:09:00 +08:00
2023-05-05 10:59:37 +08:00
export const __dirname = fileURLToPath(new URL('../', import.meta.url));
export const require = createRequire(import.meta.url);
export const pkgPath = path.resolve(__dirname, 'package.json');
export const distPath = path.resolve(__dirname, 'dist');
2023-03-24 19:41:17 +08:00
2023-05-05 10:59:37 +08:00
export const resolve = (...paths: string[]) =>
path.resolve(__dirname, ...paths);
2023-04-17 00:00:45 +08:00
2023-04-18 11:14:57 +08:00
export const exec = (command: string, options: ExecSyncOptions = {}) =>
2023-05-05 10:59:37 +08:00
execSync(command, {
stdio: 'inherit',
encoding: 'utf-8',
...options,
});
2023-04-17 00:00:45 +08:00
2023-05-05 10:59:37 +08:00
export const getPkgJSON = () => require(pkgPath);
2023-05-04 10:39:52 +08:00
export function safeExit(run: () => unknown) {
2023-05-05 10:59:37 +08:00
try {
const p = run();
2023-05-04 10:39:52 +08:00
2023-05-05 10:59:37 +08:00
if (isPromise(p)) {
return p.catch(exit);
}
} catch {
exit();
}
2023-05-04 10:39:52 +08:00
}
function exit() {
2023-05-05 10:59:37 +08:00
consola.error('已退出');
process.exit();
2023-05-04 10:39:52 +08:00
}