Skip to content

Commit

Permalink
feat(#127): add build mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Oct 31, 2021
1 parent c4457e1 commit 6658823
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
36 changes: 18 additions & 18 deletions packages/schema/src/nuxtModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const DruxtSchemaNuxtModule = function (moduleOptions = {}) {
baseUrl: moduleOptions.baseUrl,
...(this.options || {}).druxt || {},
schema: {
build: true,
...((this.options || {}).druxt || {}).schema || {},
...moduleOptions,
}
Expand All @@ -47,34 +48,33 @@ const DruxtSchemaNuxtModule = function (moduleOptions = {}) {
options
})

// Enable Vuex Store.
this.options.store = true

// Add Vuex plugin.
this.addPlugin({
src: resolve(__dirname, '../templates/store.js'),
fileName: 'store/druxt-schema.js',
options
})

this.nuxt.hook('builder:prepared', async () => {
// Generate schemas.
const druxtSchema = new DruxtSchema(options.baseUrl, options)
const { schemas } = await druxtSchema.get()
if (options.schema.build) {
this.nuxt.hook('builder:prepared', async () => {
// Generate schemas.
const druxtSchema = new DruxtSchema(options.baseUrl, options)
const { schemas } = await druxtSchema.get()

for (const name in schemas) {
const schema = schemas[name]
if (typeof schema === 'undefined') continue
for (const name in schemas) {
const schema = schemas[name]
if (typeof schema === 'undefined') continue

this.addTemplate({
src: resolve(__dirname, '../templates/schema.json'),
fileName: `schemas/${name}.json`,
options: { schema }
})
}
this.addTemplate({
src: resolve(__dirname, '../templates/schema.json'),
fileName: `schemas/${name}.json`,
options: { schema }
})
}

consola.success('Druxt schema generated')
})
consola.success('Druxt schema generated')
})
}
}

DruxtSchemaNuxtModule.meta = require('../package.json')
Expand Down
34 changes: 26 additions & 8 deletions packages/schema/src/stores/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ const DruxtSchemaStore = ({ store }) => {
* @example @lang js
* const schema = await this.$store.dispatch('druxtRouter/get', '/')
*/
async get({ state, commit }, resource = {}) {
async get(context, resource = {}) {
const { state, commit } = context
resource = {
id: null,
resourceType: null,
entityType: 'node',
bundle: null,
mode: 'default',
id: undefined,
resourceType: undefined,
entityType: undefined,
bundle: undefined,
mode: undefined,
schemaType: 'view',

...resource
Expand All @@ -90,8 +91,25 @@ const DruxtSchemaStore = ({ store }) => {

// Only load if we don't have this schema in the store.
if (!state.schemas[resource.id]) {
const schema = await this.$druxtSchema.import(resource.id)
commit('addSchema', { id: resource.id, schema })
let schema = false
// Get schema from filesystem if build mode enabled.
if ((this.$druxt.settings.schema || {}).build || typeof (this.$druxt.settings.schema || {}).build == 'undefined') {
schema = await this.$druxtSchema.import(resource.id)
if (schema) {
commit('addSchema', { id: resource.id, schema })
}
}

// Else, get schema from the JSON:API.
if (this.$druxt.settings.schema.build === false || !schema) {
if (!resource.entityType && resource.resourceType) {
const [entityType, bundle] = resource.resourceType.split('--')
resource.entityType = entityType
resource.bundle = bundle
}
const { schema } = await this.$druxtSchema.getSchema(resource)
commit('addSchema', { id: resource.id, schema })
}
}

return state.schemas[resource.id]
Expand Down
28 changes: 11 additions & 17 deletions packages/schema/templates/plugin.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
/**
* Nuxt.js plugin for Druxt.js Schema.
*/
const DruxtSchemaPlugin = {
/**
* Import a generated Druxt.js Schema by ID.
*
* @param {string} id - The Druxt.js Schema ID.
* @returns {Schema} The generated Druxt.js Schema object.
*
* @example @lang js
* const schema = await this.$druxtSchema.import('node--page--default--view')
*/
import: async id => import(`./schemas/${id}.json`).then(m => m.default || m)
}
import { DruxtSchema } from 'druxt-schema'

export default (context, inject) => {
inject('druxtSchema', DruxtSchemaPlugin)
export default ({ $druxt }, inject) => {
const schema = new DruxtSchema($druxt.settings.baseUrl)
schema.import = async id => {
try {
return import(`./schemas/${id}.json`).then(m => m.default || m)
} catch(e) {
return false
}
}
inject('druxtSchema', schema)
}

/**
Expand Down

0 comments on commit 6658823

Please sign in to comment.