1 | import { promises as fs } from 'fs';
|
2 | import camelCase from 'camelcase';
|
3 |
|
4 | export const readFile = fs.readFile;
|
5 |
|
6 | export const stat = fs.stat;
|
7 |
|
8 | export function isDir(name) {
|
9 | return stat(name)
|
10 | .then(stats => stats.isDirectory())
|
11 | .catch(() => false);
|
12 | }
|
13 |
|
14 | export function isFile(name) {
|
15 | return stat(name)
|
16 | .then(stats => stats.isFile())
|
17 | .catch(() => false);
|
18 | }
|
19 |
|
20 |
|
21 | export const stdout = console.log.bind(console);
|
22 | export const stderr = console.error.bind(console);
|
23 |
|
24 | export const isTruthy = obj => {
|
25 | if (!obj) {
|
26 | return false;
|
27 | }
|
28 |
|
29 | return obj.constructor !== Object || Object.keys(obj).length > 0;
|
30 | };
|
31 |
|
32 |
|
33 | export const removeScope = name => name.replace(/^@.*\//, '');
|
34 |
|
35 | const INVALID_ES3_IDENT = /((^[^a-zA-Z]+)|[^\w.-])|([^a-zA-Z0-9]+$)/g;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export function safeVariableName(name) {
|
42 | const normalized = removeScope(name).toLowerCase();
|
43 | const identifier = normalized.replace(INVALID_ES3_IDENT, '');
|
44 | return camelCase(identifier);
|
45 | }
|