1
1
const path = require ( "path" ) ;
2
- const normalize = require ( "normalize-path" ) ;
3
2
const fs = require ( "fs" ) ;
4
3
5
4
function TemplatePath ( ) { }
@@ -15,7 +14,7 @@ TemplatePath.getWorkingDir = function () {
15
14
* Returns the directory portion of a path.
16
15
* Works for directory and file paths and paths ending in a glob pattern.
17
16
*
18
- * @param {String } path A path
17
+ * @param {String } path - A path
19
18
* @returns {String } the directory portion of a path.
20
19
*/
21
20
TemplatePath . getDir = function ( path ) {
@@ -34,8 +33,8 @@ TemplatePath.getDir = function (path) {
34
33
*
35
34
* [1]: https://nodejs.org/api/path.html#path_path_parse_path
36
35
*
37
- * @param {String } path A path
38
36
* @returns {String } the directory portion of a path.
37
+ * @param {String } filePath - A path
39
38
*/
40
39
TemplatePath . getDirFromFilePath = function ( filePath ) {
41
40
return path . parse ( filePath ) . dir || "." ;
@@ -48,7 +47,7 @@ TemplatePath.getDirFromFilePath = function (filePath) {
48
47
*
49
48
* [1]: https://nodejs.org/api/path.html#path_path_parse_path
50
49
*
51
- * @param {String } path A path
50
+ * @param {String } path - A path
52
51
* @returns {String } the last path segment in a path
53
52
*/
54
53
TemplatePath . getLastPathSegment = function ( path ) {
@@ -59,11 +58,11 @@ TemplatePath.getLastPathSegment = function (path) {
59
58
// Trim a trailing slash if there is one
60
59
path = path . replace ( / \/ $ / , "" ) ;
61
60
62
- return path . substr ( path . lastIndexOf ( "/" ) + 1 ) ;
61
+ return path . slice ( path . lastIndexOf ( "/" ) + 1 ) ;
63
62
} ;
64
63
65
64
/**
66
- * @param {String } path A path
65
+ * @param {String } path - A path
67
66
* @returns {String[] } an array of paths pointing to each path segment of the
68
67
* provided `path`.
69
68
*/
@@ -89,34 +88,37 @@ TemplatePath.getAllDirs = function (path) {
89
88
*
90
89
* [1]: https://nodejs.org/api/path.html#path_path_normalize_path
91
90
*
92
- * @param {String } thePath The path that should be normalized.
91
+ * @param {String } thePath - The path that should be normalized.
93
92
* @returns {String } the normalized path.
94
93
*/
95
94
TemplatePath . normalize = function ( thePath ) {
96
- return normalize ( path . normalize ( thePath ) ) ;
95
+ let filePath = path . normalize ( thePath ) . split ( path . sep ) . join ( "/" ) ;
96
+ if ( filePath !== "/" && filePath . endsWith ( "/" ) ) {
97
+ return filePath . slice ( 0 , - 1 ) ;
98
+ }
99
+ return filePath ;
97
100
} ;
98
101
99
102
/**
100
103
* Joins all given path segments together.
101
104
*
102
- * It uses Node.js’ [`path.join`][1] method and the [normalize-path][2] package .
105
+ * It uses Node.js’ [`path.join`][1] method.
103
106
*
104
107
* [1]: https://nodejs.org/api/path.html#path_path_join_paths
105
- * [2]: https://www.npmjs.com/package/normalize-path
106
108
*
107
- * @param {String[] } paths An arbitrary amount of path segments.
109
+ * @param {... String } paths - An arbitrary amount of path segments.
108
110
* @returns {String } the normalized and joined path.
109
111
*/
110
112
TemplatePath . join = function ( ...paths ) {
111
- return normalize ( path . join ( ...paths ) ) ;
113
+ return TemplatePath . normalize ( path . join ( ...paths ) ) ;
112
114
} ;
113
115
114
116
/**
115
117
* Joins the given URL path segments and normalizes the resulting path.
116
118
* Maintains a single trailing slash if the last URL path argument
117
119
* had at least one.
118
120
*
119
- * @param {String[] } urlPaths
121
+ * @param {... String } urlPaths
120
122
* @returns {String } a normalized URL path described by the given URL path segments.
121
123
*/
122
124
TemplatePath . normalizeUrlPath = function ( ...urlPaths ) {
@@ -128,7 +130,7 @@ TemplatePath.normalizeUrlPath = function (...urlPaths) {
128
130
* Joins the given path segments. Since the first path is absolute,
129
131
* the resulting path will be absolute as well.
130
132
*
131
- * @param {String[] } paths
133
+ * @param {... String } paths
132
134
* @returns {String } the absolute path described by the given path segments.
133
135
*/
134
136
TemplatePath . absolutePath = function ( ...paths ) {
@@ -180,7 +182,7 @@ TemplatePath.addLeadingDotSlashArray = function (paths) {
180
182
/**
181
183
* Adds a leading dot-slash segment to `path`.
182
184
*
183
- * @param {String } path
185
+ * @param {String } pathArg
184
186
* @returns {String }
185
187
*/
186
188
TemplatePath . addLeadingDotSlash = function ( pathArg ) {
@@ -212,8 +214,8 @@ TemplatePath.stripLeadingDotSlash = function (path) {
212
214
/**
213
215
* Determines whether a path starts with a given sub path.
214
216
*
215
- * @param {String } path A path
216
- * @param {String } subPath A path
217
+ * @param {String } path - A path
218
+ * @param {String } subPath - A path
217
219
* @returns {Boolean } whether `path` starts with `subPath`.
218
220
*/
219
221
TemplatePath . startsWithSubPath = function ( path , subPath ) {
@@ -227,31 +229,31 @@ TemplatePath.startsWithSubPath = function (path, subPath) {
227
229
* Removes the `subPath` at the start of `path` if present
228
230
* and returns the remainding path.
229
231
*
230
- * @param {String } path A path
231
- * @param {String } subPath A path
232
+ * @param {String } path - A path
233
+ * @param {String } subPath - A path
232
234
* @returns {String } the `path` without `subPath` at the start of it.
233
235
*/
234
236
TemplatePath . stripLeadingSubPath = function ( path , subPath ) {
235
237
path = TemplatePath . normalize ( path ) ;
236
238
subPath = TemplatePath . normalize ( subPath ) ;
237
239
238
240
if ( subPath !== "." && path . startsWith ( subPath ) ) {
239
- return path . substr ( subPath . length + 1 ) ;
241
+ return path . slice ( subPath . length + 1 ) ;
240
242
}
241
243
242
244
return path ;
243
245
} ;
244
246
245
247
/**
246
- * @param {String } path A path
248
+ * @param {String } path - A path
247
249
* @returns {Boolean } whether `path` points to an existing directory.
248
250
*/
249
251
TemplatePath . isDirectorySync = function ( path ) {
250
252
return fs . existsSync ( path ) && fs . statSync ( path ) . isDirectory ( ) ;
251
253
} ;
252
254
253
255
/**
254
- * @param {String } path A path
256
+ * @param {String } path - A path
255
257
* @returns {Boolean } whether `path` points to an existing directory.
256
258
*/
257
259
TemplatePath . isDirectory = async function ( path ) {
@@ -325,7 +327,7 @@ TemplatePath.getExtension = function (thePath) {
325
327
* Removes the extension from a path.
326
328
*
327
329
* @param {String } path
328
- * @param {String } extension
330
+ * @param {String } [ extension]
329
331
* @returns {String }
330
332
*/
331
333
TemplatePath . removeExtension = function ( path , extension = undefined ) {
@@ -347,6 +349,7 @@ TemplatePath.removeExtension = function (path, extension = undefined) {
347
349
* e.g. `./my/dir/` stays `./my/dir/` on *nix and becomes `.\\my\\dir\\` on Windows
348
350
*
349
351
* @param {String } filePath
352
+ * @param {String } [sep="/"]
350
353
* @returns {String } a file path with the correct local directory separator.
351
354
*/
352
355
TemplatePath . normalizeOperatingSystemFilePath = function ( filePath , sep = "/" ) {
@@ -359,6 +362,7 @@ TemplatePath.normalizeOperatingSystemFilePath = function (filePath, sep = "/") {
359
362
* e.g. `./my/dir/` stays `./my/dir/` on *nix and becomes `.\\my\\dir\\` on Windows
360
363
*
361
364
* @param {String } filePath
365
+ * @param {String } [sep="/"]
362
366
* @returns {String } a file path with the correct local directory separator.
363
367
*/
364
368
TemplatePath . standardizeFilePath = function ( filePath , sep = "/" ) {
0 commit comments