UNPKG

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