Skip to content

Commit f62fd86

Browse files
committed
feat(*): relative path for template
1 parent bf52e6d commit f62fd86

File tree

7 files changed

+92
-51
lines changed

7 files changed

+92
-51
lines changed

src/commands/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = async (options = {}) => {
2525
} = options
2626

2727
// template repository
28-
const repository = formatTemplateLink(options.repository)
28+
const repository = formatTemplateLink(options.repository, cwd)
2929
if (!force) await securityCheck(process.cwd())
3030

3131
const templateStoragePath = await getTemplateStorage(repository)

src/extractProgectBaseInfo.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
const { join } = require('path')
22
const git = require('simple-git/promise')
3-
const { readJson } = require('./utils')
3+
const { readJson, isRepo } = require('./utils')
44

55

66
const gitUrlRegexp = /((git|ssh|http(s)?)|(git@[\w.]+))(:(\/\/)?)([\w.@:/\-~]+)(\.git)(\/)?$/
77

8-
const isRepo = async path => {
9-
if (!await git(path).checkIsRepo()) return false
10-
const toplevel = await git(path).revparse(['--show-toplevel'])
11-
if (toplevel.replace(/\n$/, '') !== path) return false
12-
13-
return true
14-
}
15-
168
module.exports = async path => {
179
const view = {}
1810

src/formatTemplateLink.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1-
// github:xx/xx
21
const throwError = require('./throwError')
2+
const fs = require('fs-extra')
3+
const { join, isAbsolute } = require('path')
4+
35
const githubSH = /^(github:)?[-a-zA-Z0-9@:%._\+~#=]+\/[-a-zA-Z0-9@:%._\+~#=]+$/
46
const cloneLink = /((git|ssh|http(s)?)|(git@[\w.]+))(:(\/\/)?)([\w.@:/\-~]+)(\.git)(\/)?/
7+
const isRelative = path => /^\.\.?\//.test(path)
8+
const isLocalPath = path => join(path) === path
9+
10+
11+
const dirExist = async link => {
12+
const exist = await fs.pathExists(link)
13+
if (!exist) return false
14+
15+
const stats = await fs.stat(link)
16+
if (stats.isDirectory()) return true
17+
18+
return false
19+
}
20+
21+
module.exports = (link, cwd) => {
22+
if (isRelative(link) || isAbsolute(link)) {
23+
if (isAbsolute(link) && dirExist(link)) return link
24+
25+
link = join(cwd, link)
26+
if (dirExist(link)) return link
527

6-
module.exports = link => {
7-
if (githubSH.test(link)) {
28+
throwError('Template path cannot be found. Ensure it is an exist directory.')
29+
} else if (githubSH.test(link)) {
830
return `https://github.com/${link.replace(/^github:/, '')}.git`
931
} else if (cloneLink.test(link)) {
1032
return link

src/getTemplate/clone.js

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
const fs = require('fs-extra')
22
const git = require('simple-git/promise')
3-
const throwError = require('../throwError')
4-
const semver = require('semver')
53
const log = require('../log')
64

75

8-
const createRevert = (storage, branch) => async () => {
9-
git(storage).checkout(branch)
10-
}
11-
126
const repositoryExisted = async (repository, storage) => {
137
if (!await fs.pathExists(storage)) return false
148

@@ -18,7 +12,7 @@ const repositoryExisted = async (repository, storage) => {
1812
return true
1913
}
2014

21-
module.exports = async (repository, version, storage) => {
15+
module.exports = async (repository, storage) => {
2216
if (await repositoryExisted(repository, storage)) {
2317
log.info(`pull template from ${repository}...`)
2418
await git(storage).pull()
@@ -27,33 +21,4 @@ module.exports = async (repository, version, storage) => {
2721
await fs.remove(storage)
2822
await git().clone(repository, storage)
2923
}
30-
31-
const gitT = git(storage)
32-
let tags = await gitT.tags()
33-
tags = tags.all
34-
.filter(semver.valid)
35-
.sort(semver.compare)
36-
.reverse()
37-
38-
const branchSummary = await gitT.branch()
39-
const currentBranch = branchSummary.current
40-
41-
if (version) {
42-
version = `v${version}`
43-
if (!tags.includes(version)) {
44-
throwError([
45-
'No corresponding template version was found',
46-
'Please confirm that the version number exists in the tags of the template repository.',
47-
`Expected template version: ${version}`
48-
].join('\n'))
49-
}
50-
51-
await gitT.checkout(version)
52-
log.info(`template version: ${version}`)
53-
} else if (tags.length) {
54-
await gitT.checkout(tags[0])
55-
log.info(`template version: ${tags[0]}`)
56-
}
57-
58-
return createRevert(storage, currentBranch)
5924
}

src/getTemplate/copy.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require('fs-extra')
2+
const log = require('../log')
3+
4+
5+
module.exports = async (path, storage) => {
6+
await fs.emptyDir(storage)
7+
log.info(`copy template from ${path}`)
8+
await fs.copy(path, storage)
9+
}

src/getTemplate/index.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
1+
const { isAbsolute } = require('path')
2+
const semver = require('semver')
3+
const git = require('simple-git/promise')
4+
5+
const throwError = require('../throwError')
6+
const log = require('../log')
7+
const { isRepo } = require('../utils')
8+
9+
const copy = require('./copy')
110
const clone = require('./clone')
211

312

13+
const createRevert = (storage, branch) => async () => {
14+
git(storage).checkout(branch)
15+
}
16+
417
module.exports = async (path, storage, version) => {
5-
if (/^[.|/]/.test(path)) return () => {}
6-
else return clone(path, version, storage)
18+
if (isAbsolute(path)) await copy(path, storage)
19+
else await clone(path, storage)
20+
21+
if (await isRepo(storage)) {
22+
const gitT = git(storage)
23+
let tags = await gitT.tags()
24+
tags = tags.all
25+
.filter(semver.valid)
26+
.sort(semver.compare)
27+
.reverse()
28+
29+
const branchSummary = await gitT.branch()
30+
const currentBranch = branchSummary.current
31+
32+
if (version) {
33+
version = `v${version}`
34+
if (!tags.includes(version)) {
35+
throwError([
36+
'No corresponding template version was found',
37+
'Please confirm that the version number exists in the tags of the template repository.',
38+
`Expected template version: ${version}`
39+
].join('\n'))
40+
}
41+
42+
await gitT.checkout(version)
43+
log.info(`template version: ${version}`)
44+
} else if (tags.length) {
45+
await gitT.checkout(tags[0])
46+
log.info(`template version: ${tags[0]}`)
47+
}
48+
49+
return createRevert(storage, currentBranch)
50+
}
751
}

src/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs')
22
const { promisify } = require('util')
3+
const git = require('simple-git/promise')
34

45

56
const readFile = promisify(fs.readFile)
@@ -15,3 +16,11 @@ exports.flatten = arr => arr.reduce((result, item) => {
1516
}
1617
else return [...result, item]
1718
}, [])
19+
20+
exports.isRepo = async path => {
21+
if (!await git(path).checkIsRepo()) return false
22+
const toplevel = await git(path).revparse(['--show-toplevel'])
23+
if (toplevel.replace(/\n$/, '') !== path) return false
24+
25+
return true
26+
}

0 commit comments

Comments
 (0)