Skip to content

Commit e18a90a

Browse files
Use more conventional TS style in packages/scripts (#313)
- Using `ts-results` didn't make much sense because we weren't doing anything interesting with the results, such as deferring erroring. We wanted to error immediately. It added indirection - I'm not sure why I was using anonymous functions rather than `function` - I like putting `export` directly on the function rather than the bottom of the file
1 parent 9ae6db4 commit e18a90a

File tree

8 files changed

+118
-172
lines changed

8 files changed

+118
-172
lines changed

.eslintrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ overrides:
1717
- files: ["**/*.test.ts", "**/playwright.config.js"]
1818
rules:
1919
"import/no-extraneous-dependencies": "off"
20+
- files: ["packages/scripts/**/*.ts"]
21+
rules:
22+
"no-console": "off"
2023

2124
rules:
2225
no-unexpected-multiline: off

packages/scripts/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"update-lots": "tsx src/update-lots.ts"
1010
},
1111
"dependencies": {
12-
"@prn-parking-lots/shared": "workspace:*",
13-
"ts-results": "^3.3.0"
12+
"@prn-parking-lots/shared": "workspace:*"
1413
},
1514
"devDependencies": {
1615
"tsx": "^4.19.2"

packages/scripts/src/add-city.ts

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import fs from "fs/promises";
22

3-
import results from "ts-results";
4-
53
import type { CityId } from "@prn-parking-lots/shared/src/js/model/types.ts";
64
import { determineArgs, updateCoordinates, updateParkingLots } from "./base.ts";
75

8-
const addScoreCard = async (
9-
cityId: CityId,
10-
cityName: string,
11-
): Promise<results.Result<void, string>> => {
6+
async function addScoreCard(cityId: CityId, cityName: string): Promise<void> {
127
const newEntry = {
138
name: cityName,
149
percentage: "FILL ME IN, e.g. 23%",
@@ -31,7 +26,7 @@ const addScoreCard = async (
3126
originalData = JSON.parse(rawOriginalData);
3227
} catch (err: unknown) {
3328
const { message } = err as Error;
34-
return results.Err(
29+
throw new Error(
3530
`Issue reading the score card file path ${originalFilePath}: ${message}`,
3631
);
3732
}
@@ -45,41 +40,33 @@ const addScoreCard = async (
4540
});
4641

4742
await fs.writeFile(originalFilePath, JSON.stringify(sortedData, null, 2));
48-
return results.Ok.EMPTY;
49-
};
43+
}
5044

51-
const main = async () => {
52-
const { cityName, cityId } = determineArgs("add-city", process.argv.slice(2))
53-
.mapErr((err) => new Error(`Argument error: ${err}`))
54-
.unwrap();
45+
async function main() {
46+
const { cityName, cityId } = determineArgs("add-city", process.argv.slice(2));
5547

56-
(
57-
await updateCoordinates(
58-
"add-city",
59-
cityId,
60-
true,
61-
"packages/primary/data/city-boundaries.geojson",
62-
"city-update.geojson",
63-
)
64-
).unwrap();
48+
await updateCoordinates(
49+
"add-city",
50+
cityId,
51+
true,
52+
"packages/primary/data/city-boundaries.geojson",
53+
"city-update.geojson",
54+
);
6555

66-
(
67-
await updateParkingLots(
68-
cityId,
69-
true,
70-
"parking-lots-update.geojson",
71-
`packages/primary/data/parking-lots/${cityId}.geojson`,
72-
)
73-
).unwrap();
56+
await updateParkingLots(
57+
cityId,
58+
true,
59+
"parking-lots-update.geojson",
60+
`packages/primary/data/parking-lots/${cityId}.geojson`,
61+
);
7462

75-
(await addScoreCard(cityId, cityName)).unwrap();
63+
await addScoreCard(cityId, cityName);
7664

77-
/* eslint-disable-next-line no-console */
7865
console.log(
7966
`Almost done! Now, fill in the score card values in packages/primary/data/city-stats.json. Then,
80-
run 'npm run fmt'. Then, 'npm start' and see if the site is what you expect.
67+
run 'pnpm fmt'. Then, start the server and see if the site is what you expect.
8168
`,
8269
);
83-
};
70+
}
8471

8572
main();

packages/scripts/src/base.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from "fs/promises";
22

3-
import results from "ts-results";
43
import {
54
FeatureCollection,
65
GeoJsonProperties,
@@ -11,42 +10,42 @@ import {
1110
import { parseCityIdFromJson } from "@prn-parking-lots/shared/src/js/model/cityId.ts";
1211
import type { CityId } from "@prn-parking-lots/shared/src/js/model/types";
1312

14-
const determineArgs = (
13+
export function determineArgs(
1514
scriptCommand: string,
1615
processArgv: string[],
17-
): results.Result<{ cityName: string; cityId: CityId }, string> => {
16+
): { cityName: string; cityId: CityId } {
1817
if (processArgv.length !== 1) {
19-
return new results.Err(
18+
throw new Error(
2019
`Must provide exactly one argument (the city/state name). For example,
2120
npm run ${scriptCommand} -- 'Columbus, OH'
2221
`,
2322
);
2423
}
2524
const cityName = processArgv[0];
2625
const cityId = parseCityIdFromJson(cityName);
27-
return results.Ok({ cityName, cityId });
28-
};
26+
return { cityName, cityId };
27+
}
2928

30-
const updateCoordinates = async (
29+
export async function updateCoordinates(
3130
scriptCommand: string,
3231
cityId: CityId,
3332
addCity: boolean,
3433
originalFilePath: string,
3534
updateFilePath: string,
36-
): Promise<results.Result<string, string>> => {
35+
): Promise<void> {
3736
let newData: FeatureCollection<Polygon, GeoJsonProperties>;
3837
try {
3938
const rawNewData = await fs.readFile(updateFilePath, "utf8");
4039
newData = JSON.parse(rawNewData);
4140
} catch (err: unknown) {
4241
const { message } = err as Error;
43-
return results.Err(
42+
throw new Error(
4443
`Issue reading the update file path ${updateFilePath}: ${message}`,
4544
);
4645
}
4746

4847
if (!Array.isArray(newData.features) || newData.features.length !== 1) {
49-
return results.Err(
48+
throw new Error(
5049
"The script expects exactly one entry in `features` because you can only update one city at a time.",
5150
);
5251
}
@@ -61,7 +60,7 @@ const updateCoordinates = async (
6160
originalData = JSON.parse(rawOriginalData);
6261
} catch (err: unknown) {
6362
const { message } = err as Error;
64-
return results.Err(
63+
throw new Error(
6564
`Issue reading the original data file path ${originalFilePath}: ${message}`,
6665
);
6766
}
@@ -78,7 +77,7 @@ const updateCoordinates = async (
7877
(feature) => feature?.properties?.id === cityId,
7978
);
8079
if (!cityOriginalData) {
81-
return results.Err(
80+
throw new Error(
8281
`City not found in ${originalFilePath}. To add a new city, run again with the '--add' flag, e.g. npm run ${scriptCommand} -- 'My City, AZ' --add`,
8382
);
8483
}
@@ -97,28 +96,27 @@ const updateCoordinates = async (
9796
});
9897

9998
await fs.writeFile(originalFilePath, JSON.stringify(originalData, null, 2));
100-
return results.Ok("File updated successfully!");
101-
};
99+
}
102100

103-
const updateParkingLots = async (
101+
export async function updateParkingLots(
104102
cityId: CityId,
105103
addCity: boolean,
106104
originalFilePath: string,
107105
updateFilePath: string,
108-
): Promise<results.Result<string, string>> => {
106+
): Promise<void> {
109107
let newData;
110108
try {
111109
const rawNewData = await fs.readFile(originalFilePath, "utf8");
112110
newData = JSON.parse(rawNewData);
113111
} catch (err: unknown) {
114112
const { message } = err as Error;
115-
return results.Err(
113+
throw new Error(
116114
`Issue reading the update file path parking-lots-update.geojson: ${message}`,
117115
);
118116
}
119117

120118
if (!Array.isArray(newData.features) || newData.features.length !== 1) {
121-
return results.Err(
119+
throw new Error(
122120
"The script expects exactly one entry in `features` because you can only update one city at a time.",
123121
);
124122
}
@@ -133,23 +131,20 @@ const updateParkingLots = async (
133131
originalData = JSON.parse(rawOriginalData);
134132
} catch (err: unknown) {
135133
const { message } = err as Error;
136-
return results.Err(
134+
throw new Error(
137135
`Issue reading the original data file path ${updateFilePath}: ${message}`,
138136
);
139137
}
140138
originalData.geometry.coordinates = newCoordinates;
141139

142140
await fs.writeFile(updateFilePath, JSON.stringify(originalData, null, 2));
143-
return results.Ok("File updated successfully!");
141+
return;
144142
}
143+
145144
const newFile = {
146145
type: "Feature",
147146
properties: { id: cityId },
148147
geometry: { type: newGeometryType, coordinates: newCoordinates },
149148
};
150149
await fs.writeFile(updateFilePath, JSON.stringify(newFile, null, 2));
151-
152-
return results.Ok("File updated successfully!");
153-
};
154-
155-
export { determineArgs, updateCoordinates, updateParkingLots };
150+
}
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
import { determineArgs, updateCoordinates } from "./base.ts";
22

3-
const main = async (): Promise<void> => {
3+
async function main(): Promise<void> {
44
const { cityId } = determineArgs(
55
"update-city-boundaries",
66
process.argv.slice(2),
7-
)
8-
.mapErr((err) => new Error(`Argument error: ${err}`))
9-
.unwrap();
10-
const value = (
11-
await updateCoordinates(
12-
"update-city-boundaries",
13-
cityId,
14-
false,
15-
"packages/primary/data/city-boundaries.geojson",
16-
"city-update.geojson",
17-
)
18-
).unwrap();
7+
);
8+
await updateCoordinates(
9+
"update-city-boundaries",
10+
cityId,
11+
false,
12+
"packages/primary/data/city-boundaries.geojson",
13+
"city-update.geojson",
14+
);
1915

20-
/* eslint-disable-next-line no-console */
2116
console.log(
22-
`${value} Now, run 'npm run fmt'. Then, 'npm start' and
17+
`File updatad! Now, run 'pnpm fmt'. Then, start the server and
2318
see if the site is what you expect.
2419
`,
2520
);
26-
};
21+
}
2722

2823
main();
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import { determineArgs, updateParkingLots } from "./base.ts";
22

3-
const main = async (): Promise<void> => {
4-
const { cityId } = determineArgs("update-lots", process.argv.slice(2))
5-
.mapErr((err) => new Error(`Argument error: ${err}`))
6-
.unwrap();
7-
const value = (
8-
await updateParkingLots(
9-
cityId,
10-
false,
11-
"parking-lots-update.geojson",
12-
`packages/primary/data/parking-lots/${cityId}.geojson`,
13-
)
14-
).unwrap();
3+
async function main(): Promise<void> {
4+
const { cityId } = determineArgs("update-lots", process.argv.slice(2));
5+
await updateParkingLots(
6+
cityId,
7+
false,
8+
"parking-lots-update.geojson",
9+
`packages/primary/data/parking-lots/${cityId}.geojson`,
10+
);
1511

16-
/* eslint-disable-next-line no-console */
1712
console.log(
18-
`${value} Now, run 'npm run fmt'. Then, 'npm start' and
13+
`File updated! Now, run 'pnpm fmt'. Then, start the server and
1914
see if the site is what you expect.
2015
`,
2116
);
22-
};
17+
}
2318

2419
main();

0 commit comments

Comments
 (0)