Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
Add prettier, xo and lint-staged
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Aug 31, 2017
1 parent 5514d98 commit a6459ad
Show file tree
Hide file tree
Showing 9 changed files with 8,932 additions and 224 deletions.
7 changes: 5 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const nodeVersion = require('node-version')

// Throw an error if node version is too low
if (nodeVersion.major < 8) {
console.error('Error! Aviate requires at least version 8 of Node. Please upgrade!')
console.error(
'Error! Aviate requires at least version 8 of Node. Please upgrade!'
)
process.exit(1)
}

const run = require('./lib/run')
const run = require('./lib/run')

run()
36 changes: 18 additions & 18 deletions lib/deploy.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
const { promisify } = require('util')
const copy = require('copy-dir')
const rimraf = require('rimraf')
const {promisify} = require('util')

const getConfig = require('./get-config')

// Filesystem
const rimrafAsync = promisify(rimraf)

const copyPromise = (source, dest) => new Promise((resolve, reject) => {
copy(source, dest, (err) => {
if (err) {
reject(err)
}
const copyPromise = (source, dest) =>
new Promise((resolve, reject) => {
copy(source, dest, err => {
if (err) {
reject(err)
}

resolve()
});
})
resolve()
})
})

module.exports = async () => {
const {configFile, projectRoot, outputPath} = await getConfig()
const { configFile, outputPath } = await getConfig()

let themeConfig = {}
try {
themeConfig = require(configFile)
} catch (err) {
if(err.code !== 'MODULE_NOT_FOUND') {
if (err.code !== 'MODULE_NOT_FOUND') {
console.log(err)
}
}

if(!themeConfig.distLocations) {
if (!themeConfig.distLocations) {
console.error('No `distLocations` defined in theme config')
process.exit(1)
return
}

const copies = themeConfig.distLocations.map(async (location) => {
const copies = themeConfig.distLocations.map(async location => {
await rimrafAsync(location)
await copyPromise(outputPath, location)
})

try {
await Promise.all(copies)
console.log('Copied to dist directory')
await Promise.all(copies)
console.log('Copied to dist directory')
} catch (err) {
console.error('Failed to copy dist directory')
console.error(err)
console.error('Failed to copy dist directory')
console.error(err)
}
}

19 changes: 11 additions & 8 deletions lib/get-config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
const findUp = require('find-up')
const path = require('path')
const findUp = require('find-up')

module.exports = async () => {
const configFile = await findUp('aviate.config.js')

if(!configFile) {
if (!configFile) {
const wepackConfigFile = await findUp('wepack.config.js')
if(wepackConfigFile) {
throw new Error('`wepack.config.js` found. Please rename to `aviate.config.js`')
if (wepackConfigFile) {
throw new Error(
'`wepack.config.js` found. Please rename to `aviate.config.js`'
)
}
}

if(!configFile) {
throw new Error('`aviate.config.js` not found')
if (!configFile) {
throw new Error('`aviate.config.js` not found')
}

const projectRoot = path.dirname(configFile)
const outputPath = path.join(projectRoot, '.aviate')

return {configFile, projectRoot, outputPath}
}
return { configFile, projectRoot, outputPath }
}
88 changes: 46 additions & 42 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const readline = require('readline')
const formatMessages = require('webpack-format-messages')
const chalk = require('chalk')
const readline = require('readline')
const clearScreen = require('brighten')

function getCompileTime(stats) {
if (isMultiStats(stats)) {
// Webpack multi compilations run in parallel so using the longest duration.
// https://webpack.github.io/docs/configuration.html#multiple-configurations
return stats.stats
.reduce((time, stats) => Math.max(time, getCompileTime(stats)), 0)
return stats.stats.reduce(
(time, stats) => Math.max(time, getCompileTime(stats)),
0
)
}
return stats.endTime - stats.startTime
}
Expand All @@ -18,54 +20,56 @@ function isMultiStats(stats) {
}

function displayTook(stats) {
const time = getCompileTime(stats)
const time = getCompileTime(stats)

if(time > 1000) {
return time / 1000 + 's'
}
if (time > 1000) {
return time / 1000 + 's'
}

return time + 'ms'
return time + 'ms'
}

module.exports = (compiler) => {
clearScreen()

let first = true
module.exports = compiler => {
clearScreen()

compiler.plugin('invalid', () => {
clearScreen()
console.log(chalk.yellow('Compiling...'))
})
let first = true

compiler.plugin('done', (stats) => {
if(first) {
readline.cursorTo(process.stdout, 0, 1)
readline.clearScreenDown(process.stdout)
} else {
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
}

const messages = formatMessages(stats)
compiler.plugin('invalid', () => {
clearScreen()
console.log(chalk.yellow('Compiling...'))
})

if (!messages.errors.length && !messages.warnings.length) {
// Don't clear `Starting server on HOST`
console.log(`${chalk.green('Compiled successfully!')} ${chalk.dim(`in ${displayTook(stats)}`)}`)
}
compiler.plugin('done', stats => {
if (first) {
readline.cursorTo(process.stdout, 0, 1)
readline.clearScreenDown(process.stdout)
} else {
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
}

if (messages.errors.length) {
const messages = formatMessages(stats)

console.log(chalk.inverse('Failed to compile.'))
messages.errors.forEach(e => console.log(e))
return
}
if (messages.errors.length === 0 && messages.warnings.length === 0) {
// Don't clear `Starting server on HOST`
console.log(
`${chalk.green('Compiled successfully!')} ${chalk.dim(
`in ${displayTook(stats)}`
)}`
)
}

if (messages.warnings.length) {
if (messages.errors.length > 0) {
console.log(chalk.inverse('Failed to compile.'))
messages.errors.forEach(e => console.log(e))
return
}

console.log('Compiled with warnings.')
messages.warnings.forEach(w => console.log(w))
}
if (messages.warnings.length > 0) {
console.log('Compiled with warnings.')
messages.warnings.forEach(w => console.log(w))
}

first = false
})
}
first = false
})
}
48 changes: 25 additions & 23 deletions lib/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Webpack = require('webpack')
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const addDevServerEntrypoints = require('webpack-dev-server/lib/util/addDevServerEntrypoints')
const createDomain = require('webpack-dev-server/lib/util/createDomain')
Expand All @@ -12,40 +12,42 @@ const [command] = process.argv.slice(2)

async function createWebpackConfig() {
const [config, devServer] = await webpackConfig()
if(process.env.NODE_ENV === 'development') {
if (process.env.NODE_ENV === 'development') {
addDevServerEntrypoints(config, devServer)
}
const compiler = Webpack(config)
return {compiler, config, devServer}
const compiler = webpack(config)
return { compiler, devServer }
}

async function dev() {
try {
const {compiler, config, devServer} = await createWebpackConfig()
const { compiler, devServer } = await createWebpackConfig()
logger(compiler)

const server = new WebpackDevServer(compiler, devServer)
server.listen(devServer.port, '127.0.0.1', () => {
console.log(chalk.green(`Started server on ${createDomain(devServer)}`))
console.log(chalk.yellow('Compiling...'))
}).on('error', (err) => {
if(err.code === 'EADDRINUSE') {
console.error(`Port ${devServer.port} is already in use`)
process.exit(0)
}

console.error(err)
})
server
.listen(devServer.port, '127.0.0.1', () => {
console.log(chalk.green(`Started server on ${createDomain(devServer)}`))
console.log(chalk.yellow('Compiling...'))
})
.on('error', err => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${devServer.port} is already in use`)
process.exit(0)
}

console.error(err)
})
} catch (err) {
console.error(err)
}
}

async function build() {
const {compiler, config, devServer} = await createWebpackConfig()
const { compiler } = await createWebpackConfig()
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if(err) {
if (err) {
return reject(err)
}

Expand All @@ -54,14 +56,14 @@ async function build() {
})
}

module.exports = async function run() {
if(!command || command === 'dev') {
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
module.exports = async function() {
if (!command || command === 'dev') {
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
return dev()
}

if(command === 'build') {
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
if (command === 'build') {
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
try {
console.log('Building project...')
await build()
Expand Down
Loading

0 comments on commit a6459ad

Please sign in to comment.