Skip to content

Commit

Permalink
feat: update package.json types to point to libs/index.d.ts
Browse files Browse the repository at this point in the history
This commit updates the `package.json` files in all workspace projects to point the `types` field to `./libs/index.d.ts`.

This change ensures that TypeScript can correctly resolve type definitions for the workspace projects.

Additionally, the `prepare` script has been removed as it is no longer necessary.
  • Loading branch information
DavidKk committed May 8, 2024
1 parent b9735f3 commit dbfb3b7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
"scripts": {
"commit": "git-cz",
"ci:pages": "shx rm -rf ./gh-pages && mkdir -p ./gh-pages && dumi build",
"ci:coverage": "pnpm compile && pnpm test",
"ci:build": "pnpm compile",
"ci:coverage": "pnpm run compile && pnpm run test",
"ci:build": "pnpm run compile",
"postinstall": "node ./public/scripts/postinstall.mjs",
"preinstall": "node ./public/scripts/preinstall.mjs",
"prepack": "node ./public/scripts/prepack.mjs",
"clear": "rimraf --glob ./@*/*/{libs,build,*.tsbuildinfo,tsconfig.build.json} tsconfig.build.json",
"format": "prettier --config .prettierrc.js --write \"**/*.{js,jsx,ts,tsx,d.ts,vue,md,json,yml,yaml}\"",
"lint": "eslint --fix .",
Expand All @@ -34,7 +35,7 @@
"docs": "dumi build",
"docs:dev": "rm -rf .dumi/tmp && rm -rf node_modules/.cache && dumi dev",
"dev": "jest -c ./jest.config.ts --watch",
"ok": "pnpm tidy && pnpm compile && pnpm lint && pnpm test && pnpm readme && pnpm format && pnpm docs"
"ok": "pnpm run tidy && pnpm run compile && pnpm run lint && pnpm run test && pnpm run readme && pnpm run format && pnpm run docs"
},
"peerDependencies": {
"@dumlj/mock-lib": "workspace:^2.5.22",
Expand Down
63 changes: 63 additions & 0 deletions public/scripts/prepack.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { spawn } from 'child_process'
import fs from 'fs'
import path from 'path'
import stream from 'stream'
import { promisify } from 'util'

const finished = promisify(stream.finished)

/** run shell command */
async function shell(command, options) {
const [cmd, ...args] = command.split(' ')
const cp = spawn(cmd, args, options)

const chunks = []
cp.stdout.on('data', (chunk) => chunks.push(chunk))

/** wait for command to finish */
await finished(cp.stdout)
return Buffer.concat(chunks).toString()
}

/** find all projects in workspace */
async function findProjects() {
const json = await shell('pnpm m ls --json --depth=-1')
const projects = JSON.parse(json.trim())
return projects.map(({ path: location, private: isPrivate, ...rest }) => {
return { ...rest, location, isPrivate }
})
}

/** update attribute types in package.json */
async function updatePackage(location) {
const file = path.join(location, 'package.json')
const json = await fs.promises.readFile(file, 'utf-8')
const config = JSON.parse(json)
if (typeof config?.types !== 'string') {
return
}

config.types = './libs/index.d.ts'
await fs.promises.writeFile(file, JSON.stringify(config, null, 2))
}

/** modify all "package.json" in the workspace */
async function prepack() {
const projects = await findProjects()
const promises = Array.from(
(function* () {
for (const { isPrivate, location } of projects) {
if (isPrivate) {
continue
}

yield updatePackage(location)
}
})()
)

/** wait for all promises to finish */
return Promise.allSettled(promises)
}

prepack()
30 changes: 0 additions & 30 deletions public/scripts/prepare.mjs

This file was deleted.

0 comments on commit dbfb3b7

Please sign in to comment.