Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement version-related APIs for GeneratorAPI #4000

Merged
merged 6 commits into from Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/dev-guide/generator-api.md
@@ -1,5 +1,44 @@
# Generator API

## cliVersion

Type: `string`

The version string for the **global** `@vue/cli` version that is invoking the plugin.


## assertCliVersion

- **Arguments**
- `{integer | string} range` - a semver range that `@vue/cli` needs to satisfy

- **Usage**

While api.version can be useful in general, it's sometimes nice to just declare your version.
This API exposes a simple way to do that.

Nothing happens if the provided version is satified. Otherwise, an error will be thrown.


## cliServiceVersion

Type: `string`

The version string for the **project local** `@vue/cli-service` version that is invoking the plugin.


## assertCliServiceVersion

- **Arguments**
- `{integer | string} range` - a semver range that `@vue/cli-service` needs to satisfy

- **Usage**

This API exposes a simple way to declare the required project local `@vue/cli-service` version.

Nothing happens if the provided version is satified. Otherwise, an error will be thrown.


## resolve

- **Arguments**
Expand Down
2 changes: 1 addition & 1 deletion docs/dev-guide/plugin-api.md
Expand Up @@ -7,7 +7,7 @@ Type: `string`
The version string for the `@vue/cli-service` version that is loading the plugin.


## assertVersion(range)
## assertVersion

- **Arguments**
- `{integer | string} range` - a semver range that `@vue/cli-service` needs to satisfy
Expand Down
52 changes: 51 additions & 1 deletion packages/@vue/cli/lib/GeneratorAPI.js
Expand Up @@ -4,10 +4,11 @@ const path = require('path')
const merge = require('deepmerge')
const resolve = require('resolve')
const { isBinaryFileSync } = require('isbinaryfile')
const semver = require('semver')
const mergeDeps = require('./util/mergeDeps')
const stringifyJS = require('./util/stringifyJS')
const ConfigTransform = require('./ConfigTransform')
const { getPluginLink, toShortPluginId } = require('@vue/cli-shared-utils')
const { getPluginLink, toShortPluginId, loadModule } = require('@vue/cli-shared-utils')

const isString = val => typeof val === 'string'
const isFunction = val => typeof val === 'function'
Expand Down Expand Up @@ -71,6 +72,55 @@ class GeneratorAPI {
return path.resolve(this.generator.context, _path)
}

get cliVersion () {
return require('../package.json').version
}

assertCliVersion (range) {
if (typeof range === 'number') {
if (!Number.isInteger(range)) {
throw new Error('Expected string or integer value.')
}
range = `^${range}.0.0-0`
}
if (typeof range !== 'string') {
throw new Error('Expected string or integer value.')
}

if (semver.satisfies(this.cliVersion, range)) return

throw new Error(
`Require global @vue/cli "${range}", but was invoked by "${this.cliVersion}".`
)
}

get cliServiceVersion () {
const servicePkg = loadModule(
'@vue/cli-service/package.json',
this.generator.context
)

return servicePkg.version
}

assertCliServiceVersion (range) {
if (typeof range === 'number') {
if (!Number.isInteger(range)) {
throw new Error('Expected string or integer value.')
}
range = `^${range}.0.0-0`
}
if (typeof range !== 'string') {
throw new Error('Expected string or integer value.')
}

if (semver.satisfies(this.cliServiceVersion, range)) return

throw new Error(
`Require @vue/cli-service "${range}", but was loaded with "${this.cliServiceVersion}".`
)
}

/**
* Check if the project has a given plugin.
*
Expand Down