Skip to content

Commit 806f14b

Browse files
committed
fix: ./ is missing in relative path
resolve #65
1 parent 3be721c commit 806f14b

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

src/load-config/load-milirc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ const fs = require('fs-extra')
22
const cosmiconfig = require('cosmiconfig')
33
const semver = require('semver')
44
const throwError = require('../utils/throw-error')
5-
const { join, relative } = require('path')
5+
const { join } = require('path')
66
const isRelativePath = require('../utils/is-relative-path')
7+
const relativePath = require('../utils/relative-path')
78

89

910
const explorer = cosmiconfig('mili')
@@ -52,7 +53,7 @@ module.exports = async cwd => {
5253

5354
// The relative template path saved in .milirc is relative to the dir of .milirc
5455
if (isRelativePath(config.template.repository)) {
55-
config.template.repository = relative(process.cwd(), join(cwd, config.template.repository))
56+
config.template.repository = relativePath(process.cwd(), join(cwd, config.template.repository))
5657
}
5758

5859
return config

src/utils/format-repository.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const throwError = require('./throw-error')
22
const fs = require('fs-extra')
3-
const { join, isAbsolute, relative } = require('path')
3+
const { join, isAbsolute } = require('path')
44
const validateNpmPackageName = require('validate-npm-package-name')
55
const isRelativePath = require('./is-relative-path')
6+
const relativePath = require('./relative-path')
67

78

89
const githubSH = /^(github:)?[-a-zA-Z0-9@:%._+~#=]+\/[-a-zA-Z0-9@:%._+~#=]+$/
@@ -45,7 +46,7 @@ module.exports = link => {
4546
const url = join(cwd, link)
4647
if (dirExist(url)) {
4748
// NOTE: the path saved in .milirc should be relative to the output folder, rather than process.cwd()
48-
return { type: 'local', url, owner: null, name: null, path: savepath => relative(savepath, url) }
49+
return { type: 'local', url, owner: null, name: null, path: savepath => relativePath(savepath, url) }
4950
}
5051

5152
throwError('Template path cannot be found. Ensure it is an exist directory.')

src/utils/relative-path.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { relative } = require('path')
2+
3+
module.exports = (root, path) => `./${relative(root, path)}`

tests/.eslintrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parserOptions:
2+
sourceType: module

tests/utils/format-repository.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import test from 'ava'
21
import { join, relative } from 'path'
32
import formatRepository from '../../src/utils/format-repository'
3+
import test from 'ava'
44

55
test('format github repository', t => {
66
const result = {
@@ -10,15 +10,17 @@ test('format github repository', t => {
1010
name: 'mili-template',
1111
url: 'https://github.com/Val-istar-Guo/mili-template.git',
1212
path: 'https://github.com/Val-istar-Guo/mili-template.git',
13-
};
13+
}
1414

1515
t.deepEqual(formatRepository('https://github.com/Val-istar-Guo/mili-template.git'), result)
1616
// t.deepEqual(formatRepository('git@github.com:Val-istar-Guo/mili-template.git'), result)
1717
t.deepEqual(formatRepository('Val-istar-Guo/mili-template'), result)
1818
t.deepEqual(formatRepository('github:Val-istar-Guo/mili-template'), result)
19-
20-
// formatRepository('');
21-
// formatRepository('/User/template/mili-template');
19+
20+
/*
21+
* formatRepository('');
22+
* formatRepository('/User/template/mili-template');
23+
*/
2224
})
2325

2426
test('format npm package', t => {
@@ -43,5 +45,5 @@ test('format relative path', t => {
4345
url: absolutePath,
4446
})
4547

46-
t.is(result.path(__dirname), '../../src')
48+
t.is(result.path(__dirname), './../../src')
4749
})

tests/utils/relative-path.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import relativePath from '../../src/utils/relative-path'
2+
import test from 'ava'
3+
4+
5+
test('relative path should begin with ./', t => {
6+
t.is(relativePath('src', './src/a'), './a')
7+
})

0 commit comments

Comments
 (0)