Skip to content

Commit

Permalink
chore: refactor dev workflow (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue authored May 18, 2022
1 parent 89b10a6 commit 1abd87e
Show file tree
Hide file tree
Showing 42 changed files with 742 additions and 200 deletions.
5 changes: 1 addition & 4 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ parserOptions:
parser: "@typescript-eslint/parser"
ecmaVersion: 2018 # Allows for the parsing of modern ECMAScript features
project:
- "./packages/*/*/tsconfig.json"
- "./tsconfig.json"
- "./tsconfig.eslint.json"
ecmaFeatures:
jsx: true
extraFileExtensions:
Expand Down Expand Up @@ -77,9 +77,6 @@ rules:
["^\\."],
]
overrides:
- files:
- "**/tests/**/*"
- "**/__test__/*"
- files: "*.js"
rules:
"@typescript-eslint/no-var-requires": off
Expand Down
21 changes: 10 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ jobs:

- name: Build home
run: |
pnpm -F '@rino.app/home' build
pnpm build home
- name: Build editor playground
run: |
pnpm -F '@rino.app/playground' build
pnpm build playground
- name: Upload dist (home)
uses: actions/upload-artifact@v3
Expand All @@ -43,9 +43,9 @@ jobs:

- uses: ./.github/actions/dependencies

- name: Build electron dependencies
- name: Build packages
run: |
pnpm --filter '@rino.app/electron-*...' build
pnpm build electron-renderer electron-main electron-preload common editor
build_electron:
runs-on: macos-latest
Expand All @@ -61,13 +61,9 @@ jobs:
echo "$pkgjson" > ./packages/electron/package.json
cat ./packages/electron/package.json
- name: Build electron dependencies
run: |
pnpm --filter '@rino.app/electron-*...' build
- name: Build electron
run: |
pnpm --filter '@rino.app/electron' build:all
pnpx turbo run build:all --filter='electron'
env:
ELECTRON_APPLE_ID_NAME: ${{ secrets.ELECTRON_APPLE_ID_NAME }}
ELECTRON_APPLE_ID: ${{ secrets.ELECTRON_APPLE_ID }}
Expand Down Expand Up @@ -132,6 +128,9 @@ jobs:
- name: Lint
run: pnpm lint .

- name: Check typescript types
run: pnpm typecheck

preview: # Preview deployment
runs-on: ubuntu-latest
steps:
Expand All @@ -141,11 +140,11 @@ jobs:

- name: Build home
run: |
pnpm -F '@rino.app/home' build
pnpm build home
- name: Build editor playground
run: |
pnpm -F '@rino.app/playground' build
pnpm build playground
- id: step_branch
name: Get the branch name
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ nohup.out
.venv
*.tsbuildinfo
__img_snapshots__
.turbo

