diff --git a/.github/workflows/release-client.yml b/.github/workflows/release-client.yml index e8a56bac971..ee73aace462 100644 --- a/.github/workflows/release-client.yml +++ b/.github/workflows/release-client.yml @@ -4,6 +4,7 @@ on: push: branches: - main + - fix/workspace-publish paths: - .github/workflows/release-client.yml - packages/client/** @@ -21,6 +22,9 @@ jobs: with: fetch-depth: 0 + - name: WorkSpace DepCheck + run: node scripts/workspace-publish.cjs @waline/client + - name: Install pnpm uses: pnpm/action-setup@v2 @@ -33,12 +37,6 @@ jobs: - name: Install Dependencies run: pnpm install --frozen-lockfile - - name: Build API - run: pnpm api:build - - - name: Build Client - run: pnpm client:build - - name: Publish NPM uses: JS-DevTools/npm-publish@v3 with: diff --git a/scripts/workspace-publish.cjs b/scripts/workspace-publish.cjs new file mode 100644 index 00000000000..91354af1954 --- /dev/null +++ b/scripts/workspace-publish.cjs @@ -0,0 +1,89 @@ +//example: node scripts/workspace-publish.cjs @waline/client + +const fs = require('fs'); +const path = require('path'); + +const workspaceRoot = path.join(__dirname, '../'); + +setPkg(process.argv[2], getPkgs(workspaceRoot)); + +function getPkgs(workspaceRoot) { + const workspacePkgRoot = path.join(workspaceRoot, 'packages'); + + const dirs = fs + .readdirSync(workspacePkgRoot) + .filter((dir) => { + const dirname = path.join(workspacePkgRoot, dir); + const isDir = fs.statSync(dirname).isDirectory(); + + if (!isDir) { + return false; + } + + try { + const stat = fs.statSync(path.join(dirname, 'package.json')); + + return stat.isFile(); + } catch (e) { + return false; + } + }) + .map((dir) => { + const dirname = path.join(workspacePkgRoot, dir); + const pkg = require(path.join(dirname, 'package.json')); + + return [pkg.name, { dirname, info: pkg }]; + }); + + return new Map(dirs); +} + +function setPkg(pkg, pkgs) { + const pkgMap = pkgs.get(pkg); + + if (!pkgMap) { + return; + } + + const depKeys = [ + 'dependencies', + 'devDependencies', + 'peerDependencies', + 'optionalDependencies', + 'bundleDependencies', + ]; + let writeFlag = false; + + depKeys.forEach((depKey) => { + if (!pkgMap.info[depKey]) { + return; + } + + for (const depName in pkgMap.info[depKey]) { + const ver = pkgMap.info[depKey][depName]; + + if (!ver.startsWith('workspace')) { + continue; + } + + const workPkg = pkgs.get(depName); + + if (!workPkg) { + continue; + } + + pkgMap.info[depKey][depName] = workPkg.info.version; + writeFlag = true; + } + }); + + const handler = isCI() + ? fs.writeFileSync.bind(fs, path.join(pkgMap.dirname, 'package.json')) + : console.log.bind(console); + + writeFlag && handler(JSON.stringify(pkgMap.info, null, '\t')); +} + +function isCI() { + return process.env.CI; +}