Skip to content

Commit

Permalink
fix: regression
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Aug 9, 2020
1 parent 50f0824 commit 45940f9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 41 deletions.
106 changes: 78 additions & 28 deletions src/commands/basic/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,18 @@ const addDeps = async (deps, { dev }) => {

// Nuxt.js modules are installed via multiselect prompt
if (template === 'Nuxt.js' && !deps.length) {
// Holds reference to the project specific config (.mevnrc)
const projectConfig = appData();

// Nuxt.js modules that are already installed and configured (.mevnrc)
const {
modules: configuredModules,
isConfigured,
renderingMode,
} = projectConfig;

// Supported Nuxt.js modules
const nuxtModules = [
let nuxtModules = [
'vuetify',
'pwa',
'axios',
Expand All @@ -80,6 +90,10 @@ const addDeps = async (deps, { dev }) => {
'storybook',
'markdownit',
];
if (renderingMode === 'spa') {
// nuxt-oauth requires server rendered app
nuxtModules = nuxtModules.filter((module) => module !== 'oauth');
}

// These addons doesn't require installation
const nuxtAddons = ['vuex'];
Expand All @@ -97,9 +111,6 @@ const addDeps = async (deps, { dev }) => {
(module) => !availableBuildModules.includes(module),
);

// Nuxt.js modules that are already installed and configured (.mevnrc)
const { modules: configuredModules } = appData();

// Nuxt.js modules that are available for installation
const nuxtDeps = []
.concat(nuxtModules, nuxtAddons)
Expand Down Expand Up @@ -172,9 +183,6 @@ const addDeps = async (deps, { dev }) => {
);
}

// Holds reference to the project specific config (.mevnrc)
const projectConfig = appData();

// Read initial content from nuxt.config.js
const nuxtConfig = fs
.readFileSync('./client/nuxt.config.js', 'utf8')
Expand All @@ -192,24 +200,30 @@ const addDeps = async (deps, { dev }) => {
const addons = installCandidate.filter((dep) => nuxtAddons.includes(dep));

// Configure vuex-store for Nuxt.js template
if (addons.includes('vuex')) {
const configureNuxtVuexStore = () => {
const vuexNuxtStoreTemplate = [
'export const state = () => ({',
' counter: 0',
' counter: 0,',
'})',
'',
'export const mutations = {',
' increment (state) {',
'\tstate.counter++',
' }',
' },',
'}',
];

// Navigate to the store directory and create a basic store template file
fs.writeFileSync(
'./client/store/index.js',
vuexNuxtStoreTemplate.join('\n'),
);
if (!fs.existsSync('./client/store/index.js')) {
fs.writeFileSync(
'./client/store/index.js',
vuexNuxtStoreTemplate.join('\n'),
);
}
};

if (addons.includes('vuex')) {
configureNuxtVuexStore();
}

// Configure @nuxtjs/vuetify
Expand Down Expand Up @@ -265,19 +279,19 @@ const addDeps = async (deps, { dev }) => {
// Recompute since buildModules was updated
modulesIdx = nuxtConfig.findIndex((line) => line.includes('modules:')) + 2;

// Configure @nuxtjs/content module
// Configure @nuxt/content module
if (modules.includes('content')) {
const contentConfig = [
`${' '.repeat(2)}content: {`,
`${' '.repeat(4)} //Options`,
`${' '.repeat(2)}},`,
];
nuxtConfig.splice(modulesIdx, 0, `${' '.repeat(4)}'@nuxtjs/content',`);
nuxtConfig.splice(modulesIdx, 0, `${' '.repeat(4)}'@nuxt/content',`);

const modulesEndIdx =
nuxtConfig.indexOf(`${' '.repeat(2)}],`, modulesIdx) + 1;

// Add @nuxtjs/content config beneath the modules array
// Add @nuxt/content config beneath the modules array
contentConfig.forEach((config, idx) =>
nuxtConfig.splice(modulesEndIdx + idx, 0, config),
);
Expand Down Expand Up @@ -310,10 +324,14 @@ const addDeps = async (deps, { dev }) => {
const oAuthConfig = [
`${' '.repeat(2)}oauth: {`,
`${' '.repeat(4)}sessionName: 'mySession',`,
`${' '.repeat(4)}secretKey: process.env.SECRET_KEY,`,
`${' '.repeat(4)}oauthHost: process.env.OAUTH_HOST,`,
`${' '.repeat(4)}oauthClientID: process.env.OAUTH_CLIENT_ID,`,
`${' '.repeat(4)}oauthClientSecret: process.env.OAUTH_CLIENT_SECRET,`,
`${' '.repeat(4)}secretKey: process.env.SECRET_KEY || 'SECRET_KEY',`,
`${' '.repeat(4)}oauthHost: process.env.OAUTH_HOST || 'OAUTH_HOST',`,
`${' '.repeat(
4,
)}oauthClientID: process.env.OAUTH_CLIENT_ID || 'OAUTH_CLIENT_ID',`,
`${' '.repeat(
4,
)}oauthClientSecret: process.env.OAUTH_CLIENT_SECRET || 'OAUTH_CLIENT_SECRET',`,
`${' '.repeat(4)}onLogout: (req, res) => {`,
`${' '.repeat(6)}// do something after logging out`,
`${' '.repeat(4)}},`,
Expand All @@ -322,7 +340,7 @@ const addDeps = async (deps, { dev }) => {
`${' '.repeat(4)}},`,
`${' '.repeat(2)}},`,
];
nuxtConfig.splice(modulesIdx, 0, `${' '.repeat(4)}'@nuxt-oauth',`);
nuxtConfig.splice(modulesIdx, 0, `${' '.repeat(4)}'nuxt-oauth',`);

const modulesEndIdx =
nuxtConfig.indexOf(`${' '.repeat(2)}],`, modulesIdx) + 1;
Expand All @@ -331,6 +349,9 @@ const addDeps = async (deps, { dev }) => {
oAuthConfig.forEach((config, idx) =>
nuxtConfig.splice(modulesEndIdx + idx, 0, config),
);

// It requires Vuex Store to be activated
configureNuxtVuexStore();
}

// Configure @nuxtjs/toast module
Expand Down Expand Up @@ -393,11 +414,6 @@ const addDeps = async (deps, { dev }) => {
`${' '.repeat(4)} // Options`,
`${' '.repeat(2)}},`,
];
nuxtConfig.splice(
buildModulesIdx,
0,
`${' '.repeat(4)}'@nuxtjs/storybook',`,
);

const modulesEndIdx =
nuxtConfig.indexOf(`${' '.repeat(2)}],`, modulesIdx) + 1;
Expand Down Expand Up @@ -459,11 +475,45 @@ const addDeps = async (deps, { dev }) => {
}

// Update modules entry with the installed Nuxt.js modules
configuredModules.push(...[].concat(modules, buildModules, addons));
const installedNuxtModules = [].concat(modules, buildModules, addons);
if (
installedNuxtModules.includes('nuxt-oauth') &&
!installedNuxtModules.includes('vuex')
) {
// Vuex Store is activated with nuxt-oauth
installedNuxtModules.push('vuex');
}
configuredModules.push(...installedNuxtModules);

// Update the modules entry
projectConfig.modules = configuredModules;

if (!isConfigured) {
// Additional dependencies were installed before invoking serve
await exec(
'npm install',
'Installing dependencies in the background. Hold on...',
'Dependencies were successfully installed',
{
cwd: templateDir,
},
);

// Skip configuration step involved when invoking serve
if (
['pwa', 'axios', 'content'].some((module) =>
installedNuxtModules.includes(module),
)
) {
projectConfig.isConfigured = true;
}
}

// Eslint
await exec('npm run lint -- --fix', 'Cleaning up', 'Fixed lint errors', {
cwd: templateDir,
});

fs.writeFileSync('.mevnrc', JSON.stringify(projectConfig, null, 2));

// Write back the updated content
Expand Down
3 changes: 3 additions & 0 deletions src/commands/basic/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ const fetchTemplate = async (template) => {
}

// To be written to project specific config (.mevnrc)
projectConfig.renderingMode = mode.includes('Universal')
? 'universal'
: 'spa';
projectConfig.deployTarget = deployTarget.includes('Node.js')
? 'server'
: 'static';
Expand Down
15 changes: 2 additions & 13 deletions src/commands/serve/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,12 @@ const serveProject = async (projectConfig, templateDir) => {
['pwa', 'axios', 'content'].includes(module),
);

if (!installCandidate.length && modules.length) {
// Additional dependencies were installed with add command
await exec(
'npm install',
'Installing dependencies in the background. Hold on...',
'Dependencies were successfully installed',
{
cwd: templateDir,
},
);
}

// Do not proceed if the user haven't opted for any Nuxt.js modules
if (installCandidate.length) {
// Add the @nuxtjs prefix
const modulesWithPrefix = installCandidate.map(
(module) => `${module === 'content' ? '@nuxt' : '@nuxtjs'}/${module}`, // @nuxt/content has different prefix
(module) =>
`${module === 'content' ? `@nuxt/${module}` : '@nuxtjs'}/${module}`, // @nuxt/content has different prefix
);

// @nuxtjs/pwa is to be installed as a devDependency
Expand Down

0 comments on commit 45940f9

Please sign in to comment.