From 55b69701753472c54b69e06d52f1c50a85fde338 Mon Sep 17 00:00:00 2001 From: Nicolas Beaussart Date: Tue, 4 Feb 2020 15:16:43 +0100 Subject: [PATCH] :art: apply prettier style to project --- .circleci/config.yml | 2 +- README.md | 26 ++++-- package.json | 2 +- src/commands/add/prettier.ts | 173 ++++++++++++++++++----------------- src/commands/hello/index.ts | 24 ++--- src/commands/wall/index.ts | 113 +++++++++++------------ src/index.ts | 2 +- src/utls/base-command.ts | 94 +++++++++---------- tsconfig.json | 4 +- 9 files changed, 224 insertions(+), 216 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 73dd68b..eac7d73 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -43,7 +43,7 @@ jobs: workflows: version: 2 - "nbx": + 'nbx': jobs: - node-latest - node-12 diff --git a/README.md b/README.md index fc0c509..3ccb532 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -nbx -=== +# nbx My own personal cli @@ -11,11 +10,15 @@ My own personal cli [![License](https://img.shields.io/npm/l/nbx.svg)](https://github.com/beaussart/nbx/blob/master/package.json) -* [Usage](#usage) -* [Commands](#commands) - + +- [Usage](#usage) +- [Commands](#commands) + + # Usage + + ```sh-session $ npm install -g nbx $ nbx COMMAND @@ -27,13 +30,17 @@ USAGE $ nbx COMMAND ... ``` + + # Commands + -* [`nbx add:prettier`](#nbx-addprettier) -* [`nbx hello [FILE]`](#nbx-hello-file) -* [`nbx help [COMMAND]`](#nbx-help-command) -* [`nbx wall TERMS`](#nbx-wall-terms) + +- [`nbx add:prettier`](#nbx-addprettier) +- [`nbx hello [FILE]`](#nbx-hello-file) +- [`nbx help [COMMAND]`](#nbx-help-command) +- [`nbx wall TERMS`](#nbx-wall-terms) ## `nbx add:prettier` @@ -120,4 +127,5 @@ EXAMPLE ``` _See code: [src/commands/wall/index.ts](https://github.com/beaussart/nbx/blob/v0.0.0/src/commands/wall/index.ts)_ + diff --git a/package.json b/package.json index 4ef7580..b4e188e 100644 --- a/package.json +++ b/package.json @@ -84,4 +84,4 @@ "pre-commit": "pretty-quick --staged" } } -} \ No newline at end of file +} diff --git a/src/commands/add/prettier.ts b/src/commands/add/prettier.ts index 6440526..896163b 100644 --- a/src/commands/add/prettier.ts +++ b/src/commands/add/prettier.ts @@ -1,63 +1,66 @@ // import {flags} from '@oclif/command' -import {BaseCommand} from '../../utls/base-command' -import * as latestVersion from 'latest-version' -import {system, filesystem} from 'gluegun' -import * as prompts from 'prompts' -import * as fs from 'fs' -import {plugins, statusMatrix, add, commit, config as gitConfig} from 'isomorphic-git' +import { BaseCommand } from '../../utls/base-command'; +import * as latestVersion from 'latest-version'; +import { system, filesystem } from 'gluegun'; +import * as prompts from 'prompts'; +import * as fs from 'fs'; +import { plugins, statusMatrix, add, commit, config as gitConfig } from 'isomorphic-git'; -plugins.set('fs', fs) +plugins.set('fs', fs); export default class Prettier extends BaseCommand { - static description = 'describe the command here' + static description = 'describe the command here'; static examples = [ `$ nbx wall hello world from ./src/hello.ts! `, - ] + ]; static flags = { ...BaseCommand.flags, - } + }; async run() { - const packagePath = filesystem.path('.', 'package.json') + const packagePath = filesystem.path('.', 'package.json'); if (filesystem.exists(packagePath) !== 'file') { - this.error('There is no package.json not found in the current folder') + this.error('There is no package.json not found in the current folder'); } - const packageJson = filesystem.read(packagePath, 'json') + const packageJson = filesystem.read(packagePath, 'json'); if (packageJson.devDependencies.prettier) { - this.error('Prettier is already installed in this project.') + this.error('Prettier is already installed in this project.'); } - const {mask, shouldCommit} = await prompts([ - { - type: 'text', - message: 'On what files it should run prettier', - 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.')}) + const { mask, shouldCommit } = await prompts( + [ + { + type: 'text', + message: 'On what files it should run prettier', + 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() + this.initGit(); } - await this.addDevDependency('prettier', shouldCommit) - await this.addDevDependency('husky', shouldCommit) - await this.addDevDependency('pretty-quick', shouldCommit) + await this.addDevDependency('prettier', shouldCommit); + await this.addDevDependency('husky', shouldCommit); + await this.addDevDependency('pretty-quick', shouldCommit); await this.runWithSpinner('Adding package.json scripts', async () => { - const packageJsonWithDeps = filesystem.read(packagePath, 'json') + const packageJsonWithDeps = filesystem.read(packagePath, 'json'); const finalPackageJson = { ...packageJsonWithDeps, scripts: { @@ -72,16 +75,16 @@ hello world from ./src/hello.ts! 'pre-commit': 'pretty-quick --staged', }, }, - } + }; - await filesystem.write(packagePath, finalPackageJson, {jsonIndent: 2}) - await system.exec('yarn') + await filesystem.write(packagePath, finalPackageJson, { jsonIndent: 2 }); + await system.exec('yarn'); if (shouldCommit) { - await add({filepath: 'package.json', dir: '.'}) - await commit({dir: '.', message: ':wrench: add script and husky to package.json'}) + await add({ filepath: 'package.json', dir: '.' }); + await commit({ dir: '.', message: ':wrench: add script and husky to package.json' }); } - }) + }); await this.runWithSpinner('Adding .prettierrc config file', async () => { const prettierRcFile = { @@ -90,79 +93,80 @@ hello world from ./src/hello.ts! printWidth: 120, quoteProps: 'consistent', trailingComma: 'all', - } + }; - await filesystem.write(filesystem.path('.', '.prettierrc'), prettierRcFile, {jsonIndent: 2}) + await filesystem.write(filesystem.path('.', '.prettierrc'), prettierRcFile, { jsonIndent: 2 }); if (shouldCommit) { - await add({filepath: '.prettierrc', dir: '.'}) - await commit({dir: '.', message: ':wrench: add prettierrc config file'}) + await add({ filepath: '.prettierrc', dir: '.' }); + await commit({ dir: '.', message: ':wrench: add prettierrc config file' }); } - }) + }); await this.runWithSpinner('Updating code to match prettier style', async () => { - await system.exec('yarn format:write') + await system.exec('yarn format:write'); if (shouldCommit) { - const commitsPromice = (await statusMatrix({dir: '.', pattern: '**'})) - .filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) - .map(arr => arr[0]) - .map(filepath => add({filepath, dir: '.'})) + const commitsPromice = (await statusMatrix({ dir: '.', pattern: '**' })) + .filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) + .map(arr => arr[0]) + .map(filepath => add({ filepath, dir: '.' })); - await Promise.all(commitsPromice) - await commit({dir: '.', message: ':art: apply prettier style to project'}) + await Promise.all(commitsPromice); + await commit({ dir: '.', message: ':art: apply prettier style to project' }); } - }) + }); - await this.handleMaybeEslint(shouldCommit) + await this.handleMaybeEslint(shouldCommit); } async initGit(): Promise { - const {config} = await this.getConfig() + const { config } = await this.getConfig(); if (!config?.git?.user) { - this.error('Missing config key git.user for git commits') + this.error('Missing config key git.user for git commits'); } if (!config?.git?.email) { - this.error('Missing config key git.email for git commits') + this.error('Missing config key git.email for git commits'); } await gitConfig({ dir: '.', path: 'user.name', value: config?.git?.user, - }) + }); await gitConfig({ dir: '.', path: 'user.email', value: config?.git?.email, - }) - const changes = (await statusMatrix({dir: '.', pattern: '**'})) - .filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) + }); + const changes = (await statusMatrix({ dir: '.', pattern: '**' })).filter( + ([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1), + ); if (changes.length > 0) { - this.error('There is unsaved changed in the git repository, aborting') + this.error('There is unsaved changed in the git repository, aborting'); } } async addDevDependency(name: string, shouldCommit: boolean): Promise { await this.runWithSpinner(`Adding ${name} dependency`, async () => { - const versionToInstall = await latestVersion(name) - await system.exec(`yarn add -D ${name}@${versionToInstall}`) + const versionToInstall = await latestVersion(name); + await system.exec(`yarn add -D ${name}@${versionToInstall}`); if (shouldCommit) { - await add({filepath: 'package.json', dir: '.'}) - await add({filepath: 'yarn.lock', dir: '.'}) - await commit({dir: '.', message: `:heavy_plus_sign: add ${name}@${versionToInstall}`}) + await add({ filepath: 'package.json', dir: '.' }); + await add({ filepath: 'yarn.lock', dir: '.' }); + await commit({ dir: '.', message: `:heavy_plus_sign: add ${name}@${versionToInstall}` }); } - }) + }); } async handleMaybeEslint(shouldCommit: boolean) { - const eslintPath = filesystem.path('.', '.eslintrc') - const hasEslintFile = filesystem.isFile(eslintPath) + const eslintPath = filesystem.path('.', '.eslintrc'); + const hasEslintFile = filesystem.isFile(eslintPath); if (!hasEslintFile) { - return + return; } - const {shouldOverrideEslint} = await prompts( + const { shouldOverrideEslint } = await prompts( [ { type: 'confirm', @@ -171,31 +175,28 @@ hello world from ./src/hello.ts! initial: true, }, ], - {onCancel: () => this.error('User canceled prompt.')}, - ) + { onCancel: () => this.error('User canceled prompt.') }, + ); if (!shouldOverrideEslint) { - return + return; } - await this.addDevDependency('eslint-config-prettier', shouldCommit) - await this.addDevDependency('eslint-plugin-prettier', shouldCommit) + await this.addDevDependency('eslint-config-prettier', shouldCommit); + await this.addDevDependency('eslint-plugin-prettier', shouldCommit); await this.runWithSpinner('Updating .eslintrc', async () => { - const eslintConfig = filesystem.read(eslintPath, 'json') + const eslintConfig = filesystem.read(eslintPath, 'json'); const finalEslintConfig = { ...eslintConfig, - extends: [ - ...eslintConfig.extends, - 'plugin:prettier/recommended', - ], - } + extends: [...eslintConfig.extends, 'plugin:prettier/recommended'], + }; - await filesystem.write(eslintPath, finalEslintConfig, {jsonIndent: 2}) + await filesystem.write(eslintPath, finalEslintConfig, { jsonIndent: 2 }); if (shouldCommit) { - await add({filepath: '.eslintrc', dir: '.'}) - await commit({dir: '.', message: ':wrench: update eslint to use prettier'}) + await add({ filepath: '.eslintrc', dir: '.' }); + await commit({ dir: '.', message: ':wrench: update eslint to use prettier' }); } - }) + }); } } diff --git a/src/commands/hello/index.ts b/src/commands/hello/index.ts index 039655f..1493f34 100644 --- a/src/commands/hello/index.ts +++ b/src/commands/hello/index.ts @@ -1,31 +1,31 @@ -import {Command, flags} from '@oclif/command' +import { Command, flags } from '@oclif/command'; export default class Index extends Command { - static description = 'describe the command here' + static description = 'describe the command here'; static examples = [ `$ nbx hello hello world from ./src/hello.ts! `, - ] + ]; static flags = { - help: flags.help({char: 'h'}), + help: flags.help({ char: 'h' }), // flag with a value (-n, --name=VALUE) - name: flags.string({char: 'n', description: 'name to print'}), + name: flags.string({ char: 'n', description: 'name to print' }), // flag with no value (-f, --force) - force: flags.boolean({char: 'f'}), - } + force: flags.boolean({ char: 'f' }), + }; - static args = [{name: 'file'}] + static args = [{ name: 'file' }]; async run() { - const {args, flags} = this.parse(Index) + const { args, flags } = this.parse(Index); - const name = flags.name || 'world' - this.log(`hello ${name} (${args.file}) from ./src/commands/hello.ts`) + const name = flags.name || 'world'; + this.log(`hello ${name} (${args.file}) from ./src/commands/hello.ts`); if (args.file && flags.force) { - this.log(`you input --force and --file: ${args.file}`) + this.log(`you input --force and --file: ${args.file}`); } } } diff --git a/src/commands/wall/index.ts b/src/commands/wall/index.ts index a1177ad..4790372 100644 --- a/src/commands/wall/index.ts +++ b/src/commands/wall/index.ts @@ -1,10 +1,10 @@ -import {flags} from '@oclif/command' -import {BaseCommand} from '../../utls/base-command' -import axios from 'axios' -import {filesystem} from 'gluegun' +import { flags } from '@oclif/command'; +import { BaseCommand } from '../../utls/base-command'; +import axios from 'axios'; +import { filesystem } from 'gluegun'; function booleanToNumber(value: boolean): string { - return value ? '1' : '0' + return value ? '1' : '0'; } interface WallhavenItem { @@ -14,91 +14,92 @@ interface WallhavenItem { } export default class Wall extends BaseCommand { - static description = 'describe the command here' + static description = 'describe the command here'; static examples = [ `$ nbx wall hello world from ./src/hello.ts! `, - ] + ]; - static args = [ - {name: 'terms', description: 'The search terms for the wallpaper', required: true}, - ] + static args = [{ name: 'terms', description: 'The search terms for the wallpaper', required: true }]; static flags = { ...BaseCommand.flags, - random: flags.boolean({char: 'r', description: 'Pick one randomly'}), - sketchy: flags.boolean({char: 's', description: 'Enables sketchy search'}), - general: flags.boolean({char: 'g', description: 'Enable general category'}), - anime: flags.boolean({char: 'a', description: 'Enable anime category'}), - people: flags.boolean({char: 'p', description: 'Enable people category'}), - output: flags.string({char: 'o', description: 'Output for the wallpaper'}), - force: flags.boolean({char: 'f', description: 'Override the file if found'}), - } + random: flags.boolean({ char: 'r', description: 'Pick one randomly' }), + sketchy: flags.boolean({ char: 's', description: 'Enables sketchy search' }), + general: flags.boolean({ char: 'g', description: 'Enable general category' }), + anime: flags.boolean({ char: 'a', description: 'Enable anime category' }), + people: flags.boolean({ char: 'p', description: 'Enable people category' }), + output: flags.string({ char: 'o', description: 'Output for the wallpaper' }), + force: flags.boolean({ char: 'f', description: 'Override the file if found' }), + }; async run() { - const {args, argv, flags} = this.parse(Wall) - const {general, anime, people, output, force, random, sketchy} = flags - const {terms} = args - const {print: {spin}} = this.tools + const { args, argv, flags } = this.parse(Wall); + const { general, anime, people, output, force, random, sketchy } = flags; + const { terms } = args; + const { + print: { spin }, + } = this.tools; - this.vprint(args, 'args') - this.vprint(argv, 'argv') - this.vprint(flags, 'flags') + this.vprint(args, 'args'); + this.vprint(argv, 'argv'); + this.vprint(flags, 'flags'); if (!general && !anime && !people) { - this.error('You must use at least one category flag') + this.error('You must use at least one category flag'); } - const spinFetchList = spin('Searching wallpapers') - - const categories = `${booleanToNumber(general)}${booleanToNumber( - anime - )}${booleanToNumber(people)}` - const {data: {data}} = await axios.get<{ data: WallhavenItem[] }>('https://wallhaven.cc/api/v1/search', {params: { - q: `${terms}`, - sorting: random ? 'random' : 'relevance', - categories, - purity: `1${booleanToNumber(sketchy)}0`, - atleast: '1920x1080', - ratios: '16x9', - }}) - spinFetchList.succeed() + const spinFetchList = spin('Searching wallpapers'); + + const categories = `${booleanToNumber(general)}${booleanToNumber(anime)}${booleanToNumber(people)}`; + const { + data: { data }, + } = await axios.get<{ data: WallhavenItem[] }>('https://wallhaven.cc/api/v1/search', { + params: { + q: `${terms}`, + sorting: random ? 'random' : 'relevance', + categories, + purity: `1${booleanToNumber(sketchy)}0`, + atleast: '1920x1080', + ratios: '16x9', + }, + }); + spinFetchList.succeed(); if (!data || data.length === 0) { - this.error('No image found, try another search term') + this.error('No image found, try another search term'); } - const firstImage = data[0] - this.vprint(firstImage, 'Image raw data') + const firstImage = data[0]; + this.vprint(firstImage, 'Image raw data'); - const filename = output ? output : `wallhaven-${firstImage.id}.jpg` + const filename = output ? output : `wallhaven-${firstImage.id}.jpg`; - const spinner = spin('Downloading wallpaper') + const spinner = spin('Downloading wallpaper'); if (filesystem.exists(filename)) { if (force) { - spinner.warn('Overrinding ' + filename) + spinner.warn('Overrinding ' + filename); } else { - spinner.fail('The file ' + filename + ' already exist') - this.error('The flag force was not set, aborting') + spinner.fail('The file ' + filename + ' already exist'); + this.error('The flag force was not set, aborting'); } } - const writer = filesystem.createWriteStream(filename, {}) + const writer = filesystem.createWriteStream(filename, {}); const response = await axios.get(firstImage.path, { method: 'GET', responseType: 'stream', - }) - + }); - response.data.pipe(writer) + response.data.pipe(writer); await new Promise((resolve, reject) => { - writer.on('finish', resolve) - writer.on('error', reject) - }) - spinner.succeed() + writer.on('finish', resolve); + writer.on('error', reject); + }); + spinner.succeed(); } } diff --git a/src/index.ts b/src/index.ts index 4caa481..63a56fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1 @@ -export {run} from '@oclif/command' +export { run } from '@oclif/command'; diff --git a/src/utls/base-command.ts b/src/utls/base-command.ts index 5b84b34..bec0f1e 100644 --- a/src/utls/base-command.ts +++ b/src/utls/base-command.ts @@ -1,6 +1,6 @@ -import {Command, flags} from '@oclif/command' -import {Input} from '@oclif/parser' -import {print} from 'gluegun' +import { Command, flags } from '@oclif/command'; +import { Input } from '@oclif/parser'; +import { print } from 'gluegun'; export interface BaseCommandFlags { verbose: boolean; @@ -14,60 +14,60 @@ export interface NbxConfig { } export abstract class BaseCommand extends Command { - static flags = { - verbose: flags.boolean({char: 'v', description: 'Verbose output'}), - help: flags.help({char: 'h'}), - }; + static flags = { + verbose: flags.boolean({ char: 'v', description: 'Verbose output' }), + help: flags.help({ char: 'h' }), + }; - flags?: BaseCommandFlags; + flags?: BaseCommandFlags; - public tools = { - print: print, - }; + public tools = { + print: print, + }; - async init() { - // do some initialization - const {flags, args, argv} = this.parse(> this.constructor) - this.flags = flags + async init() { + // do some initialization + const { flags, args, argv } = this.parse(>this.constructor); + this.flags = flags; - if (flags.verbose) { - this.tools.print.debug(args, 'args') - this.tools.print.debug(argv, 'argv') - this.tools.print.debug(flags, 'flags') - } + if (flags.verbose) { + this.tools.print.debug(args, 'args'); + this.tools.print.debug(argv, 'argv'); + this.tools.print.debug(flags, 'flags'); } + } - vprint(value: any, title?: string) { - if (this.flags && this.flags.verbose) { - this.tools.print.debug(value, title) - } + vprint(value: any, title?: string) { + if (this.flags && this.flags.verbose) { + this.tools.print.debug(value, title); } + } - async runWithSpinner(name: string, func: (spinner: any) => Promise): Promise { - const spinner = this.tools.print.spin(name) - try { - const maybeRet = await func(spinner) - if (spinner.isSpinning) { - spinner.succeed() - } - return maybeRet - // eslint-disable-next-line unicorn/catch-error-name - } catch (maybeError) { - spinner.fail(maybeError.message) - this.error(maybeError) + async runWithSpinner(name: string, func: (spinner: any) => Promise): Promise { + const spinner = this.tools.print.spin(name); + try { + const maybeRet = await func(spinner); + if (spinner.isSpinning) { + spinner.succeed(); } + return maybeRet; + // eslint-disable-next-line unicorn/catch-error-name + } catch (maybeError) { + spinner.fail(maybeError.message); + this.error(maybeError); } + } - async getConfig(): Promise<{ config: NbxConfig; path: string }> { - const cosmic = await import('cosmiconfig').then(cosmic => cosmic.cosmiconfig) - const explorer = cosmic('nbx') - const data = await explorer.search() - if (!data || data.isEmpty || !data.config) { - this.error('Config not found. Tried to look for a .nbxrc, .nbxrc.json, .nbxrc.yaml, .nbxrc.yml, .nbxrc.js') - } - return { - config: data?.config, - path: data?.filepath, - } + async getConfig(): Promise<{ config: NbxConfig; path: string }> { + const cosmic = await import('cosmiconfig').then(cosmic => cosmic.cosmiconfig); + const explorer = cosmic('nbx'); + const data = await explorer.search(); + if (!data || data.isEmpty || !data.config) { + this.error('Config not found. Tried to look for a .nbxrc, .nbxrc.json, .nbxrc.yaml, .nbxrc.yml, .nbxrc.js'); } + return { + config: data?.config, + path: data?.filepath, + }; + } } diff --git a/tsconfig.json b/tsconfig.json index b4c5d76..75eeb31 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,5 @@ "strict": true, "target": "es2017" }, - "include": [ - "src/**/*" - ] + "include": ["src/**/*"] }