From 7dcfd05580c9bfe50cb5db466e22f543bc2c23af Mon Sep 17 00:00:00 2001 From: Nicolas Beaussart Date: Thu, 6 Feb 2020 20:25:34 +0100 Subject: [PATCH 1/5] :rotating_light: fix chagelog.md file --- .prettierignore | 26 ++++++++++++++++++++++++++ CHANGELOG.md | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..b55f23c --- /dev/null +++ b/.prettierignore @@ -0,0 +1,26 @@ +.DS_Store +node_modules +/dist +babel.config.js + +# local env files +.env.local +.env.*.local + +# Log files +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Editor directories and files +.idea +*.iml +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? +.history + +CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md index ba17d74..f45b221 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 🚑 Critical Hotfixes -- [`319dd45`](https://github.com/beaussan/nbx/commit/319dd45) fix changelog to match prettier style +- [`319dd45`](https://github.com/beaussan/nbx/commit/319dd45) fix changelog to match prettier style # [v2.0.6](https://github.com/beaussan/nbx/compare/v2.0.5...v2.0.6) (2020-02-06) From 964185b069f004271ad842ed05e00bde3225a344 Mon Sep 17 00:00:00 2001 From: Nicolas Beaussart Date: Thu, 6 Feb 2020 20:25:58 +0100 Subject: [PATCH 2/5] :pencil: add docs for commands --- src/commands/add/prettier.ts | 8 +------- src/commands/wall/index.ts | 5 ++--- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/commands/add/prettier.ts b/src/commands/add/prettier.ts index 5478799..f63afba 100644 --- a/src/commands/add/prettier.ts +++ b/src/commands/add/prettier.ts @@ -9,13 +9,7 @@ import { BaseAddCommand } from '../../utls/base-add-command'; plugins.set('fs', fs); export default class Prettier extends BaseAddCommand { - static description = 'describe the command here'; - - static examples = [ - `$ nbx wall -hello world from ./src/hello.ts! -`, - ]; + static description = 'add prettier to project and format it'; static flags = { ...BaseCommand.flags, diff --git a/src/commands/wall/index.ts b/src/commands/wall/index.ts index 4790372..320471a 100644 --- a/src/commands/wall/index.ts +++ b/src/commands/wall/index.ts @@ -14,11 +14,10 @@ interface WallhavenItem { } export default class Wall extends BaseCommand { - static description = 'describe the command here'; + static description = 'download a wallpaper from wallhaven using search'; static examples = [ - `$ nbx wall -hello world from ./src/hello.ts! + `$ nbx wall -r "cat" -o "wall.jpg" -fg `, ]; From 3abbc078735d05aa616b09cb48a1c40bc982487e Mon Sep 17 00:00:00 2001 From: Nicolas Beaussart Date: Thu, 6 Feb 2020 20:32:11 +0100 Subject: [PATCH 3/5] :recycle: move code around --- src/commands/add/prettier.ts | 10 +++++----- src/utls/base-add-command.ts | 11 ++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/commands/add/prettier.ts b/src/commands/add/prettier.ts index f63afba..497a1cd 100644 --- a/src/commands/add/prettier.ts +++ b/src/commands/add/prettier.ts @@ -16,14 +16,11 @@ export default class Prettier extends BaseAddCommand { }; async run() { - const packagePath = filesystem.path('.', 'package.json'); - - if (filesystem.exists(packagePath) !== 'file') { + if (!this.hasDirPackageJson()) { this.error('There is no package.json not found in the current folder'); } - const packageJson = filesystem.read(packagePath, 'json'); - if (packageJson.devDependencies.prettier) { + if (this.hasDevDependencyInPackageJson('prettier')) { this.error('Prettier is already installed in this project.'); } @@ -54,6 +51,7 @@ export default class Prettier extends BaseAddCommand { await this.addDevDependency('pretty-quick', shouldCommit); await this.runWithSpinner('Adding package.json scripts', async () => { + const packagePath = filesystem.path('.', 'package.json'); const packageJsonWithDeps = filesystem.read(packagePath, 'json'); const finalPackageJson = { ...packageJsonWithDeps, @@ -65,7 +63,9 @@ export default class Prettier extends BaseAddCommand { 'format:check': `prettier --list-different "${mask}"`, }, husky: { + ...packageJsonWithDeps.husky, hooks: { + ...packageJsonWithDeps?.husky?.hooks, 'pre-commit': 'pretty-quick --staged', }, }, diff --git a/src/utls/base-add-command.ts b/src/utls/base-add-command.ts index 0b7e6bb..b0f1b1b 100644 --- a/src/utls/base-add-command.ts +++ b/src/utls/base-add-command.ts @@ -1,7 +1,7 @@ import { BaseCommand } from './base-command'; import { add, commit, config as gitConfig, statusMatrix } from 'isomorphic-git'; import * as latestVersion from 'latest-version'; -import { system } from 'gluegun'; +import { filesystem, system } from 'gluegun'; export abstract class BaseAddCommand extends BaseCommand { static flags = { @@ -34,6 +34,15 @@ export abstract class BaseAddCommand extends BaseCommand { } } + hasDirPackageJson() { + return filesystem.isFile(filesystem.path('.', 'package.json')); + } + + hasDevDependencyInPackageJson(name: string): boolean { + const packageJson = filesystem.read('package.sjon', 'json'); + return Boolean(packageJson.devDependencies[name]); + } + async gitAddUnstaged() { const commitsPromice = (await statusMatrix({ dir: '.', pattern: '**' })) .filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) From 49a8b7434bd686345bb90d9f83f5023914cddd41 Mon Sep 17 00:00:00 2001 From: Nicolas Beaussart Date: Thu, 6 Feb 2020 20:52:17 +0100 Subject: [PATCH 4/5] :sparkles: add tailwind add command ! --- src/commands/add/tailwind.ts | 136 +++++++++++++++++++++++++++++++++++ src/utls/base-add-command.ts | 5 ++ 2 files changed, 141 insertions(+) create mode 100644 src/commands/add/tailwind.ts diff --git a/src/commands/add/tailwind.ts b/src/commands/add/tailwind.ts new file mode 100644 index 0000000..f0fb4f5 --- /dev/null +++ b/src/commands/add/tailwind.ts @@ -0,0 +1,136 @@ +import { BaseCommand } from '../../utls/base-command'; +import { system, filesystem, patching } from 'gluegun'; +import * as prompts from 'prompts'; +import * as fs from 'fs'; +import { plugins, add, commit } from 'isomorphic-git'; +import { BaseAddCommand } from '../../utls/base-add-command'; + +plugins.set('fs', fs); + +export default class Prettier extends BaseAddCommand { + static description = 'add tailwindcss to a project'; + + static flags = { + ...BaseCommand.flags, + }; + + async run() { + if (!this.hasDirPackageJson()) { + this.error('There is no package.json not found in the current folder'); + } + + if (!this.hasDependencyInPackageJson('react-scripts')) { + this.error('This script support only for now create react apps.'); + } + + 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) { + this.initGit(); + } + + await this.installForCreateReactApps(shouldCommit); + } + + async installForCreateReactApps(shouldCommit: boolean): Promise { + await this.addDevDependency('@fullhuman/postcss-purgecss', shouldCommit); + await this.addDevDependency('autoprefixer', shouldCommit); + await this.addDevDependency('npm-run-all', shouldCommit); + await this.addDevDependency('postcss-cli', shouldCommit); + await this.addDependency('tailwindcss', shouldCommit); + + await this.runWithSpinner('Generating tailwind initial config', async () => { + await system.exec('yarn tailwindcss init --full'); + + if (shouldCommit) { + await add({ filepath: 'tailwind.config.js', dir: '.' }); + await commit({ dir: '.', message: ':wrench: add tailwind config file' }); + } + }); + await this.runWithSpinner('Generating postcss', async () => { + await filesystem.write( + 'postcss.config.js', + `const purgecss = require('@fullhuman/postcss-purgecss')({ + content: ['./src/**/*.jsx', './src/**/*.js', './src/index.js', './public/index.html'], + css: ['./src/tailwind.css'], + // Include any special characters you're using in this regular expression + defaultExtractor: content => content.match(/[A-Za-z0-9-_:/]+/g) || [], +}); +module.exports = { + plugins: [ + require('tailwindcss')('./tailwind.config.js'), + require('autoprefixer'), + ...(process.env.NODE_ENV === 'production' ? [purgecss] : []), + ], +}; +`, + ); + + if (shouldCommit) { + await add({ filepath: 'postcss.config.js', dir: '.' }); + await commit({ dir: '.', message: ':wrench: add postcss config file' }); + } + }); + + await this.runWithSpinner('Generating tailwind full css', async () => { + await filesystem.dir('src/css'); + await filesystem.write( + 'src/css/tailwind.src.css', + `@tailwind base; + +@tailwind components; + +@tailwind utilities; +`, + ); + + if (shouldCommit) { + await add({ filepath: 'src/css/tailwind.src.css', dir: '.' }); + await commit({ dir: '.', message: ':wrench: add tailwind css file' }); + } + }); + + await this.runWithSpinner('Adding package.json scripts', async () => { + const packagePath = filesystem.path('.', 'package.json'); + const packageJsonWithDeps = filesystem.read(packagePath, 'json'); + const finalPackageJson = { + ...packageJsonWithDeps, + scripts: { + ...packageJsonWithDeps.scripts, + 'start': 'npm-run-all -p start:css start:js', + 'build': 'npm-run-all build:css build:js', + 'start:js': 'react-scripts start', + 'build:js': 'react-scripts build', + 'start:css': 'postcss src/css/tailwind.src.css -o src/tailwind.css -w', + 'build:css': 'postcss src/css/tailwind.src.css -o src/tailwind.css --env production', + }, + }; + + await filesystem.write(packagePath, finalPackageJson, { jsonIndent: 2 }); + + if (shouldCommit) { + await add({ filepath: 'package.json', dir: '.' }); + await commit({ dir: '.', message: ':wrench: add script for tailwind to package.json' }); + } + }); + + await this.runWithSpinner('Adding full tailwind css to .gitignore', async () => { + await patching.append('.gitignore', '\n# ignore tailwind generated css\nsrc/tailwind.css'); + + if (shouldCommit) { + await add({ filepath: '.gitignore', dir: '.' }); + await commit({ dir: '.', message: ':see_no_evil: add generated tailwind to .gitignore' }); + } + }); + } +} diff --git a/src/utls/base-add-command.ts b/src/utls/base-add-command.ts index b0f1b1b..c0862ba 100644 --- a/src/utls/base-add-command.ts +++ b/src/utls/base-add-command.ts @@ -43,6 +43,11 @@ export abstract class BaseAddCommand extends BaseCommand { return Boolean(packageJson.devDependencies[name]); } + hasDependencyInPackageJson(name: string): boolean { + const packageJson = filesystem.read('package.sjon', 'json'); + return Boolean(packageJson.dependencies[name]); + } + async gitAddUnstaged() { const commitsPromice = (await statusMatrix({ dir: '.', pattern: '**' })) .filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) From b3dfbf2d77c3a2e8aef522af19dbe7a51472a98a Mon Sep 17 00:00:00 2001 From: Nicolas Beaussart Date: Thu, 6 Feb 2020 20:53:18 +0100 Subject: [PATCH 5/5] :rotating_light: remove useless ignore lint comments --- src/commands/add/prettier.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/commands/add/prettier.ts b/src/commands/add/prettier.ts index 497a1cd..905b6fe 100644 --- a/src/commands/add/prettier.ts +++ b/src/commands/add/prettier.ts @@ -57,9 +57,7 @@ export default class Prettier extends BaseAddCommand { ...packageJsonWithDeps, scripts: { ...packageJsonWithDeps.scripts, - // eslint-disable-next-line no-useless-escape 'format:write': `prettier --write "${mask}"`, - // eslint-disable-next-line no-useless-escape 'format:check': `prettier --list-different "${mask}"`, }, husky: {