Skip to content

Commit f46774b

Browse files
authored
Extract local path logic and add tests. (vuejs#341)
* Extract local path logic and add tests. * Add appveyor to run test on Windows. * Fix weird race issues. - Always pass call callback function on generate. - Previously tests were randomly failing when running few times in a row. * Windows will scandir for template. * Add missing dev dependencies. * Node v4 will get npm v2 which fails on windows. - update readme
1 parent a9301f6 commit f46774b

File tree

8 files changed

+72
-20
lines changed

8 files changed

+72
-20
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A simple CLI for scaffolding Vue.js projects.
44

55
### Installation
66

7-
Prerequisites: [Node.js](https://nodejs.org/en/) (>=4.x, 6.x preferred) and [Git](https://git-scm.com/).
7+
Prerequisites: [Node.js](https://nodejs.org/en/) (>=4.x, 6.x preferred), npm version 3+ and [Git](https://git-scm.com/).
88

99
``` bash
1010
$ npm install -g vue-cli
@@ -211,7 +211,7 @@ Arguments:
211211
}
212212
}
213213
```
214-
214+
215215
- `helpers`: some helpers you can use to log results.
216216
- `chalk`: the `chalk` module
217217
- `logger`: [the built-in vue-cli logger](/lib/logger.js)

appveyor.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
environment:
2+
matrix:
3+
- nodejs_version: "5"
4+
- nodejs_version: "6"
5+
6+
install:
7+
- ps: Install-Product node $env:nodejs_version
8+
- npm install
9+
10+
test_script:
11+
- node --version
12+
- npm --version
13+
- npm test
14+
15+
cache:
16+
- node_modules -> yarn.lock
17+
18+
build: off

bin/vue-init

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var logger = require('../lib/logger')
1313
var generate = require('../lib/generate')
1414
var checkVersion = require('../lib/check-version')
1515
var warnings = require('../lib/warnings')
16+
var { isLocalPath, getTemplatePath } = require('../lib/local-path')
1617

1718
/**
1819
* Usage.
@@ -97,10 +98,8 @@ if (exists(to)) {
9798

9899
function run () {
99100
// check if template is local
100-
if (/^[./]|(\w:)/.test(template)) {
101-
var templatePath = template.charAt(0) === '/' || /^\w:/.test(template)
102-
? template
103-
: path.normalize(path.join(process.cwd(), template))
101+
if (isLocalPath(template)) {
102+
var templatePath = getTemplatePath(template)
104103
if (exists(templatePath)) {
105104
generate(name, templatePath, to, function (err) {
106105
if (err) logger.fatal(err)

lib/local-path.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var path = require('path')
2+
3+
module.exports = {
4+
isLocalPath: function (templatePath) {
5+
return /^[./]|(^[a-zA-Z]:)/.test(templatePath)
6+
},
7+
8+
getTemplatePath: function (templatePath) {
9+
return path.isAbsolute(templatePath)
10+
? templatePath
11+
: path.normalize(path.join(process.cwd(), templatePath))
12+
}
13+
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
"webpack-merge": "^2.3.1"
7878
},
7979
"devDependencies": {
80+
"babel-preset-es2015": "^6.22.0",
81+
"babel-preset-stage-2": "^6.22.0",
8082
"chai": "^3.5.0",
8183
"cross-env": "^1.0.7",
8284
"eslint": "^2.7.0",

test/e2e/mock-meta-json/template/.gitkeep

Whitespace-only changes.

test/e2e/test.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ const async = require('async')
1010
const extend = Object.assign || require('util')._extend
1111
const generate = require('../../lib/generate')
1212
const metadata = require('../../lib/options')
13+
const { isLocalPath, getTemplatePath } = require('../../lib/local-path')
1314

14-
const MOCK_META_JSON_PATH = './test/e2e/mock-meta-json'
15-
const MOCK_TEMPLATE_REPO_PATH = './test/e2e/mock-template-repo'
15+
const MOCK_META_JSON_PATH = path.resolve('./test/e2e/mock-meta-json')
16+
const MOCK_TEMPLATE_REPO_PATH = path.resolve('./test/e2e/mock-template-repo')
1617
const MOCK_TEMPLATE_BUILD_PATH = path.resolve('./test/e2e/mock-template-build')
17-
const MOCK_METADATA_REPO_JS_PATH = './test/e2e/mock-metadata-repo-js'
18-
const MOCK_SKIP_GLOB = './test/e2e/mock-skip-glob'
18+
const MOCK_METADATA_REPO_JS_PATH = path.resolve('./test/e2e/mock-metadata-repo-js')
19+
const MOCK_SKIP_GLOB = path.resolve('./test/e2e/mock-skip-glob')
1920

2021
function monkeyPatchInquirer (answers) {
2122
// monkey patch inquirer
@@ -67,16 +68,16 @@ describe('vue-cli', () => {
6768
})
6869
})
6970

70-
it('adds additional data to meta data', () => {
71-
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH)
71+
it('adds additional data to meta data', done => {
72+
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH, done)
7273
expect(data.destDirName).to.equal('test')
7374
expect(data.inPlace).to.equal(false)
7475
})
7576

76-
it('sets `inPlace` to true when generating in same directory', () => {
77+
it('sets `inPlace` to true when generating in same directory', done => {
7778
const currentDir = process.cwd()
7879
process.chdir(MOCK_TEMPLATE_BUILD_PATH)
79-
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH)
80+
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH, done)
8081
expect(data.destDirName).to.equal('test')
8182
expect(data.inPlace).to.equal(true)
8283
process.chdir(currentDir)
@@ -199,4 +200,27 @@ describe('vue-cli', () => {
199200
done()
200201
})
201202
})
203+
204+
it('checks for local path', () => {
205+
expect(isLocalPath('../')).to.equal(true)
206+
expect(isLocalPath('../../')).to.equal(true)
207+
expect(isLocalPath('../template')).to.equal(true)
208+
expect(isLocalPath('../template/abc')).to.equal(true)
209+
expect(isLocalPath('./')).to.equal(true)
210+
expect(isLocalPath('.')).to.equal(true)
211+
expect(isLocalPath('c:/')).to.equal(true)
212+
expect(isLocalPath('D:/')).to.equal(true)
213+
214+
expect(isLocalPath('webpack')).to.equal(false)
215+
expect(isLocalPath('username/rep')).to.equal(false)
216+
expect(isLocalPath('bitbucket:username/rep')).to.equal(false)
217+
})
218+
219+
it('normalizes template path', () => {
220+
expect(getTemplatePath('/')).to.equal('/')
221+
expect(getTemplatePath('/absolute/path')).to.equal('/absolute/path')
222+
223+
expect(getTemplatePath('..')).to.equal(path.join(__dirname, '/../../..'))
224+
expect(getTemplatePath('../template')).to.equal(path.join(__dirname, '/../../../template'))
225+
})
202226
})

yarn.lock

+2-6
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ babel-preset-latest@^6.16.0:
747747
babel-preset-es2016 "^6.22.0"
748748
babel-preset-es2017 "^6.22.0"
749749

750-
babel-preset-stage-2@^6.18.0:
750+
babel-preset-stage-2@^6.18.0, babel-preset-stage-2@^6.22.0:
751751
version "6.22.0"
752752
resolved "https://registry.npmjs.org/babel-preset-stage-2/-/babel-preset-stage-2-6.22.0.tgz#ccd565f19c245cade394b21216df704a73b27c07"
753753
dependencies:
@@ -3516,18 +3516,14 @@ minimatch@3.0.2:
35163516
dependencies:
35173517
brace-expansion "^1.0.0"
35183518

3519-
minimist@0.0.8:
3519+
minimist@0.0.8, minimist@~0.0.1:
35203520
version "0.0.8"
35213521
resolved "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
35223522

35233523
minimist@^1.1.0, minimist@^1.2.0:
35243524
version "1.2.0"
35253525
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
35263526

3527-
minimist@~0.0.1:
3528-
version "0.0.10"
3529-
resolved "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"
3530-
35313527
mkdirp@0.3.0:
35323528
version "0.3.0"
35333529
resolved "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz#1bbf5ab1ba827af23575143490426455f481fe1e"

0 commit comments

Comments
 (0)