11( ( ) => {
22 const chalk = require ( 'chalk' )
33 const execa = require ( 'execa' )
4+ const path = require ( 'path' )
5+ const semanticRelease = require ( 'semantic-release' )
6+ // const { WritableStreamBuffer } = require('stream-buffers');
7+ // const stdoutBuffer = WritableStreamBuffer();
8+ // const stderrBuffer = WritableStreamBuffer();
49
510 const args = require ( 'minimist' ) ( process . argv . slice ( 2 ) )
6- const targets = args . _
7-
8- if ( process . env . CI && targets [ 0 ] ) {
9- console . log ( "execute zip >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" )
10- execa . sync ( 'zip' , [ '-r' , `${ targets [ 0 ] } -dist.zip` , '.' , '-i' , 'dist' ] )
11- console . log ( "execute semantic-release >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" )
12- const a = execa . sync ( 'npx' , [ 'semantic-release' ] )
13- console . log ( "A" , a )
14- console . log ( "finished execution >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" )
15- } else {
16- console . log ( chalk . redBright ( "You can't run semantic-release locally" ) )
11+ const target = args . _ [ 0 ]
12+ const pkg = require ( path . resolve ( __dirname , `../packages/${ target } ` , 'package.json' ) )
13+ const releaserc = {
14+ branches : [
15+ 'master' ,
16+ 'next' ,
17+ 'next-major' ,
18+ { name : 'beta' , prerelease : true } ,
19+ { name : 'alpha' , prerelease : true }
20+ ] ,
21+ plugins : [
22+ '@semantic-release/commit-analyzer' ,
23+ '@semantic-release/release-notes-generator' ,
24+ '@semantic-release/npm' ,
25+ [
26+ '@semantic-release/github' ,
27+ {
28+ assets : [
29+ {
30+ path : `${ target } -dist.zip` ,
31+ name : `${ target } -dist-\$\{nextRelease.gitTag\}.zip` ,
32+ label : 'Distribution code (zip)'
33+ }
34+ ]
35+ }
36+ ]
37+ ]
38+ }
39+
40+ run ( target )
41+
42+ async function run ( target : string ) {
43+ const nextVersion = await getNextVersion ( )
44+ await prepare ( target , nextVersion )
45+ await release ( )
46+ }
47+
48+ async function prepare ( target : string , nextVersion : string ) {
49+ const buildScript = path . resolve ( __dirname , 'build.js' )
50+ execa . sync ( 'npx' , [ 'ts-node' , buildScript , '--nextVersion' , nextVersion ] )
51+ execa . sync ( 'zip' , [ '-r' , `${ target } -dist.zip` , '.' , '-i' , 'dist' ] )
52+ execa . sync ( 'npx' , [ 'semantic-release' ] )
53+ }
54+
55+ async function release ( ) {
56+ try {
57+ const result = await semanticRelease ( {
58+ // Core options
59+ branches : releaserc . branches ,
60+ repositoryUrl : pkg . repository . url ,
61+ plugins : releaserc . plugins
62+ } , {
63+ // Run semantic-release from `/path/to/git/repo/root` without having to change local process `cwd` with `process.chdir()`
64+ cwd : '' ,
65+ // Pass the variable `MY_ENV_VAR` to semantic-release without having to modify the local `process.env`
66+ env : { ...process . env } ,
67+ // Store stdout and stderr to use later instead of writing to `process.stdout` and `process.stderr`
68+ // stdout: stdoutBuffer,
69+ // stderr: stderrBuffer
70+ } ) ;
71+
72+ if ( result ) {
73+ const { lastRelease, commits, nextRelease, releases } = result ;
74+
75+ console . log ( `Published ${ nextRelease . type } release version ${ nextRelease . version } containing ${ commits . length } commits.` ) ;
76+
77+ if ( lastRelease . version ) {
78+ console . log ( `The last release was "${ lastRelease . version } ".` ) ;
79+ }
80+
81+ for ( const release of releases ) {
82+ console . log ( `The release was published with plugin "${ release . pluginName } ".` ) ;
83+ }
84+ } else {
85+ console . log ( 'No release published.' ) ;
86+ }
87+
88+ // Get stdout and stderr content
89+ // const logs = stdoutBuffer.getContentsAsString('utf8');
90+ // const errors = stderrBuffer.getContentsAsString('utf8');
91+ // console.log(logs, errors);
92+
93+ } catch ( err ) {
94+ console . error ( 'The automated release failed with %O' , err )
95+ }
96+ }
97+
98+ async function getNextVersion ( ) : Promise < string > {
99+ try {
100+ const { nextRelease } = await semanticRelease ( {
101+ branches : releaserc . branches ,
102+ repositoryUrl : pkg . repository . url ,
103+ dryRun : true ,
104+ ci : false ,
105+ plugins : [ '@semantic-release/commit-analyzer' ]
106+ } )
107+ if ( nextRelease ) return nextRelease . version
108+ else console . log ( 'No release will bepublished' )
109+ } catch ( err ) {
110+ console . error ( 'Failed to retrieve next version with %O' , err )
111+ }
112+ return pkg . version
17113 }
18114} ) ( )
0 commit comments