Browse Source

add onlyLint flag to prettier

pull/62/head
Nicolas Beaussart 6 years ago
parent
commit
2d8acf049e
  1. 1
      jest.config.js
  2. 43
      src/commands/add/prettier.ts
  3. 94
      src/tests/add/__snapshots__/prettier.spec.ts.snap
  4. 107
      src/tests/add/prettier.spec.ts

1
jest.config.js

@ -2,4 +2,5 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true,
testPathIgnorePatterns: ['/node_modules', '/lib/'],
};

43
src/commands/add/prettier.ts

@ -3,12 +3,18 @@ import { BaseCommand } from '../../utls/base-command';
import { system, filesystem } from 'gluegun';
import * as prompts from 'prompts';
import { BaseAddCommand } from '../../utls/base-add-command';
import { flags } from '@oclif/command';
export default class Prettier extends BaseAddCommand {
static description = 'add prettier to project and format it';
static flags = {
...BaseCommand.flags,
onlyLint: flags.boolean({
default: false,
char: 'l',
description: 'only install for linters',
}),
};
async run() {
@ -16,11 +22,36 @@ export default class Prettier extends BaseAddCommand {
this.error('There is no package.json not found in the current folder');
}
const { shouldCommit } = await prompts(
[
{
type: 'confirm',
message: 'Do you want gitmoji commits with the prettier setup ?',
name: 'shouldCommit',
initial: true,
},
],
{ onCancel: () => this.error('User canceled prompt.') },
);
if (shouldCommit) {
await this.initGit();
}
const {
flags: { onlyLint },
} = this.parse(Prettier);
if (onlyLint) {
await this.handleMaybeEslint(shouldCommit);
await this.handleMaybeTslint(shouldCommit);
return;
}
if (this.hasDevDependencyInPackageJson('prettier')) {
this.error('Prettier is already installed in this project.');
}
const { mask, shouldCommit } = await prompts(
const { mask } = await prompts(
[
{
type: 'text',
@ -28,20 +59,10 @@ export default class Prettier extends BaseAddCommand {
name: 'mask',
initial: '**/*.{js,vue,json,ts,tsx,md,yml,html}',
},
{
type: 'confirm',
message: 'Do you want gitmoji commits with the prettier setup ?',
name: 'shouldCommit',
initial: true,
},
],
{ onCancel: () => this.error('User canceled prompt.') },
);
if (shouldCommit) {
this.initGit();
}
await this.addDevDependency('prettier', shouldCommit);
await this.addDevDependency('husky', shouldCommit);
await this.addDevDependency('pretty-quick', shouldCommit);

94
src/tests/add/__snapshots__/prettier.spec.ts.snap

@ -1,10 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`nbx add:tailwind should fail if no package.json found 1`] = `Array []`;
exports[`nbx add:prettier should fail if no package.json found 1`] = `Array []`;
exports[`nbx add:tailwind should fail if prettier is found in package.json 1`] = `Array []`;
exports[`nbx add:prettier should fail if prettier is found in package.json 1`] = `Array []`;
exports[`nbx add:tailwind should print help correctly 1`] = `
exports[`nbx add:prettier should print help correctly 1`] = `
Array [
"add prettier to project and format it
",
@ -13,13 +13,14 @@ Array [
OPTIONS
-h, --help show CLI help
-l, --onlyLint only install for linters
-v, --verbose Verbose output
--[no-]spinner Enable spinner in cli output, true by default",
"",
]
`;
exports[`nbx add:tailwind should work with commits 1`] = `
exports[`nbx add:prettier should work with commits 1`] = `
"<html>
<body>
<a>testokiii</a>
@ -28,7 +29,7 @@ exports[`nbx add:tailwind should work with commits 1`] = `
"
`;
exports[`nbx add:tailwind should work with commits 2`] = `
exports[`nbx add:prettier should work with commits 2`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",
@ -45,7 +46,7 @@ Array [
]
`;
exports[`nbx add:tailwind should work with eslint and commits and with an empty .eslintrc 1`] = `
exports[`nbx add:prettier should work with eslint and commits and with an empty .eslintrc 1`] = `
"<html>
<body>
<a>testokiii</a>
@ -54,7 +55,7 @@ exports[`nbx add:tailwind should work with eslint and commits and with an empty
"
`;
exports[`nbx add:tailwind should work with eslint and commits and with an empty .eslintrc 2`] = `
exports[`nbx add:prettier should work with eslint and commits and with an empty .eslintrc 2`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",
@ -77,7 +78,18 @@ Array [
]
`;
exports[`nbx add:tailwind should work with eslint and commits and with extends as array .eslintrc 1`] = `
exports[`nbx add:prettier should work with eslint and commits and with an empty .eslintrc and only lint 1`] = `
Array [
"Adding eslint-config-prettier as a dev dependency",
"The step was done without any error.",
"Adding eslint-plugin-prettier as a dev dependency",
"The step was done without any error.",
"Updating .eslintrc",
"The step was done without any error.",
]
`;
exports[`nbx add:prettier should work with eslint and commits and with extends as array .eslintrc 1`] = `
"<html>
<body>
<a>testokiii</a>
@ -86,7 +98,7 @@ exports[`nbx add:tailwind should work with eslint and commits and with extends a
"
`;
exports[`nbx add:tailwind should work with eslint and commits and with extends as array .eslintrc 2`] = `
exports[`nbx add:prettier should work with eslint and commits and with extends as array .eslintrc 2`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",
@ -109,7 +121,7 @@ Array [
]
`;
exports[`nbx add:tailwind should work with eslint and commits and with extends as array .eslintrc.json 1`] = `
exports[`nbx add:prettier should work with eslint and commits and with extends as array .eslintrc.json 1`] = `
"<html>
<body>
<a>testokiii</a>
@ -118,7 +130,7 @@ exports[`nbx add:tailwind should work with eslint and commits and with extends a
"
`;
exports[`nbx add:tailwind should work with eslint and commits and with extends as array .eslintrc.json 2`] = `
exports[`nbx add:prettier should work with eslint and commits and with extends as array .eslintrc.json 2`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",
@ -141,7 +153,7 @@ Array [
]
`;
exports[`nbx add:tailwind should work with eslint and commits and with extends as string .eslintrc 1`] = `
exports[`nbx add:prettier should work with eslint and commits and with extends as string .eslintrc 1`] = `
"<html>
<body>
<a>testokiii</a>
@ -150,7 +162,7 @@ exports[`nbx add:tailwind should work with eslint and commits and with extends a
"
`;
exports[`nbx add:tailwind should work with eslint and commits and with extends as string .eslintrc 2`] = `
exports[`nbx add:prettier should work with eslint and commits and with extends as string .eslintrc 2`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",
@ -173,7 +185,7 @@ Array [
]
`;
exports[`nbx add:tailwind should work with tslint and commits and with an empty config 1`] = `
exports[`nbx add:prettier should work with tslint and commits and with an empty config 1`] = `
"<html>
<body>
<a>testokiii</a>
@ -182,7 +194,16 @@ exports[`nbx add:tailwind should work with tslint and commits and with an empty
"
`;
exports[`nbx add:tailwind should work with tslint and commits and with an empty config 2`] = `
exports[`nbx add:prettier should work with tslint and commits and with an empty config 2`] = `
"<html>
<body>
<a>testokiii</a>
</body>
</html>
"
`;
exports[`nbx add:prettier should work with tslint and commits and with an empty config 3`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",
@ -205,7 +226,16 @@ Array [
]
`;
exports[`nbx add:tailwind should work with tslint and commits and with an existing extends as array 1`] = `
exports[`nbx add:prettier should work with tslint and commits and with an existing extends as array 1`] = `
"<html>
<body>
<a>testokiii</a>
</body>
</html>
"
`;
exports[`nbx add:prettier should work with tslint and commits and with an existing extends as array 2`] = `
"<html>
<body>
<a>testokiii</a>
@ -214,7 +244,7 @@ exports[`nbx add:tailwind should work with tslint and commits and with an existi
"
`;
exports[`nbx add:tailwind should work with tslint and commits and with an existing extends as array 2`] = `
exports[`nbx add:prettier should work with tslint and commits and with an existing extends as array 3`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",
@ -237,7 +267,7 @@ Array [
]
`;
exports[`nbx add:tailwind should work with tslint and commits and with an existing extends as string 1`] = `
exports[`nbx add:prettier should work with tslint and commits and with an existing extends as string 1`] = `
"<html>
<body>
<a>testokiii</a>
@ -246,7 +276,16 @@ exports[`nbx add:tailwind should work with tslint and commits and with an existi
"
`;
exports[`nbx add:tailwind should work with tslint and commits and with an existing extends as string 2`] = `
exports[`nbx add:prettier should work with tslint and commits and with an existing extends as string 2`] = `
"<html>
<body>
<a>testokiii</a>
</body>
</html>
"
`;
exports[`nbx add:prettier should work with tslint and commits and with an existing extends as string 3`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",
@ -269,7 +308,16 @@ Array [
]
`;
exports[`nbx add:tailwind should work with tslint and commits and with an existing rules config 1`] = `
exports[`nbx add:prettier should work with tslint and commits and with an existing rules config 1`] = `
"<html>
<body>
<a>testokiii</a>
</body>
</html>
"
`;
exports[`nbx add:prettier should work with tslint and commits and with an existing rules config 2`] = `
"<html>
<body>
<a>testokiii</a>
@ -278,7 +326,7 @@ exports[`nbx add:tailwind should work with tslint and commits and with an existi
"
`;
exports[`nbx add:tailwind should work with tslint and commits and with an existing rules config 2`] = `
exports[`nbx add:prettier should work with tslint and commits and with an existing rules config 3`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",
@ -301,7 +349,7 @@ Array [
]
`;
exports[`nbx add:tailwind should work without commits 1`] = `
exports[`nbx add:prettier should work without commits 1`] = `
"<html>
<body>
<a>testokiii</a>
@ -310,7 +358,7 @@ exports[`nbx add:tailwind should work without commits 1`] = `
"
`;
exports[`nbx add:tailwind should work without commits 2`] = `
exports[`nbx add:prettier should work without commits 2`] = `
Array [
"Adding prettier as a dev dependency",
"The step was done without any error.",

107
src/tests/add/prettier.spec.ts

@ -50,17 +50,26 @@ export const expectEslintWithParams = ({
eslintName,
eslintJson,
expectedEslint,
onlyLint = false,
argLint = '',
}: {
eslintName: string;
eslintJson: object | string;
expectedEslint: object;
onlyLint?: boolean;
argLint?: string;
}): TestRun =>
expectGitCommits({
before: async () => {
await beforeCreateHtml();
filesystem.write(eslintName, eslintJson);
prompts.inject(['**/*.{js,vue,json,ts,tsx,md,yml,html}', true, true]);
if (onlyLint) {
prompts.inject([true, true]);
} else {
await beforeCreateHtml();
prompts.inject([true, '**/*.{js,vue,json,ts,tsx,md,yml,html}', true]);
}
},
args: [...(onlyLint ? [argLint] : [])],
checks: async () => {
const [
latestEslintPluginPrettier,
@ -69,10 +78,20 @@ export const expectEslintWithParams = ({
latestVersion('eslint-plugin-prettier'),
latestVersion('eslint-config-prettier'),
]);
if (onlyLint) {
const packageJson = filesystem.read('package.json', 'json');
expect(packageJson).toStrictEqual({
devDependencies: {
'eslint-plugin-prettier': latestEslintPluginPrettier,
'eslint-config-prettier': latestEslintConfigPrettier,
},
});
} else {
await checkFiles({
'eslint-plugin-prettier': latestEslintPluginPrettier,
'eslint-config-prettier': latestEslintConfigPrettier,
});
}
const eslintOutput = filesystem.read(eslintName, 'json');
expect(eslintOutput).toStrictEqual(expectedEslint);
},
@ -99,6 +118,9 @@ export const expectEslintWithParams = ({
`:heavy_plus_sign: add eslint-config-prettier@${latestEslintConfigPrettier} as a dev dependency`,
'M\tpackage.json',
'M\tyarn.lock',
...(onlyLint
? []
: [
':art: apply prettier style to project',
'M\tpackage.json',
'M\ttest.html',
@ -115,6 +137,7 @@ export const expectEslintWithParams = ({
`:heavy_plus_sign: add prettier@${latestPrettier} as a dev dependency`,
'M\tpackage.json',
'M\tyarn.lock',
]),
];
},
});
@ -122,16 +145,25 @@ export const expectEslintWithParams = ({
export const expectTslintWithParams = ({
tslintJson,
expectedTslint,
onlyLint = false,
argLint = '',
}: {
tslintJson: object | string;
expectedTslint: object;
onlyLint?: boolean;
argLint?: string;
}): TestRun =>
expectGitCommits({
before: async () => {
await beforeCreateHtml();
filesystem.write('tslint.json', tslintJson);
prompts.inject(['**/*.{js,vue,json,ts,tsx,md,yml,html}', true, true]);
if (onlyLint) {
prompts.inject([true, true]);
} else {
await beforeCreateHtml();
prompts.inject([true, '**/*.{js,vue,json,ts,tsx,md,yml,html}', true]);
}
},
args: [...(onlyLint ? [argLint] : [])],
checks: async () => {
const [
latestTslintConfigPrettier,
@ -140,10 +172,21 @@ export const expectTslintWithParams = ({
latestVersion('tslint-config-prettier'),
latestVersion('tslint-plugin-prettier'),
]);
if (onlyLint) {
const packageJson = filesystem.read('package.json', 'json');
expect(packageJson).toStrictEqual({
devDependencies: {
'tslint-config-prettier': latestTslintConfigPrettier,
'tslint-plugin-prettier': latestTslintPluginPrettier,
},
});
} else {
await checkFiles({
'tslint-config-prettier': latestTslintConfigPrettier,
'tslint-plugin-prettier': latestTslintPluginPrettier,
});
}
const tslintOutput = filesystem.read('tslint.json', 'json');
expect(tslintOutput).toStrictEqual(expectedTslint);
},
@ -170,6 +213,9 @@ export const expectTslintWithParams = ({
`:heavy_plus_sign: add tslint-config-prettier@${latestTslintPluginPrettier} as a dev dependency`,
'M\tpackage.json',
'M\tyarn.lock',
...(onlyLint
? []
: [
':art: apply prettier style to project',
'M\tpackage.json',
'M\ttest.html',
@ -187,13 +233,14 @@ export const expectTslintWithParams = ({
`:heavy_plus_sign: add prettier@${latestPrettier} as a dev dependency`,
'M\tpackage.json',
'M\tyarn.lock',
]),
];
},
});
testCli({
runCommand: (args: string[]) => Prettier.run(args),
name: 'nbx add:tailwind',
name: 'nbx add:prettier',
defaultArgs: ['--no-spinner'],
tests: [
{
@ -203,6 +250,7 @@ testCli({
'There is no package.json not found in the current folder',
before: async () => {
filesystem.remove('package.json');
prompts.inject([true, '**/*.{js,vue,json,ts,tsx,md,yml,html}']);
},
}),
},
@ -215,6 +263,9 @@ testCli({
prettier: '2.0.0',
},
},
before: async () => {
prompts.inject([true, '**/*.{js,vue,json,ts,tsx,md,yml,html}']);
},
}),
},
{
@ -224,7 +275,7 @@ testCli({
checks: checkFiles,
before: async () => {
await beforeCreateHtml();
prompts.inject(['**/*.{js,vue,json,ts,tsx,md,yml,html}', false]);
prompts.inject([false, '**/*.{js,vue,json,ts,tsx,md,yml,html}']);
},
}),
},
@ -263,7 +314,7 @@ testCli({
checks: checkFiles,
before: async () => {
await beforeCreateHtml();
prompts.inject(['**/*.{js,vue,json,ts,tsx,md,yml,html}', true]);
prompts.inject([true, '**/*.{js,vue,json,ts,tsx,md,yml,html}']);
},
}),
},
@ -279,6 +330,35 @@ testCli({
},
}),
},
{
name: 'work with tslint and commits and with only lint with l flag',
runner: expectTslintWithParams({
tslintJson: {},
onlyLint: true,
argLint: '-l',
expectedTslint: {
extends: ['tslint-plugin-prettier', 'tslint-config-prettier'],
rules: {
prettier: true,
},
},
}),
},
{
name:
'work with tslint and commits and with only lint with onlyLint flag',
runner: expectTslintWithParams({
tslintJson: {},
onlyLint: true,
argLint: '--onlyLint',
expectedTslint: {
extends: ['tslint-plugin-prettier', 'tslint-config-prettier'],
rules: {
prettier: true,
},
},
}),
},
{
name: 'work with tslint and commits and with an existing rules config',
runner: expectTslintWithParams({
@ -352,6 +432,19 @@ testCli({
},
}),
},
{
name:
'work with eslint and commits and with an empty .eslintrc and only lint',
runner: expectEslintWithParams({
eslintName: '.eslintrc',
eslintJson: {},
onlyLint: true,
argLint: '-l',
expectedEslint: {
extends: ['plugin:prettier/recommended'],
},
}),
},
{
name: 'work with eslint and commits and with extends as array .eslintrc',
runner: expectEslintWithParams({

Loading…
Cancel
Save