Skip to content

Commit f446fbb

Browse files
committed
Add format script
1 parent b048972 commit f446fbb

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ are using it within `package.json` simply write `typescript-scripts [script name
3535

3636
All the usual eslint flags can be passed to this script, they will be passed on to eslint.
3737

38+
- `typescript-scripts format`
39+
40+
Formats all the files in the repository using prettier.
41+
42+
TODO - allow passing an argument to configure which files/folders should be formatted
43+
3844
- compile (TODO)
3945

4046
- test (TODO)

index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"use strict";
44

5-
import { init, lint } from "./scripts";
5+
import { format, init, lint } from "./scripts";
66

77
const args = process.argv.slice(2);
88
const scriptName = args[0];
@@ -20,6 +20,11 @@ switch (scriptName) {
2020
process.exit(0);
2121
break;
2222

23+
case "format":
24+
format();
25+
process.exit(0);
26+
break;
27+
2328
case "lint": {
2429
const status = lint();
2530
process.exit(status);

scripts/format.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import spawn from "cross-spawn";
4+
5+
const appDirectory = fs.realpathSync(process.cwd());
6+
// const args = process.argv.slice(3);
7+
8+
function format(): number {
9+
const result = spawn.sync(
10+
path.join(appDirectory, "./node_modules/.bin/prettier"),
11+
["--write", "./**/*"],
12+
{ stdio: "inherit" }
13+
);
14+
15+
if (result.error) {
16+
console.error(result.error);
17+
}
18+
19+
return typeof result.status === "number" ? result.status : 1;
20+
}
21+
22+
export default format;

scripts/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
import format from "./format";
12
import init from "./init";
23
import lint from "./lint";
34

4-
export { init, lint };
5+
export { format, init, lint };

0 commit comments

Comments
 (0)