Skip to content

Commit

Permalink
Extract local path logic and add tests. (#341)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zigomir authored Feb 11, 2017
1 parent a9301f6 commit f46774b
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A simple CLI for scaffolding Vue.js projects.

### Installation

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

``` bash
$ npm install -g vue-cli
Expand Down Expand Up @@ -211,7 +211,7 @@ Arguments:
}
}
```

- `helpers`: some helpers you can use to log results.
- `chalk`: the `chalk` module
- `logger`: [the built-in vue-cli logger](/lib/logger.js)
Expand Down
18 changes: 18 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
environment:
matrix:
- nodejs_version: "5"
- nodejs_version: "6"

install:
- ps: Install-Product node $env:nodejs_version
- npm install

test_script:
- node --version
- npm --version
- npm test

cache:
- node_modules -> yarn.lock

build: off
7 changes: 3 additions & 4 deletions bin/vue-init
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var logger = require('../lib/logger')
var generate = require('../lib/generate')
var checkVersion = require('../lib/check-version')
var warnings = require('../lib/warnings')
var { isLocalPath, getTemplatePath } = require('../lib/local-path')

This comment has been minimized.

Copy link
@nkovacs

nkovacs May 6, 2017

This does not work on Node 4, since it doesn't support destructuring.


/**
* Usage.
Expand Down Expand Up @@ -97,10 +98,8 @@ if (exists(to)) {

function run () {
// check if template is local
if (/^[./]|(\w:)/.test(template)) {
var templatePath = template.charAt(0) === '/' || /^\w:/.test(template)
? template
: path.normalize(path.join(process.cwd(), template))
if (isLocalPath(template)) {
var templatePath = getTemplatePath(template)
if (exists(templatePath)) {
generate(name, templatePath, to, function (err) {
if (err) logger.fatal(err)
Expand Down
13 changes: 13 additions & 0 deletions lib/local-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var path = require('path')

module.exports = {
isLocalPath: function (templatePath) {
return /^[./]|(^[a-zA-Z]:)/.test(templatePath)
},

getTemplatePath: function (templatePath) {
return path.isAbsolute(templatePath)
? templatePath
: path.normalize(path.join(process.cwd(), templatePath))
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
"webpack-merge": "^2.3.1"
},
"devDependencies": {
"babel-preset-es2015": "^6.22.0",
"babel-preset-stage-2": "^6.22.0",
"chai": "^3.5.0",
"cross-env": "^1.0.7",
"eslint": "^2.7.0",
Expand Down
Empty file.
40 changes: 32 additions & 8 deletions test/e2e/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ const async = require('async')
const extend = Object.assign || require('util')._extend
const generate = require('../../lib/generate')
const metadata = require('../../lib/options')
const { isLocalPath, getTemplatePath } = require('../../lib/local-path')

const MOCK_META_JSON_PATH = './test/e2e/mock-meta-json'
const MOCK_TEMPLATE_REPO_PATH = './test/e2e/mock-template-repo'
const MOCK_META_JSON_PATH = path.resolve('./test/e2e/mock-meta-json')
const MOCK_TEMPLATE_REPO_PATH = path.resolve('./test/e2e/mock-template-repo')
const MOCK_TEMPLATE_BUILD_PATH = path.resolve('./test/e2e/mock-template-build')
const MOCK_METADATA_REPO_JS_PATH = './test/e2e/mock-metadata-repo-js'
const MOCK_SKIP_GLOB = './test/e2e/mock-skip-glob'
const MOCK_METADATA_REPO_JS_PATH = path.resolve('./test/e2e/mock-metadata-repo-js')
const MOCK_SKIP_GLOB = path.resolve('./test/e2e/mock-skip-glob')

function monkeyPatchInquirer (answers) {
// monkey patch inquirer
Expand Down Expand Up @@ -67,16 +68,16 @@ describe('vue-cli', () => {
})
})

it('adds additional data to meta data', () => {
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH)
it('adds additional data to meta data', done => {
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH, done)
expect(data.destDirName).to.equal('test')
expect(data.inPlace).to.equal(false)
})

it('sets `inPlace` to true when generating in same directory', () => {
it('sets `inPlace` to true when generating in same directory', done => {
const currentDir = process.cwd()
process.chdir(MOCK_TEMPLATE_BUILD_PATH)
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH)
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH, done)
expect(data.destDirName).to.equal('test')
expect(data.inPlace).to.equal(true)
process.chdir(currentDir)
Expand Down Expand Up @@ -199,4 +200,27 @@ describe('vue-cli', () => {
done()
})
})

it('checks for local path', () => {
expect(isLocalPath('../')).to.equal(true)
expect(isLocalPath('../../')).to.equal(true)
expect(isLocalPath('../template')).to.equal(true)
expect(isLocalPath('../template/abc')).to.equal(true)
expect(isLocalPath('./')).to.equal(true)
expect(isLocalPath('.')).to.equal(true)
expect(isLocalPath('c:/')).to.equal(true)
expect(isLocalPath('D:/')).to.equal(true)

expect(isLocalPath('webpack')).to.equal(false)
expect(isLocalPath('username/rep')).to.equal(false)
expect(isLocalPath('bitbucket:username/rep')).to.equal(false)
})

it('normalizes template path', () => {
expect(getTemplatePath('/')).to.equal('/')
expect(getTemplatePath('/absolute/path')).to.equal('/absolute/path')

expect(getTemplatePath('..')).to.equal(path.join(__dirname, '/../../..'))
expect(getTemplatePath('../template')).to.equal(path.join(__dirname, '/../../../template'))
})
})
8 changes: 2 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ babel-preset-latest@^6.16.0:
babel-preset-es2016 "^6.22.0"
babel-preset-es2017 "^6.22.0"

babel-preset-stage-2@^6.18.0:
babel-preset-stage-2@^6.18.0, babel-preset-stage-2@^6.22.0:
version "6.22.0"
resolved "https://registry.npmjs.org/babel-preset-stage-2/-/babel-preset-stage-2-6.22.0.tgz#ccd565f19c245cade394b21216df704a73b27c07"
dependencies:
Expand Down Expand Up @@ -3516,18 +3516,14 @@ [email protected]:
dependencies:
brace-expansion "^1.0.0"

[email protected]:
[email protected], minimist@~0.0.1:
version "0.0.8"
resolved "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"

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

minimist@~0.0.1:
version "0.0.10"
resolved "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"

[email protected]:
version "0.3.0"
resolved "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz#1bbf5ab1ba827af23575143490426455f481fe1e"
Expand Down

0 comments on commit f46774b

Please sign in to comment.