Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 01294d4

Browse files
committed
🔨 refactor(sync): update sync script
1 parent 3c8651c commit 01294d4

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

.github/workflows/sync.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: cnpm sync
5+
6+
on:
7+
schedule:
8+
- cron: 0 */6 * * *
9+
push:
10+
branches: [ master ]
11+
pull_request:
12+
branches: [ master ]
13+
14+
jobs:
15+
sync:
16+
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
node-version: [14.x]
20+
steps:
21+
- uses: actions/checkout@v2
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v1
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
- run: yarn
27+
- run: npm i cnpm -g
28+
- run: node scripts/sync cnpm

scripts/sync.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
const { execSync } = require('child_process')
22
const execa = require('execa')
33
const { resolve } = require('path')
4+
const check = require('./sync_status')
45

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

9-
async function syncPackage(pkg) {
10-
await Promise.all([execa('tnpm', ['sync', pkg]), execa('cnpm', ['sync', pkg])])
10+
const clients = process.argv.slice(2)
11+
12+
async function syncPackage(pkg, clients) {
13+
const tasks = clients.map((client) => execa(client, ['sync', pkg]))
14+
await Promise.all(tasks)
15+
1116
finished.push(pkg)
1217
console.log(`[${finished.length}/${data.length}] ${pkg} sync finished`)
1318
}
1419

15-
async function sync() {
20+
async function sync(clients) {
1621
console.log('\n=== start sync ===\n')
1722
const packages = data.map((item) => item.name)
1823

1924
console.log(`sync ${packages.length} packages:\n${packages.join('\n')}\n`)
20-
const task = packages.map((pkg) => syncPackage(pkg))
25+
const task = packages.map((pkg) => syncPackage(pkg, clients))
2126
await Promise.all(task)
2227

2328
console.log('\n=== check sync status ===\n')
24-
await execa('node', [resolve(__dirname, 'sync_status.js')], { stdio: 'inherit' })
29+
await check(packages, clients)
2530

2631
console.log('\n=== sync finished ===')
27-
process.exit(1)
2832
}
2933

30-
sync()
34+
sync(clients)

scripts/sync_status.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
1-
const { execSync } = require('child_process')
21
const execa = require('execa')
32

4-
const originData = execSync('npx lerna ls --json').toString()
5-
const data = JSON.parse(originData)
6-
73
const failed = []
84
const finished = []
95

10-
async function checkSyncStatus(pkg) {
11-
const npmVersion = await execa('npm', ['show', pkg, 'version'])
12-
const tnpmVersion = await execa('tnpm', ['show', pkg, 'version'])
13-
const cnpmVersion = await execa('cnpm', ['show', pkg, 'version'])
6+
async function checkSyncStatus(packages, pkg, clients) {
7+
const versions = await Promise.all(clients.map((client) => execa(client, ['show', pkg, 'version'])))
148

159
finished.push(pkg)
16-
console.log(`[${finished.length}/${data.length}] ---->`, pkg)
17-
if (npmVersion.stdout !== tnpmVersion.stdout || npmVersion.stdout !== cnpmVersion.stdout) {
18-
console.log(`===> npm: ${npmVersion.stdout}, tnpm: ${tnpmVersion.stdout}, cnpm: ${cnpmVersion.stdout}`)
10+
console.log(`[${finished.length}/${packages.length}] ---->`, pkg)
11+
12+
const isSuccess = versions.every((version) => version.stdout === versions[0].stdout)
13+
14+
if (!isSuccess) {
15+
console.log(`===> ${clients.map((client, i) => `${client}: ${versions[i].stdout}`).join(' ')}`)
1916
failed.push(pkg)
2017
}
2118
}
2219

23-
async function start() {
24-
const packages = data.filter((item) => item.private === false).map((item) => item.name)
25-
26-
const task = packages.map((pkg) => checkSyncStatus(pkg))
20+
module.exports = async function check(packages, clients) {
21+
const task = packages.map((pkg) => checkSyncStatus(packages, pkg, ['npm', ...clients]))
2722
await Promise.all(task)
28-
29-
if (failed.length) {
30-
console.log(`output command => tnpm sync ${failed.join(' ')}`)
31-
}
3223
}
33-
34-
start()

0 commit comments

Comments
 (0)