-
Notifications
You must be signed in to change notification settings - Fork 542
/
initUtils.ts
118 lines (107 loc) · 2.77 KB
/
initUtils.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { promises as fs } from 'fs';
import { execSync } from 'child_process';
import pathUtils from 'path';
export const TEMPLATE_GIT_URL =
'https://github.com/MetaMask/template-snap-monorepo.git';
export const SNAP_LOCATION = 'packages/snap/';
/**
* Checks if the destination folder exists and if it's empty. Otherwise create it.
*
* @param directory - The desination folder.
*/
export async function prepareWorkingDirectory(
directory: string,
): Promise<void> {
try {
const isCurrentDirectory = directory === process.cwd();
if (!isCurrentDirectory) {
try {
await fs.mkdir(directory, { recursive: true });
} catch (err) {
throw new Error('Init Error: Failed to create new directory.');
}
}
const existingFiles = await fs.readdir(directory);
if (existingFiles.length > 0) {
throw new Error(`Directory ${directory} not empty.`);
}
} catch (err) {
throw new Error(
`Init Error: Failed to prepare working directory with message: ${err.message}`,
);
}
}
/**
* Clones the template in a directory.
*
* @param directory - The directory to clone the template in.
*/
export async function cloneTemplate(directory: string) {
try {
execSync(`git clone --depth=1 ${TEMPLATE_GIT_URL} ${directory}`, {
stdio: [2],
});
} catch (err) {
throw new Error('Init Error: Failed to clone the template.');
}
}
/**
* Check if git is installed.
*
* @returns True if git is installed, or false otherwise.
*/
export function isGitInstalled() {
try {
execSync('git --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}
/**
* Check if the actual working dir is a git repository.
*
* @param directory - The directory to check.
* @returns True if it's a git repository otherwise false.
*/
export function isInGitRepository(directory: string) {
try {
execSync('git rev-parse --is-inside-work-tree', {
stdio: 'ignore',
cwd: pathUtils.resolve(__dirname, directory),
});
return true;
} catch (err) {
return false;
}
}
/**
* Init a git repository.
*
* @param directory - The directory to init.
*/
export async function gitInit(directory: string) {
try {
execSync('git init', {
stdio: 'ignore',
cwd: pathUtils.resolve(__dirname, directory),
});
} catch (err) {
throw new Error('Init Error: Failed to init a new git repository.');
}
}
/**
* Install dependencies in a yarn project.
*
* @param directory - The directory containing the project.
*/
export async function yarnInstall(directory: string) {
try {
execSync('yarn install', {
stdio: [0, 1, 2],
cwd: pathUtils.resolve(__dirname, directory),
});
} catch (err) {
throw new Error('Init Error: Failed to install dependencies.');
}
}