Browse Source

👽 update to new isomorphic git api

pull/31/head
Nicolas Beaussart 6 years ago
parent
commit
635bc9e773
No known key found for this signature in database GPG Key ID: 51D5A407BFCE64A9
  1. 24
      src/commands/add/prettier.ts
  2. 24
      src/commands/add/tailwind.ts
  3. 46
      src/utls/base-add-command.ts

24
src/commands/add/prettier.ts

@ -2,7 +2,6 @@
import { BaseCommand } from '../../utls/base-command';
import { system, filesystem } from 'gluegun';
import * as prompts from 'prompts';
import { add, commit } from 'isomorphic-git';
import { BaseAddCommand } from '../../utls/base-add-command';
export default class Prettier extends BaseAddCommand {
@ -70,9 +69,8 @@ export default class Prettier extends BaseAddCommand {
await system.exec('yarn');
if (shouldCommit) {
await add({ filepath: 'package.json', dir: '.' });
await commit({
dir: '.',
await this.gitAdd({ filepath: 'package.json' });
await this.gitCommit({
message: ':wrench: add script and husky to package.json',
});
}
@ -92,9 +90,8 @@ export default class Prettier extends BaseAddCommand {
});
if (shouldCommit) {
await add({ filepath: '.prettierrc', dir: '.' });
await commit({
dir: '.',
await this.gitAdd({ filepath: '.prettierrc' });
await this.gitCommit({
message: ':wrench: add prettierrc config file',
});
}
@ -105,8 +102,7 @@ export default class Prettier extends BaseAddCommand {
if (shouldCommit) {
await this.gitAddUnstaged();
await commit({
dir: '.',
await this.gitCommit({
message: ':art: apply prettier style to project',
});
}
@ -160,9 +156,8 @@ export default class Prettier extends BaseAddCommand {
await filesystem.write(eslintPath, finalEslintConfig, { jsonIndent: 2 });
if (shouldCommit) {
await add({ filepath: eslintFileName, dir: '.' });
await commit({
dir: '.',
await this.gitAdd({ filepath: eslintFileName });
await this.gitCommit({
message: ':wrench: update eslint to use prettier',
});
}
@ -207,9 +202,8 @@ export default class Prettier extends BaseAddCommand {
await filesystem.write(tslintPath, finalEslintConfig, { jsonIndent: 2 });
if (shouldCommit) {
await add({ filepath: 'tslint.json', dir: '.' });
await commit({
dir: '.',
await this.gitAdd({ filepath: 'tslint.json' });
await this.gitCommit({
message: ':wrench: update tslint to use prettier',
});
}

24
src/commands/add/tailwind.ts

@ -1,7 +1,6 @@
import { BaseCommand } from '../../utls/base-command';
import { system, filesystem, patching } from 'gluegun';
import * as prompts from 'prompts';
import { add, commit } from 'isomorphic-git';
import { BaseAddCommand } from '../../utls/base-add-command';
export default class Tailwind extends BaseAddCommand {
@ -50,9 +49,8 @@ export default class Tailwind extends BaseAddCommand {
await system.exec('yarn tailwindcss init --full');
if (shouldCommit) {
await add({ filepath: 'tailwind.config.js', dir: '.' });
await commit({
dir: '.',
await this.gitAdd({ filepath: 'tailwind.config.js' });
await this.gitCommit({
message: ':wrench: add tailwind config file',
});
}
@ -77,8 +75,8 @@ module.exports = {
);
if (shouldCommit) {
await add({ filepath: 'postcss.config.js', dir: '.' });
await commit({ dir: '.', message: ':wrench: add postcss config file' });
await this.gitAdd({ filepath: 'postcss.config.js' });
await this.gitCommit({ message: ':wrench: add postcss config file' });
}
});
@ -95,8 +93,8 @@ module.exports = {
);
if (shouldCommit) {
await add({ filepath: 'src/css/tailwind.src.css', dir: '.' });
await commit({ dir: '.', message: ':wrench: add tailwind css file' });
await this.gitAdd({ filepath: 'src/css/tailwind.src.css' });
await this.gitCommit({ message: ':wrench: add tailwind css file' });
}
});
@ -119,9 +117,8 @@ module.exports = {
await filesystem.write(packagePath, finalPackageJson, { jsonIndent: 2 });
if (shouldCommit) {
await add({ filepath: 'package.json', dir: '.' });
await commit({
dir: '.',
await this.gitAdd({ filepath: 'package.json' });
await this.gitCommit({
message: ':wrench: add script for tailwind to package.json',
});
}
@ -131,9 +128,8 @@ module.exports = {
await patching.append('.gitignore', '\n# ignore tailwind generated css\nsrc/tailwind.css');
if (shouldCommit) {
await add({ filepath: '.gitignore', dir: '.' });
await commit({
dir: '.',
await this.gitAdd({ filepath: '.gitignore' });
await this.gitCommit({
message: ':see_no_evil: add generated tailwind to .gitignore',
});
}

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

@ -1,11 +1,9 @@
import { BaseCommand } from './base-command';
import { add, plugins, commit, config as gitConfig, statusMatrix } from 'isomorphic-git';
import { add, commit, setConfig, statusMatrix } from 'isomorphic-git';
import * as latestVersion from 'latest-version';
import { filesystem, system } from 'gluegun';
import * as fs from 'fs';
plugins.set('fs', fs);
export abstract class BaseAddCommand extends BaseCommand {
static flags = {
...BaseCommand.flags,
@ -20,12 +18,14 @@ export abstract class BaseAddCommand extends BaseCommand {
this.error('Missing config key git.email for git commits');
}
try {
await gitConfig({
await setConfig({
fs,
dir: '.',
path: 'user.name',
value: config?.git?.user,
});
await gitConfig({
await setConfig({
fs,
dir: '.',
path: 'user.email',
value: config?.git?.email,
@ -33,14 +33,26 @@ export abstract class BaseAddCommand extends BaseCommand {
} catch {
this.error('Could not set git config. Maybe the command was not run in a git repository.');
}
const changes = (await statusMatrix({ dir: '.', pattern: '**' })).filter(
([_, head, workdir]) => head !== workdir,
const changes = (await statusMatrix({ dir: '.', fs })).filter(
([, head, workdir]) => head !== workdir,
);
if (changes.length > 0) {
this.error('There is unsaved changed in the git repository, aborting');
}
}
async gitAdd({ filepath, dir = '.' }: { filepath: string; dir?: string }) {
await add({ filepath, dir, fs });
}
async gitCommit({ message }: { message: string }) {
await commit({
dir: '.',
fs,
message,
});
}
hasDirPackageJson() {
return filesystem.isFile(filesystem.path('.', 'package.json'));
}
@ -62,10 +74,10 @@ export abstract class BaseAddCommand extends BaseCommand {
}
async gitAddUnstaged() {
const commitsPromice = (await statusMatrix({ dir: '.', pattern: '**' }))
.filter(([_, head, workdir]) => head !== workdir)
const commitsPromice = (await statusMatrix({ dir: '.', fs }))
.filter(([, head, workdir]) => head !== workdir)
.map(arr => arr[0])
.map(filepath => add({ filepath, dir: '.' }));
.map(filepath => this.gitAdd({ filepath }));
await Promise.all(commitsPromice);
}
@ -75,10 +87,9 @@ export abstract class BaseAddCommand extends BaseCommand {
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: '.',
await this.gitAdd({ filepath: 'package.json' });
await this.gitAdd({ filepath: 'yarn.lock' });
await this.gitCommit({
message: `:heavy_plus_sign: add ${name}@${versionToInstall} as a dev dependency`,
});
}
@ -90,10 +101,9 @@ export abstract class BaseAddCommand extends BaseCommand {
const versionToInstall = await latestVersion(name);
await system.exec(`yarn add ${name}@${versionToInstall}`);
if (shouldCommit) {
await add({ filepath: 'package.json', dir: '.' });
await add({ filepath: 'yarn.lock', dir: '.' });
await commit({
dir: '.',
await this.gitAdd({ filepath: 'package.json' });
await this.gitAdd({ filepath: 'yarn.lock' });
await this.gitCommit({
message: `:heavy_plus_sign: add ${name}@${versionToInstall} as a dependency`,
});
}

Loading…
Cancel
Save