Skip to content

Commit

Permalink
Combine deployment scripts into a single; remove git-repository depen…
Browse files Browse the repository at this point in the history
…dency (kriasoft#1127)
  • Loading branch information
frenzzy authored Feb 10, 2017
1 parent ed5dfa9 commit 18aaf2a
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 116 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"eslint-plugin-react": "^6.9.0",
"file-loader": "^0.10.0",
"front-matter": "^2.1.2",
"git-repository": "^0.1.4",
"glob": "^7.1.1",
"json-loader": "^0.5.4",
"lint-staged": "^3.3.0",
Expand Down Expand Up @@ -217,7 +216,7 @@
"copy": "babel-node tools/run copy",
"bundle": "babel-node tools/run bundle",
"build": "babel-node tools/run build",
"deploy": "babel-node tools/run deployToAzureWebApps",
"deploy": "babel-node tools/run deploy",
"render": "babel-node tools/run render",
"serve": "babel-node tools/run runServer",
"start": "babel-node tools/run start"
Expand Down
86 changes: 86 additions & 0 deletions tools/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* React Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2014-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

import path from 'path';
import fetch from 'node-fetch';
import { spawn } from './lib/cp';
import { makeDir } from './lib/fs';
import run from './run';

// GitHub Pages
const remote = {
name: 'github',
url: 'https://github.com/<user>/<repo>.git',
branch: 'gh-pages',
website: 'https://<user>.github.io/<repo>/',
static: true,
};

// Azure Web Apps
// const remote = {
// name: 'azure',
// url: 'https://<user>@<app>.scm.azurewebsites.net:443/<app>.git',
// branch: 'master',
// website: `http://<app>.azurewebsites.net`,
// };

const options = {
cwd: path.resolve(__dirname, '../build', remote.static ? 'public' : ''),
stdio: ['ignore', 'inherit', 'inherit'],
};

/**
* Deploy the contents of the `/build` folder to a remote server via Git.
*/
async function deploy() {
// Initialize a new repository
await makeDir('build/public');
await spawn('git', ['init', '--quiet'], options);

// Changing a remote's URL
let isRemoteExists = false;
try {
await spawn('git', ['config', '--get', `remote.${remote.name}.url`], options);
isRemoteExists = true;
} catch (error) {
/* skip */
}
await spawn('git', ['remote', isRemoteExists ? 'set-url' : 'add', remote.name, remote.url], options);

// Fetch the remote repository if it exists
let isRefExists = false;
try {
await spawn('git', ['ls-remote', '--exit-code', remote.url, remote.branch], options);
isRefExists = true;
} catch (error) {
/* skip */
}
if (isRefExists) {
await spawn('git', ['fetch', remote.name], options);
await spawn('git', ['reset', `${remote.name}/${remote.branch}`, '--hard'], options);
await spawn('git', ['clean', '--force'], options);
}

// Build the project in RELEASE mode which
// generates optimized and minimized bundles
process.argv.push('--release');
if (remote.static) process.argv.push('--static');
await run(require('./build').default);

// Push the contents of the build folder to the remote server via Git
await spawn('git', ['add', '.', '--all'], options);
await spawn('git', ['commit', '--message', `Update ${new Date().toISOString()}`], options);
await spawn('git', ['push', remote.name, `master:${remote.branch}`, '--force', '--set-upstream'], options);

// Check if the site was successfully deployed
const response = await fetch(remote.website);
console.log(`${remote.website} => ${response.status} ${response.statusText}`);
}

export default deploy;
60 changes: 0 additions & 60 deletions tools/deployToAzureWebApps.js

This file was deleted.

46 changes: 0 additions & 46 deletions tools/deployToGitHubPages.js

This file was deleted.

22 changes: 22 additions & 0 deletions tools/lib/cp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* React Starter Kit (https://www.reactstarterkit.com/)
*
* Copyright © 2014-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

import cp from 'child_process';

export const spawn = (command, args, options) => new Promise((resolve, reject) => {
cp.spawn(command, args, options).on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`${command} ${args.join(' ')} => ${code} (error)`));
}
});
});

export default { spawn };
10 changes: 10 additions & 0 deletions tools/lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ export const copyDir = async (source, target) => {
export const cleanDir = (pattern, options) => new Promise((resolve, reject) =>
rimraf(pattern, { glob: options }, (err, result) => (err ? reject(err) : resolve(result))),
);

export default {
readFile,
writeFile,
copyFile,
readDir,
makeDir,
copyDir,
cleanDir,
};
2 changes: 1 addition & 1 deletion tools/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const RUNNING_REGEXP = /The server is running at http:\/\/(.*?)\//;

let server;
let pending = true;
const { output } = webpackConfig.find(x => x.target === 'node');
const { output } = webpackConfig.find(x => x.name === 'server');
const serverPath = path.join(output.path, output.filename);

// Launch or restart the Node.js server
Expand Down
2 changes: 1 addition & 1 deletion tools/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function start() {
await new Promise((resolve) => {
// Save the server-side bundle files to the file system after compilation
// https://github.com/webpack/webpack-dev-server/issues/62
webpackConfig.find(x => x.target === 'node').plugins.push(
webpackConfig.find(x => x.name === 'server').plugins.push(
new WriteFilePlugin({ log: false }),
);

Expand Down
8 changes: 2 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2949,10 +2949,6 @@ getpass@^0.1.1:
dependencies:
assert-plus "^1.0.0"

git-repository@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/git-repository/-/git-repository-0.1.4.tgz#678391ab6e0c8b3c24f73762ecebb63c769474ac"

glob-base@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/glob-base/-/glob-base-0.3.0.tgz#dbb164f6221b1c0b1ccf82aea328b497df0ea3c4"
Expand Down Expand Up @@ -4041,7 +4037,7 @@ lodash.uniq@^4.3.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

[email protected]:
[email protected], lodash@^4.2.0:
version "4.12.0"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.12.0.tgz#2bd6dc46a040f59e686c972ed21d93dc59053258"

Expand All @@ -4053,7 +4049,7 @@ lodash@^3.10.1:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

lodash@^4.0.0, lodash@^4.1.0, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0, lodash@^4.5.1, lodash@^4.6.1:
lodash@^4.0.0, lodash@^4.1.0, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.3.0, lodash@^4.5.1, lodash@^4.6.1:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

Expand Down

0 comments on commit 18aaf2a

Please sign in to comment.