Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 27, 2020
1 parent cf97d0d commit 53be150
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 197 deletions.
88 changes: 45 additions & 43 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ var Module = require('module')
var yaml = require('js-yaml')
var json = require('parse-json')
var debug = require('debug')('unified-engine:configuration')
var resolve = require('load-plugin').resolve
var loadPlugin = require('load-plugin')
var plain = require('is-plain-obj')
var fault = require('fault')
var FindUp = require('./find-up')

module.exports = Config

var own = {}.hasOwnProperty
var extname = path.extname
var basename = path.basename
var dirname = path.dirname
var relative = path.relative

var loaders = {
'.json': loadJson,
Expand All @@ -30,8 +26,6 @@ var defaultLoader = loadJson
Config.prototype.load = load

function Config(options) {
var rcName = options.rcName
var packageField = options.packageField
var names = []

this.cwd = options.cwd
Expand All @@ -40,14 +34,22 @@ function Config(options) {
this.configTransform = options.configTransform
this.defaultConfig = options.defaultConfig

if (rcName) {
names.push(rcName, rcName + '.js', rcName + '.yml', rcName + '.yaml')
if (options.rcName) {
names.push(
options.rcName,
options.rcName + '.js',
options.rcName + '.yml',
options.rcName + '.yaml'
)
debug('Looking for `%s` configuration files', names)
}

if (packageField) {
if (options.packageField) {
names.push('package.json')
debug('Looking for `%s` fields in `package.json` files', packageField)
debug(
'Looking for `%s` fields in `package.json` files',
options.packageField
)
}

this.given = {settings: options.settings, plugins: options.plugins}
Expand All @@ -64,9 +66,8 @@ function Config(options) {

function load(filePath, callback) {
var self = this
var searchPath = filePath || path.resolve(this.cwd, 'stdin.js')

self.findUp.load(searchPath, done)
self.findUp.load(filePath || path.resolve(this.cwd, 'stdin.js'), done)

function done(error, file) {
if (error || file) {
Expand All @@ -79,31 +80,37 @@ function load(filePath, callback) {

function create(buf, filePath) {
var self = this
var transform = self.configTransform
var defaults = self.defaultConfig
var fn = (filePath && loaders[extname(filePath)]) || defaultLoader
var fn = (filePath && loaders[path.extname(filePath)]) || defaultLoader
var options = {prefix: self.pluginPrefix, cwd: self.cwd}
var result = {settings: {}, plugins: []}
var contents = buf ? fn.apply(self, arguments) : undefined

if (transform && contents !== undefined) {
contents = transform(contents, filePath)
if (self.configTransform && contents !== undefined) {
contents = self.configTransform(contents, filePath)
}

// Exit if we did find a `package.json`, but it does not have configuration.
if (buf && contents === undefined && basename(filePath) === 'package.json') {
if (
buf &&
contents === undefined &&
path.basename(filePath) === 'package.json'
) {
return
}

if (contents === undefined) {
if (defaults) {
merge(result, defaults, Object.assign({}, options, {root: self.cwd}))
if (self.defaultConfig) {
merge(
result,
self.defaultConfig,
Object.assign({}, options, {root: self.cwd})
)
}
} else {
merge(
result,
contents,
Object.assign({}, options, {root: dirname(filePath)})
Object.assign({}, options, {root: path.dirname(filePath)})
)
}

Expand All @@ -119,7 +126,7 @@ function loadScript(buf, filePath) {
if (!submodule) {
submodule = new Module(filePath, module)
submodule.filename = filePath
submodule.paths = Module._nodeModulePaths(dirname(filePath))
submodule.paths = Module._nodeModulePaths(path.dirname(filePath))
submodule._compile(String(buf), filePath)
submodule.loaded = true
Module._cache[filePath] = submodule
Expand All @@ -129,24 +136,20 @@ function loadScript(buf, filePath) {
}

function loadYaml(buf, filePath) {
return yaml.safeLoad(buf, {filename: basename(filePath)})
return yaml.safeLoad(buf, {filename: path.basename(filePath)})
}

function loadJson(buf, filePath) {
var result = json(buf, filePath)

if (basename(filePath) === 'package.json') {
if (path.basename(filePath) === 'package.json') {
result = result[this.packageField]
}

return result
}

function merge(target, raw, options) {
var root = options.root
var cwd = options.cwd
var prefix = options.prefix

if (typeof raw === 'object' && raw !== null) {
addPreset(raw)
} else {
Expand Down Expand Up @@ -176,11 +179,10 @@ function merge(target, raw, options) {
}

function addEach(result) {
var length = result.length
var index = -1
var value

while (++index < length) {
while (++index < result.length) {
value = result[index]

if (value !== null && typeof value === 'object' && 'length' in value) {
Expand Down Expand Up @@ -210,7 +212,7 @@ function merge(target, raw, options) {
}

function addModule(id, value) {
var fp = resolve(id, {cwd: root, prefix: prefix})
var fp = loadPlugin.resolve(id, {cwd: options.root, prefix: options.prefix})
var result

if (fp) {
Expand All @@ -219,7 +221,7 @@ function merge(target, raw, options) {
} catch (error) {
throw fault(
'Cannot parse script `%s`\n%s',
relative(root, fp),
path.relative(options.root, fp),
error.stack
)
}
Expand All @@ -228,17 +230,21 @@ function merge(target, raw, options) {
if (typeof result === 'function') {
addPlugin(result, value)
} else {
merge(target, result, Object.assign({}, options, {root: dirname(fp)}))
merge(
target,
result,
Object.assign({}, options, {root: path.dirname(fp)})
)
}
} catch (_) {
throw fault(
'Error: Expected preset or plugin, not %s, at `%s`',
result,
relative(root, fp)
path.relative(options.root, fp)
)
}
} else {
fp = relative(cwd, path.resolve(root, id))
fp = path.relative(options.cwd, path.resolve(options.root, id))
addPlugin(
failingModule(fp, new Error('Could not find module `' + id + '`')),
value
Expand Down Expand Up @@ -266,15 +272,11 @@ function reconfigure(entry, value) {
}

function find(entries, plugin) {
var length = entries.length
var index = -1
var entry

while (++index < length) {
entry = entries[index]

if (entry[0] === plugin) {
return entry
while (++index < entries.length) {
if (entries[index][0] === plugin) {
return entries[index]
}
}
}
Expand Down
35 changes: 14 additions & 21 deletions lib/file-pipeline/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,30 @@ module.exports = configure

// Collect configuration for a file based on the context.
function configure(context, file, fileSet, next) {
var config = context.configuration
var processor = context.processor

if (stats(file).fatal) {
return next()
}

config.load(file.path, handleConfiguration)
context.configuration.load(file.path, handleConfiguration)

function handleConfiguration(error, configuration) {
var plugins
var options
var index = -1
var plugin
var length
var index
var name
var options

if (error) {
return next(error)
}

// Store configuration on the context object.
debug('Using settings `%j`', configuration.settings)
processor.data('settings', configuration.settings)

plugins = configuration.plugins
length = plugins.length
index = -1
context.processor.data('settings', configuration.settings)

debug('Using `%d` plugins', length)
debug('Using `%d` plugins', configuration.plugins.length)

while (++index < length) {
plugin = plugins[index][0]
options = plugins[index][1]
while (++index < configuration.plugins.length) {
plugin = configuration.plugins[index][0]
options = configuration.plugins[index][1]

if (options === false) {
continue
Expand All @@ -52,11 +42,14 @@ function configure(context, file, fileSet, next) {
options = undefined
}

name = plugin.displayName || plugin.name || 'function'
debug('Using plugin `%s`, with options `%j`', name, options)
debug(
'Using plugin `%s`, with options `%j`',
plugin.displayName || plugin.name || 'function',
options
)

try {
processor.use(plugin, options, fileSet)
context.processor.use(plugin, options, fileSet)
} catch (error_) {
/* istanbul ignore next - Should not happen anymore! */
return next(error_)
Expand Down
16 changes: 5 additions & 11 deletions lib/file-pipeline/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ var debug = require('debug')('unified-engine:file-pipeline:copy')

module.exports = copy

var stat = fs.stat
var dirname = path.dirname
var resolve = path.resolve
var relative = path.relative

// Move a file.
function copy(context, file, fileSet, next) {
var output = context.output
var multi = fileSet.expected > 1
var outpath = output
var currentPath = file.path

Expand All @@ -23,11 +17,11 @@ function copy(context, file, fileSet, next) {
return next()
}

outpath = resolve(context.cwd, outpath)
outpath = path.resolve(context.cwd, outpath)

debug('Copying `%s`', currentPath)

stat(outpath, onstatfile)
fs.stat(outpath, onstatfile)

function onstatfile(error, stats) {
if (error) {
Expand All @@ -40,7 +34,7 @@ function copy(context, file, fileSet, next) {
)
}

stat(dirname(outpath), onstatparent)
fs.stat(path.dirname(outpath), onstatparent)
} else {
done(stats.isDirectory())
}
Expand All @@ -57,13 +51,13 @@ function copy(context, file, fileSet, next) {
}

function done(directory) {
if (!directory && multi) {
if (!directory && fileSet.expected > 1) {
return next(
new Error('Cannot write multiple files to single output: ' + outpath)
)
}

file[directory ? 'dirname' : 'path'] = relative(file.cwd, outpath)
file[directory ? 'dirname' : 'path'] = path.relative(file.cwd, outpath)

debug('Copying document from %s to %s', currentPath, file.path)

Expand Down
7 changes: 2 additions & 5 deletions lib/file-pipeline/file-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ var stats = require('vfile-statistics')

module.exports = fileSystem

var writeFile = fs.writeFile
var resolve = path.resolve

// Write a virtual file to the file-system.
// Ignored when `output` is not given.
function fileSystem(context, file, fileSet, next) {
Expand Down Expand Up @@ -37,10 +34,10 @@ function fileSystem(context, file, fileSet, next) {
return next()
}

destinationPath = resolve(context.cwd, destinationPath)
destinationPath = path.resolve(context.cwd, destinationPath)
debug('Writing document to `%s`', destinationPath)

file.stored = true

writeFile(destinationPath, file.toString(), next)
fs.writeFile(destinationPath, file.toString(), next)
}
7 changes: 2 additions & 5 deletions lib/file-pipeline/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ var stats = require('vfile-statistics')

module.exports = read

var resolve = path.resolve
var readFile = fs.readFile

// Fill a file with its contents when not already filled.
function read(context, file, fileSet, next) {
var filePath = file.path
Expand All @@ -21,10 +18,10 @@ function read(context, file, fileSet, next) {
debug('Not reading failed file `%s`', filePath)
next()
} else {
filePath = resolve(context.cwd, filePath)
filePath = path.resolve(context.cwd, filePath)

debug('Reading `%s` in `%s`', filePath, 'utf8')
readFile(filePath, 'utf8', onread)
fs.readFile(filePath, 'utf8', onread)
}

function onread(error, contents) {
Expand Down

0 comments on commit 53be150

Please sign in to comment.