Skip to content

Commit

Permalink
Implement with clipanion
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadayuki committed Oct 25, 2019
1 parent 8e8fb4a commit 4736a40
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/frolint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"@yamadayuki/ogh": "^1.1.0",
"arg": "^4.1.0",
"chalk": "^2.4.2",
"clipanion": "^2.1.3",
"command-exists": "^1.2.8",
"eslint": "^6.0.1",
"eslint-config-wantedly": "^0.7.11",
"eslint-config-wantedly-typescript": "^0.7.11",
Expand All @@ -32,5 +34,8 @@
"preuninstall": "node index.js uninstall",
"test": "jest",
"build": "tsc -b ."
},
"devDependencies": {
"@types/command-exists": "^1.2.0"
}
}
5 changes: 5 additions & 0 deletions packages/frolint/src/Context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BaseContext } from "clipanion";

export type FrolintContext = BaseContext & {
cwd: string;
};
13 changes: 13 additions & 0 deletions packages/frolint/src/commands/DefaultCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Command } from "clipanion";
import { FrolintContext } from "../Context";

export class DefaultCommand extends Command<FrolintContext> {
public static usage = Command.Usage({
description: "apply ESLint and Prettier",
});

@Command.Path()
public async execute() {
console.log("Hello world");
}
}
10 changes: 10 additions & 0 deletions packages/frolint/src/commands/HelpCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Command } from "clipanion";
import { FrolintContext } from "../Context";

export class HelpCommand extends Command<FrolintContext> {
@Command.Path("--help")
@Command.Path("-h")
public async execute() {
this.context.stdout.write(this.cli.usage(null));
}
}
25 changes: 25 additions & 0 deletions packages/frolint/src/commands/InstallCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Command } from "clipanion";
import { FrolintContext } from "../Context";
import { gitExists, isInsideGitRepository } from "../utils";

export class InstallCommand extends Command<FrolintContext> {
public static usage = Command.Usage({
description: "install git pre-commit hook for frolint",
details: `
This command will insert the pre-commit hook script into .git/hooks/pre-commit. If there is no .git/hooks/pre-commit, this command will create the script file.
`,
});

@Command.Path("install")
public async execute() {
if (!gitExists()) {
return;
}

if (!isInsideGitRepository(this.context.cwd)) {
return;
}

this.context.stdout.write("Install");
}
}
25 changes: 25 additions & 0 deletions packages/frolint/src/commands/UninstallCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Command } from "clipanion";
import { FrolintContext } from "../Context";
import { gitExists, isInsideGitRepository } from "../utils";

export class UninstallCommand extends Command<FrolintContext> {
public static usage = Command.Usage({
description: "uninstall git pre-commit hook for frolint",
details: `
This command will remove the pre-commit hook script from .git/hooks/pre-commit.
`,
});

@Command.Path("uninstall")
public async execute() {
if (!gitExists()) {
return;
}

if (!isInsideGitRepository(this.context.cwd)) {
return;
}

this.context.stdout.write("Uninstall");
}
}
4 changes: 4 additions & 0 deletions packages/frolint/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { DefaultCommand } from "./DefaultCommand";
export { HelpCommand } from "./HelpCommand";
export { InstallCommand } from "./InstallCommand";
export { UninstallCommand } from "./UninstallCommand";
24 changes: 23 additions & 1 deletion packages/frolint/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
console.log("hello world");
import { Cli } from "clipanion";
import { DefaultCommand, HelpCommand, InstallCommand, UninstallCommand } from "./commands";
import { FrolintContext } from "./Context";
const pkg = require("../package.json");

const cli = new Cli<FrolintContext>({
binaryLabel: "FROntend LINt Tool",
binaryName: "frolint",
binaryVersion: pkg.version,
});

cli.register(DefaultCommand);
cli.register(HelpCommand);
cli.register(InstallCommand);
cli.register(UninstallCommand);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
cli.runExit(process.argv.slice(2), {
stdin: process.stdin,
stdout: process.stdout,
stderr: process.stderr,
cwd: process.cwd(),
});
18 changes: 18 additions & 0 deletions packages/frolint/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { execSync } from "child_process";
import { sync as commandExistsSync } from "command-exists";

export function isInsideGitRepository(cwd?: string) {
try {
return Boolean(
execSync("git rev-parse --is-inside-work-tree", { cwd })
.toString()
.trim()
);
} catch (err) {
return false;
}
}

export function gitExists() {
return commandExistsSync("git");
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2017",
"module": "commonjs",
"strict": true,
"noImplicitAny": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"experimentalDecorators": true
"experimentalDecorators": true,
"resolveJsonModule": true
}
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,11 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/command-exists@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/command-exists/-/command-exists-1.2.0.tgz#d97e0ed10097090e4ab0367ed425b0312fad86f3"
integrity sha512-ugsxEJfsCuqMLSuCD4PIJkp5Uk2z6TCMRCgYVuhRo5cYQY3+1xXTQkSlPtkpGHuvWMjS2KTeVQXxkXRACMbM6A==

"@types/eslint-visitor-keys@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
Expand Down Expand Up @@ -2034,6 +2039,13 @@ cli-width@^2.0.0:
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639"
integrity sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=

clipanion@^2.1.3:
version "2.1.3"
resolved "https://registry.yarnpkg.com/clipanion/-/clipanion-2.1.3.tgz#009975deb732620f9067af62452241c9ea14ec5a"
integrity sha512-3aFXWQrMG3wvEPvINE+RvRIVcAFCv3vvZzpjYJKJCJH9uykr+rGO+C1UjTFgwVs45oj0KqRBa4nVlAMtLID1tA==
dependencies:
chalk "^2.4.2"

cliui@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-4.1.0.tgz#348422dbe82d800b3022eef4f6ac10bf2e4d1b49"
Expand Down

0 comments on commit 4736a40

Please sign in to comment.