-
Notifications
You must be signed in to change notification settings - Fork 16
Support creating projects in current directory #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fhammerschmidt
wants to merge
1
commit into
rescript-lang:master
Choose a base branch
from
fhammerschmidt:issue-53-current-directory
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| open Node | ||
|
|
||
| let currentDirectoryArgument = "." | ||
| let packageNameRegExp = /^[a-z0-9-]+$/ | ||
|
|
||
| let allowedCurrentDirectoryEntries = [ | ||
| ".git", | ||
| ".gitattributes", | ||
| ".gitignore", | ||
| "licence", | ||
| "licence.md", | ||
| "license", | ||
| "license.md", | ||
| "readme", | ||
| "readme.md", | ||
| ] | ||
|
|
||
| let isCurrentDirectoryProject = projectName => projectName === currentDirectoryArgument | ||
|
|
||
| let getPackageName = (~cwd=Process.cwd(), projectName) => | ||
| isCurrentDirectoryProject(projectName) ? Path.basename(cwd) : projectName | ||
|
|
||
| let getProjectPath = (~cwd=Process.cwd(), projectName) => | ||
| isCurrentDirectoryProject(projectName) ? cwd : Path.join2(cwd, projectName) | ||
|
|
||
| let isAllowedCurrentDirectoryEntry = entry => { | ||
| let normalizedEntry = entry->String.toLowerCase | ||
|
|
||
| allowedCurrentDirectoryEntries | ||
| ->Array.find(allowedEntry => allowedEntry === normalizedEntry) | ||
| ->Option.isSome | ||
| } | ||
|
|
||
| let validateCurrentDirectory = cwd => { | ||
| let disallowedEntries = | ||
| Fs.readdirSync(cwd)->Array.filter(entry => !(entry->isAllowedCurrentDirectoryEntry)) | ||
|
|
||
| switch disallowedEntries { | ||
| | [] => Ok() | ||
| | _ => Error("The current directory contains files that could conflict with project creation.") | ||
| } | ||
| } | ||
|
|
||
| let validateProjectName = (~cwd=Process.cwd(), projectName) => { | ||
| let packageName = getPackageName(~cwd, projectName) | ||
|
|
||
| if packageName->String.trim->String.length === 0 { | ||
| Error("Project name must not be empty.") | ||
| } else if !(packageNameRegExp->RegExp.test(packageName)) { | ||
| 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)) { | ||
| Error(`The folder ${projectName} already exist in the current directory.`) | ||
| } else { | ||
| Ok() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| open Node | ||
|
|
||
| let testRoot = Path.join2(Process.cwd(), ".tmp-new-project-location-test") | ||
| let currentDirectoryConflictMessage = "The current directory contains files that could conflict with project creation." | ||
|
|
||
| let cleanupTestRoot = async () => | ||
| await Fs.Promises.rm(testRoot, ~options={recursive: true, force: true}) | ||
|
|
||
| let resetTestRoot = async projectDirectoryName => { | ||
| await cleanupTestRoot() | ||
| let projectPath = Path.join2(testRoot, projectDirectoryName) | ||
| await Fs.Promises.mkdir(projectPath, ~options={recursive: true}) | ||
| projectPath | ||
| } | ||
|
|
||
| let assertValidationOk = result => | ||
| switch result { | ||
| | Ok() => () | ||
| | Error(message) => Assert.fail(`Expected project name to be valid, got: ${message}`) | ||
| } | ||
|
|
||
| let assertValidationError = (result, expectedMessage) => | ||
| switch result { | ||
| | Error(message) => Assert.strictEqual(message, expectedMessage) | ||
| | Ok() => Assert.fail(`Expected validation error: ${expectedMessage}`) | ||
| } | ||
|
|
||
| Test.describe("NewProjectLocation", () => { | ||
| Test.test("uses the current directory basename as the package name", () => { | ||
| NewProjectLocation.getPackageName(~cwd="/tmp/my-app", ".")->Assert.strictEqual("my-app") | ||
| }) | ||
|
|
||
| Test.test("uses the current directory as the project path", () => { | ||
| NewProjectLocation.getProjectPath(~cwd="/tmp/my-app", ".")->Assert.strictEqual("/tmp/my-app") | ||
| }) | ||
|
|
||
| Test.testAsync("allows creating in a repository with README and license files", async () => { | ||
| let projectPath = await resetTestRoot("my-app") | ||
| await Fs.Promises.mkdir(Path.join2(projectPath, ".git")) | ||
| await Fs.Promises.writeFile(Path.join2(projectPath, "README.md"), "") | ||
| await Fs.Promises.writeFile(Path.join2(projectPath, "LICENSE"), "") | ||
|
|
||
| NewProjectLocation.validateProjectName(~cwd=projectPath, ".")->assertValidationOk | ||
| await cleanupTestRoot() | ||
| }) | ||
|
|
||
| Test.testAsync("rejects creating in a current directory with project files", async () => { | ||
| let projectPath = await resetTestRoot("my-app") | ||
| await Fs.Promises.writeFile(Path.join2(projectPath, "src"), "") | ||
|
|
||
| NewProjectLocation.validateProjectName(~cwd=projectPath, ".")->assertValidationError( | ||
| currentDirectoryConflictMessage, | ||
| ) | ||
| await cleanupTestRoot() | ||
| }) | ||
|
|
||
| Test.testAsync("rejects creating a nested project that already exists", async () => { | ||
| let _ = await resetTestRoot("existing-app") | ||
|
|
||
| NewProjectLocation.validateProjectName(~cwd=testRoot, "existing-app")->assertValidationError( | ||
| "The folder existing-app already exist in the current directory.", | ||
| ) | ||
| await cleanupTestRoot() | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current-directory detection only matches the exact string
".", so a common equivalent input likecreate-rescript-app ./ --template viteis treated as a regular project name and then rejected by the package-name regex. This makes the new current-directory flow fail for a valid path form users often pass from shell completion/scripts; normalizing the argument (e.g., resolving./to.) before validation would avoid this regression.Useful? React with 👍 / 👎.