Browse Source

♻️ move code around

pull/4/head
Nicolas Beaussart 6 years ago
parent
commit
3abbc07873
No known key found for this signature in database GPG Key ID: 51D5A407BFCE64A9
  1. 10
      src/commands/add/prettier.ts
  2. 11
      src/utls/base-add-command.ts

10
src/commands/add/prettier.ts

@ -16,14 +16,11 @@ export default class Prettier extends BaseAddCommand {
}; };
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.');
} }
@ -54,6 +51,7 @@ export default class Prettier extends BaseAddCommand {
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,
@ -65,7 +63,9 @@ export default class Prettier extends BaseAddCommand {
'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',
}, },
}, },

11
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,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() { 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