Skip to content

Commit 5ae8706

Browse files
authored
Merge pull request #24 from SecJS/feat/len-add-path
feat: Add Path class helper
2 parents be702d5 + 5be6baf commit 5ae8706

File tree

7 files changed

+247
-3
lines changed

7 files changed

+247
-3
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,33 @@ npm install @secjs/utils
4040

4141
## Classes Usage
4242

43+
### Path
44+
45+
> Use Path to get the absolute path from project folders.
46+
47+
```ts
48+
import { Path } from '@secjs/utils'
49+
50+
// pwd method will depend if you NODE_ENV environment variable is set.
51+
// if you are using ci, testing or ts-development, pwd will return like this:
52+
Path.pwd() // '/home/your/computer/path/your-project-name'
53+
54+
// if your NODE_ENV is not set, or you are using a diffrent value from ci, testing or ts-development,
55+
// your pwd will return like this:
56+
Path.pwd() // '/home/your/computer/path/your-project-name/dist'
57+
58+
// you can change your buildFolder name using forBuildFolder method
59+
Path.forBuild('build').pwd()
60+
// '/home/your/computer/path/your-project-name/build'
61+
62+
Path.pwd('/src/') // '/home/your/computer/path/your-project-name/src'
63+
64+
Path.public() // '/home/your/computer/path/your-project-name/public'
65+
Path.assets() // '/home/your/computer/path/your-project-name/public/assets'
66+
```
67+
68+
---
69+
4370
### Json
4471

4572
> Use Json to parse json without errors, deep copy and more.

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './src/Classes/Json'
2+
export * from './src/Classes/Path'
23
export * from './src/Classes/Token'
34
export * from './src/Classes/Clean'
45
export * from './src/Classes/Route'

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.3.9",
3+
"version": "1.4.0",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",

src/Classes/Path.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* @secjs/utils
3+
*
4+
* (c) João Lenon <lenon@secjs.com.br>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
export class Path {
11+
private static _tempBuild = null
12+
private static _forceBuild = false
13+
private static _defaultBuild = 'dist'
14+
private static _verifyNodeEnv = true
15+
16+
static forBuild(name: string) {
17+
this._tempBuild = name
18+
19+
return this
20+
}
21+
22+
static switchEnvVerify() {
23+
this._verifyNodeEnv = !this._verifyNodeEnv
24+
25+
return this
26+
}
27+
28+
static changeBuild(name: string) {
29+
this._defaultBuild = name
30+
31+
return this
32+
}
33+
34+
static forceBuild() {
35+
this._forceBuild = true
36+
37+
return this
38+
}
39+
40+
static nodeCwdPath() {
41+
let cwdNodePath = process.cwd()
42+
43+
if (this._tempBuild) {
44+
cwdNodePath += this.adjustSlashes(this._tempBuild)
45+
46+
this._tempBuild = null
47+
48+
return cwdNodePath
49+
}
50+
51+
if (this._forceBuild) {
52+
cwdNodePath += this.adjustSlashes(this._defaultBuild)
53+
54+
this._forceBuild = false
55+
}
56+
57+
if (!this._forceBuild && this._verifyNodeEnv) {
58+
if (['ci', 'testing', 'ts-development'].includes(process.env.NODE_ENV)) {
59+
cwdNodePath += this.adjustSlashes(this._defaultBuild)
60+
}
61+
}
62+
63+
return this.adjustSlashes(cwdNodePath)
64+
}
65+
66+
static pwd(subPath = '/') {
67+
return `${this.nodeCwdPath()}${this.adjustSlashes(subPath)}`
68+
}
69+
70+
static app(subPath = '/') {
71+
return this.pwd('app' + this.adjustSlashes(subPath))
72+
}
73+
74+
static logs(subPath = '/') {
75+
return this.storage('logs' + this.adjustSlashes(subPath))
76+
}
77+
78+
static start(subPath = '/') {
79+
return this.pwd('start' + this.adjustSlashes(subPath))
80+
}
81+
82+
static views(subPath = '/') {
83+
return this.resources('views' + this.adjustSlashes(subPath))
84+
}
85+
86+
static config(subPath = '/') {
87+
return this.pwd('config' + this.adjustSlashes(subPath))
88+
}
89+
90+
static tests(subPath = '/') {
91+
return this.pwd('tests' + this.adjustSlashes(subPath))
92+
}
93+
94+
static public(subPath = '/') {
95+
return this.pwd('public' + this.adjustSlashes(subPath))
96+
}
97+
98+
static assets(subPath = '/') {
99+
return this.public('assets' + this.adjustSlashes(subPath))
100+
}
101+
102+
static storage(subPath = '/') {
103+
return this.pwd('storage' + this.adjustSlashes(subPath))
104+
}
105+
106+
static database(subPath = '/') {
107+
return this.pwd('database' + this.adjustSlashes(subPath))
108+
}
109+
110+
static locales(subPath = '/') {
111+
return this.resources('locales' + this.adjustSlashes(subPath))
112+
}
113+
114+
static resources(subPath = '/') {
115+
return this.pwd('resources' + this.adjustSlashes(subPath))
116+
}
117+
118+
static providers(subPath = '/') {
119+
return this.pwd('providers' + this.adjustSlashes(subPath))
120+
}
121+
122+
private static adjustSlashes(path: string) {
123+
let subPathArray = path.split('/')
124+
125+
subPathArray = subPathArray.filter(p => p !== '')
126+
subPathArray.unshift('')
127+
128+
return subPathArray.join('/')
129+
}
130+
}

