Skip to content

Commit

Permalink
migrate scripts into workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher J Baker committed Jul 12, 2024
1 parent 8d76372 commit 1d058b3
Show file tree
Hide file tree
Showing 20 changed files with 480 additions and 205 deletions.
2 changes: 2 additions & 0 deletions .depcheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ ignore-dirs:
ignores:
- "@types/*"
- "@testing-library/jest-dom"
- jsdom
- typescript
- vite
- vite-plugin-dts
- vitest
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18
20
381 changes: 294 additions & 87 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"private": true,
"type": "module",
"homepage": "https://www.bitovi.com/open-source/react-to-web-component",
"namespace": "r2wc",
"workspaces": [
"packages/*"
],
"scripts": {
"check-packages": "ts-node ./scripts/check-packages",
"typecheck": "tsc --noEmit ; nx run-many -t typecheck",
"eslint": "eslint . ; nx run-many -t eslint",
"prettier": "prettier --check . ; nx run-many -t prettier",
"depcheck": "depcheck . ; nx run-many -t depcheck",
"check-packages": "nx run scripts:check-packages",
"typecheck": "nx run-many -t typecheck",
"eslint": "nx run-many -t eslint",
"prettier": "nx run-many -t prettier",
"depcheck": "nx run-many -t depcheck",
"clean": "nx run-many -t clean",
"build": "nx run-many -t build",
"reset": "nx run-many -t clean && nx reset"
Expand All @@ -23,8 +24,6 @@
"jsdom": "^22.0.0",
"nx": "19.4.2",
"prettier": "^3.3.2",
"semver": "^7.5.0",
"ts-node": "^10.9.1",
"typescript": "^5.5.3",
"vite": "^5.3.3",
"vite-plugin-dts": "^3.9.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@
"test:coverage": "vitest run --coverage",
"clean": "rm -rf tsconfig.tsbuildinfo dist",
"build": "vite build"
},
"devDependencies": {
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0"
}
}
5 changes: 1 addition & 4 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"jsx": "react-jsx"
},
"extends": "../../tsconfig.react.json",
"include": ["src"]
}
4 changes: 0 additions & 4 deletions packages/legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@
"dependencies": {
"@r2wc/core": "^1.0.0"
},
"devDependencies": {
"@types/react": "^16.0.0",
"@types/react-dom": "^16.0.0"
},
"peerDependencies": {
"react": "^16.0.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0"
Expand Down
5 changes: 1 addition & 4 deletions packages/legacy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"jsx": "react-jsx"
},
"extends": "../../tsconfig.react.json",
"include": ["src"]
}
5 changes: 1 addition & 4 deletions packages/react-to-web-component/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"jsx": "react-jsx"
},
"extends": "../../tsconfig.react.json",
"include": ["src"]
}
20 changes: 20 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "scripts",
"private": "true",
"version": "0.0.0",
"type": "module",
"scripts": {
"script": "NODE_OPTIONS=\"--no-warnings --import ./register.js\" node",
"check-packages": "npm run script -- ./src/check-packages.ts",
"typecheck": "tsc --noEmit",
"eslint": "eslint .",
"prettier": "prettier --check .",
"depcheck": "depcheck .",
"clean": "rm -rf tsconfig.tsbuildinfo dist"
},
"dependencies": {
"glob": "^11.0.0",
"semver": "^7.6.2",
"ts-node": "^10.9.2"
}
}
4 changes: 4 additions & 0 deletions packages/scripts/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { register } from "node:module"
import { pathToFileURL } from "node:url"

register("ts-node/esm/transpile-only", pathToFileURL("./"))
66 changes: 66 additions & 0 deletions packages/scripts/src/check-packages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import semver from "semver"

import project from "./lib/project.js"
import workspaces, { Package } from "./lib/workspaces.js"

for (const workspace of Object.values(workspaces)) {
log(workspace, "Processing.")

if ("private" in workspace) {
if (workspace.private === false) {
error(workspace, `Remove "private: false".`)
} else {
warn(workspace, `Private.`)
}
}

if (!workspace.private && workspace.directory !== "legacy") {
const nameSplit = workspace.name.match(/(?:@([^/]+)\/)?([^/]+)/)

if (!nameSplit) {
error(
workspace,
`Package name must be in the format @<namespace>/<package>.`,
)
} else {
if (nameSplit[1] !== project.namespace) {
error(
workspace,
`Package must be in the "${project.namespace}" namespace.`,
)
}

if (nameSplit[2] !== workspace.directory) {
error(workspace, `Package name and directory must match.`)
}
}
}

if (!semver.valid(workspace.version)) {
error(workspace, `Invalid version: ${workspace.version}`)
}

if (workspace.dependencies) {
for (const [name, range] of Object.entries(workspace.dependencies)) {
if (workspaces[name]) {
if (!semver.satisfies(workspaces[name].version, range)) {
error(workspace, `Unsatisfiable package: ${name}@${range}`)
}
}
}
}
}

/* eslint-disable no-console */
function log({ directory }: Package, message: string) {
console.log(`${directory} - ${message}`)
}

function warn({ directory }: Package, message: string) {
console.warn(`${directory} - ${message}`)
}

function error({ directory }: Package, message: string) {
console.error(`${directory} - ${message}`)
process.exitCode = 1
}
10 changes: 10 additions & 0 deletions packages/scripts/src/lib/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fs from "node:fs"
import path from "node:path"

export const dirname = path.join(import.meta.dirname, "..", "..", "..", "..")

const packagePath = path.join(dirname, "package.json")
const packageContents = fs.readFileSync(packagePath, "utf-8")
const packageData = JSON.parse(packageContents)

export default packageData
44 changes: 44 additions & 0 deletions packages/scripts/src/lib/workspaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fs from "node:fs"
import path from "node:path"

import { globSync } from "glob"

import project, { dirname } from "./project.js"

interface BarePackage {
name: string
private?: boolean
version: string
dependencies?: Record<string, string>
devDependencies?: Record<string, string>
}

export interface Package extends BarePackage {
dirname: string
directory: string
}

const list = globSync(project.workspaces, { cwd: dirname })
.map((workspace) => {
const split = workspace.split("/")
const directory = split[split.length - 1]

return {
dirname: workspace,
directory,
pkgPath: path.join(dirname, workspace, "package.json"),
}
})
.filter(({ pkgPath }) => fs.existsSync(pkgPath))
.map(({ dirname, directory, pkgPath }) => ({
dirname,
directory,
...(JSON.parse(fs.readFileSync(pkgPath, "utf-8")) as BarePackage),
}))

const workspaces: Record<string, Package> = {}
export default workspaces

for (const pkg of list) {
workspaces[pkg.name] = pkg
}
4 changes: 4 additions & 0 deletions packages/scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.node.json",
"include": ["src"]
}
54 changes: 0 additions & 54 deletions scripts/check-packages.ts

This file was deleted.

35 changes: 0 additions & 35 deletions scripts/lib/packages.ts

This file was deleted.

5 changes: 0 additions & 5 deletions tsconfig.json → tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "bundler",
"target": "ES2020",
"lib": ["DOM", "DOM.Iterable", "ESNext"],

"declaration": true,
"composite": true,
"incremental": true,
Expand Down
12 changes: 12 additions & 0 deletions tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2022",
"lib": ["ES2022"]
},
"ts-node": {
"esm": true
}
}
10 changes: 10 additions & 0 deletions tsconfig.react.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "bundler",
"target": "ES2020",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"jsx": "react-jsx"
}
}

0 comments on commit 1d058b3

Please sign in to comment.