-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.ts
88 lines (79 loc) · 2.05 KB
/
utils.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import fs from 'fs';
import validateProjectName from 'validate-npm-package-name';
import chalk from 'chalk';
import path from 'path';
export function makeDir(root: string, options = { recursive: true }): Promise<void> {
return fs.promises.mkdir(root, options);
}
export async function isWriteable(directory: string): Promise<boolean> {
try {
await fs.promises.access(directory, (fs.constants || fs).W_OK);
return true;
} catch (err) {
return false;
}
}
export function validateNpmName(name: string): {
valid: boolean;
problems?: string[];
} {
const nameValidation = validateProjectName(name);
if (nameValidation.validForNewPackages) {
return { valid: true };
}
return {
valid: false,
problems: [...(nameValidation.errors || []), ...(nameValidation.warnings || [])],
};
}
export function getPkgManager(): string {
return 'yarn';
}
export function isFolderEmpty(root: string, name: string): boolean {
const validFiles = [
'.DS_Store',
'.git',
'.gitattributes',
'.gitignore',
'.gitlab-ci.yml',
'.hg',
'.hgcheck',
'.hgignore',
'.idea',
'.npmignore',
'.travis.yml',
'LICENSE',
'Thumbs.db',
'docs',
'mkdocs.yml',
'npm-debug.log',
'yarn-debug.log',
'yarn-error.log',
];
const conflicts = fs
.readdirSync(root)
.filter(file => !validFiles.includes(file))
// Support IntelliJ IDEA-based editors
.filter(file => !/\.iml$/.test(file));
if (conflicts.length > 0) {
console.log(`The directory ${chalk.green(name)} contains files that could conflict:`);
console.log();
for (const file of conflicts) {
try {
const stats = fs.lstatSync(path.join(root, file));
if (stats.isDirectory()) {
console.log(` ${chalk.blue(file)}/`);
} else {
console.log(` ${file}`);
}
} catch {
console.log(` ${file}`);
}
}
console.log();
console.log('Either try using a new directory name, or remove the files listed above.');
console.log();
return false;
}
return true;
}