Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: use zx to improve scripting dev experience #271

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions @app/db/.gmrc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"_": "command",
"shadow": true,
// NOTE: this script does nothing when envvar `IN_TESTS` is `1`
"command": "node scripts/dump-db.js"
"command": "zx scripts/dump-db.mjs"
}
],

Expand All @@ -93,7 +93,7 @@
"_": "command",
"shadow": true,
// NOTE: this script does nothing unless envvar `IN_TESTS` is `1`
"command": "node scripts/test-seed.js"
"command": "zx scripts/test-seed.mjs"
}
],

Expand Down
4 changes: 3 additions & 1 deletion @app/db/scripts/dump-db.js → @app/db/scripts/dump-db.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { spawn } = require("child_process");
#!/usr/bin/env zx

import { spawn } from "child_process";

if (process.env.IN_TESTS === "1") {
process.exit(0);
Expand Down
12 changes: 8 additions & 4 deletions @app/db/scripts/test-seed.js → @app/db/scripts/test-seed.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const { writeFile } = require("fs").promises;
const pg = require("pg");
#!/usr/bin/env zx

import pg from "pg";
import { fs } from "zx";

const { Pool } = pg;

if (process.env.IN_TESTS !== "1") {
process.exit(0);
Expand All @@ -10,10 +14,10 @@ async function main() {
if (!connectionString) {
throw new Error("GM_DBURL not set!");
}
const pgPool = new pg.Pool({ connectionString });
const pgPool = new Pool({ connectionString });
try {
await pgPool.query("delete from graphile_worker.jobs;");
await writeFile(
await fs.writeFile(
`${__dirname}/../__tests__/jest.watch.hack.ts`,
`export const ts = ${Date.now()};\n`
);
Expand Down
4 changes: 2 additions & 2 deletions docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"version": "0.0.0",
"scripts": {
"setup": "node ./scripts/yarn-setup.js",
"setup": "zx ./scripts/yarn-setup.mjs",
"start": "yarn compose up server",
"bash": "yarn compose exec server bash",
"dev": "yarn compose:exec:dev bash",
Expand All @@ -13,7 +13,7 @@
"db:up": "yarn compose up -d db",
"compose": "docker-compose -f ../docker-compose.yml",
"compose:exec:dev": "yarn down && yarn compose up -d dev && yarn compose exec dev ",
"reset:volumes": "node ./scripts/clean-volumes.js",
"reset:volumes": "zx ./scripts/clean-volumes.mjs",
"rebuild": "yarn compose build",
"down": "yarn compose down --remove-orphans"
},
Expand Down
17 changes: 0 additions & 17 deletions docker/scripts/clean-volumes.js

This file was deleted.

14 changes: 14 additions & 0 deletions docker/scripts/clean-volumes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env zx

const { basename, dirname, resolve } = path;

const projectName = basename(dirname(resolve(__dirname, ".."))).replace(
/[^a-z0-9]/g,
""
);

try {
await $`docker volume rm ${projectName}_vscode-extensions ${projectName}_devcontainer_db-volume ${projectName}_devcontainer_node_modules-volume ${projectName}_devcontainer_vscode-extensions`;
} catch (e) {
/* noop */
}
28 changes: 14 additions & 14 deletions docker/scripts/yarn-setup.js → docker/scripts/yarn-setup.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { runSync } = require("../../scripts/lib/run");
const { basename, dirname, resolve } = require("path");
const platform = require("os").platform();
const { safeRandomString } = require("../../scripts/lib/random");
const fs = require("fs");
const fsp = fs.promises;
#!/usr/bin/env zx

import { $, fs, path } from "zx";
import { safeRandomString } from "../../scripts/lib/random.mjs";

const { basename, dirname, resolve } = path;
const platform = os.platform();
const DOCKER_DOTENV_PATH = `${__dirname}/../.env`;

if (platform !== "win32" && !process.env.UID) {
Expand All @@ -17,7 +17,7 @@ if (platform !== "win32" && !process.env.UID) {
async function main() {
// Check that docker/.env exists
try {
await fsp.access(DOCKER_DOTENV_PATH, fs.constants.F_OK);
await fs.access(DOCKER_DOTENV_PATH, fs.constants.F_OK);
} catch (e) {
// Does not exist, write it
const password = safeRandomString(30);
Expand Down Expand Up @@ -46,7 +46,7 @@ POSTGRES_PASSWORD=${password}
DATABASE_HOST=db
ROOT_DATABASE_URL=postgres://postgres:${password}@db/postgres
`;
await fsp.writeFile(DOCKER_DOTENV_PATH, data);
await fs.writeFile(DOCKER_DOTENV_PATH, data);
}

// The `docker-compose` project name defaults to the directory name containing
Expand All @@ -58,30 +58,30 @@ ROOT_DATABASE_URL=postgres://postgres:${password}@db/postgres
// On Windows we must run 'yarn.cmd' rather than 'yarn'
const yarnCmd = platform === "win32" ? "yarn.cmd" : "yarn";

runSync(yarnCmd, ["down"]);
runSync(yarnCmd, ["db:up"]);
await $`${yarnCmd} down`;
await $`${yarnCmd} db:up`;

// Fix permissions
runSync(yarnCmd, [
await $`${yarnCmd} ${[
"compose",
"run",
"server",
"sudo",
"bash",
"-c",
"chmod o+rwx /var/run/docker.sock && chown -R node /work/node_modules /work/@app/*/node_modules",
]);
]}`;

// Run setup as normal
runSync(yarnCmd, [
await $`${yarnCmd} ${[
"compose",
"run",
"-e",
`PROJECT_NAME=${projectName}`,
"server",
"yarn",
"setup",
]);
]}`;
}

main().catch((e) => {
Expand Down
35 changes: 14 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
"description": "Description of project here",
"scripts": {
"setup": "yarn && yarn setup:env auto && yarn setup:db && yarn setup:packages",
"setup:env": "node ./scripts/setup_env.js",
"setup:db": "node ./scripts/setup_db.js",
"setup:env": "zx ./scripts/setup_env.mjs",
"setup:db": "zx ./scripts/setup_db.mjs",
"setup:packages": "lerna run setup",
"start": "node ./scripts/start.js",
"start": "zx ./scripts/start.mjs",
"pretest": "lerna run pretest",
"test": "node scripts/test.js",
"test": "zx scripts/test.mjs",
"posttest": "lerna run posttest",
"test:watch": "node scripts/test.js --watch",
"test:watch": "zx scripts/test.mjs --watch",
"lint": "tsc -b && yarn prettier:all --check && yarn eslint .",
"lint:fix": "yarn eslint --fix . && yarn prettier:all --write",
"eslint": "eslint --ext .js,.jsx,.ts,.tsx,.graphql",
"prettier:all": "prettier --ignore-path .eslintignore \"**/*.{js,jsx,ts,tsx,graphql,md}\"",
"eslint": "eslint --ext .js,.mjs,.jsx,.ts,.tsx,.graphql",
"prettier:all": "prettier --ignore-path .eslintignore \"**/*.{js,mjs,jsx,ts,tsx,graphql,md}\"",
"depcheck": "lerna exec --stream \"yarn depcheck --ignores=\"@app/config,@app/client,tslib,webpack,babel-plugin-import,source-map-support,@graphql-codegen/*,*eslint*,@typescript-eslint/*,graphql-toolkit,net,tls,dayjs\" --ignore-dirs=\".next\"\"",
"dev": "yarn && lerna run codegen --stream && tsc -b && concurrently --kill-others --names \"TSC,WATCH,RUN,TEST\" --prefix \"({name})\" --prefix-colors \"yellow.bold,yellow.bold,cyan.bold,greenBright.bold\" \"tsc -b --watch --preserveWatchOutput\" \"lerna run --parallel watch\" \"lerna run --parallel dev\" \"yarn test:watch --delay 10\"",
"build": "lerna run build",
"licenses": "yarn --silent licenses generate-disclaimer > LICENSES.md",
"clean": "node ./scripts/clean.js",
"reset": "yarn clean && node ./scripts/delete-env-file.js",
"clean": "zx ./scripts/clean.mjs",
"reset": "yarn clean && zx ./scripts/delete-env-file.mjs",
"--shortcuts to run commands in workspaces--": "",
"client": "yarn workspace @app/client",
"components": "yarn workspace @app/components",
Expand All @@ -41,7 +41,8 @@
"abort-controller": "^3.0.0",
"graphql": "^15.4.0",
"lerna": "^4.0.0",
"string-width": "^5.0.0"
"string-width": "^5.0.0",
"zx": "^4.1.1"
},
"devDependencies": {
"@babel/core": "^7.14.3",
Expand Down Expand Up @@ -83,23 +84,15 @@
"pg-connection-string": "2.x"
},
"workspaces": {
"packages": [
"@app/*",
"docker"
],
"nohoist": [
"**/cypress"
]
"packages": ["@app/*", "docker"],
"nohoist": ["**/cypress"]
},
"prettier": {
"trailingComma": "es5",
"proseWrap": "always",
"overrides": [
{
"files": [
"*.yml",
"*.yaml"
],
"files": ["*.yml", "*.yaml"],
"options": {
"printWidth": 120
}
Expand Down
35 changes: 15 additions & 20 deletions scripts/_setup_utils.js → scripts/_setup_utils.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { fs, os } from "zx";

import { readDotenv, withDotenvUpdater } from "./lib/dotenv.mjs";
import { safeRandomString } from "./lib/random.mjs";

if (parseInt(process.version.split(".")[0], 10) < 10) {
throw new Error("This project requires Node.js >= 10.0.0");
}

const fsp = require("fs").promises;
const { runSync } = require("./lib/run");
const { withDotenvUpdater, readDotenv } = require("./lib/dotenv");
const { safeRandomString } = require("./lib/random");

// fixes runSync not throwing ENOENT on windows
const platform = require("os").platform();
const yarnCmd = platform === "win32" ? "yarn.cmd" : "yarn";
export const yarnCmd = os.platform() === "win32" ? "yarn.cmd" : "yarn";

const projectName = process.env.PROJECT_NAME;
export const projectName = process.env.PROJECT_NAME;

exports.withDotenvUpdater = withDotenvUpdater;
exports.readDotenv = readDotenv;
exports.runSync = runSync;
exports.yarnCmd = yarnCmd;
exports.projectName = projectName;
export { readDotenv, withDotenvUpdater };

exports.updateDotenv = function updateDotenv(add, answers) {
export function updateDotenv(add, answers) {
add(
"GRAPHILE_LICENSE",
null,
Expand Down Expand Up @@ -142,11 +137,11 @@ exports.updateDotenv = function updateDotenv(add, answers) {
# The name of the folder you cloned graphile-starter to (so we can run docker-compose inside a container):`
);
}
};
}

exports.checkGit = async function checkGit() {
export async function checkGit() {
try {
const gitStat = await fsp.stat(`${__dirname}/../.git`);
const gitStat = await fs.stat(`${__dirname}/../.git`);
if (!gitStat || !gitStat.isDirectory()) {
throw new Error("No .git folder found");
}
Expand All @@ -170,16 +165,16 @@ exports.checkGit = async function checkGit() {
console.error();
process.exit(1);
}
};
}

exports.runMain = (main) => {
export const runMain = (main) => {
main().catch((e) => {
console.error(e);
process.exit(1);
});
};

exports.outro = (message) => {
export const outro = (message) => {
console.log();
console.log();
console.log("____________________________________________________________");
Expand Down
11 changes: 0 additions & 11 deletions scripts/clean.js

This file was deleted.

14 changes: 14 additions & 0 deletions scripts/clean.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node

import { $ } from "zx";

void (async function () {
try {
await $`rm -rf ${__dirname}/../@app/*/dist`;
await $`rm -rf ${__dirname}/../@app/*/tsconfig.tsbuildinfo`;
await $`rm -rf ${__dirname}/../@app/client/.next`;
} catch (e) {
console.error("Failed to clean up");
console.error(e);
}
})();
8 changes: 0 additions & 8 deletions scripts/delete-env-file.js

This file was deleted.

11 changes: 11 additions & 0 deletions scripts/delete-env-file.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node

import { fs } from "zx";

void (async function () {
try {
await fs.unlink(`${__dirname}/../.env`);
} catch (e) {
/* NOOP */
}
})();
Loading