Browse Source

Merge pull request #4 from beaussan/feat/docs

Feat/docs
pull/7/head
Nicolas Beaussart 6 years ago
committed by GitHub
parent
commit
9d1202249a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      .prettierignore
  2. 2
      CHANGELOG.md
  3. 20
      src/commands/add/prettier.ts
  4. 136
      src/commands/add/tailwind.ts
  5. 5
      src/commands/wall/index.ts
  6. 16
      src/utls/base-add-command.ts

26
.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

2
CHANGELOG.md

@ -2,7 +2,7 @@
## 🚑 Critical Hotfixes ## 🚑 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) # [v2.0.6](https://github.com/beaussan/nbx/compare/v2.0.5...v2.0.6) (2020-02-06)

20
src/commands/add/prettier.ts

@ -9,27 +9,18 @@ import { BaseAddCommand } from '../../utls/base-add-command';
plugins.set('fs', fs); plugins.set('fs', fs);
export default class Prettier extends BaseAddCommand { 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 = { static flags = {
...BaseCommand.flags, ...BaseCommand.flags,
}; };
async run() { 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'); 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.'); this.error('Prettier is already installed in this project.');
} }
@ -60,18 +51,19 @@ hello world from ./src/hello.ts!
await this.addDevDependency('pretty-quick', shouldCommit); await this.addDevDependency('pretty-quick', shouldCommit);
await this.runWithSpinner('Adding package.json scripts', async () => { await this.runWithSpinner('Adding package.json scripts', async () => {
const packagePath = filesystem.path('.', 'package.json');
const packageJsonWithDeps = filesystem.read(packagePath, 'json'); const packageJsonWithDeps = filesystem.read(packagePath, 'json');
const finalPackageJson = { const finalPackageJson = {
...packageJsonWithDeps, ...packageJsonWithDeps,
scripts: { scripts: {
...packageJsonWithDeps.scripts, ...packageJsonWithDeps.scripts,
// eslint-disable-next-line no-useless-escape
'format:write': `prettier --write "${mask}"`, 'format:write': `prettier --write "${mask}"`,
// eslint-disable-next-line no-useless-escape
'format:check': `prettier --list-different "${mask}"`, 'format:check': `prettier --list-different "${mask}"`,
}, },
husky: { husky: {
...packageJsonWithDeps.husky,
hooks: { hooks: {
...packageJsonWithDeps?.husky?.hooks,
'pre-commit': 'pretty-quick --staged', 'pre-commit': 'pretty-quick --staged',
}, },
}, },

136
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<void> {
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' });
}
});
}
}

5
src/commands/wall/index.ts

@ -14,11 +14,10 @@ interface WallhavenItem {
} }
export default class Wall extends BaseCommand { export default class Wall extends BaseCommand {
static description = 'describe the command here';
static description = 'download a wallpaper from wallhaven using search';
static examples = [ static examples = [
`$ nbx wall
hello world from ./src/hello.ts!
`$ nbx wall -r "cat" -o "wall.jpg" -fg
`, `,
]; ];

16
src/utls/base-add-command.ts

@ -1,7 +1,7 @@
import { BaseCommand } from './base-command'; import { BaseCommand } from './base-command';
import { add, commit, config as gitConfig, statusMatrix } from 'isomorphic-git'; import { add, commit, config as gitConfig, statusMatrix } from 'isomorphic-git';
import * as latestVersion from 'latest-version'; import * as latestVersion from 'latest-version';
import { system } from 'gluegun';
import { filesystem, system } from 'gluegun';
export abstract class BaseAddCommand extends BaseCommand { export abstract class BaseAddCommand extends BaseCommand {
static flags = { static flags = {
@ -34,6 +34,20 @@ 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]);
}
hasDependencyInPackageJson(name: string): boolean {
const packageJson = filesystem.read('package.sjon', 'json');
return Boolean(packageJson.dependencies[name]);
}
async gitAddUnstaged() { async gitAddUnstaged() {
const commitsPromice = (await statusMatrix({ dir: '.', pattern: '**' })) const commitsPromice = (await statusMatrix({ dir: '.', pattern: '**' }))
.filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1)) .filter(([_, head, workdir, stage]) => !(head === 1 && workdir === 1 && stage === 1))

Loading…
Cancel
Save