-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathNewProjectValidation.res
More file actions
30 lines (26 loc) · 990 Bytes
/
Copy pathNewProjectValidation.res
File metadata and controls
30 lines (26 loc) · 990 Bytes
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
open Node
let packageNameRegExp = /^[a-z0-9-]+$/
let devcontainerDirectoryName = ".devcontainer"
let containsOnlyDevcontainerDirectory = projectPath =>
try {
switch Fs.readdirSync(projectPath) {
| [entry] if entry === devcontainerDirectoryName =>
Path.join2(projectPath, devcontainerDirectoryName)->Fs.statSync->Fs.Stats.isDirectory
| _ => false
}
} catch {
| Exn.Error(_) => false
}
let validateProjectName = (~cwd=Process.cwd(), projectName) =>
if projectName->String.trim->String.length === 0 {
Error("Project name must not be empty.")
} else if !(packageNameRegExp->RegExp.test(projectName)) {
Error("Project name may only contain lower case letters, numbers and hyphens.")
} else {
let projectPath = Path.join2(cwd, projectName)
if Fs.existsSync(projectPath) && !(projectPath->containsOnlyDevcontainerDirectory) {
Error(`The folder ${projectName} already exist in the current directory.`)
} else {
Ok()
}
}