-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add workspace dep check shell
- Loading branch information
Showing
2 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |