@@ -67,11 +67,72 @@ function _exec(command: string, args: string[], opts: { cwd?: string }, logger:
6767 return stdout . toString ( 'utf-8' ) ;
6868}
6969
70+ async function _publishSnapshot (
71+ pkg : PackageInfo ,
72+ branch : string ,
73+ message : string ,
74+ logger : logging . Logger ,
75+ githubToken : string ,
76+ ) {
77+ if ( ! pkg . snapshot ) {
78+ logger . warn ( `Skipping ${ pkg . name } .` ) ;
79+
80+ return ;
81+ }
82+
83+ logger . info ( `Publishing ${ pkg . name } to repo ${ JSON . stringify ( pkg . snapshotRepo ) } .` ) ;
84+
85+ const root = process . cwd ( ) ;
86+ const publishLogger = logger . createChild ( 'publish' ) ;
87+ publishLogger . debug ( 'Temporary directory: ' + root ) ;
88+
89+ const url = `https://${ githubToken ? githubToken + '@' : '' } github.com/${ pkg . snapshotRepo } .git` ;
90+ const destPath = path . join ( root , path . basename ( pkg . snapshotRepo ) ) ;
91+
92+ _exec ( 'git' , [ 'clone' , url ] , { cwd : root } , publishLogger ) ;
93+ if ( branch ) {
94+ _exec ( 'git' , [ 'checkout' , '-B' , branch ] , { cwd : destPath } , publishLogger ) ;
95+ }
96+
97+ // Clear snapshot directory before publishing to remove deleted build files.
98+ try {
99+ _exec ( 'git' , [ 'rm' , '-rf' , './' ] , { cwd : destPath } , publishLogger ) ;
100+ } catch {
101+ // Ignore errors on delete. :shrug:
102+ }
103+ _copy ( pkg . dist , destPath ) ;
104+
105+ if ( githubToken ) {
106+ _exec ( 'git' , [ 'config' , 'commit.gpgSign' , 'false' ] , { cwd : destPath } , publishLogger ) ;
107+ }
108+
109+ // Add the header to the existing README.md (or create a README if it doesn't exist).
110+ const readmePath = path . join ( destPath , 'README.md' ) ;
111+ let readme = readmeHeaderFn ( pkg ) ;
112+ try {
113+ readme += fs . readFileSync ( readmePath , 'utf-8' ) ;
114+ } catch { }
115+
116+ fs . writeFileSync ( readmePath , readme ) ;
117+
118+ // Make sure that every snapshots is unique (otherwise we would need to make sure git accepts
119+ // empty commits).
120+ fs . writeFileSync ( path . join ( destPath , 'uniqueId' ) , '' + new Date ( ) ) ;
121+
122+ // Commit and push.
123+ _exec ( 'git' , [ 'add' , '.' ] , { cwd : destPath } , publishLogger ) ;
124+ _exec ( 'git' , [ 'commit' , '-a' , '-m' , message ] , { cwd : destPath } , publishLogger ) ;
125+ _exec ( 'git' , [ 'tag' , pkg . snapshotHash ] , { cwd : destPath } , publishLogger ) ;
126+ _exec ( 'git' , [ 'push' , 'origin' , branch ] , { cwd : destPath } , publishLogger ) ;
127+ _exec ( 'git' , [ 'push' , '--tags' , 'origin' , branch ] , { cwd : destPath } , publishLogger ) ;
128+ }
129+
70130
71131export interface SnapshotsOptions {
72132 force ?: boolean ;
73133 githubTokenFile ?: string ;
74134 githubToken ?: string ;
135+ branch ?: string ;
75136}
76137
77138export default async function ( opts : SnapshotsOptions , logger : logging . Logger ) {
@@ -83,6 +144,12 @@ export default async function(opts: SnapshotsOptions, logger: logging.Logger) {
83144
84145 const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'angular-cli-publish-' ) ) ;
85146 const message = execSync ( `git log --format="%h %s" -n1` ) . toString ( ) . trim ( ) ;
147+ let branch = opts . branch || 'master' ;
148+
149+ // CIRCLE_BRANCH
150+ if ( typeof process . env [ 'CIRCLE_BRANCH' ] == 'string' ) {
151+ branch = '' + process . env [ 'CIRCLE_BRANCH' ] ;
152+ }
86153
87154 const githubToken = (
88155 opts . githubToken
@@ -129,53 +196,8 @@ export default async function(opts: SnapshotsOptions, logger: logging.Logger) {
129196 }
130197
131198 for ( const packageName of Object . keys ( packages ) ) {
132- const pkg = packages [ packageName ] ;
133-
134- if ( ! pkg . snapshot ) {
135- logger . warn ( `Skipping ${ pkg . name } .` ) ;
136- continue ;
137- }
138-
139- logger . info ( `Publishing ${ pkg . name } to repo ${ JSON . stringify ( pkg . snapshotRepo ) } .` ) ;
140-
141- const publishLogger = logger . createChild ( 'publish' ) ;
142- publishLogger . debug ( 'Temporary directory: ' + root ) ;
143-
144- const url = `https://${ githubToken ? githubToken + '@' : '' } github.com/${ pkg . snapshotRepo } .git` ;
145- _exec ( 'git' , [ 'clone' , url ] , { cwd : root } , publishLogger ) ;
146-
147- const destPath = path . join ( root , path . basename ( pkg . snapshotRepo ) ) ;
148- // Clear snapshot directory before publishing to remove deleted build files.
149- try {
150- _exec ( 'git' , [ 'rm' , '-rf' , './' ] , { cwd : destPath } , publishLogger ) ;
151- } catch {
152- // Ignore errors on delete. :shrug:
153- }
154- _copy ( pkg . dist , destPath ) ;
155-
156- if ( githubToken ) {
157- _exec ( 'git' , [ 'config' , 'commit.gpgSign' , 'false' ] , { cwd : destPath } , publishLogger ) ;
158- }
159-
160- // Add the header to the existing README.md (or create a README if it doesn't exist).
161- const readmePath = path . join ( destPath , 'README.md' ) ;
162- let readme = readmeHeaderFn ( pkg ) ;
163- try {
164- readme += fs . readFileSync ( readmePath , 'utf-8' ) ;
165- } catch { }
166-
167- fs . writeFileSync ( readmePath , readme ) ;
168-
169- // Make sure that every snapshots is unique (otherwise we would need to make sure git accepts
170- // empty commits).
171- fs . writeFileSync ( path . join ( destPath , 'uniqueId' ) , '' + new Date ( ) ) ;
172-
173- // Commit and push.
174- _exec ( 'git' , [ 'add' , '.' ] , { cwd : destPath } , publishLogger ) ;
175- _exec ( 'git' , [ 'commit' , '-a' , '-m' , message ] , { cwd : destPath } , publishLogger ) ;
176- _exec ( 'git' , [ 'tag' , pkg . snapshotHash ] , { cwd : destPath } , publishLogger ) ;
177- _exec ( 'git' , [ 'push' , 'origin' ] , { cwd : destPath } , publishLogger ) ;
178- _exec ( 'git' , [ 'push' , '--tags' , 'origin' ] , { cwd : destPath } , publishLogger ) ;
199+ process . chdir ( root ) ;
200+ await _publishSnapshot ( packages [ packageName ] , branch , message , logger , githubToken ) ;
179201 }
180202
181203 return 0 ;
0 commit comments