|
1 | | -/** |
2 | | - * Scripts to check unpublished version and run publish |
3 | | - */ |
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import { existsSync, readdirSync } from 'fs'; |
4 | 3 | import { join } from 'path'; |
5 | | -import { spawnSync } from 'child_process'; |
6 | | -import { IPackageInfo, getPackageInfos } from './getPackageInfos'; |
| 4 | +import * as fse from 'fs-extra'; |
| 5 | +import * as axios from 'axios'; |
| 6 | +import { getVersions } from 'ice-npm-utils'; |
7 | 7 |
|
8 | | -if (process.env.BRANCH_NAME !== 'master') { |
9 | | - console.log('No Publish', process.env.BRANCH_NAME); |
10 | | - process.exit(0); |
| 8 | +// Set by github actions |
| 9 | +const branchName = process.env.BRANCH_NAME; |
| 10 | +const rootDir = join(__dirname, '../'); |
| 11 | +const REGISTRY = 'https://registry.npmjs.org/'; |
| 12 | + |
| 13 | +if (!branchName) { |
| 14 | + throw new Error('Only support publish in GitHub Actions env'); |
11 | 15 | } |
12 | 16 |
|
13 | | -function publish(pkg: string, version: string, directory: string): void { |
14 | | - console.log('[PUBLISH]', `${pkg}@${version}`); |
| 17 | +(async () => { |
| 18 | + const packageDirs = getPackagesPaths(join(rootDir, 'packages')); |
| 19 | + |
| 20 | + for (const pkgDir of packageDirs) { |
| 21 | + // eslint-disable-next-line no-await-in-loop |
| 22 | + await publishPackage(pkgDir); |
| 23 | + } |
| 24 | + |
| 25 | +})().catch(err => { |
| 26 | + console.error(err); |
| 27 | + process.exit(1); |
| 28 | +}); |
| 29 | + |
| 30 | +async function publishPackage(packageDir) { |
| 31 | + const pkgData = await fse.readJSON(join(packageDir, 'package.json')); |
| 32 | + const { version, name } = pkgData; |
| 33 | + const npmTag = branchName === 'master' ? 'latest' : 'beta'; |
| 34 | + |
| 35 | + const versionExist = await checkVersionExist(name, version, REGISTRY); |
| 36 | + if (versionExist) { |
| 37 | + console.log(`${name}@${version} 已存在,无需发布。`); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const isProdVersion = /^\d+\.\d+\.\d+$/.test(version); |
| 42 | + if (branchName === 'master' && !isProdVersion) { |
| 43 | + throw new Error(`禁止在 master 分支发布非正式版本 ${version}`); |
| 44 | + } |
| 45 | + |
| 46 | + if (branchName !== 'master' && isProdVersion) { |
| 47 | + console.log(`非 master 分支 ${branchName},不发布正式版本 ${version}`); |
| 48 | + return; |
| 49 | + } |
15 | 50 |
|
16 | | - spawnSync('npm', [ |
17 | | - 'publish', |
18 | | - // use default registry |
19 | | - ], { |
| 51 | + console.log('start publish', version, npmTag); |
| 52 | + execSync('npm install', { |
| 53 | + cwd: packageDir, |
20 | 54 | stdio: 'inherit', |
21 | | - cwd: directory, |
22 | 55 | }); |
| 56 | + execSync(`npm publish --tag ${npmTag}`, { |
| 57 | + cwd: packageDir, |
| 58 | + stdio: 'inherit', |
| 59 | + }); |
| 60 | + |
| 61 | + console.log('start notify'); |
| 62 | + const response = await axios.default({ |
| 63 | + url: process.env.DING_WEBHOOK, |
| 64 | + method: 'post', |
| 65 | + headers: { |
| 66 | + 'Content-Type': 'application/json;charset=utf-8', |
| 67 | + }, |
| 68 | + data: { |
| 69 | + msgtype: 'markdown', |
| 70 | + markdown: { |
| 71 | + title: `${name}@${version} 发布成功`, |
| 72 | + text: `${name}@${version} 发布成功`, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }); |
| 76 | + console.log('notify success', response.data); |
23 | 77 | } |
24 | 78 |
|
25 | | -// Entry |
26 | | -console.log('[PUBLISH] Start:'); |
27 | | - |
28 | | -Promise.all([ |
29 | | - getPackageInfos(join(__dirname, '../packages')) |
30 | | -]).then((result: IPackageInfo[][]) => { |
31 | | - |
32 | | - let publishedCount = 0; |
33 | | - // Publish |
34 | | - for (let i = 0; i < result.length; i++) { |
35 | | - const packageInfos: IPackageInfo[] = result[i]; |
36 | | - for (let j = 0; j < packageInfos.length; j++) { |
37 | | - const { name, directory, localVersion, shouldPublish } = packageInfos[j]; |
38 | | - if (shouldPublish) { |
39 | | - publishedCount++; |
40 | | - console.log(`--- ${name}@${localVersion} ---`); |
41 | | - publish(name, localVersion, directory); |
42 | | - } |
43 | | - } |
| 79 | + |
| 80 | +async function checkVersionExist(name: string, version: string, registry?: string): Promise<boolean> { |
| 81 | + try { |
| 82 | + const versions = await getVersions(name, registry); |
| 83 | + return versions.indexOf(version) !== -1; |
| 84 | + } catch (err) { |
| 85 | + console.error('checkVersionExist error', err); |
| 86 | + return false; |
44 | 87 | } |
45 | | - console.log(`[PUBLISH] Complete (count=${publishedCount}).`) |
46 | | -}); |
| 88 | +} |
| 89 | + |
| 90 | +function getPackagesPaths(dir) { |
| 91 | + const packagesPaths: string[] = readdirSync(dir).map(dirname => { |
| 92 | + return join(dir, dirname); |
| 93 | + }).filter((dirpath) => { |
| 94 | + return existsSync(join(dirpath, 'package.json')); |
| 95 | + }); |
| 96 | + |
| 97 | + return packagesPaths; |
| 98 | +} |
0 commit comments