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 Linter and Prettier features #124

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions src/commands/basic/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,108 @@ const boilerplate = {
nuxt: 'https://github.com/madlabsinc/mevn-nuxt-boilerplate.git',
};

const addToScripts = utility => {
const fileToBeWritten = JSON.parse(
fs.readFileSync(path.resolve('package.json')).toString(),
);
if (
utility !== 'prettier' &&
utility !== 'prettier-eslint' &&
utility !== 'none'
) {
fileToBeWritten['scripts']['lint'] = utility;
} else {
fileToBeWritten['scripts']['prettify'] =
"prettier --single-quote --write '**/*.js'";
}
fs.writeFileSync(
path.resolve('package.json'),
JSON.stringify(fileToBeWritten),
);
};

// add .rc and .ignore files to the resepctive directories
const createLinterUtilityConfig = async utility => {
const templateFile = fs
.readFileSync(`${__dirname}/../../../src/templates/linter/.${utility}rc.js`)
.toString();
await execa.shell('touch .' + utility + 'rc.js');
await execa.shell('touch .' + utility + 'ignore');
fs.writeFileSync(`./.${utility}rc.js`, templateFile);
};

// Install utility with specified configuration
const installLintUtility = async (cmd, templateDir) => {
const utility = cmd
.split(' ')
.slice(-1)
.pop();

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

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

try {
await execa.shell(cmd);
await addToScripts(utility);
await createLinterUtilityConfig(utility);
} catch (err) {
installSpinner.fail('Something went wrong');
process.exit(1);
}

// Doesn't show up for some reason
installSpinner.text = `Installing ${utility} for Server`;

// Navigate to the server directory: If else check since Nuxt doesn't have a client dir
if (templateDir !== '') {
process.chdir(
path.resolve(process.cwd(), `../../${projectName}`, 'server'),
);
} else {
process.chdir(path.resolve(process.cwd(), `../${projectName}`, 'server'));
}
try {
await execa.shell(cmd);
await addToScripts(utility);
await createLinterUtilityConfig(utility);
} catch (err) {
installSpinner.fail('Something went wrong');
process.exit(1);
}

// revert back to the project root
process.chdir('../..');

installSpinner.succeed(`Succcessfully installed ${utility}`);
};

// Configure settings for the Linter Utility
const configureLintUtility = async (template, linter, requirePrettier) => {
let templateDir = '';
if (template !== 'nuxt') templateDir = 'client';
// Installs the linter of choice.
if (linter !== 'none')
await installLintUtility(`npm i -D ${linter}`, templateDir);

// Install prettier
if (requirePrettier) {
if (linter !== 'eslint') {
// Configure prettier for jshint and jslint
await installLintUtility('npm i -D prettier', templateDir);
} else {
// Configure eslint-prettier presets.
await installLintUtility('npm i -D prettier-eslint', templateDir);
}
}
};

const makeInitialCommit = async () => {
process.chdir(projectName);
await execa('git', ['init']);
Expand Down Expand Up @@ -103,6 +205,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
6 changes: 6 additions & 0 deletions src/templates/linter/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
"rules": {
"semi": ["error", "always"],
"quotes": ["error", "double"]
}
}
7 changes: 7 additions & 0 deletions src/templates/linter/.jshintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
"undef": true,
"unused": true,
"globals": {
"MY_GLOBAL": true
}
}
7 changes: 7 additions & 0 deletions src/templates/linter/.jslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
"undef": true,
"unused": true,
"globals": {
"MY_GLOBAL": true
}
}
7 changes: 7 additions & 0 deletions src/templates/linter/.prettier-eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
trailingComma: "es5",
tabWidth: 4,
semi: false,
singleQuote: true
};

7 changes: 7 additions & 0 deletions src/templates/linter/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
trailingComma: "es5",
tabWidth: 4,
semi: false,
singleQuote: true
};