# yarn https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
.yarn/*
Expand Down
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
"version": "0.0.0",
"private": true,
"scripts": {
"lint": "eslint --ignore-path .gitignore --ext '.js,.jsx,.ts,.tsx'",
"fix": "eslint --ignore-path .gitignore --ext '.js,.jsx,.ts,.tsx' --fix .",
"dev": "rino-cli dev",
"build": "rino-cli build",
"test": "rino-cli test",
"lint": "eslint --ignore-path .gitignore --ext '.js,.jsx,.ts,.tsx,.mjs'",
"fix": "eslint --ignore-path .gitignore --ext '.js,.jsx,.ts,.tsx,.mjs' --fix .",
"postinstall": "./scripts/post-install.sh && husky install",
"test:coverage": "./scripts/test-coverge.sh",
"clean": "./scripts/clean.mjs"
"test:coverage": "./scripts/test-coverage.sh",
"clean": "rino-cli clean",
"typecheck": "tsc -b tsconfig.json"
},
"devDependencies": {
"@lhci/cli": "^0.9.0",
"@rino.app/cli": "workspace:*",
"@svgr/cli": "^6.2.1",
"@types/node": "^16.11.36",
"@typescript-eslint/eslint-plugin": "^5.25.0",
Expand All @@ -23,6 +28,7 @@
"eslint-plugin-simple-import-sort": "^7.0.0",
"husky": "^8.0.1",
"prettier": "^2.6.2",
"turbo": "^1.2.9",
"typescript": "^4.6.4"
},
"renovate": {
Expand Down Expand Up @@ -50,7 +56,6 @@
"dependencyDashboard": true
},
"pnpm": {
"overrides": {
}
"overrides": {}
}
}
5 changes: 5 additions & 0 deletions packages/cli/bin/rino-cli.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { run } from "../src/index.mjs"

run()
20 changes: 20 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@rino.app/cli",
"version": "0.0.0",
"private": true,
"type": "module",
"license": "GPL-3.0",
"bin": {
"rino-cli": "bin/rino-cli.mjs"
},
"devDependencies": {
"@types/lodash-es": "^4.17.6",
"commander": "^9.2.0",
"fast-glob": "^3.2.11",
"json5": "^2.2.1",
"lodash-es": "^4.17.21",
"merge-istanbul": "^1.1.4",
"monorepo-license-checker": "^0.3.0",
"nyc": "^15.1.0"
}
}
10 changes: 10 additions & 0 deletions packages/cli/src/commands/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { spawnSync } from "node:child_process"

import { findTurboBin } from "../utils/find-turbo-bin.mjs"

export function build(packages, { onlyDependencys = false } = {}) {
const turboBin = findTurboBin()
const args = ["run", "build", ...packages.map((name) => `--filter=${name}${onlyDependencys ? "^" : ""}...`)]
console.log("$", turboBin + " " + args.join(" "))
spawnSync(turboBin, args, { stdio: "inherit" })
}
37 changes: 37 additions & 0 deletions packages/cli/src/commands/clean.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import glob from "fast-glob"
import fs from "node:fs/promises"

import { findRoot } from "../utils/find-root.mjs"

const TO_CLEAN = [".next", "dist", "dist-types", ".cache", "coverage", "coverage-e2e", "out", "build", ".turbo"]
const TO_IGNORE = ["node_modules"]

async function cleanAll() {
const root = findRoot()

const dirs = [
...glob.sync(
TO_CLEAN.map((dir) => `**/${dir}/`),
{
ignore: TO_IGNORE.map((dir) => `**/${dir}/**`),
cwd: root,
onlyDirectories: true,
absolute: true,
},
),
...glob.sync("node_modules/.cache", {
cwd: root,
onlyDirectories: true,
absolute: true,
}),
]

for (const dir of dirs) {
await fs.rm(dir, { recursive: true, force: true })
console.log(`deleted ${dir}`)
}

console.log(`deleted ${dirs.length} items in total`)
}

export { cleanAll as clean }
12 changes: 12 additions & 0 deletions packages/cli/src/commands/dev.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { spawnSync } from "node:child_process"

import { findTurboBin } from "../utils/find-turbo-bin.mjs"
import { build } from "./build.mjs"

export function dev(packages) {
build(packages, { onlyDependencys: true })
const turboBin = findTurboBin()
const args = ["run", "dev", ...packages.map((name) => `--filter=${name}...`), "--parallel"]
console.log("$", turboBin + " " + args.join(" "))
spawnSync(turboBin, args, { stdio: "inherit" })
}
53 changes: 53 additions & 0 deletions packages/cli/src/commands/generate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import glob from "fast-glob"
import path from "node:path"

import { findRoot } from "../utils/find-root.mjs"
import { readJson } from "../utils/read-json.mjs"
import { writeJson } from "../utils/write-json.mjs"

async function generateTsconfig() {
const root = findRoot()
const tsconfigPaths = glob.sync("packages/*/*/tsconfig.json", { cwd: root, ignore: "**/node_modules/**" })
tsconfigPaths.sort()

const rootTsconfigPath = path.join(root, "tsconfig.json")
const rootTsconfigJson = { include: [], files: [], references: [] }

rootTsconfigJson.references = tsconfigPaths.map((tsconfigPath) => ({
path: tsconfigPath,
}))

await writeJson(rootTsconfigPath, rootTsconfigJson)

for (let tsconfigPath of glob.sync("packages/*/src/tsconfig.json", { cwd: root, ignore: "**/node_modules/**" })) {
const absPath = path.join(root, tsconfigPath)
const oldJson = await readJson(absPath)
const newJson = {
...oldJson,
compilerOptions: {
composite: true,
outDir: "../dist-types/",
...oldJson.compilerOptions,
},
}
await writeJson(absPath, newJson)
}

for (let tsconfigPath of glob.sync("packages/*/test/tsconfig.json", { cwd: root, ignore: "**/node_modules/**" })) {
const absPath = path.join(root, tsconfigPath)
const oldJson = await readJson(absPath)
const newJson = {
...oldJson,
compilerOptions: {
composite: true,
outDir: "./.cache/typescript/",
...oldJson.compilerOptions,
},
}
await writeJson(absPath, newJson)
}
}

export function generate() {
generateTsconfig()
}
24 changes: 24 additions & 0 deletions packages/cli/src/commands/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { spawnSync } from "node:child_process"
import path from "node:path"

import { findRoot } from "../utils/find-root.mjs"
import { findTurboBin } from "../utils/find-turbo-bin.mjs"

export function test(testPath) {
const root = findRoot()
const absTestPath = path.resolve(process.cwd(), testPath)
const packageName = path.relative(root, absTestPath).split(path.sep)[1]
const turboBin = findTurboBin()

if (packageName) {
const args = ["run", "test:vitest", `--filter=${packageName}`, "--", absTestPath]
console.log("$", turboBin + " " + args.join(" "))
spawnSync(turboBin, args, { stdio: "inherit", cwd: root })
} else if (root == absTestPath) {
const args = ["run", "test:vitest"]
console.log("$", turboBin + " " + args.join(" "))
spawnSync(turboBin, args, { stdio: "inherit", cwd: root })
} else {
throw new Error(`Test path ${absTestPath} must be inside a package`)
}
}
49 changes: 49 additions & 0 deletions packages/cli/src/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { program } from "commander"

import { build } from "./commands/build.mjs"
import { clean } from "./commands/clean.mjs"
import { dev } from "./commands/dev.mjs"
import { generate } from "./commands/generate.mjs"
import { test } from "./commands/test.mjs"

export function run() {
program
.command("clean")
.description("Clean all temporary files")
.action(() => {
clean()
})

program
.command("gen")
.description("Generate files")
.action(() => {
generate()
})

program
.command("dev")
.description("Run dev commands for selected packages")
.argument("<packages...>", "Package names to run")
.action((packages) => {
dev(packages)
})

program
.command("build")
.description("Run build commands for selected packages")
.argument("<packages...>", "Package names to run")
.action((packages) => {
build(packages)
})

program
.command("test")
.description("Test")
.argument("[path]", "File path or directory path", process.cwd())
.action((path) => {
test(path)
})

program.parse()
}
11 changes: 11 additions & 0 deletions packages/cli/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"outDir": "../dist-types/"
},
"include": [
"./"
],
"references": []
}
8 changes: 8 additions & 0 deletions packages/cli/src/utils/find-root.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { execSync } from "child_process"

/**
* Find the git repo root absolute path.
*/
export function findRoot() {
return execSync("git rev-parse --show-toplevel", { encoding: "utf-8" }).trim()
}
7 changes: 7 additions & 0 deletions packages/cli/src/utils/find-turbo-bin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import path from "node:path"

import { findRoot } from "./find-root.mjs"

export function findTurboBin() {
return path.resolve(findRoot(), "node_modules", ".bin", "turbo")
}
6 changes: 6 additions & 0 deletions packages/cli/src/utils/read-json.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import JSON5 from "json5"
import fs from "node:fs/promises"

export async function readJson(filePath) {
return JSON5.parse(await fs.readFile(filePath, { encoding: "utf-8" }))
}
Loading

0 comments on commit 1abd87e

Please sign in to comment.