Skip to content

Commit 0a47c4d

Browse files
committed
fix: make path work on windows machine
1 parent cc1049c commit 0a47c4d

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

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.7.9",
3+
"version": "1.8.0",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon <lenonSec7@gmail.com>",

src/Helpers/Path.ts

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import { normalize, sep } from 'path'
11+
1012
export class Path {
1113
private static _tempBuild = null
1214
private static _forceBuild = false
13-
private static _defaultBuild = 'dist'
15+
private static _defaultBuild = `${sep}dist`
1416
private static _verifyNodeEnv = true
1517

1618
static noBuild() {
17-
this._tempBuild = '/'
19+
this._tempBuild = sep
1820

1921
return this
2022
}
2123

2224
static forBuild(name: string) {
23-
this._tempBuild = name
25+
this._tempBuild = normalize(`${sep}${name}`)
2426

2527
return this
2628
}
@@ -38,7 +40,7 @@ export class Path {
3840
}
3941

4042
static changeBuild(name: string) {
41-
this._defaultBuild = name
43+
this._defaultBuild = normalize(`${sep}${name}`)
4244

4345
return this
4446
}
@@ -47,94 +49,95 @@ export class Path {
4749
let cwdNodePath = process.cwd()
4850

4951
if (this._tempBuild) {
50-
cwdNodePath += this.adjustSlashes(this._tempBuild)
52+
cwdNodePath += this._tempBuild
5153

5254
this._tempBuild = null
5355

54-
return cwdNodePath
56+
return this.removeSlashFromEnd(cwdNodePath)
5557
}
5658

5759
if (this._forceBuild) {
58-
cwdNodePath += this.adjustSlashes(this._defaultBuild)
60+
cwdNodePath += this._defaultBuild
5961

60-
return cwdNodePath
62+
return this.removeSlashFromEnd(cwdNodePath)
6163
}
6264

6365
if (
6466
!this._forceBuild &&
6567
this._verifyNodeEnv &&
6668
process.env.NODE_TS === 'false'
6769
) {
68-
cwdNodePath += this.adjustSlashes(this._defaultBuild)
70+
cwdNodePath += this._defaultBuild
6971

70-
return cwdNodePath
72+
return this.removeSlashFromEnd(cwdNodePath)
7173
}
7274

73-
return this.adjustSlashes(cwdNodePath)
75+
return this.removeSlashFromEnd(cwdNodePath)
7476
}
7577

76-
static pwd(subPath = '/') {
77-
return `${this.nodeCwdPath()}${this.adjustSlashes(subPath)}`
78-
}
78+
static pwd(subPath = sep) {
79+
const pwd = normalize(`${this.nodeCwdPath()}${sep}${normalize(subPath)}`)
7980

80-
static app(subPath = '/') {
81-
return this.pwd('app' + this.adjustSlashes(subPath))
81+
return this.removeSlashFromEnd(pwd)
8282
}
8383

84-
static logs(subPath = '/') {
85-
return this.storage('logs' + this.adjustSlashes(subPath))
84+
static app(subPath = sep) {
85+
return this.pwd('app' + sep + normalize(subPath))
8686
}
8787

88-
static start(subPath = '/') {
89-
return this.pwd('start' + this.adjustSlashes(subPath))
88+
static logs(subPath = sep) {
89+
return this.storage('logs' + sep + normalize(subPath))
9090
}
9191

92-
static views(subPath = '/') {
93-
return this.resources('views' + this.adjustSlashes(subPath))
92+
static start(subPath = sep) {
93+
return this.pwd('start' + sep + normalize(subPath))
9494
}
9595

96-
static config(subPath = '/') {
97-
return this.pwd('config' + this.adjustSlashes(subPath))
96+
static views(subPath = sep) {
97+
return this.resources('views' + sep + normalize(subPath))
9898
}
9999

100-
static tests(subPath = '/') {
101-
return this.pwd('tests' + this.adjustSlashes(subPath))
100+
static config(subPath = sep) {
101+
return this.pwd('config' + sep + normalize(subPath))
102102
}
103103

104-
static public(subPath = '/') {
105-
return this.pwd('public' + this.adjustSlashes(subPath))
104+
static tests(subPath = sep) {
105+
return this.pwd('tests' + sep + normalize(subPath))
106106
}
107107

108-
static assets(subPath = '/') {
109-
return this.public('assets' + this.adjustSlashes(subPath))
108+
static public(subPath = sep) {
109+
return this.pwd('public' + sep + normalize(subPath))
110110
}
111111

112-
static storage(subPath = '/') {
113-
return this.pwd('storage' + this.adjustSlashes(subPath))
112+
static assets(subPath = sep) {
113+
return this.public('assets' + sep + normalize(subPath))
114114
}
115115

116-
static database(subPath = '/') {
117-
return this.pwd('database' + this.adjustSlashes(subPath))
116+
static storage(subPath = sep) {
117+
return this.pwd('storage' + sep + normalize(subPath))
118118
}
119119

120-
static locales(subPath = '/') {
121-
return this.resources('locales' + this.adjustSlashes(subPath))
120+
static database(subPath = sep) {
121+
return this.pwd('database' + sep + normalize(subPath))
122122
}
123123

124-
static resources(subPath = '/') {
125-
return this.pwd('resources' + this.adjustSlashes(subPath))
124+
static locales(subPath = sep) {
125+
return this.resources('locales' + sep + normalize(subPath))
126126
}
127127

128-
static providers(subPath = '/') {
129-
return this.pwd('providers' + this.adjustSlashes(subPath))
128+
static resources(subPath = sep) {
129+
return this.pwd('resources' + sep + normalize(subPath))
130130
}
131131

132-
private static adjustSlashes(path: string) {
133-
let subPathArray = path.split('/')
132+
static providers(subPath = sep) {
133+
return this.pwd('providers' + sep + normalize(subPath))
134+
}
134135

135-
subPathArray = subPathArray.filter(p => p !== '')
136-
subPathArray.unshift('')
136+
private static removeSlashFromEnd(path: string) {
137+
if (path.endsWith(sep)) {
138+
return path.slice(0, -1)
139+
}
137140

138-
return subPathArray.join('/')
141+
return path
139142
}
140143
}

0 commit comments

Comments
 (0)