Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/NewProjectLocation.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ open Node

let currentDirectoryArgument = "."
let packageNameRegExp = /^[a-z0-9-]+$/
let devcontainerDirectoryName = ".devcontainer"

let allowedCurrentDirectoryEntries = [
".git",
Expand All @@ -27,17 +28,38 @@ let getPackageName = (~cwd=Process.cwd(), projectName) =>
let getProjectPath = (~cwd=Process.cwd(), projectName) =>
isCurrentDirectoryProject(projectName) ? cwd : Path.join2(cwd, projectName)

let isAllowedCurrentDirectoryEntry = entry => {
let hasDevcontainerDirectory = projectPath =>
try {
Path.join2(projectPath, devcontainerDirectoryName)->Fs.statSync->Fs.Stats.isDirectory
} catch {
| Exn.Error(_) => false
}

let containsOnlyDevcontainerDirectory = projectPath =>
try {
switch Fs.readdirSync(projectPath) {
| [entry] if entry === devcontainerDirectoryName => hasDevcontainerDirectory(projectPath)
| _ => false
}
} catch {
| Exn.Error(_) => false
}

let isAllowedCurrentDirectoryEntry = (~cwd, entry) => {
let normalizedEntry = entry->String.toLowerCase

allowedCurrentDirectoryEntries
->Array.find(allowedEntry => allowedEntry === normalizedEntry)
->Option.isSome
switch entry {
| ".devcontainer" => hasDevcontainerDirectory(cwd)
| _ =>
allowedCurrentDirectoryEntries
->Array.find(allowedEntry => allowedEntry === normalizedEntry)
->Option.isSome
}
}

let validateCurrentDirectory = cwd => {
let disallowedEntries =
Fs.readdirSync(cwd)->Array.filter(entry => !(entry->isAllowedCurrentDirectoryEntry))
Fs.readdirSync(cwd)->Array.filter(entry => !isAllowedCurrentDirectoryEntry(~cwd, entry))

switch disallowedEntries {
| [] => Ok()
Expand All @@ -54,7 +76,10 @@ let validateProjectName = (~cwd=Process.cwd(), projectName) => {
Error("Project name may only contain lower case letters, numbers and hyphens.")
} else if isCurrentDirectoryProject(projectName) {
validateCurrentDirectory(cwd)
} else if Fs.existsSync(getProjectPath(~cwd, projectName)) {
} else if (
Fs.existsSync(getProjectPath(~cwd, projectName)) &&
!(getProjectPath(~cwd, projectName)->containsOnlyDevcontainerDirectory)
) {
Error(`The folder ${projectName} already exist in the current directory.`)
} else {
Ok()
Expand Down
8 changes: 8 additions & 0 deletions src/bindings/Node.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ module Fs = {
@module("node:fs")
external readFileSync: (string, @as(json`"utf8"`) _) => string = "readFileSync"

type stats

@module("node:fs") external statSync: string => stats = "statSync"

module Stats = {
@send external isDirectory: stats => bool = "isDirectory"
}

module Promises = {
@module("node:fs") @scope("promises")
external readFile: (string, @as(json`"utf8"`) _) => promise<string> = "readFile"
Expand Down
46 changes: 46 additions & 0 deletions test/NewProjectLocationTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,50 @@ Test.describe("NewProjectLocation", () => {
)
await cleanupTestRoot()
})

Test.testAsync("allows devcontainer-only project directories", async () => {
let projectPath = await resetTestRoot("my-app")
await Fs.Promises.mkdir(Path.join2(projectPath, ".devcontainer"))

NewProjectLocation.validateProjectName(~cwd=testRoot, "my-app")->assertValidationOk
NewProjectLocation.validateProjectName(~cwd=projectPath, ".")->assertValidationOk
NewProjectLocation.validateProjectName(~cwd=projectPath, "./")->assertValidationOk

await Fs.Promises.writeFile(Path.join2(projectPath, "README.md"), "")
NewProjectLocation.validateProjectName(~cwd=projectPath, ".")->assertValidationOk
await cleanupTestRoot()
})

Test.testAsync("rejects devcontainer directories with conflicting project files", async () => {
let projectPath = await resetTestRoot("my-app")
await Fs.Promises.mkdir(Path.join2(projectPath, ".devcontainer"))
await Fs.Promises.writeFile(Path.join2(projectPath, "package.json"), "{}")

NewProjectLocation.validateProjectName(~cwd=testRoot, "my-app")->assertValidationError(
"The folder my-app already exist in the current directory.",
)
NewProjectLocation.validateProjectName(~cwd=projectPath, ".")->assertValidationError(
currentDirectoryConflictMessage,
)
NewProjectLocation.validateProjectName(~cwd=projectPath, "./")->assertValidationError(
currentDirectoryConflictMessage,
)
await cleanupTestRoot()
})

Test.testAsync("rejects a .devcontainer file", async () => {
let projectPath = await resetTestRoot("my-app")
await Fs.Promises.writeFile(Path.join2(projectPath, ".devcontainer"), "")

NewProjectLocation.validateProjectName(~cwd=testRoot, "my-app")->assertValidationError(
"The folder my-app already exist in the current directory.",
)
NewProjectLocation.validateProjectName(~cwd=projectPath, ".")->assertValidationError(
currentDirectoryConflictMessage,
)
NewProjectLocation.validateProjectName(~cwd=projectPath, "./")->assertValidationError(
currentDirectoryConflictMessage,
)
await cleanupTestRoot()
})
})
Loading