-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b755eb
commit dec9bda
Showing
9 changed files
with
136 additions
and
45 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
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
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
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
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,75 @@ | ||
const { execSync } = require('child_process') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
interface SimplePackageJson { | ||
name: string | ||
version: string | ||
} | ||
|
||
function getLatestVersion(packageName: string): string { | ||
try { | ||
return execSync(`npm show ${packageName} version`, { encoding: 'utf-8' }).trim() | ||
} catch (e) { | ||
return 'unavailable' | ||
} | ||
} | ||
|
||
const publishCommands: string[] = [] | ||
|
||
function compareVersions(packagePath: string, packageName: string, currentVersion: string): void { | ||
const latestVersion = getLatestVersion(packageName) | ||
if (latestVersion === 'unavailable') { | ||
console.log(`Unable to fetch latest version for ${packageName}.`) | ||
return | ||
} | ||
|
||
if (currentVersion !== latestVersion) { | ||
const relativePath = path.relative(process.cwd(), packagePath) | ||
const publishCommand = `(cd ${relativePath} && npm publish)` | ||
|
||
console.log( | ||
`Publish new version for ${packageName} in ${packagePath} from npm version ${latestVersion} to ${currentVersion}`, | ||
) | ||
publishCommands.push(publishCommand) | ||
} | ||
} | ||
function checkPackageJson(packagePath: string): void { | ||
const packageJsonPath = path.join(packagePath, 'package.json') | ||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) as SimplePackageJson | ||
|
||
const { name, version } = packageJson | ||
compareVersions(packagePath, name, version) | ||
} | ||
|
||
function getPackagePaths(directoryPath: string): string[] { | ||
const files = fs.readdirSync(directoryPath) | ||
const packagePaths: string[] = [] | ||
|
||
for (const file of files) { | ||
const filePath = path.join(directoryPath, file) | ||
const fileStat = fs.statSync(filePath) | ||
if (fileStat.isDirectory() && fs.existsSync(path.join(filePath, 'package.json'))) { | ||
packagePaths.push(filePath) | ||
} else if (fileStat.isDirectory()) { | ||
packagePaths.push(...getPackagePaths(filePath)) | ||
} | ||
} | ||
|
||
return packagePaths | ||
} | ||
|
||
function main(): void { | ||
const packagesPath = path.join(__dirname, '..', 'packages') | ||
const packagePaths = getPackagePaths(packagesPath) | ||
|
||
for (const packagePath of packagePaths) { | ||
checkPackageJson(packagePath) | ||
} | ||
if (publishCommands.length > 0) { | ||
console.log('\nPublish Commands:') | ||
console.log(publishCommands.join('\n')) | ||
} | ||
} | ||
|
||
main() |