Skip to content

Commit

Permalink
Merge pull request binary-com#4619 from binary-hamedanchi/mohammad/sp…
Browse files Browse the repository at this point in the history
…lit_release_process

[new-app] mohammad/split_release_process
  • Loading branch information
ashkanx authored Nov 15, 2018
2 parents 4791064 + 4b9ca70 commit 0ce008f
Show file tree
Hide file tree
Showing 41 changed files with 572 additions and 289 deletions.
10 changes: 5 additions & 5 deletions 404.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@

/**
* A helper function to add suport of HTML5 routing to gh-pages
* @return {boolean} if window.location.href doesn't contain app.html it will returns false immediately
* @return {boolean} if window.location.href doesn't contain app.html it will return false immediately
*/
function handleReactUrl() {

var l = window.location;

// if the url is not related to new app, returns false.
var lang_regex_string = '(.*)\/(' + all_languages.join('|') + ')\/app\/(\\w+\\.html)?';
var rx_result = new RegExp(lang_regex_string).exec(l.pathname)
var lang_regex_string = '(.*)\/app\/(' + all_languages.join('|') + ')\/(\\w+\\.html)?';
var rx_result = new RegExp(lang_regex_string).exec(l.pathname);

if (!new RegExp(lang_regex_string).test(l.pathname) || (rx_result[3] && rx_result[3] !== 'index.html')) {
return false;
}

var sub_directories = rx_result[1];

// Since there are two sub entries after CNAME URL for the app 'www.binary.com/en/app'
// Since there are two sub entries after CNAME URL for the app 'www.binary.com/app/en'
// the value of segment_count should be 2.
var segment_count = 2 + sub_directories.split('/').length;

Expand All @@ -105,7 +105,7 @@

// Converting the path and query strings into just query strings and
// then redirects the browser to the new url with only query strings and hash fragment.
// https://www.binary.com/en/app/statement -> https://www.binary.com/en/app?p=/statement
// https://www.binary.com/app/en/statement -> https://www.binary.com/app/en?p=/statement
l.replace(
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
pathname.split('/').slice(0, segment_count).join('/') + '/?p=' +
Expand Down
35 changes: 3 additions & 32 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,7 @@
module.exports = function (grunt) {
// map release parameters to the required branch, origin, and target gh-pages sub-folder
global.release_config = {
production : { branch: 'master', target_folder: '', origin: '[email protected]:binary-static-deployed/binary-static.git', CNAME: 'www.binary.com' },
staging : { branch: 'master', target_folder: '', origin: '[email protected]:binary-com/binary-static.git', CNAME: 'staging.binary.com' },
translations: { branch: 'translations', target_folder: 'translations', origin: '[email protected]:binary-com/binary-static.git', CNAME: 'staging.binary.com' },
};

global.is_production = grunt.cli.tasks[0] === 'release';

if (global.is_production) {
Object.keys(global.release_config).forEach(function (target) {
if (grunt.option(target)) {
global.release_target = target;
}
});
}
const { initGlobals } = require('./build/config/init');

if (global.release_target) {
global.release_info = global.release_config[global.release_target];
global.branch_prefix = '';
global.branch = global.release_info.target_folder;
} else {
global.branch_prefix = 'br_';
global.branch = grunt.option('branch');
}

global.dist = `dist${global.branch ? `/${global.branch_prefix}${global.branch}` : ''}`;
global.path = grunt.option('path');

global.compileCommand = function(params) {
return `cd ${process.cwd()} && ./scripts/render.js ${params || ''} ${global.branch ? '-b ' + global.branch_prefix + global.branch : ''} ${global.path ? '-p ' + global.path : ''}`;
};
module.exports = function (grunt) {
initGlobals(grunt);

require('time-grunt')(grunt);

Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ In order to remove the created folders from your gh-pages, you can use either:
- `grunt shell:compile_dev --path=about-us` to re-compile only template(s) which serve about-us path in URL.
- To fix eslint errors run `npm run eslint`

### Sections
The codebase is divided into different sections. Passing `--section=[all|app|app_2]` to almost all grunt commands causes to run it on the specified section (excluding tests since it should run on the whole source code).

Current sections are:
- `app` represents current version of the website including `app`, `static`, `landing_pages` folders in the source code.
- `app_2` is the next version which its source code is mainly inside the `app_2` folders.
- `all` denotes to all available sections and is the default value if `--section` is not specified.

Although section is mandatory for release, but it is optional for the rest of commands as there is a default value (`all`).

## Release

```
grunt release --staging=1|--production=1|--translations=1 [--cleanup] [--reset]
grunt release --staging=1|--production=1|--translations=1 --section=all|app|app_2 [--cleanup] [--reset]
```
(The value is needed when more than one option is used)

Expand All @@ -95,6 +104,8 @@ grunt release --staging=1|--production=1|--translations=1 [--cleanup] [--reset]
- In order to prevent accidentally releasing to the wrong target, it is mandatory to provide one of these parameters.
- Your remote origin will be checked to be the correct target of the given parameter.
- Your current branch will be checked to be the correct branch of the given parameter.
- `--section` (mandatory)
- In order to prevent accidentally releases, it is mandatory to specify the section.
- `--cleanup` [optional]
- Create CNAME file with proper value according to remote origin
- Deploy to gh-pages with the option `add: false`
Expand Down
2 changes: 1 addition & 1 deletion build/aliases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ css:
- 'cssmin'

js:
- 'webpack:all'
- 'webpack:section'
- 'babel:landing_pages'

test:
Expand Down
44 changes: 27 additions & 17 deletions build/babel.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
module.exports = {
landing_pages: {
options: {
minified : true,
plugins : ['transform-remove-strict-mode'],
presets : ['env'],
sourceMap : true,
sourceType: 'script',
},
files: [
{
expand: true,
cwd : 'src/javascript/landing_pages/',
src : ['*.js'],
dest : global.dist + '/js/landing_pages/'
module.exports = function (grunt) {
const config = {
app: {
options: {
minified : true,
plugins : ['transform-remove-strict-mode'],
presets : ['env'],
sourceMap : true,
sourceType: 'script',
},
],
},
files : [
{
expand: true,
cwd : 'src/javascript/landing_pages/',
src : ['*.js'],
dest : global.dist + '/js/landing_pages/'
},
],
},
app_2: {},
get all() {
return this.app;
},
};

return {
landing_pages: config[global.section],
};
};
35 changes: 35 additions & 0 deletions build/config/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const targets_config = {
production: {
origin: '[email protected]:binary-static-deployed/binary-static.git',
CNAME : 'www.binary.com',
},
staging: {
origin: '[email protected]:binary-com/binary-static.git',
CNAME : 'staging.binary.com',
},
};

// map release parameters to the required branch, origin, CNAME, and target gh-pages sub-folder
const release_config = {
production : { branch: 'master', target_folder: '', origin: targets_config.production.origin, CNAME: targets_config.production.CNAME },
staging : { branch: 'master', target_folder: '', origin: targets_config.staging.origin, CNAME: targets_config.staging.CNAME },
translations: { branch: 'translations', target_folder: 'translations', origin: targets_config.staging.origin, CNAME: targets_config.staging.CNAME },
};

const node_modules_paths = {
binary_style: 'node_modules/@binary-com/binary-style',
smartcharts : 'node_modules/@binary-com/smartcharts',
};

const config = {
branch_prefix : 'br_',
valid_sections : ['all', 'app', 'app_2'],
default_section: 'all',
app_2_folder : 'app',
};

module.exports = {
release_config,
node_modules_paths,
config,
};
45 changes: 45 additions & 0 deletions build/config/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const Constants = require('./constants');

const isRelease = (grunt) => grunt.cli.tasks[0] === 'release';

const getReleaseTarget = (grunt) => Object.keys(global.release_config).find(grunt.option);

const getSection = (grunt) => {
const section = grunt.option('section') || Constants.config.default_section;

if (!Constants.config.valid_sections.includes(section)) {
grunt.fail.fatal(`Unknown section: '${section}'.\nValid sections are: ${Constants.config.valid_sections.join(', ')}.`);
}

return section;
};

const checkSection = (grunt) => {
if (!grunt.option('section')) {
grunt.fail.fatal(`It is mandatory to specify the section when releasing.\nValid sections are: ${Constants.config.valid_sections.join(', ')}`);
}
};

const getDistPath = () => `dist${global.branch ? `/${global.branch_prefix}${global.branch}` : ''}`;

const generateCompileCommand = (params) => (
[
`cd ${process.cwd()}`,
'&& ./scripts/render.js',
...[ // parameters
params || '',
global.branch ? `-b ${global.branch_prefix}${global.branch}` : '',
global.path ? `-p ${global.path}` : '',
`-s ${global.section}`,
],
].join(' ')
);

module.exports = {
isRelease,
getReleaseTarget,
getSection,
checkSection,
getDistPath,
generateCompileCommand,
};
40 changes: 40 additions & 0 deletions build/config/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const Constants = require('./constants');
const Helpers = require('./helpers');

const initGlobals = (grunt) => {
// ----- global info -----
global.node_modules_paths = Constants.node_modules_paths;
global.release_config = Constants.release_config;

// ----- release info -----
global.is_release = Helpers.isRelease(grunt);
if (global.is_release) {
Helpers.checkSection(grunt); // To prevent mistakes, section is mandatory when releasing
global.release_target = Helpers.getReleaseTarget(grunt);
}

// ----- branch info -----
if (global.release_target) {
global.release_info = global.release_config[global.release_target];
global.branch_prefix = '';
global.branch = global.release_info.target_folder;
} else {
global.branch_prefix = Constants.config.branch_prefix;
global.branch = grunt.option('branch');
}

// ----- section -----
global.section = Helpers.getSection(grunt);

// ----- paths -----
global.dist = Helpers.getDistPath();
global.dist_app_2 = `${global.dist}/${Constants.config.app_2_folder}`;
global.path = grunt.option('path');

// ----- compile templates -----
global.compileCommand = params => Helpers.generateCompileCommand(params);
};

module.exports = {
initGlobals,
};
9 changes: 6 additions & 3 deletions build/connect.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const rewrite = require('connect-modrewrite');
const serveIndex = require('serve-index');
const serveStatic = require('serve-static');
const languages = require('../scripts/common').languages;

module.exports = function (grunt) {
lang_regex = languages.join('|').toLowerCase();

return {
livereload: {
options: {
Expand All @@ -11,7 +14,7 @@ module.exports = function (grunt) {
protocol : 'https',
base : 'dist',
open : {
appName: {
appName: {
app: 'Google\ Chrome'
},
target : 'https://localhost',
Expand All @@ -23,8 +26,8 @@ module.exports = function (grunt) {

const rules = [
'^/binary-static/(.*)$ /$1',
'^/(.*)/app/index(\\.html)?/(.*)$ /$1/app/$2 [L]',
'^/(.*)/app/.*$ /$1/app/ [L]',
`^/app/(${lang_regex})/index(\\.html)?/(.*)$ /app/$1/$2 [L]`,
`^/app/(${lang_regex})/.*$ /app/$1/ [L]`,
];
middlewares.push(rewrite(rules));

Expand Down
Loading

0 comments on commit 0ce008f

Please sign in to comment.