UNPKG

2.97 kBPlain TextView Raw
1#!/usr/bin/env node
2
3const shell = require('shelljs');
4const publishUtils = require('../src/utils');
5const path = require('path');
6const packageJson = require(path.resolve('./package.json'));
7const parseRepo = require('parse-repo');
8
9const parseArgs = require('../src/parse-args');
10const build = require('../src/build');
11const { config, ...args } = parseArgs(packageJson);
12
13// get GIT url
14console.log('=> Getting the git remote URL');
15let GIT_URL = publishUtils.exec(
16 `git config --get remote.${args.GIT_REMOTE}.url`
17);
18
19if (!GIT_URL) {
20 console.log('This project is not configured with a remote git repo');
21 process.exit(-1);
22}
23
24build(
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
33if (args.DRY_RUN) {
34 return;
35}
36
37// go to the out directory and create a *new* Git repo
38shell.cd(args.OUTPUT_DIR);
39publishUtils.exec('git init');
40
41// if --source-branch specified, it needs to exist in the new repo too
42const { SOURCE_BRANCH } = args;
43if (!!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
48publishUtils.exec(`git config user.name ${JSON.stringify(config.gitUsername)}`);
49publishUtils.exec(`git config user.email ${JSON.stringify(config.gitEmail)}`);
50
51// disable GPG signing
52publishUtils.exec('git config commit.gpgsign false');
53
54// check if source branch argument is set, and checkout to it
55if(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".
62publishUtils.exec('git add .');
63publishUtils.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.
69console.log('=> Deploying storybook');
70
71if (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
79const { TARGET_BRANCH } = args;
80
81publishUtils.exec(
82 `git push --force --quiet ${GIT_URL} ${SOURCE_BRANCH}:${TARGET_BRANCH}`
83);
84shell.cd('..');
85shell.rm('-rf', args.OUTPUT_DIR);
86
87if (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}