Skip to content

Commit

Permalink
chore: faster sync script (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lxxyx committed Aug 17, 2020
1 parent 7d96bbf commit dbde476
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.18",
"benchmark": "^2.1.4",
"execa": "^4.0.3",
"gh-pages": "^1.2.0",
"lerna": "3",
"lerna-relinker": "^1.4.0",
Expand Down
32 changes: 32 additions & 0 deletions scripts/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { execSync } = require('child_process');
const execa = require('execa');
const { resolve } = require('path');

const originData = execSync('npx lerna ls --json').toString();
const data = JSON.parse(originData);
const finished = [];

async function syncPackage(pkg) {
await execa('tnpm', ['sync', pkg]);
finished.push(pkg);
console.log(`[${finished.length}/${data.length}] ${pkg} sync finished`);
}

async function sync() {
console.log('\n=== start sync ===\n');
const packages = data.map((item) => item.name);

console.log(`sync ${packages.length} packages:\n${packages.join('\n')}\n`);
const task = packages.map((pkg) => syncPackage(pkg));
await Promise.all(task);

console.log('\n=== check sync status ===\n');
await execa('node', [resolve(__dirname, 'sync_status.js')], {
stdio: 'inherit',
});

console.log('\n=== sync finished ===');
process.exit(1);
}

sync();
43 changes: 26 additions & 17 deletions scripts/sync_status.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
const { execSync } = require('child_process');
const execa = require('execa');

const originData = execSync('npx lerna ls --json').toString();
const data = JSON.parse(originData);

const arr = [];

for (const item of data) {
console.log('---->', item.name);
if (item.private === false) {
const npmVersion = execSync(`npm show ${item.name} version`)
.toString()
.replace('\n', '');
const tnpmVersion = execSync(`tnpm show ${item.name} version`)
.toString()
.replace('\n', '');
if (npmVersion !== tnpmVersion) {
console.log(`===> npm: ${npmVersion}, tnpm: ${tnpmVersion}`);
arr.push(item.name);
}
const failed = [];
const finished = [];

async function checkSyncStatus(pkg) {
const npmVersion = await execa('npm', ['show', pkg, 'version']);
const tnpmVersion = await execa('tnpm', ['show', pkg, 'version']);

finished.push(pkg);
console.log(`[${finished.length}/${data.length}] ---->`, pkg);
if (npmVersion.stdout !== tnpmVersion.stdout) {
console.log(`===> npm: ${npmVersion.stdout}, tnpm: ${tnpmVersion.stdout}`);
failed.push(item.name);
}
}

if (arr.length) {
console.log(`output command => tnpm sync ${arr.join(' ')}`);
async function start() {
const packages = data
.filter((item) => item.private === false)
.map((item) => item.name);

const task = packages.map((pkg) => checkSyncStatus(pkg));
await Promise.all(task);

if (failed.length) {
console.log(`output command => tnpm sync ${failed.join(' ')}`);
}
}

start();

0 comments on commit dbde476

Please sign in to comment.