Skip to content

Commit

Permalink
refactor: unify code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Sep 23, 2018
1 parent 01fa9af commit 658ea75
Show file tree
Hide file tree
Showing 21 changed files with 308 additions and 39 deletions.
2 changes: 2 additions & 0 deletions packages/@vuepress/core/lib/build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports = async function build (sourceDir, cliOptions = {}) {
process.env.NODE_ENV = 'production'

Expand Down
2 changes: 2 additions & 0 deletions packages/@vuepress/core/lib/dev.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports = async function dev (sourceDir, cliOptions = {}) {
const path = require('path')
const webpack = require('webpack')
Expand Down
2 changes: 2 additions & 0 deletions packages/@vuepress/core/lib/eject.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const path = require('path')
const { chalk, fs, logger } = require('@vuepress/shared-utils')

Expand Down
2 changes: 2 additions & 0 deletions packages/@vuepress/core/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

exports.dev = require('./dev')
exports.build = require('./build')
exports.eject = require('./eject')
10 changes: 8 additions & 2 deletions packages/@vuepress/core/lib/plugin-api/abstract/AsyncOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ const { logger, chalk, datatypes: { isFunction }} = require('@vuepress/shared-ut
const Option = require('./Option')

/**
* Expose Asynchronous Option.
* Expose asynchronous option class.
*/

class AsyncOption extends Option {
/**
* Asynchronous serial running
*
* @param args
* @param {Array<AsyncFunction>} args
* @api public
*/

async asyncApply (...args) {
Expand All @@ -42,8 +44,10 @@ class AsyncOption extends Option {

/**
* Asynchronous serial running
*
* @param args
* @param {Array<AsyncFunction>} args
* @api public
*/

async parallelApply (...args) {
Expand Down Expand Up @@ -72,8 +76,10 @@ class AsyncOption extends Option {

/**
* Process a value via a pipeline.
*
* @param input
* @returns {*}
* @returns {any}
* @api public
*/

async pipeline (input) {
Expand Down
18 changes: 16 additions & 2 deletions packages/@vuepress/core/lib/plugin-api/abstract/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const { logger, chalk, compose, datatypes: { isFunction }} = require('@vuepress/shared-utils')

/**
* Expose Option.
* Expose synchronous option class.
*/

class Option {
Expand All @@ -18,8 +18,10 @@ class Option {

/**
* Set value with name.
*
* @param {string} name
* @param {T} value
* @api public
*/

add (name, value) {
Expand All @@ -31,7 +33,9 @@ class Option {

/**
* Delete value with name.
*
* @param {string} name
* @api public
*/

delete (name) {
Expand All @@ -44,7 +48,9 @@ class Option {

/**
* Clean option store
*
* @param {string} name
* @api public
*/

clear (name) {
Expand All @@ -53,25 +59,31 @@ class Option {

/**
* Get values.
*
* @returns {any<T>}
* @api public
*/

get values () {
return this.items.map(item => item.value)
}

/**
* Get applied values
*
* @returns {Array|*|any[]}
* @api public
*/

get appliedValues () {
return this.appliedItems && this.appliedItems.map(item => item.value)
}

/**
* Get values.
* Get entries.
*
* @returns {any<T>}
* @api public
*/

get entries () {
Expand All @@ -80,7 +92,9 @@ class Option {

/**
* Synchronous running
*
* @param {Array<Function>} args
* @api public
*/

syncApply (...args) {
Expand Down
20 changes: 18 additions & 2 deletions packages/@vuepress/core/lib/plugin-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ module.exports = class PluginAPI {
this.options = {}
this._pluginContext = context
this._pluginQueue = []
this._initializeOptions(PLUGIN_OPTION_MAP)
this.initializeOptions(PLUGIN_OPTION_MAP)
}

/**
* Get enabled plugins
*
* @returns {array}
* @api public
*/

get enabledPlugins () {
Expand All @@ -37,7 +39,9 @@ module.exports = class PluginAPI {

/**
* Get disabled plugins
*
* @returns {array}
* @api public
*/

get disabledPlugins () {
Expand All @@ -46,6 +50,8 @@ module.exports = class PluginAPI {

/**
* apply plugin.
*
* @api public
*/

apply () {
Expand All @@ -60,9 +66,11 @@ module.exports = class PluginAPI {

/**
* Normalize plugin and push it to the plugin queue.
*
* @param {object} pluginRaw
* @param {object} pluginOptions
* @returns {module.PluginAPI}
* @api public
*/

use (pluginRaw, pluginOptions = {}) {
Expand All @@ -84,8 +92,10 @@ module.exports = class PluginAPI {

/**
* Use plugin by config.
*
* @param pluginsConfig
* @returns {module.PluginAPI}
* @api public
*/

useByPluginsConfig (pluginsConfig) {
Expand All @@ -98,9 +108,11 @@ module.exports = class PluginAPI {

/**
* initialize plugin options.
*
* @api private
*/

_initializeOptions () {
initializeOptions () {
Object.keys(PLUGIN_OPTION_MAP).forEach(key => {
const option = PLUGIN_OPTION_MAP[key]
this.options[option.name] = instantiateOption(option.name)
Expand All @@ -109,10 +121,12 @@ module.exports = class PluginAPI {

/**
* Register plugin option.
*
* @param {string} key
* @param {any} value
* @param {string} pluginName
* @returns {module.PluginAPI}
* @api private
*/

registerOption (key, value, pluginName) {
Expand All @@ -132,6 +146,8 @@ module.exports = class PluginAPI {

/**
* apply plugin.
*
* @api private
*/

applyPlugin ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

'use strict'

/**
Expand Down
43 changes: 40 additions & 3 deletions packages/@vuepress/core/lib/prepare/AppContext.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
'use strict'

/**
* Module dependencies.
*/

const path = require('path')
const createMarkdown = require('../markdown/index')
const loadConfig = require('./loadConfig')
Expand All @@ -8,17 +14,23 @@ const Page = require('./Page')
const I18n = require('./I18n')
const PluginAPI = require('../plugin-api/index')

/**
* Expose AppContext.
*/

module.exports = class AppContext {
/**
* Instantiate the app context with a new API
* @param { string } sourceDir
*
* @param {string} sourceDir
* @param {{
* isProd: boolean,
* plugins: pluginsConfig,
* theme: themeNameConfig
* temp: string
* }} options
*/

constructor (sourceDir, cliOptions = {}, isProd) {
this.sourceDir = sourceDir
this.cliOptions = cliOptions
Expand Down Expand Up @@ -47,8 +59,11 @@ module.exports = class AppContext {

/**
* Load pages, load plugins, apply plugins / plugin options, etc.
*
* @returns {Promise<void>}
* @api private
*/

async process () {
this.normalizeHeadTagUrls()
this.markdown = createMarkdown(this.siteConfig)
Expand All @@ -72,7 +87,10 @@ module.exports = class AppContext {

/**
* Apply internal and user plugins
*
* @api private
*/

resolvePlugins () {
const themeConfig = this.themeConfig
const siteConfig = this.siteConfig
Expand Down Expand Up @@ -111,7 +129,10 @@ module.exports = class AppContext {

/**
* normalize head tag urls for base
*
* @api private
*/

normalizeHeadTagUrls () {
if (this.base !== '/' && this.siteConfig.head) {
this.siteConfig.head.forEach(tag => {
Expand All @@ -132,7 +153,10 @@ module.exports = class AppContext {

/**
* Make template configurable
*
* @api private
*/

resolveTemplates () {
let { ssrTemplate, devTemplate } = this.siteConfig
const templateDir = path.resolve(this.vuepressDir, 'templates')
Expand All @@ -156,8 +180,11 @@ module.exports = class AppContext {

/**
* Find all page source files located in sourceDir
*
* @returns {Promise<void>}
* @api private
*/

async resolvePages () {
// resolve pageFiles
const patterns = ['**/*.md', '!.vuepress', '!node_modules']
Expand All @@ -179,8 +206,11 @@ module.exports = class AppContext {

/**
* Add a page
* @returns { Promise<void> }
*
* @returns {Promise<void>}
* @api public
*/

async addPage (options) {
options.permalinkPattern = this.siteConfig.permalink
const page = new Page(options)
Expand All @@ -194,15 +224,19 @@ module.exports = class AppContext {

/**
* Resolve theme
* @returns { Promise<void> }
*
* @returns {Promise<void>}
* @api private
*/

async resolveTheme () {
const theme = this.siteConfig.theme || this.cliOptions.theme
Object.assign(this, (await loadTheme(theme, this.sourceDir, this.vuepressDir)))
}

/**
* Get the data to be delivered to the client.
*
* @returns {{
* title: string,
* description: string,
Expand All @@ -211,7 +245,9 @@ module.exports = class AppContext {
* themeConfig: ThemeConfig,
* locales: Locales
* }}
* @api public
*/

getSiteData () {
return {
title: this.siteConfig.title || '',
Expand All @@ -233,6 +269,7 @@ module.exports = class AppContext {
* tempPath: string
* }}
*/

function createTemp (tempPath) {
if (!tempPath) {
tempPath = path.resolve(__dirname, '../../.temp')
Expand Down
Loading

0 comments on commit 658ea75

Please sign in to comment.