1 | #!/usr/bin/env node
|
2 |
|
3 | const shell = require('shelljs');
|
4 | const publishUtils = require('../src/utils');
|
5 | const path = require('path');
|
6 | const packageJson = require(path.resolve('./package.json'));
|
7 | const parseRepo = require('parse-repo');
|
8 |
|
9 | const parseArgs = require('../src/parse-args');
|
10 | const build = require('../src/build');
|
11 | const { config, ...args } = parseArgs(packageJson);
|
12 |
|
13 | // get GIT url
|
14 | console.log('=> Getting the git remote URL');
|
15 | let GIT_URL = publishUtils.exec(
|
16 | `git config --get remote.${args.GIT_REMOTE}.url`
|
17 | );
|
18 |
|
19 | if (!GIT_URL) {
|
20 | console.log('This project is not configured with a remote git repo');
|
21 | process.exit(-1);
|
22 | }
|
23 |
|
24 | build(
|
25 | args.SKIP_BUILD,
|
26 | args.OUTPUT_DIR,
|
27 | packageJson,
|
28 | args.PACKAGES_DIRECTORY,
|
29 | args.NPM_SCRIPT,
|
30 | args.MONOREPO_INDEX_GENERATOR
|
31 | );
|
32 |
|
33 | if (args.DRY_RUN) {
|
34 | return;
|
35 | }
|
36 |
|
37 | // go to the out directory and create a *new* Git repo
|
38 | shell.cd(args.OUTPUT_DIR);
|
39 | publishUtils.exec('git init');
|
40 |
|
41 | // if --source-branch specified, it needs to exist in the new repo too
|
42 | const { SOURCE_BRANCH } = args;
|
43 | if (!!SOURCE_BRANCH) {
|
44 | publishUtils.exec(`git checkout -b ${SOURCE_BRANCH}`);
|
45 | }
|
46 |
|
47 | // inside this git repo we'll pretend to be a new user
|
48 | publishUtils.exec(`git config user.name ${JSON.stringify(config.gitUsername)}`);
|
49 | publishUtils.exec(`git config user.email ${JSON.stringify(config.gitEmail)}`);
|
50 |
|
51 | // disable GPG signing
|
52 | publishUtils.exec('git config commit.gpgsign false');
|
53 |
|
54 | // check if source branch argument is set, and checkout to it
|
55 | if(args.SOURCE_BRANCH) {
|
56 | publishUtils.exec(`git checkout -b ${JSON.stringify(args.SOURCE_BRANCH)}`);
|
57 | console.log(`=> Deploying from source branch: ${JSON.stringify(args.SOURCE_BRANCH)}`);
|
58 | }
|
59 |
|
60 | // The first and only commit to this new Git repo contains all the
|
61 | // files present with the commit message "Deploy to GitHub Pages".
|
62 | publishUtils.exec('git add .');
|
63 | publishUtils.exec(`git commit -m ${JSON.stringify(config.commitMessage)}`);
|
64 |
|
65 | // Force push from the current repo's source branch (master by default) to the remote
|
66 | // repo's gh-pages branch. (All previous history on the gh-pages branch
|
67 | // will be lost, since we are overwriting it.) We redirect any output to
|
68 | // /dev/null to hide any sensitive credential data that might otherwise be exposed.
|
69 | console.log('=> Deploying storybook');
|
70 |
|
71 | if (args.CI_DEPLOY) {
|
72 | const { host, repository } = parseRepo(GIT_URL);
|
73 |
|
74 | if (args.HOST_TOKEN) {
|
75 | GIT_URL = `https://${args.HOST_TOKEN}@${host}/${repository}`;
|
76 | }
|
77 | }
|
78 |
|
79 | const { TARGET_BRANCH } = args;
|
80 |
|
81 | publishUtils.exec(
|
82 | `git push --force --quiet ${GIT_URL} ${SOURCE_BRANCH}:${TARGET_BRANCH}`
|
83 | );
|
84 | shell.cd('..');
|
85 | shell.rm('-rf', args.OUTPUT_DIR);
|
86 |
|
87 | if (TARGET_BRANCH !== 'gh-pages') {
|
88 | const host = GIT_URL.replace('github.com', 'rawgit.com')
|
89 | .replace('.git', '')
|
90 | .replace(/\/$/, '');
|
91 | const rawGitUrl = `${host}/${TARGET_BRANCH}/index.html`;
|
92 |
|
93 | console.log(`=> Storybook deployed to: ${rawGitUrl}`);
|
94 | } else {
|
95 | console.log(
|
96 | `=> Storybook deployed to: ${publishUtils.getGHPagesUrl(GIT_URL)}`
|
97 | );
|
98 | }
|