-
-
Notifications
You must be signed in to change notification settings - Fork 755
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
5a535ec
commit a90d071
Showing
3 changed files
with
117 additions
and
53 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
165 changes: 114 additions & 51 deletions
165
templates/plate-playground-template/scripts/sync-plate.cjs
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 |
---|---|---|
@@ -1,69 +1,132 @@ | ||
const { execSync } = require('node:child_process'); | ||
const fs = require('node:fs'); | ||
// sync plate packages | ||
const { exec } = require('node:child_process'); | ||
const fs = require('node:fs/promises'); | ||
const path = require('node:path'); | ||
const util = require('node:util'); | ||
|
||
const execPromise = util.promisify(exec); | ||
|
||
const TARGET_PATH = path.join(__dirname, '../', './package.json'); | ||
const files = fs.readFileSync(TARGET_PATH); | ||
const json = JSON.parse(files); | ||
|
||
const INCLUDE_PACKAGES = new Set( | ||
Object.keys(json.dependencies).filter((pkg) => pkg.startsWith('@udecode')) | ||
); | ||
|
||
const versionMap = new Map(); | ||
const updatedPackages = []; | ||
|
||
function fetchLatestVersions() { | ||
console.info('Fetching latest versions...'); | ||
INCLUDE_PACKAGES.forEach((pkg) => { | ||
try { | ||
const version = execSync(`npm view ${pkg} version`, { | ||
encoding: 'utf8', | ||
}).trim(); | ||
const currentVersion = json.dependencies[pkg] | ||
? json.dependencies[pkg].replace(/^\D*/, '') | ||
: 'Not installed'; | ||
console.info(); | ||
console.info(`fetch success Package: ${pkg}`); | ||
console.info(`Current version: ${currentVersion}`); | ||
console.info(`Latest version: ${version}`); | ||
console.info( | ||
'#################################################################################' | ||
); | ||
versionMap.set(pkg, { currentVersion, version }); // Store both current and latest versions | ||
} catch (error) { | ||
console.error(`Error fetching version for ${pkg}: ${error.message}`); | ||
|
||
async function getPackageJson() { | ||
try { | ||
const files = await fs.readFile(TARGET_PATH, 'utf8'); | ||
|
||
return JSON.parse(files); | ||
} catch (error) { | ||
console.error('Error reading package.json:', error.message); | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
async function fetchPackageVersion(pkg) { | ||
try { | ||
const { stdout } = await execPromise(`npm view ${pkg} version`); | ||
|
||
return stdout.trim(); | ||
} catch (error) { | ||
console.error(`Error fetching version for ${pkg}:`, error.message); | ||
|
||
return null; | ||
} | ||
} | ||
|
||
async function fetchLatestVersions(packages, packageJson) { | ||
console.info('Fetching latest plate versions in parallel...'); | ||
|
||
const versionPromises = packages.map(async (pkg) => { | ||
const version = await fetchPackageVersion(pkg); | ||
|
||
if (version) { | ||
const currentVersion = | ||
packageJson.dependencies[pkg]?.replace(/^\D*/, '') ?? 'Not installed'; | ||
|
||
return [pkg, { currentVersion, version }]; | ||
} | ||
|
||
return null; | ||
}); | ||
|
||
const results = await Promise.all(versionPromises); | ||
|
||
return new Map(results.filter(Boolean)); | ||
} | ||
|
||
function updatedVersion() { | ||
console.info('Updating package.json...'); | ||
versionMap.forEach((versions, name) => { | ||
if (json.dependencies[name] && INCLUDE_PACKAGES.has(name)) { | ||
const currentVersion = versions.currentVersion; | ||
const newVersion = versions.version; | ||
async function updatePackageVersions(packageJson, versionMap) { | ||
const updatedPackages = []; | ||
|
||
for (const [name, versions] of versionMap) { | ||
if (packageJson.dependencies[name]) { | ||
const { currentVersion, version: newVersion } = versions; | ||
|
||
if (currentVersion !== newVersion) { | ||
json.dependencies[name] = newVersion; | ||
packageJson.dependencies[name] = newVersion; | ||
updatedPackages.push({ currentVersion, name, newVersion }); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
fs.writeFileSync(TARGET_PATH, JSON.stringify(json, null, 2)); | ||
await fs.writeFile(TARGET_PATH, JSON.stringify(packageJson, null, 2)); | ||
|
||
console.info('Update success'); | ||
return updatedPackages; | ||
} | ||
|
||
fetchLatestVersions(); | ||
updatedVersion(); | ||
async function main() { | ||
try { | ||
// Read package.json | ||
const packageJson = await getPackageJson(); | ||
|
||
if (updatedPackages.length > 0) { | ||
console.info('The following packages were updated:'); | ||
updatedPackages.forEach((pkg) => { | ||
console.info(`${pkg.name}: ${pkg.currentVersion} -> ${pkg.newVersion}`); | ||
}); | ||
} else { | ||
console.info('No packages were updated.'); | ||
// Filter @udecode packages | ||
const INCLUDE_PACKAGES = Object.keys(packageJson.dependencies).filter( | ||
(pkg) => pkg.startsWith('@udecode') | ||
); | ||
|
||
// Fetch latest versions in parallel | ||
const versionMap = await fetchLatestVersions(INCLUDE_PACKAGES, packageJson); | ||
|
||
// Update package.json with new versions | ||
const updatedPackages = await updatePackageVersions( | ||
packageJson, | ||
versionMap | ||
); | ||
|
||
// Log results | ||
if (updatedPackages.length > 0) { | ||
console.log('\nThe following packages were updated.'); | ||
updatedPackages.forEach(({ currentVersion, name, newVersion }) => { | ||
console.log( | ||
'\u001B[32m%s\u001B[0m', | ||
`${name}: ${currentVersion} -> ${newVersion}` | ||
); | ||
}); | ||
|
||
console.info('\nRunning pnpm install...'); | ||
const { execSync } = require('node:child_process'); | ||
|
||
try { | ||
const args = process.argv.slice(2); | ||
const shouldInstall = args.includes('--install'); | ||
|
||
if (!shouldInstall) { | ||
console.info('Skipping pnpm install. Use --install flag to run it.'); | ||
|
||
return; | ||
} | ||
|
||
execSync('pnpm install', { stdio: 'inherit' }); | ||
console.info('pnpm install completed successfully.'); | ||
} catch (error) { | ||
console.error('Error running pnpm install:', error.message); | ||
process.exit(1); | ||
} | ||
} else { | ||
console.info('\nNo packages were updated.'); | ||
} | ||
} catch (error) { | ||
console.error('Error updating packages:', error.message); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
main(); |