Skip to content

Commit b2a6cc4

Browse files
committed
fix(#4): use path module to add windows support
1 parent 08e529b commit b2a6cc4

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/utils/filename.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
*/
55
'use strict';
66

7+
const path = require('path');
8+
79
/**
810
* @type {string} filename without path
9-
* @param {string} path filename concat with path
11+
* @param {string} p filename concat with path
1012
*/
11-
const getFilename = (path) => path.substring(path.lastIndexOf('/') + 1);
13+
const getFilename = (p) => path.basename(p);
1214

1315
/**
1416
* @type {string} path of folder
15-
* @param {string} path filename concat with path
17+
* @param {string} p filename concat with path
1618
*/
17-
const getFolderPath = (path) => path.substring(0, path.lastIndexOf('/') + 1);
19+
const getFolderPath = (p) => path.join(path.dirname(p), path.sep);
1820

1921
/**
2022
* @type {string} base name
@@ -29,24 +31,26 @@ const getBasename = (filename, ignoreMiddleExtensions = false) =>
2931

3032
/**
3133
* @type {string[]} all folders
32-
* @param {string} path path of folder
34+
* @param {string} p path of folder
3335
*/
34-
const getAllFolders = (path) =>
35-
path.split('/').filter((folder) => folder !== '');
36+
const getAllFolders = (p) =>
37+
p.split(path.sep).filter((folder) => folder !== '');
3638

3739
/**
3840
* @type {string[]} all sub paths
39-
* @param {string} path path of folder
41+
* @param {string} p path of folder
4042
*/
41-
const getSubPaths = (path) => {
42-
const folders = getAllFolders(path);
43+
const getSubPaths = (p) => {
44+
const folders = getAllFolders(p);
4345
let subPaths = [];
4446

4547
const handler = (array) =>
4648
array.reduce((acc, folder, index) => {
4749
if (folder) {
4850
acc.push(
49-
index === 0 ? `${folder}/` : `${acc[acc.length - 1]}${folder}/`
51+
index === 0
52+
? path.join(folder, path.sep)
53+
: path.join(acc[acc.length - 1], folder, path.sep)
5054
);
5155
}
5256
return acc;
@@ -65,7 +69,7 @@ const getSubPaths = (path) => {
6569
* @param {string} repositoryRoot path of repository root
6670
*/
6771
const getPathFromRepositoryRoot = (fullPath, repositoryRoot) =>
68-
fullPath.replace(`${repositoryRoot}/`, '');
72+
fullPath.replace(path.join(repositoryRoot, path.sep), '');
6973

7074
module.exports = {
7175
getFolderPath,

0 commit comments

Comments
 (0)