#!/usr/bin/env node const shell = require('shelljs'); const publishUtils = require('../src/utils'); const path = require('path'); const packageJson = require(path.resolve('./package.json')); const parseRepo = require('parse-repo'); const parseArgs = require('../src/parse-args'); const build = require('../src/build'); const { config, ...args } = parseArgs(packageJson); // get GIT url console.log('=> Getting the git remote URL'); let GIT_URL = publishUtils.exec( `git config --get remote.${args.GIT_REMOTE}.url` ); if (!GIT_URL) { console.log('This project is not configured with a remote git repo'); process.exit(-1); } build( args.SKIP_BUILD, args.OUTPUT_DIR, packageJson, args.PACKAGES_DIRECTORY, args.NPM_SCRIPT, args.MONOREPO_INDEX_GENERATOR ); if (args.DRY_RUN) { return; } // go to the out directory and create a *new* Git repo shell.cd(args.OUTPUT_DIR); publishUtils.exec('git init'); // if --source-branch specified, it needs to exist in the new repo too const { SOURCE_BRANCH } = args; if (!!SOURCE_BRANCH) { publishUtils.exec(`git checkout -b ${SOURCE_BRANCH}`); } // inside this git repo we'll pretend to be a new user publishUtils.exec(`git config user.name ${JSON.stringify(config.gitUsername)}`); publishUtils.exec(`git config user.email ${JSON.stringify(config.gitEmail)}`); // disable GPG signing publishUtils.exec('git config commit.gpgsign false'); // check if source branch argument is set, and checkout to it if(args.SOURCE_BRANCH) { publishUtils.exec(`git checkout -b ${JSON.stringify(args.SOURCE_BRANCH)}`); console.log(`=> Deploying from source branch: ${JSON.stringify(args.SOURCE_BRANCH)}`); } // The first and only commit to this new Git repo contains all the // files present with the commit message "Deploy to GitHub Pages". publishUtils.exec('git add .'); publishUtils.exec(`git commit -m ${JSON.stringify(config.commitMessage)}`); // Force push from the current repo's source branch (master by default) to the remote // repo's gh-pages branch. (All previous history on the gh-pages branch // will be lost, since we are overwriting it.) We redirect any output to // /dev/null to hide any sensitive credential data that might otherwise be exposed. console.log('=> Deploying storybook'); if (args.CI_DEPLOY) { const { host, repository } = parseRepo(GIT_URL); if (args.HOST_TOKEN) { GIT_URL = `https://${args.HOST_TOKEN}@${host}/${repository}`; } } const { TARGET_BRANCH } = args; publishUtils.exec( `git push --force --quiet ${GIT_URL} ${SOURCE_BRANCH}:${TARGET_BRANCH}` ); shell.cd('..'); shell.rm('-rf', args.OUTPUT_DIR); if (TARGET_BRANCH !== 'gh-pages') { const host = GIT_URL.replace('github.com', 'rawgit.com') .replace('.git', '') .replace(/\/$/, ''); const rawGitUrl = `${host}/${TARGET_BRANCH}/index.html`; console.log(`=> Storybook deployed to: ${rawGitUrl}`); } else { console.log( `=> Storybook deployed to: ${publishUtils.getGHPagesUrl(GIT_URL)}` ); }