@@ -29,6 +29,7 @@ const path = require('path');
29
29
const runCommand = require ( './run-command' ) ;
30
30
31
31
const packagesDirPath = path . resolve ( __dirname , '../packages' ) ;
32
+ const npmrcPath = path . resolve ( process . env . HOME , '.npmrc' ) ;
32
33
33
34
async function isPackageVersionPublished ( packageName , version ) {
34
35
return fetch ( `https://registry.npmjs.org/${ encodeURI ( packageName ) } /${ version } ` )
@@ -38,8 +39,9 @@ async function isPackageVersionPublished(packageName, version) {
38
39
async function isValidPackagePath ( packageDir ) {
39
40
const packageJsonPath = `${ packageDir } /package.json` ;
40
41
try {
42
+ // check to see if the file already exists - if so do nothing
41
43
await fs . stat ( packageJsonPath ) ;
42
- return true
44
+ return true ;
43
45
} catch {
44
46
return false ;
45
47
}
@@ -52,6 +54,29 @@ async function getPackageInfo(packageDir) {
52
54
} ;
53
55
}
54
56
57
+ let npmrcCleanupRequired = false ;
58
+ async function setupNpm ( ) {
59
+ try {
60
+ await fs . stat ( npmrcPath ) ;
61
+ return ;
62
+ } catch { }
63
+ // For npm publication to work, the NPM token must be stored in the .npmrc file in the user home directory
64
+ if ( process . env . GITHUB_ACTIONS && process . env . NPM_TOKEN ) {
65
+ await fs . writeFile (
66
+ path . resolve ( process . env . HOME , '.npmrc' ) ,
67
+ `//registry.npmjs.org/:_authToken=${ process . env . NPM_TOKEN } \n` ,
68
+ 'utf8' ) ;
69
+ npmrcCleanupRequired = true ;
70
+ }
71
+ }
72
+
73
+ async function cleanupNpmrc ( ) {
74
+ if ( ! npmrcCleanupRequired ) {
75
+ return ;
76
+ }
77
+ await fs . unlink ( npmrcPath ) ;
78
+ }
79
+
55
80
async function publishPackagesIfNeeded ( packageInfo ) {
56
81
const pkgJson = packageInfo . pkg ;
57
82
const isAlreadyPublished = await isPackageVersionPublished ( pkgJson . name , pkgJson . version ) ;
@@ -112,4 +137,6 @@ async function publishPackagesIfNeeded(packageInfo) {
112
137
throw new Error ( 'Unable to publish packages: cyclical dependencies encountered.' ) ;
113
138
}
114
139
}
115
- } ) ( ) . catch ( ( e ) => { throw e } ) ;
140
+ } ) ( ) . catch ( ( e ) => {
141
+ throw e ;
142
+ } ) . finally ( cleanupNpmrc ) ;
0 commit comments