Skip to content

Commit a5e8a0f

Browse files
authored
Merge pull request #32 from SecJS/fix/len-adjust-globs-path
fix: Adjust Folder globs and Path environment for build
2 parents a5e16bd + a677eda commit a5e8a0f

File tree

9 files changed

+91
-74
lines changed

9 files changed

+91
-74
lines changed

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ console.log(existentFolder.moveSync('path/to/move'))
134134
// you can use getFilesByPattern method to get all files in the folder that match some pattern
135135
// if recursive is true, will go inside subFolders too
136136
const recursive = true
137-
console.log(existentFolder.getFilesByPattern('path/to/**/*.ts', recursive)) // [...files instance]
137+
console.log(existentFolder.getFilesByPattern('**/*.ts', recursive)) // [...files instance]
138138

139139
// you can use getFoldersByPattern method to get all folders in the folder that match some pattern
140-
console.log(existentFolder.getFoldersByPattern('path/to/**/folder', recursive)) // [...folders instance]
140+
console.log(existentFolder.getFoldersByPattern('**', recursive)) // [...folders instance]
141141

142142
// Folder uses readable streams in async methods to not block the event loop when handling huge files content
143143
await existentFolder.load()
@@ -156,22 +156,31 @@ await existentFolder.create()
156156
```ts
157157
import { Path } from '@secjs/utils'
158158

159-
// pwd method will depend if you NODE_ENV environment variable is set.
160-
// if you are using ci, testing or ts-development, pwd will return like this:
159+
// If NODE_TS is set to true, Path.pwd will always return without the build folder in the end of the path
161160
Path.pwd() // '/home/your/computer/path/your-project-name'
162161

163-
// if your NODE_ENV is not set, or you are using a diffrent value from ci, testing or ts-development,
164-
// your pwd will return like this:
162+
// If NODE_TS is set to false, Path.pwd will always return with the build folder in the end of the path
165163
Path.pwd() // '/home/your/computer/path/your-project-name/dist'
166164

167-
// you can change your buildFolder name using forBuildFolder method
168-
Path.forBuild('build').pwd()
165+
// You can use the method switchEnvVerify to turn off the NODE_TS environment variable verification
166+
Path.switchEnvVerify()
167+
168+
// You can change the default build folder name using changeBuild method
169+
Path.changeBuild('build')
169170
// '/home/your/computer/path/your-project-name/build'
170171

171-
Path.pwd('/src/') // '/home/your/computer/path/your-project-name/src'
172+
// you can change your build folder name using forBuild method too
173+
Path.forBuild('buildd').pwd()
174+
// '/home/your/computer/path/your-project-name/buildd'
175+
176+
Path.pwd('/src/') // '/home/your/computer/path/your-project-name/build/src'
177+
178+
// You can use switchBuild to turn on or turn off the forceBuild parameter
179+
// forceBuild on
180+
Path.switchBuild().public() // '/home/your/computer/path/your-project-name/build/public'
172181

173-
Path.public() // '/home/your/computer/path/your-project-name/public'
174-
Path.assets() // '/home/your/computer/path/your-project-name/public/assets'
182+
// forceBuild off
183+
Path.switchBuild().assets() // '/home/your/computer/path/your-project-name/public/assets'
175184
```
176185

177186
---

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/utils",
3-
"version": "1.4.3",
3+
"version": "1.4.4",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",

