Skip to content

Commit

Permalink
Move a couple scripts to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbmatter committed Apr 20, 2021
1 parent 0e98fac commit c11da9f
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 115 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
"project": "./tsconfig.json",
"extraFileExtensions": [".mjs"]
},
"env": {
"browser": true,
Expand Down
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rollup exports package.json field
ESM tools
- when done set module: true in package.json and change back to .js

disable injuries without god mode before league? https://discord.com/channels/@me/831754887058423819/833206526780768277

Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
},
"scripts": {
"start": "node tools/server.js",
"build": "node tools/build.js",
"deploy": "yarn run lint && yarn test && node tools/deploy.js",
"build": "node tools/build.mjs",
"deploy": "yarn run lint && yarn test && node tools/deploy.mjs",
"deploy-all": "yarn run lint && yarn test && node tools/deploy-all.js",
"start-watch": "concurrently --kill-others \"yarn run start\" \"yarn run watch\"",
"watch": "node tools/watch.js",
"lint": "concurrently --raw \"yarn run lint-js\" \"yarn run lint-ts\"",
"lint-js": "eslint \"*.{js,ts,tsx}\" \"public/*.js\" \"src/**/*.{js,ts,tsx}\" \"tools/**/*.{js,ts,tsx}\"",
"lint-ts": "node tools/pre-test.js && yarn run tsc",
"prettier": "prettier --write \"*.{js,json,scss,ts,tsx}\" \"public/**/*.{js,json,scss,ts,tsx}\" \"src/**/*.{js,json,scss,ts,tsx}\" \"tools/**/*.{js,json,scss,ts,tsx}\"",
"test": "node tools/pre-test.js && cross-env SPORT=basketball jest --testPathIgnorePatterns football && cross-env SPORT=football jest --testRegex \"football.*\\.test\\.[jt]sx?\"",
"test-e2e": "node tools/pre-test.js && karma start karma.conf.js",
"test-e2e-watch": "node tools/pre-test.js && karma start karma.conf.js --auto-watch --no-single-run",
"test-e2e-browserstack": "node tools/pre-test.js && karma start karma-browserstack.conf.js",
"lint-js": "eslint \"*.{mjs,js,ts,tsx}\" \"public/*.js\" \"src/**/*.{mjs,js,ts,tsx}\" \"tools/**/*.{mjs,js,ts,tsx}\"",
"lint-ts": "node tools/pre-test.mjs && yarn run tsc",
"prettier": "prettier --write \"*.{mjs,js,json,scss,ts,tsx}\" \"public/**/*.{mjs,js,json,scss,ts,tsx}\" \"src/**/*.{mjs,js,json,scss,ts,tsx}\" \"tools/**/*.{mjs,js,json,scss,ts,tsx}\"",
"test": "node tools/pre-test.mjs && cross-env SPORT=basketball jest --testPathIgnorePatterns football && cross-env SPORT=football jest --testRegex \"football.*\\.test\\.[jt]sx?\"",
"test-e2e": "node tools/pre-test.mjs && karma start karma.conf.js",
"test-e2e-watch": "node tools/pre-test.mjs && karma start karma.conf.js --auto-watch --no-single-run",
"test-e2e-browserstack": "node tools/pre-test.mjs && karma start karma-browserstack.conf.js",
"postinstall": "patch-package"
},
"lint-staged": {
Expand Down
14 changes: 1 addition & 13 deletions src/worker/core/realRosters/getLeagueInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@ import assert from "assert";
import { PHASE } from "../../../common";
import getLeagueInfo from "./getLeagueInfo";
import * as fs from "fs";
import * as path from "path";

describe("worker/core/realRosters/getLeagueInfo", () => {
let originalFetch: any;
beforeAll(() => {
const realPlayerData = JSON.parse(
fs.readFileSync(
path.join(
__dirname,
"..",
"..",
"..",
"..",
"data",
"real-player-data.basketball.json",
),
"utf8",
),
fs.readFileSync("data/real-player-data.basketball.json", "utf8"),
);
originalFetch = global.fetch;
(global as any).fetch = async () => {
Expand Down
3 changes: 0 additions & 3 deletions tools/build.js

This file was deleted.

3 changes: 3 additions & 0 deletions tools/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import build from "./lib/build.js";

await build();
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const cheerio = require("cheerio");
const fs = require("fs");
const path = require("path");
import cheerio from "cheerio";
import fs from "fs";
import path from "path";

// Scrape all https://www.basketball-reference.com/friv/colleges.fcgi?college=acadia
const folder = "/media/external/BBGM/basketball-colleges/html";
Expand Down
File renamed without changes.
10 changes: 0 additions & 10 deletions tools/deploy-all.js

This file was deleted.

8 changes: 8 additions & 0 deletions tools/deploy-all.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import deploy from "./lib/deploy.mjs";

const sports = ["basketball", "football", "hockey"];

for (const sport of sports) {
process.env.SPORT = sport;
await deploy();
}
3 changes: 0 additions & 3 deletions tools/deploy.js

This file was deleted.

3 changes: 3 additions & 0 deletions tools/deploy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import deploy from "./lib/deploy.mjs";

await deploy();
56 changes: 0 additions & 56 deletions tools/esbuild.js

This file was deleted.

56 changes: 56 additions & 0 deletions tools/esbuild.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import esbuild from "esbuild";
import alias from "esbuild-plugin-alias";
import path from "path";
import getSport from "./lib/getSport.js";

const __dirname = path.dirname(new URL(import.meta.url).pathname);

/**
* Currently this is not used for anything. Eventually maybe it can replace the current rollup build script. Would need to do:
*
* - Figure out what to do with Karma and Jest
* - Run pluginSportFunctions on both isSport and bySport
* - Do something about extra rollup plugins (like blacklist)
* - Compare bundle size (maybe still use terser)
* - Figure out what targets to use for the prod/legacy builds, and confirm it all works
* - Might not support all the features needed for legacy
* - https://esbuild.github.io/content-types/#javascript
* - https://github.com/evanw/esbuild/issues/297
* - Could still use babel on output
*/

const sport = getSport();

const names = ["ui", "worker"];
await Promise.all(
names.map(async name => {
await esbuild.build({
entryPoints: [`src/${name}/index.${name === "ui" ? "tsx" : "ts"}`],
bundle: true,
sourcemap: true,
inject: ["tools/lib/react-shim.js"],
define: {
"process.env.NODE_ENV": '"development"',
"process.env.SPORT": JSON.stringify(sport),
},
outfile: `build/gen/${name}.js`,
plugins: [
// Not sure why this is required, docs say it should pick up on tsconfig.json settings
alias({
"player-names": path.join(
__dirname,
"../src/worker/data/names-test.json",
),
"league-schema": path.join(
__dirname,
"../build/files/league-schema.json",
),
"bbgm-polyfills": path.join(
__dirname,
"../src/common/polyfills-noop.ts",
),
}),
],
});
}),
);
16 changes: 9 additions & 7 deletions tools/lib/deploy.js → tools/lib/deploy.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { spawn } = require("child_process");
const cloudflare = require("cloudflare");
const build = require("./build");
const buildFuncs = require("./buildFuncs");
const getSport = require("./getSport");
const cloudflareConfig = require("../../../../.config/cloudflare.json"); // eslint-disable-line import/no-unresolved
import { spawn } from "child_process";
import cloudflare from "cloudflare";
import { readFile } from 'fs/promises';
import build from "./build.js";
import buildFuncs from "./buildFuncs.js";
import getSport from "./getSport.js";

const getSubdomain = () => {
if (process.argv[2] === "beta" || process.argv[2] === "play") {
Expand Down Expand Up @@ -33,6 +33,8 @@ const mySpawn = (command, args) => {
};

const deploy = async () => {
const cloudflareConfig = JSON.parse(await readFile(new URL("../../../../.config/cloudflare.json", import.meta.url)));

await build();

const subdomain = getSubdomain();
Expand Down Expand Up @@ -110,4 +112,4 @@ const deploy = async () => {
console.log("\nDone!");
};

module.exports = deploy;
export default deploy;
4 changes: 2 additions & 2 deletions tools/pre-test.js → tools/pre-test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs");
const generateJSONSchema = require("./lib/generateJSONSchema");
import fs from "fs";
import generateJSONSchema from "./lib/generateJSONSchema.js";

const jsonSchema = generateJSONSchema("test");
fs.mkdirSync("build/files", { recursive: true });
Expand Down
21 changes: 17 additions & 4 deletions tools/races.js → tools/races.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { csvParse } = require("d3-dsv");
const fs = require("fs");
const path = require("path");
import { csvParse } from "d3-dsv";
import fs from "fs";
import path from "path";

const __dirname = path.dirname(new URL(import.meta.url).pathname);

const csv = fs.readFileSync(path.join(__dirname, "races.csv"), "utf8");
const rows = csvParse(csv);
Expand All @@ -20,6 +22,7 @@ for (const row of rows) {

let code = `// This file is generated by tools/races.js
import { isSport } from "../../common";
import type { Race } from "../../common/types";
const defaultRaces: Record<string, Record<Race, number>> = ${JSON.stringify(
Expand All @@ -28,6 +31,17 @@ const defaultRaces: Record<string, Record<Race, number>> = ${JSON.stringify(
"\t",
)};
if (isSport("hockey")) {
defaultRaces.USA = {
asian: 2,
black: 2,
brown: 3,
white: 93,
};
defaultRaces.Germany = defaultRaces.USA;
defaultRaces.Spain = defaultRaces.USA;
}
`;

// Second pass - get the other countries based on templates
Expand All @@ -39,7 +53,6 @@ for (const row of rows) {

code += `\nexport default defaultRaces;\n`;

console.log(code);
const filename = path.join(__dirname, "../src/worker/data/defaultRaces.ts");
fs.writeFileSync(filename, code);
console.log(`Wrote data to ${filename}`);
8 changes: 5 additions & 3 deletions tools/server.js → tools/server.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fs = require("fs");
const http = require("http");
const path = require("path");
import fs from "fs";
import http from "http";
import path from "path";

const __dirname = path.dirname(new URL(import.meta.url).pathname);

const port = 3000;

Expand Down

0 comments on commit c11da9f

Please sign in to comment.