Skip to content

Commit

Permalink
feat: adds to module.paths for themes
Browse files Browse the repository at this point in the history
  • Loading branch information
msimmer committed Oct 17, 2022
1 parent c7d92b7 commit 20dedec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
5 changes: 1 addition & 4 deletions packages/b-ber-lib/src/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ class State {

const cwd = process.cwd()
const cwdArr = cwd.split('/')

const modulePaths = new Set([...module.paths])

let cwdPath
Expand All @@ -336,12 +335,10 @@ class State {
do {
cwdPath = `${cwdArr.join('/')}/node_modules`
if (modulePaths.has(cwdPath)) continue

module.paths.push(cwdPath)
} while (cwdArr.pop())

log.info('Using the following module paths')
module.paths.forEach(p => log.info(`[${p}]`))

// Theme is set, using a built-in theme
if (themes[this.config.theme]) {
set(this, 'theme', themes[this.config.theme])
Expand Down
39 changes: 20 additions & 19 deletions packages/b-ber-lib/src/Theme.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-continue */
/* eslint-disable global-require, import/no-dynamic-require */

import path from 'path'
Expand Down Expand Up @@ -147,36 +148,36 @@ class Theme {
}

static set = (name, force = false) => {
const { themes } = getThemes()

let theme

const cwd = process.cwd()
const cwdArr = cwd.split('/')
const modulePaths = new Set([...module.paths])

let cwdPath

// Add modules paths that reference the current b-ber project
do {
cwdPath = `${cwdArr.join('/')}/node_modules`
if (modulePaths.has(cwdPath)) continue

module.paths.push(cwdPath)
} while (cwdArr.pop())

if (defaultThemes[name]) {
theme = defaultThemes[name]
} else {
// check for both vendor and user paths
const userPath = path.resolve(state.config.themes_directory, name)
const vendorPath = path.resolve('node_modules', name)

// prefer the user path if both exist, since a user might've
// copied over the vendor theme and is changing it
const themePath = fs.existsSync(userPath)
? userPath
: fs.existsSync(vendorPath)
? vendorPath
: null

if (themes.indexOf(name) < 0 || !themePath) {
log.error(`Theme [${name}] is not installed`)
try {
theme = require(name)
} catch (err) {
log.error(`Could not load theme [${name}]`)
}

theme = require(themePath)
}

return createProjectThemeDirectory(name)
.then(() => copyThemeAssets(theme))
.then(() => updateConfig(name))
.then(() => !force && log.notice('Updated theme to', name))
.then(() => !force && log.notice(`Updated theme [${name}]`))
.catch(log.error)
}
}
Expand Down

0 comments on commit 20dedec

Please sign in to comment.