src/utils/global.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Path as PathInstance } from '../Classes/Path'
2+
3+
export {}
4+
5+
declare global {
6+
class Path {
7+
static nodeCwdPath(): string
8+
static forceBuild(): typeof Path
9+
static pwd(subPath?: string): string
10+
static app(subPath?: string): string
11+
static switchEnvVerify(): typeof Path
12+
static logs(subPath?: string): string
13+
static start(subPath?: string): string
14+
static views(subPath?: string): string
15+
static tests(subPath?: string): string
16+
static config(subPath?: string): string
17+
static public(subPath?: string): string
18+
static assets(subPath?: string): string
19+
static storage(subPath?: string): string
20+
static locales(subPath?: string): string
21+
static database(subPath?: string): string
22+
static resources(subPath?: string): string
23+
static providers(subPath?: string): string
24+
static forBuild(name: string): typeof Path
25+
static changeBuild(name: string): typeof Path
26+
}
27+
}
28+
29+
const _global = global as any
30+
_global.Path = PathInstance

tests/Classes/path.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Path } from '../../src/Classes/Path'
2+
3+
describe('\n Path Class', () => {
4+
it('should get application pwd path', () => {
5+
const myMainPath = process.cwd()
6+
const mySrcPath = myMainPath + '/src'
7+
const mySrcAppPath = myMainPath + '/src/app'
8+
9+
expect(Path.pwd()).toBe(myMainPath)
10+
expect(Path.pwd('/src')).toBe(mySrcPath)
11+
expect(Path.pwd('/src/')).toBe(mySrcPath)
12+
expect(Path.pwd('///src///')).toBe(mySrcPath)
13+
expect(Path.pwd('///src///app///')).toBe(mySrcAppPath)
14+
})
15+
16+
it('should get application storage path', () => {
17+
const myStoragePath = process.cwd() + '/storage'
18+
const myStorageLogsPath = myStoragePath + '/logs'
19+
20+
expect(Path.storage()).toBe(myStoragePath)
21+
expect(Path.storage('logs')).toBe(myStorageLogsPath)
22+
expect(Path.storage('///logs///')).toBe(myStorageLogsPath)
23+
})
24+
25+
it('should get application views path', () => {
26+
const myViewsPath = process.cwd() + '/resources/views'
27+
const myViewsMailPath = myViewsPath + '/Mail'
28+
29+
expect(Path.views()).toBe(myViewsPath)
30+
expect(Path.views('Mail')).toBe(myViewsMailPath)
31+
expect(Path.views('///Mail///')).toBe(myViewsMailPath)
32+
})
33+
34+
it('should get node cwd path based on ts build folder validations', () => {
35+
const myMainPath = process.cwd()
36+
const myMainDistPath = process.cwd() + '/dist'
37+
38+
expect(Path.forceBuild().nodeCwdPath()).toBe(myMainDistPath)
39+
expect(Path.forBuild('build').nodeCwdPath()).toBe(myMainPath + '/build')
40+
expect(Path.forceBuild().nodeCwdPath()).toBe(myMainDistPath)
41+
expect(Path.forceBuild().changeBuild('/test').nodeCwdPath()).toBe(
42+
myMainPath + '/test',
43+
)
44+
45+
Path.changeBuild('/dist')
46+
47+
process.env.NODE_ENV = 'testing'
48+
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
49+
50+
process.env.NODE_ENV = 'ts-development'
51+
expect(Path.nodeCwdPath()).toBe(myMainDistPath)
52+
53+
process.env.NODE_ENV = 'production'
54+
expect(Path.nodeCwdPath()).toBe(myMainPath)
55+
})
56+
})

0 commit comments

Comments
 (0)