Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(publisher): move github publish logic to own file
set up for theoretical multiple publishers
- Loading branch information
1 parent
02fe569
commit bdaff3c
Showing
4 changed files
with
109 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import ora from 'ora'; | ||
import path from 'path'; | ||
import GitHub from '../util/github'; | ||
|
||
export default async (artifacts, packageJSON, forgeConfig, authToken, tag) => { | ||
if (!(forgeConfig.github_repository && typeof forgeConfig.github_repository === 'object' && | ||
forgeConfig.github_repository.owner && forgeConfig.github_repository.name)) { | ||
console.error('In order to publish to github you must set the "github_repository.owner" and "github_repository.name" properties in your forge config. See the docs for more info'.red); // eslint-disable-line | ||
process.exit(1); | ||
} | ||
|
||
const github = new GitHub(authToken); | ||
|
||
let release; | ||
try { | ||
release = (await github.getGitHub().repos.getReleases({ | ||
owner: forgeConfig.github_repository.owner, | ||
repo: forgeConfig.github_repository.name, | ||
per_page: 100, | ||
})).find(testRelease => testRelease.tag_name === (tag || `v${packageJSON.version}`)); | ||
if (!release) { | ||
throw { code: 404 }; // eslint-disable-line | ||
} | ||
} catch (err) { | ||
if (err.code === 404) { | ||
// Release does not exist, let's make it | ||
release = await github.getGitHub().repos.createRelease({ | ||
owner: forgeConfig.github_repository.owner, | ||
repo: forgeConfig.github_repository.name, | ||
tag_name: tag || `v${packageJSON.version}`, | ||
name: tag || `v${packageJSON.version}`, | ||
draft: true, | ||
}); | ||
} else { | ||
// Unknown error | ||
throw err; | ||
} | ||
} | ||
|
||
let uploaded = 0; | ||
const uploadSpinner = ora.ora(`Uploading Artifacts ${uploaded}/${artifacts.length}`).start(); | ||
const updateSpinner = () => { | ||
uploadSpinner.text = `Uploading Artifacts ${uploaded}/${artifacts.length}`; | ||
}; | ||
|
||
try { | ||
await Promise.all(artifacts.map(artifactPath => | ||
new Promise(async (resolve) => { | ||
const done = () => { | ||
uploaded += 1; | ||
updateSpinner(); | ||
resolve(); | ||
}; | ||
if (release.assets.find(asset => asset.name === path.basename(artifactPath))) { | ||
return done(); | ||
} | ||
await github.getGitHub().repos.uploadAsset({ | ||
owner: forgeConfig.github_repository.owner, | ||
repo: forgeConfig.github_repository.name, | ||
id: release.id, | ||
filePath: artifactPath, | ||
name: path.basename(artifactPath), | ||
}); | ||
return done(); | ||
}) | ||
)); | ||
} catch (err) { | ||
updateSpinner.fail(); | ||
throw err; | ||
} | ||
|
||
uploadSpinner.succeed(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import path from 'path'; | ||
|
||
export default (relativeTo, paths) => { | ||
let result; | ||
for (const testPath of paths.concat(paths.map(mapPath => path.resolve(relativeTo, mapPath)))) { | ||
try { | ||
result = require(testPath); | ||
return typeof result === 'object' && result && result.default ? result.default : result; | ||
} catch (err) { | ||
// Ignore the error | ||
} | ||
} | ||
}; |