Skip to content

Commit

Permalink
chore: add workspace dep check shell
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Feb 14, 2024
1 parent aac7eb8 commit 81f9612
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/release-client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- fix/workspace-publish
paths:
- .github/workflows/release-client.yml
- packages/client/**
Expand All @@ -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

Expand All @@ -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:
Expand Down
89 changes: 89 additions & 0 deletions scripts/workspace-publish.cjs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 81f9612

Please sign in to comment.