src/Classes/Folder.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,14 @@ export class Folder {
504504
const files = []
505505

506506
this._files.forEach(file => {
507-
if (minimatch(file.path, Path.pwd(pattern))) {
507+
if (minimatch(file.path, `${this.path}/${pattern}`)) {
508508
files.push(file)
509509
}
510510

511511
if (recursive) {
512-
files.push(...Folder.getSubFiles(this._folders, pattern))
512+
files.push(
513+
...Folder.getSubFiles(this._folders, `${this.path}/${pattern}`),
514+
)
513515
}
514516
})
515517

@@ -526,24 +528,28 @@ export class Folder {
526528
const folders = []
527529

528530
this._folders.forEach(folder => {
529-
if (minimatch(folder.path, Path.pwd(pattern))) {
531+
if (minimatch(folder.path, `${this.path}/${pattern}`)) {
530532
folders.push(folder)
531533
}
532534

533535
if (recursive && folder.folders.length) {
534-
folders.push(...Folder.getSubFolders(folder, recursive))
536+
folders.push(
537+
...Folder.getSubFolders(folder, recursive, `${this.path}/${pattern}`),
538+
)
535539
}
536540
})
537541

538542
return folders
539543
}
540544

541545
private static getSubFiles(folders: Folder[], pattern = null): File[] {
546+
if (pattern) pattern = isAbsolute(pattern) ? pattern : Path.pwd(pattern)
547+
542548
const files = []
543549

544550
folders.forEach(folder => {
545551
folder.files.forEach(file => {
546-
if (pattern && minimatch(file.path, Path.pwd(pattern))) {
552+
if (pattern && minimatch(file.path, pattern)) {
547553
files.push(file)
548554
}
549555

@@ -563,10 +569,12 @@ export class Folder {
563569
recursive: boolean,
564570
pattern = null,
565571
): Folder[] {
572+
if (pattern) pattern = isAbsolute(pattern) ? pattern : Path.pwd(pattern)
573+
566574
const subFolders = []
567575

568576
folder.folders.forEach(folder => {
569-
if (pattern && minimatch(folder.path, Path.pwd(pattern))) {
577+
if (pattern && minimatch(folder.path, pattern)) {
570578
subFolders.push(folder)
571579
}
572580

src/Classes/Path.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ export class Path {
1919
return this
2020
}
2121

22-
static switchEnvVerify() {
23-
this._verifyNodeEnv = !this._verifyNodeEnv
22+
static switchBuild() {
23+
this._forceBuild = !this._forceBuild
2424

2525
return this
2626
}
2727

28-
static changeBuild(name: string) {
29-
this._defaultBuild = name
28+
static switchEnvVerify() {
29+
this._verifyNodeEnv = !this._verifyNodeEnv
3030

3131
return this
3232
}
3333

34-
static forceBuild() {
35-
this._forceBuild = true
34+
static changeBuild(name: string) {
35+
this._defaultBuild = name
3636

3737
return this
3838
}
@@ -51,13 +51,17 @@ export class Path {
5151
if (this._forceBuild) {
5252
cwdNodePath += this.adjustSlashes(this._defaultBuild)
5353

54-
this._forceBuild = false
54+
return cwdNodePath
5555
}
5656

57-
if (!this._forceBuild && this._verifyNodeEnv) {
58-
if (['ci', 'testing', 'ts-development'].includes(process.env.NODE_ENV)) {
59-
cwdNodePath += this.adjustSlashes(this._defaultBuild)
60-
}
57+
if (
58+
!this._forceBuild &&
59+
this._verifyNodeEnv &&
60+
process.env.NODE_TS === 'true'
61+
) {
62+
cwdNodePath += this.adjustSlashes(this._defaultBuild)
63+
64+
return cwdNodePath
6165
}
6266

6367
return this.adjustSlashes(cwdNodePath)

src/utils/global.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ declare global {
3131

3232
class Path {
3333
static nodeCwdPath(): string
34-
static forceBuild(): typeof Path
34+
static switchBuild(): typeof Path
35+
static switchEnvVerify(): typeof Path
36+
static forBuild(name: string): typeof Path
37+
static changeBuild(name: string): typeof Path
3538
static pwd(subPath?: string): string
3639
static app(subPath?: string): string
37-
static switchEnvVerify(): typeof Path
3840
static logs(subPath?: string): string
3941
static start(subPath?: string): string
4042
static views(subPath?: string): string
41-
static tests(subPath?: string): string
4243
static config(subPath?: string): string
44+
static tests(subPath?: string): string
4345
static public(subPath?: string): string
4446
static assets(subPath?: string): string
4547
static storage(subPath?: string): string
46-
static locales(subPath?: string): string
4748
static database(subPath?: string): string
49+
static locales(subPath?: string): string
4850
static resources(subPath?: string): string
4951
static providers(subPath?: string): string
50-
static forBuild(name: string): typeof Path
51-
static changeBuild(name: string): typeof Path
5252
}
5353

5454
class File {
@@ -209,7 +209,7 @@ declare global {
209209
* @param options.withSub Default is true
210210
* @param options.withFileContent Default is false
211211
*/
212-
loadSync(options?: { withSub?: boolean; withFileContent?: boolean })
212+
loadSync(options?: { withSub?: boolean; withFileContent?: boolean }): Folder
213213
/**
214214
* load
215215
*

tests/Classes/folder.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,22 @@ describe('\n Folder Class', () => {
135135
})
136136

137137
it('should get all files and folders that match the pattern', async () => {
138-
const files = bigFolder
139-
.loadSync()
140-
.getFilesByPattern('tests/folder-class-test/**/*.txt', true)
138+
const files = bigFolder.loadSync().getFilesByPattern('**/*.txt', true)
141139

142140
expect(files.length).toBe(3)
143141

144142
files.forEach(file => expect(file.extension).toBe('.txt'))
145143

146-
const folders = bigFolder.getFoldersByPattern(
147-
'tests/folder-class-test/big/*',
148-
true,
149-
)
144+
const folders = bigFolder.getFoldersByPattern('*', true)
150145

151-
expect(folders.length).toBe(2)
146+
expect(folders.length).toBe(1)
152147
expect(folders[0].name).toBe('hello')
153-
expect(folders[1].name).toBe('nice')
148+
149+
const folderSubFolder = bigFolder.getFoldersByPattern('**', true)
150+
151+
expect(folderSubFolder.length).toBe(2)
152+
expect(folderSubFolder[0].name).toBe('hello')
153+
expect(folderSubFolder[1].name).toBe('nice')
154154
})
155155

156156
it('should load all the files and subFolders with the files content', async () => {

tests/Classes/path.spec.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,20 @@ describe('\n Path Class', () => {
4444
const myMainPath = process.cwd()
4545
const myMainDistPath = process.cwd() + '/dist'
4646

47-
expect(Path.forceBuild().nodeCwdPath()).toBe(myMainDistPath)
47+
Path.switchBuild()
48+
49+
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
4850
expect(Path.forBuild('build').nodeCwdPath()).toBe(myMainPath + '/build')
49-
expect(Path.forceBuild().nodeCwdPath()).toBe(myMainDistPath)
50-
expect(Path.forceBuild().changeBuild('/test').nodeCwdPath()).toBe(
51-
myMainPath + '/test',
52-
)
51+
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
52+
expect(Path.changeBuild('/test').nodeCwdPath()).toBe(myMainPath + '/test')
5353

54-
Path.changeBuild('/dist')
54+
Path.changeBuild('/dist').switchBuild()
5555

56-
process.env.NODE_ENV = 'testing'
56+
process.env.NODE_TS = 'true'
5757
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
5858

59-
process.env.NODE_ENV = 'ts-development'
60-
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
59+
Path.switchEnvVerify()
6160

62-
process.env.NODE_ENV = 'production'
6361
expect(Path.nodeCwdPath()).toBe(myMainPath)
6462
})
6563
})

tests/global/folder.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,22 @@ describe('\n Folder Class Global', () => {
138138
})
139139

140140
it('should get all files and folders that match the pattern', async () => {
141-
const files = bigFolder
142-
.loadSync()
143-
.getFilesByPattern('tests/folder-class-global-test/**/*.txt', true)
141+
const files = bigFolder.loadSync().getFilesByPattern('**/*.txt', true)
144142

145143
expect(files.length).toBe(3)
146144

147145
files.forEach(file => expect(file.extension).toBe('.txt'))
148146

149-
const folders = bigFolder.getFoldersByPattern(
150-
'tests/folder-class-global-test/big/*',
151-
true,
152-
)
147+
const folders = bigFolder.getFoldersByPattern('*', true)
153148

154-
expect(folders.length).toBe(2)
149+
expect(folders.length).toBe(1)
155150
expect(folders[0].name).toBe('hello')
156-
expect(folders[1].name).toBe('nice')
151+
152+
const folderSubFolder = bigFolder.getFoldersByPattern('**', true)
153+
154+
expect(folderSubFolder.length).toBe(2)
155+
expect(folderSubFolder[0].name).toBe('hello')
156+
expect(folderSubFolder[1].name).toBe('nice')
157157
})
158158

159159
it('should load all the files and subFolders with the files content', async () => {

tests/global/path.spec.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,20 @@ describe('\n Path Class Global', () => {
4444
const myMainPath = process.cwd()
4545
const myMainDistPath = process.cwd() + '/dist'
4646

47-
expect(Path.forceBuild().nodeCwdPath()).toBe(myMainDistPath)
47+
Path.switchBuild()
48+
49+
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
4850
expect(Path.forBuild('build').nodeCwdPath()).toBe(myMainPath + '/build')
49-
expect(Path.forceBuild().nodeCwdPath()).toBe(myMainDistPath)
50-
expect(Path.forceBuild().changeBuild('/test').nodeCwdPath()).toBe(
51-
myMainPath + '/test',
52-
)
51+
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
52+
expect(Path.changeBuild('/test').nodeCwdPath()).toBe(myMainPath + '/test')
5353

54-
Path.changeBuild('/dist')
54+
Path.changeBuild('/dist').switchBuild()
5555

56-
process.env.NODE_ENV = 'testing'
56+
process.env.NODE_TS = 'true'
5757
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
5858

59-
process.env.NODE_ENV = 'ts-development'
60-
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
59+
Path.switchEnvVerify()
6160

62-
process.env.NODE_ENV = 'production'
6361
expect(Path.nodeCwdPath()).toBe(myMainPath)
6462
})
6563
})

0 commit comments

Comments
 (0)