Skip to content

Commit

Permalink
script
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Oct 27, 2024
1 parent 5a535ec commit a90d071
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 53 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"test:wa": "yarn g:test:wa",
"test:watch": "yarn g:test:watch",
"typecheck": "yarn g:typecheck",
"typecheck:all": "yarn g:typecheck:all"
"typecheck:all": "yarn g:typecheck:all",
"sync:plate": "node templates/plate-playground-template/scripts/sync-plate.cjs"
},
"resolutions": {
"eslint-plugin-import": "2.27.5",
Expand Down
2 changes: 1 addition & 1 deletion templates/plate-playground-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@udecode/plate-break": "^39.0.0",
"@udecode/plate-caption": "^39.0.0",
"@udecode/plate-code-block": "^39.0.0",
"@udecode/plate-combobox": "^39.0.0",
"@udecode/plate-combobox": "39.0.0",
"@udecode/plate-comments": "^39.0.0",
"@udecode/plate-common": "39.2.15",
"@udecode/plate-csv": "^39.1.6",
Expand Down
165 changes: 114 additions & 51 deletions templates/plate-playground-template/scripts/sync-plate.cjs
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();

0 comments on commit a90d071

Please sign in to comment.