Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt for additional linter features #117

Closed
wants to merge 13 commits into from
111 changes: 110 additions & 1 deletion src/commands/basic/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,96 @@ const boilerplate = {
nuxt: 'https://github.com/madlabsinc/mevn-nuxt-boilerplate.git',
};

// add lint and prettify scripts
const addToScripts = (currentDir, side, linter) => {
side.map(item => {
const reqPath = `${currentDir}/${projectName}/${item}/package.json`.toString();
let configFile = JSON.parse(fs.readFileSync(reqPath).toString());
configFile['scripts'][
'lint'
] = `node ./node_modules/${linter}/bin/${linter}`;
configFile['scripts']['prettify'] =
"prettier --single-quote --write '**/{*.js,*.jsx}'";
fs.writeFileSync(reqPath, JSON.stringify(configFile));
console.log(configFile);
});
};

// install utility (Linter and Prettier)
const installLintUtility = async (template, cmd, templateDir) => {
const utility = cmd
.split(' ')
.slice(-1)
.pop();

const installSpinner = new Spinner(`Installing ${utility} for server`);
installSpinner.start();

// Hop over to the server directory except for the case of Nuxt-js template
if (templateDir) {
process.chdir(path.resolve(projectName, templateDir));
}

try {
await execa.shell(cmd);
await execa.shell('touch .' + utility + 'ignore');
await execa.shell('touch .' + utility + 'rc.js');
} catch (err) {
installSpinner.fail('Something went wrong');
process.exit(1);
}
if (template !== 'nuxt') {
installSpinner.text = `Installing ${utility} for client`;
// Navigate to the client directory except for the case of Nuxt-js template
process.chdir(path.resolve(`../../${projectName}`, 'client'));
try {
await execa.shell(cmd);
// Empty config files
await execa.shell('touch .' + utility + 'ignore');
await execa.shell('touch .' + utility + 'rc.js');
} catch (err) {
installSpinner.fail('Something went wrong');
process.exit(1);
}
}
installSpinner.succeed(`Succcessfully installed ${utility}`);

// Revert to Project Root path
process.chdir('../..');
};

const configureLintUtility = async (template, linter, requirePrettier) => {
let templateDir = 'server';

// Installs the linter of choice.
if (linter !== 'none')
await installLintUtility(template, `npm i -D ${linter}`, templateDir);

// ToDo: Create and populate .{linter}rc and .{linter}ignore files
if (requirePrettier) {
// Install prettier.
if (linter !== 'eslint') {
// Configure prettier for jshint and jslint
await installLintUtility(template, 'npm i -D prettier', templateDir);
} else {
// Configure eslint-prettier presets.
await installLintUtility(
template,
'npm i -D prettier-eslint',
templateDir,
);
}
// set up scripts
if (template === 'nuxt') {
await addToScripts(process.cwd(), ['server'], linter);
} else {
await addToScripts(process.cwd(), ['server', 'client'], linter);
}
}
};

const makeInitialCommit = async () => {
process.chdir(projectName);
process.chdir(path.resolve(projectName));
await execa('git', ['init']);
await execa('git', ['add', '.']);
await execa('git', ['commit', '-m', 'Initial commit', '-m', 'From Mevn-CLI']);
Expand Down Expand Up @@ -97,6 +185,27 @@ const fetchTemplate = async template => {
projectConfig.join('\n').toString(),
);

// Prompts asking for the linter of choice and prettier support.

const { linterOfChoice } = await inquirer.prompt([
{
name: 'linterOfChoice',
type: 'list',
message: 'Please select your favourite linter',
choices: ['eslint', 'jslint', 'jshint', 'none'],
},
]);

const { requirePrettier } = await inquirer.prompt([
{
name: 'requirePrettier',
type: 'confirm',
message: 'Do you require Prettier',
},
]);

await configureLintUtility(template, linterOfChoice, requirePrettier);

if (template === 'nuxt') {
const { requirePwaSupport } = await inquirer.prompt([
{
Expand Down