Skip to content

Commit 10d06f9

Browse files
committed
feat: could load template from npm
resolve #37
1 parent 033abb4 commit 10d06f9

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"sanitization": "^0.3.0",
5656
"semver": "^6.0.0",
5757
"sha1": "^1.1.1",
58-
"simple-git": "^1.110.0"
58+
"simple-git": "^1.110.0",
59+
"validate-npm-package-name": "^3.0.0"
5960
}
6061
}

src/download-template/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ const throwError = require('../utils/throw-error')
44

55
const copy = require('./copy')
66
const clone = require('./clone')
7+
const npmInstall = require('./npm-install')
78

89

910
module.exports = async (repository, version) => {
1011
const localStoragePath = getLocalStoragePath(repository, version)
1112

1213
if (repository.type === 'local') await copy(repository, version, localStoragePath)
1314
else if (repository.type === 'git') await clone(repository, version, localStoragePath)
15+
else if (repository.type == 'npm') await npmInstall(repository, version, localStoragePath)
1416
else {
1517
throwError([
1618
'Cannot parse template git repository or local path.',
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fs = require('fs-extra')
2+
const { join } = require('path')
3+
const childProcess = require('child_process')
4+
const { promisify } = require('util')
5+
const log = require('../utils/log')
6+
7+
8+
9+
const genIndexFile = name => `
10+
const { join } = require('path')
11+
const config = require('${name}')
12+
config.path = join('./node_modules/${name}', config.path)
13+
14+
module.exports = config
15+
`
16+
17+
const exec = promisify(childProcess.exec)
18+
module.exports = async (repository, version, storage) => {
19+
await fs.emptyDir(storage)
20+
log.info(`install ${repository.name} template from npm...`)
21+
22+
await fs.writeJSON(join(storage, 'package.json'), {
23+
main: 'index.js',
24+
description: '',
25+
license: 'MIT',
26+
})
27+
await fs.writeFile(join(storage, 'index.js'), genIndexFile(repository.name))
28+
await fs.writeFile(join(storage, '.npmrc'), 'package-lock=false')
29+
30+
const command = `npm install ${repository.name}@${version.number}`
31+
await exec(command, { cwd: storage })
32+
}

src/get-template-versions.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ const git = require('simple-git/promise')
22
const semver = require('semver')
33
const log = require('./utils/log')
44
const isRepo = require('./utils/is-repository')
5+
const childProcess = require('child_process')
6+
const { promisify } = require('util')
57

68

9+
const exec = promisify(childProcess.exec)
710
const getRepositoryVersions = async (repository) => {
811
const result = await git().listRemote(['--tags', repository.url])
912
const arr = result.split('\n')
@@ -29,12 +32,20 @@ const getRepositoryVersions = async (repository) => {
2932
return versions
3033
}
3134

35+
const getNpmVersion = async repository => {
36+
const { stdout, stderr } = await exec(`npm view ${repository.name} versions --json`)
37+
if (stderr) console.error(stderr)
38+
return JSON.parse(stdout).reverse().map(number => ({ number }))
39+
}
40+
3241
module.exports = async (repository) => {
3342
log.info('check template versions')
3443
if (repository.type === 'git') {
3544
return await getRepositoryVersions(repository)
3645
} else if (repository.type === 'local' && await isRepo(repository.url)) {
3746
return await getRepositoryVersions(repository)
47+
} else if (repository.type === 'npm') {
48+
return await getNpmVersion(repository)
3849
} else {
3950
return []
4051
}

src/utils/format-repository.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const throwError = require('./throw-error')
22
const fs = require('fs-extra')
33
const { join, isAbsolute } = require('path')
4+
const validateNpmPackageName = require("validate-npm-package-name")
45

56
const githubSH = /^(github:)?[-a-zA-Z0-9@:%._\+~#=]+\/[-a-zA-Z0-9@:%._\+~#=]+$/
67
const gitUrlRegexp = /((git|ssh|http(s)?)|(git@[\w.]+))(:(\/\/)?)([\w.@:/\-~]+)(\.git)(\/)?$/
@@ -52,6 +53,9 @@ module.exports = (link) => {
5253
return formatRepository(`https://github.com/${link.replace(/^github:/, '')}.git`)
5354
} else if (gitUrlRegexp.test(link)) {
5455
return formatRepository(link)
56+
} else if (/^npm:/.test(link) && validateNpmPackageName(link.substring('npm:'.length))) {
57+
// npm:xxxx/xxx
58+
return { type: 'npm', url: link, owner: '', name: link.substring('npm:'.length), path: link }
5559
}
5660

5761
throwError(`Invalid repository url: ${link}`)

0 commit comments

Comments
 (0)