Skip to content

Commit 6c976c0

Browse files
authored
Merge pull request #11 from rulasg/fix-parentFolder
fix(New-Note): rename AvoidChildFolder to AddNoteFolder and add DateToday parameter
2 parents 90fd8c5 + b97b2d5 commit 6c976c0

2 files changed

Lines changed: 155 additions & 86 deletions

File tree

Test/public/newNotes.test.ps1

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function Test_AddNotesToday_Simple{
1+
function Test_NewNotes_Simple_DateToday{
22

33
Reset-InvokeCommandMock
44

@@ -17,18 +17,43 @@ function Test_AddNotesToday_Simple{
1717
$categoryPath = New-TestingFolder -Path "./TestNotesRoot/$category" -PassThru
1818

1919
# Add note with folder using -Force
20-
$path = New-NoteToday $category $title -NoOpen -Force
20+
$path = New-Note $category $title -NoOpen -Force -DateToday
2121

2222
$content = Get-Content -Path $path -Raw
2323

2424
Assert-IsTrue $content.StartsWith($header)
2525

26-
# File should be in a folder of its own name
26+
# File should be in the catergory name
2727
$parentPath = $path | Split-Path -parent
2828
Assert-AreEqualPath -Expected $categoryPath -Presented $parentPath
2929

3030
}
3131

32+
function Test_NewNote_Simple_WithDate{
33+
34+
Reset-InvokeCommandMock
35+
36+
New-TestingFolder "TestNotesRoot"
37+
MockCallToString 'Invoke-NotesHelperNotesRoot' -OutString "./TestNotesRoot"
38+
39+
$category = "TestClient"
40+
$title = "This is the title of the note"
41+
$date = "240101"
42+
43+
$header = "# {category} - {title} ({date})"
44+
$header = $header -replace "{category}", $category
45+
$header = $header -replace "{title}", $title
46+
$header = $header -replace "{date}", $date
47+
48+
# Add note with date
49+
$path = New-Note $category $title -NoOpen -Force -Date $date
50+
51+
$content = Get-Content -Path $path -Raw
52+
53+
Assert-IsTrue $content.StartsWith($header)
54+
55+
}
56+
3257
function Test_AddNotesToday_Simple_AddNoteFolder {
3358

3459
Reset-InvokeCommandMock
@@ -42,6 +67,7 @@ function Test_AddNotesToday_Simple_AddNoteFolder {
4267
New-TestingFolder -Path "./TestNotesRoot/$category"
4368

4469
# Add folder for the note -AddNoteFolder
70+
# $path = New-NoteToday $category $title -NoOpen -AddNoteFolder
4571
$path = New-NoteToday $category $title -NoOpen -AddNoteFolder
4672

4773
# File should be in a folder of its own name
@@ -139,15 +165,16 @@ function Test_NewNotes_SUCCESS{
139165
# Act
140166
$result = New-Note howto "someting that may be useful" -NoOpen
141167

142-
$expectedPath = "./TestNotesRoot/howto/howto-someting_that_may_be_useful/howto-someting_that_may_be_useful.md"
168+
# $expectedPath = "./TestNotesRoot/howto/howto-someting_that_may_be_useful/howto-someting_that_may_be_useful.md"
169+
$expectedPath = "./TestNotesRoot/howto/howto-someting_that_may_be_useful.md"
143170

144171
Assert-AreEqualPath -Expected $expectedPath -Presented $result
145172

146173
# With date
147174

148175
$result = New-Note howto "someting that may be useful" -NoOpen -Date $today
149176

150-
$expectedPath = "./TestNotesRoot/howto/$today-howto-someting_that_may_be_useful/$today-howto-someting_that_may_be_useful.md"
177+
$expectedPath = "./TestNotesRoot/howto/$today-howto-someting_that_may_be_useful.md"
151178

152179
Assert-AreEqualPath -Expected $expectedPath -Presented $result
153180

@@ -167,15 +194,16 @@ function Test_NewNotes_SUCCESS_WithRootPath{
167194
# Act
168195
$result = New-Note howto "someting that may be useful" -NoOpen -RootPath $RootFolder
169196

170-
$expectedPath = "./$RootFolder/howto/howto-someting_that_may_be_useful/howto-someting_that_may_be_useful.md"
197+
# $expectedPath = "./$RootFolder/howto/howto-someting_that_may_be_useful/howto-someting_that_may_be_useful.md"
198+
$expectedPath = "./$RootFolder/howto/howto-someting_that_may_be_useful.md"
171199

172200
Assert-AreEqualPath -Expected $expectedPath -Presented $result
173201

174202
# With date
175203

176204
$result = New-Note howto "someting that may be useful" -NoOpen -Date $today -RootPath $RootFolder
177205

178-
$expectedPath = "./$RootFolder/howto/$today-howto-someting_that_may_be_useful/$today-howto-someting_that_may_be_useful.md"
206+
$expectedPath = "./$RootFolder/howto/$today-howto-someting_that_may_be_useful.md"
179207

180208
Assert-AreEqualPath -Expected $expectedPath -Presented $result
181209

public/newNotes.ps1

Lines changed: 120 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ function New-Note{
1212
[Parameter()][string] $IssueUrl,
1313
[Parameter()][string] [ValidateSet("none","meetingmini")] $Template = "none",
1414
[Parameter()][switch] $NoOpen,
15-
[Parameter()][switch] $AvoidChildFolder,
15+
[Parameter()][switch] $AddNoteFolder,
1616
[Parameter()][switch] $Force,
17-
[Parameter()][string] $RootPath
17+
[Parameter()][string] $RootPath,
18+
[Parameter()][switch] $DateToday
1819

1920
)
2021

@@ -32,30 +33,35 @@ function New-Note{
3233
return
3334
}
3435

35-
$fullTitle = getFullTitle -Category $Category -Section $Section -Title $Title -Date $Date
36+
# Replace date with today if specified
37+
if($DateToday) {
38+
$Date = Get-Date -Format "yyMMdd"
39+
}
40+
41+
$fileName = getFileName -Category $Category -Section $Section -Title $Title -Date $Date
3642

3743
# Create the note base folder
38-
if($AvoidChildFolder){
39-
# use folder as the parent folder of the note
40-
$fullPath = Join-Path -Path $folder -ChildPath "$fullTitle.md"
41-
} else {
44+
if($AddNoteFolder){
4245
# Create full path for the note file
43-
$noteFolder = Join-Path -Path $folder -ChildPath $fullTitle
46+
$noteFolder = Join-Path -Path $folder -ChildPath $fileName
4447

4548
if (-not (Test-Path -Path $noteFolder)) {
4649
New-Item -Path $noteFolder -ItemType Directory -Force | Out-Null
4750
Write-Verbose "Created note folder: $noteFolder"
4851
}
49-
50-
$fullPath =$noteFolder | Join-Path -ChildPath "$fullTitle.md"
52+
53+
$fullPath =$noteFolder | Join-Path -ChildPath "$fileName.md"
54+
} else {
55+
# use folder as the parent folder of the note
56+
$fullPath = Join-Path -Path $folder -ChildPath "$fileName.md"
5157
}
5258

5359
# Check if file already exists
5460
if (-Not (Test-Path -Path $fullPath)) {
5561

5662
$header = [string]::IsNullOrWhiteSpace($Section) ? $Category : $Section
5763

58-
$content = getFileContent $Template $Title $header -Notes $Notes -IssueUrl $IssueUrl
64+
$content = getFileContent $Template $Title $header -Notes $Notes -IssueUrl $IssueUrl -Date $Date
5965

6066
# If $Force check that the folders of $fullPath exists and if not create it
6167
if ($Force) {
@@ -81,7 +87,7 @@ function New-Note{
8187

8288
} Export-ModuleMember -Function New-Note -Alias "note"
8389

84-
function getFullTitle{
90+
function getFileName{
8591
[CmdletBinding()]
8692
param(
8793
[Parameter(Mandatory)][string] $Category,
@@ -147,85 +153,120 @@ function New-NoteToday{
147153
[Parameter()][string] $RootPath
148154
)
149155

150-
# FILENAME
151-
152-
$folder = Get-NoteFolder -RootPath $RootPath -Category $Category -Section $Section -Force:$Force
153-
154-
if(-Not $folder) {
155-
Write-Error "Failed to create the folder for the note. Try -Force."
156-
return
157-
}
158-
159-
if(-Not (Test-Path -Path $folder)) {
160-
Write-Error "Base folder for note does not exist '$folder'. Try -Force."
161-
return
162-
}
156+
$ret = New-Note `
157+
-Category $Category `
158+
-Section $Section `
159+
-Title $Title `
160+
-Notes $Notes `
161+
-IssueUrl $IssueUrl `
162+
-Template $Template `
163+
-NoOpen:$NoOpen `
164+
-AddNoteFolder:$AddNoteFolder `
165+
-Force:$Force `
166+
-RootPath $RootPath `
167+
-DateToday
168+
169+
return $ret
163170

164-
# Extract just the folder name from the path
165-
$today = Get-Date -Format "yyMMdd"
171+
} Export-ModuleMember -Function New-NoteToday -Alias "note"
166172

167-
$header = [string]::IsNullOrWhiteSpace($Section) ? $Category : $Section
173+
# function New-NoteToday{
174+
# # Add Force parameter to support creation of client folder if it doesn't exist
175+
# [CmdletBinding()]
176+
# [alias("note")]
177+
# param(
178+
# [Parameter(Mandatory,Position = 0)][string] $Category,
179+
# [Parameter(Mandatory,Position = 1)][string] $Title,
180+
# [Parameter()][string] $Section,
181+
# [Parameter()][string] $Notes,
182+
# [Parameter()][string] $IssueUrl,
183+
# [Parameter()][string] [ValidateSet("none","meetingmini")] $Template = "none",
184+
# [Parameter()][switch] $NoOpen,
185+
# [Parameter()][switch] $AddNoteFolder,
186+
# [Parameter()][switch] $Force,
187+
# [Parameter()][string] $RootPath,
188+
# [Parameter()][string] $Date
189+
# )
190+
191+
# # FILENAME
192+
193+
# $folder = Get-NoteFolder -RootPath $RootPath -Category $Category -Section $Section -Force:$Force
194+
195+
# if(-Not $folder) {
196+
# Write-Error "Failed to create the folder for the note. Try -Force."
197+
# return
198+
# }
199+
200+
# if(-Not (Test-Path -Path $folder)) {
201+
# Write-Error "Base folder for note does not exist '$folder'. Try -Force."
202+
# return
203+
# }
204+
205+
# # Extract just the folder name from the path
206+
# $today = if([string]::IsNullOrWhiteSpace($Date)) { Get-Date -Format "yyMMdd" } else { $Date }
207+
208+
# $header = [string]::IsNullOrWhiteSpace($Section) ? $Category : $Section
168209

169-
# Create FullTitle using folder name and title, replacing spaces with underscores
170-
$fullTitle = "{0}-{1}-{2}" -f $today, $header, $Title
210+
# # Create FullTitle using folder name and title, replacing spaces with underscores
211+
# $fullTitle = "{0}-{1}-{2}" -f $today, $header, $Title
171212

172-
# Normilize fullTitle by removing special characters and replacing spaces with underscores
173-
$fullTitle = $fullTitle -replace '\s+', '_'
213+
# # Normilize fullTitle by removing special characters and replacing spaces with underscores
214+
# $fullTitle = $fullTitle -replace '\s+', '_'
174215

175-
# Create the note base folder
216+
# # Create the note base folder
176217

177-
if($AddNoteFolder){
178-
# Create full path for the note file
179-
$noteFolder = Join-Path -Path $folder -ChildPath $fullTitle
218+
# if($AddNoteFolder){
219+
# # Create full path for the note file
220+
# $noteFolder = Join-Path -Path $folder -ChildPath $fullTitle
180221

181-
if (-not (Test-Path -Path $noteFolder)) {
182-
New-Item -Path $noteFolder -ItemType Directory -Force | Out-Null
183-
Write-Verbose "Created note folder: $noteFolder"
184-
}
185-
186-
$fullPath =$noteFolder | Join-Path -ChildPath "$fullTitle.md"
187-
} else {
188-
# use folder as the parent folder of the note
189-
$fullPath = Join-Path -Path $folder -ChildPath "$fullTitle.md"
190-
}
191-
192-
# Check if file already exists
193-
if (-Not (Test-Path -Path $fullPath)) {
222+
# if (-not (Test-Path -Path $noteFolder)) {
223+
# New-Item -Path $noteFolder -ItemType Directory -Force | Out-Null
224+
# Write-Verbose "Created note folder: $noteFolder"
225+
# }
226+
227+
# $fullPath =$noteFolder | Join-Path -ChildPath "$fullTitle.md"
228+
# } else {
229+
# # use folder as the parent folder of the note
230+
# $fullPath = Join-Path -Path $folder -ChildPath "$fullTitle.md"
231+
# }
232+
233+
# # Check if file already exists
234+
# if (-Not (Test-Path -Path $fullPath)) {
194235

195-
# Get template content
196-
$content = Get-TemplatePath $Template | Get-FileContent
236+
# # Get template content
237+
# $content = Get-TemplatePath $Template | Get-FileContent
197238

198-
# Replace placeholders in the template with actual values
199-
$content = $content -replace '{title}' , $Title
200-
$content = $content -replace '{header}' , $header
201-
$content = $content -replace '{date}' , $today
202-
$content = $content -replace '{notes}' , ([string]::IsNullOrWhiteSpace($Notes) ? '-' : $Notes)
203-
$content = $content -replace '{issueurl}' , ([string]::IsNullOrWhiteSpace($IssueUrl) ? '<IssueUrl>' : $IssueUrl)
204-
205-
206-
# If $Force check that the folders of $fullPath exists and if not create it
207-
if ($Force) {
208-
$parentFolder = Split-Path -Path $fullPath -Parent
209-
if (-Not (Test-Path -Path $parentFolder)) {
210-
New-Item -Path $parentFolder -ItemType Directory -Force | Out-Null
211-
Write-Verbose "Created folder: $parentFolder"
212-
}
213-
}
214-
215-
# Create the file with content
216-
Set-Content -Path $fullPath -Value $content -Force
217-
}
239+
# # Replace placeholders in the template with actual values
240+
# $content = $content -replace '{title}' , $Title
241+
# $content = $content -replace '{header}' , $header
242+
# $content = $content -replace '{date}' , $today
243+
# $content = $content -replace '{notes}' , ([string]::IsNullOrWhiteSpace($Notes) ? '-' : $Notes)
244+
# $content = $content -replace '{issueurl}' , ([string]::IsNullOrWhiteSpace($IssueUrl) ? '<IssueUrl>' : $IssueUrl)
245+
246+
247+
# # If $Force check that the folders of $fullPath exists and if not create it
248+
# if ($Force) {
249+
# $parentFolder = Split-Path -Path $fullPath -Parent
250+
# if (-Not (Test-Path -Path $parentFolder)) {
251+
# New-Item -Path $parentFolder -ItemType Directory -Force | Out-Null
252+
# Write-Verbose "Created folder: $parentFolder"
253+
# }
254+
# }
255+
256+
# # Create the file with content
257+
# Set-Content -Path $fullPath -Value $content -Force
258+
# }
218259

219-
if( -not $NoOpen) {
220-
# Open file in VS Code with cursor at the end
221-
$gotocmd = "{0}{1}" -f $fullPath, ":9999"
222-
code --goto $gotocmd
223-
}
260+
# if( -not $NoOpen) {
261+
# # Open file in VS Code with cursor at the end
262+
# $gotocmd = "{0}{1}" -f $fullPath, ":9999"
263+
# code --goto $gotocmd
264+
# }
224265

225-
# Return file just created
226-
return $fullPath
266+
# # Return file just created
267+
# return $fullPath
227268

228-
} Export-ModuleMember -Function New-NoteToday -Alias "note"
269+
# } Export-ModuleMember -Function New-NoteToday -Alias "note"
229270

230271
function New-NoteTodayClient{
231272
[CmdletBinding()]

0 commit comments

Comments
 (0)