Skip to content
Permalink
Browse files

refactor(publisher): move github publish logic to own file

set up for theoretical multiple publishers
  • Loading branch information
MarshallOfSound committed Dec 29, 2016
1 parent 02fe569 commit bdaff3ce81115f41f00f231f099946d85270d8e9
Showing with 109 additions and 75 deletions.
  1. +9 −10 src/electron-forge-make.js
  2. +14 −65 src/electron-forge-publish.js
  3. +73 −0 src/publishers/github.js
  4. +13 −0 src/util/require-search.js
@@ -9,6 +9,7 @@ import electronHostArch from './util/electron-host-arch';
import getForgeConfig from './util/forge-config';
import packager from './electron-forge-package';
import readPackageJSON from './util/read-package-json';
import requireSearch from './util/require-search';
import resolveDir from './util/resolve-dir';

const main = async () => {
@@ -89,16 +90,14 @@ const main = async () => {

for (const target of targets) {
const makeSpinner = ora.ora(`Making for target: ${target.cyan} - On platform: ${declaredPlatform.cyan} - For arch: ${targetArch.cyan}`).start();
let maker;
try {
maker = require(`./makers/${process.platform}/${target}.js`);
} catch (err1) {
try {
maker = require(`./makers/generic/${target}.js`);
} catch (err2) {
makeSpinner.fail();
throw new Error(`Could not find a build target with the name: ${target} for the platform: ${declaredPlatform}`);
}
const maker = requireSearch(__dirname, [
`./makers/${process.platform}/${target}.js`,
`./makers/generic/${target}.js`,
`electron-forge-maker-${target}`,
]);
if (!maker) {
makeSpinner.fail();
throw new Error(`Could not find a build target with the name: ${target} for the platform: ${declaredPlatform}`);
}
try {
outputs.push(await (maker.default || maker)(packageDir, appName, targetArch, forgeConfig, packageJSON));
@@ -6,8 +6,8 @@ import ora from 'ora';

import './util/terminate';
import getForgeConfig from './util/forge-config';
import GitHub from './util/github';
import readPackageJSON from './util/read-package-json';
import requireSearch from './util/require-search';
import resolveDir from './util/resolve-dir';

import make from './electron-forge-make';
@@ -19,8 +19,9 @@ const main = async () => {
program
.version(require('../package.json').version)
.arguments('[cwd]')
.option('--auth-token', 'Provided GitHub authorization token')
.option('--auth-token', 'Authorization token for your publisher target (if required)')
.option('-t, --tag', 'The tag to publish to on GitHub')
.option('--target', 'The deployment target, defaults to "github"')
.allowUnknownOption(true)
.action((cwd) => {
if (!cwd) return;
@@ -48,73 +49,21 @@ const main = async () => {

const forgeConfig = await getForgeConfig(dir);

if (!(forgeConfig.github_repository && typeof forgeConfig.github_repository === 'object' &&
forgeConfig.github_repository.owner && forgeConfig.github_repository.name)) {
console.error('In order to publish 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(program.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 === (program.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: program.tag || `v${packageJSON.version}`,
name: program.tag || `v${packageJSON.version}`,
draft: true,
});
} else {
// Unknown error
throw err;
}
}
if (!program.target) program.target = 'github';

let uploaded = 0;
const uploadSpinner = ora.ora(`Uploading Artifacts ${uploaded}/${artifacts.length}`).start();
const updateSpinner = () => {
uploadSpinner.text = `Uploading Artifacts ${uploaded}/${artifacts.length}`;
};
const targetSpinner = ora.ora(`Resolving publish target: ${`${program.target}`.cyan}`).start();

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;
const publisher = requireSearch(__dirname, [
`./publishers/${program.target}.js`,
`electron-forge-publisher-${program.target}`,
]);
if (!publisher) {
targetSpinner.fail();
throw new Error(`Could not find a publish target with the name: ${program.target}`);
}
targetSpinner.succeed();

uploadSpinner.succeed();
await publisher(artifacts, packageJSON, forgeConfig, program.authToken, program.tag);
};

main();
@@ -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();
};
@@ -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
}
}
};

0 comments on commit bdaff3c

Please sign in to comment.
You can’t perform that action at this time.