|
1 | | -import * as path from 'path'; |
2 | | -import * as tar from 'tar'; |
3 | | -import * as semver from 'semver'; |
4 | | -import { MaterialsLibConfig } from '@vize/types'; |
5 | | -import { dist } from '../dist'; |
6 | | -import { curl, error, getLibPaths, getLibVersion, getLibConfig, LibPaths } from '../../utils'; |
| 1 | +import { Releaser } from './releaser'; |
7 | 2 |
|
8 | 3 | export function release() { |
9 | 4 | const releaser = new Releaser(); |
10 | 5 | return releaser.runRelease(); |
11 | 6 | } |
12 | | - |
13 | | -class Releaser { |
14 | | - constructor() { |
15 | | - this.paths = getLibPaths(); |
16 | | - this.config = getLibConfig(this.paths); |
17 | | - } |
18 | | - |
19 | | - private readonly paths: LibPaths; |
20 | | - |
21 | | - private readonly config: MaterialsLibConfig; |
22 | | - |
23 | | - private getURI(suffix = '') { |
24 | | - return `${this.config.releaseTo}/cgi/materials/versions/${this.config.libName}${suffix}`; |
25 | | - } |
26 | | - |
27 | | - private checkVersionValid = async (): Promise<Maybe<string>> => { |
28 | | - const currentVersion = getLibVersion(this.paths.root); |
29 | | - const uri = this.getURI(); |
30 | | - let data; |
31 | | - try { |
32 | | - const result = await curl(uri, { |
33 | | - method: 'GET', |
34 | | - timeout: 10 * 1000, |
35 | | - contentType: 'application/json', |
36 | | - }); |
37 | | - data = result.data; |
38 | | - } catch (e) { |
39 | | - console.error(e); |
40 | | - throw new Error('Get versions failed'); |
41 | | - } |
42 | | - |
43 | | - const { |
44 | | - code, |
45 | | - data: { current: onlineVersion }, |
46 | | - } = JSON.parse(data); |
47 | | - if (code !== 0) { |
48 | | - throw new Error(`Request "${uri}" error: code = ${code}`); |
49 | | - } |
50 | | - |
51 | | - if (!onlineVersion) { |
52 | | - return currentVersion; |
53 | | - } |
54 | | - |
55 | | - return semver.gt(currentVersion, onlineVersion) ? currentVersion : null; |
56 | | - }; |
57 | | - |
58 | | - private createReleasePackage = async (version: string) => { |
59 | | - const targetPath = path.resolve(this.paths.temp, `${this.config.libName}_${version}.tgz`); |
60 | | - |
61 | | - await tar.c( |
62 | | - { |
63 | | - gzip: true, |
64 | | - file: targetPath, |
65 | | - preservePaths: false, |
66 | | - }, |
67 | | - ['src', 'dist', './.vizerc', './package.json', './package-lock.json'], |
68 | | - ); |
69 | | - return targetPath; |
70 | | - }; |
71 | | - |
72 | | - private uploadReleasePackage = async (version: string, packagePath: string) => { |
73 | | - const uri = this.getURI(`/${version}`); |
74 | | - const { data } = await curl(uri, { |
75 | | - method: 'POST', |
76 | | - files: packagePath, |
77 | | - }); |
78 | | - console.log(data.toString()); |
79 | | - }; |
80 | | - |
81 | | - public runRelease = async () => { |
82 | | - const version = await this.checkVersionValid(); |
83 | | - if (!version) { |
84 | | - return error(`Materials version already exists.`); |
85 | | - } |
86 | | - |
87 | | - await dist(); |
88 | | - const packagePath: string = await this.createReleasePackage(version); |
89 | | - await this.uploadReleasePackage(version, packagePath); |
90 | | - }; |
91 | | -} |
0 commit comments