Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
make sure. deploy works and run prettier on everything
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Dec 3, 2018
1 parent 4c52064 commit 0014e98
Show file tree
Hide file tree
Showing 9 changed files with 1,654 additions and 94 deletions.
13 changes: 8 additions & 5 deletions CHANGELOG.md
@@ -1,18 +1,21 @@
# Change Log

### v1.2.0

09-Sep-2016

* Add a trailing slash to the URL where it's the correct GH pages URL.
- Add a trailing slash to the URL where it's the correct GH pages URL.

### v1.1.0

08-Aug-2016

* Fix issue when there's already a build-storybook command. See: [#1](https://github.com/kadirahq/storybook-deployer/issues/1).
* Add some better logs when executing commands.
* Now we create a random directory to build the output.
- Fix issue when there's already a build-storybook command. See: [#1](https://github.com/kadirahq/storybook-deployer/issues/1).
- Add some better logs when executing commands.
- Now we create a random directory to build the output.

### v1.0.0

07-May-2016

* Initial Release
- Initial Release
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -25,7 +25,7 @@ or like this for AWS S3:
```json
{
"scripts": {
"deploy-storybook": "storybook-to-aws-s3",
"deploy-storybook": "storybook-to-aws-s3"
}
}
```
Expand Down
20 changes: 10 additions & 10 deletions bin/storybook_to_aws_s3
@@ -1,12 +1,12 @@
#!/usr/bin/env node

const shell = require("shelljs");
const publishUtils = require("../src/utils");
const build = require("../src/build");
const path = require("path");
const packageJson = require(path.resolve("./package.json"));
const argv = require("yargs").argv;
const parseArgs = require("../src/parse-args");
const shell = require('shelljs');
const publishUtils = require('../src/utils');
const build = require('../src/build');
const path = require('path');
const packageJson = require(path.resolve('./package.json'));
const argv = require('yargs').argv;
const parseArgs = require('../src/parse-args');
const { config, ...args } = parseArgs(packageJson, argv);

if (!args.BUCKET_PATH) {
Expand All @@ -25,11 +25,11 @@ build(
args.MONOREPO_INDEX_GENERATOR
);

console.log("=> Deploying storybook");
console.log('=> Deploying storybook');
publishUtils.exec(
`aws --profile ${args.AWS_PROFILE} s3 sync ${args.OUTPUT_DIR} ${args.S3_PATH}`
);

shell.rm("-rf", args.OUTPUT_DIR);
shell.rm('-rf', args.OUTPUT_DIR);

console.log("=> Storybook deployed to: ${S3_PATH}");
console.log('=> Storybook deployed to: ${S3_PATH}');
69 changes: 34 additions & 35 deletions bin/storybook_to_ghpages
@@ -1,24 +1,23 @@
#!/usr/bin/env node

const shell = require("shelljs");
const publishUtils = require("../src/utils");
const path = require("path");
const fs = require("fs");
const packageJson = require(path.resolve("./package.json"));
const argv = require("yargs").argv;
const parseRepo = require("parse-repo");
const shell = require('shelljs');
const publishUtils = require('../src/utils');
const path = require('path');
const packageJson = require(path.resolve('./package.json'));
const argv = require('yargs').argv;
const parseRepo = require('parse-repo');

const parseArgs = require("../src/parse-args");
const build = require("../src/build");
const parseArgs = require('../src/parse-args');
const build = require('../src/build');
const { config, ...args } = parseArgs(packageJson, argv);

// get GIT url
console.log("=> Getting the git remote 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");
console.log('This project is not configured with a remote git repo');
process.exit(-1);
}

Expand All @@ -33,61 +32,61 @@ build(

// go to the out directory and create a *new* Git repo
shell.cd(args.OUTPUT_DIR);
publishUtils.exec("git init");
publishUtils.exec('git init');

// 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));
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");
publishUtils.exec('git config commit.gpgsign false');

// 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));
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");
console.log('=> Deploying storybook');
if (args.CI_DEPLOY) {
const repositoryDetails = parseRepo(GIT_URL);

if (repositoryDetails.host === "github.com" && args.HOST_TOKEN) {
if (repositoryDetails.host === 'github.com' && args.HOST_TOKEN) {
GIT_URL =
"https://" +
'https://' +
args.HOST_TOKEN +
"@" +
'@' +
repositoryDetails.host +
"/" +
'/' +
repositoryDetails.repository;
}
}

publishUtils.exec(
"git push --force --quiet " +
'git push --force --quiet ' +
GIT_URL +
" " +
' ' +
args.SOURCE_BRANCH +
":" +
':' +
args.TARGET_BRANCH
);
shell.cd("..");
shell.rm("-rf", args.OUTPUT_DIR);
shell.cd('..');
shell.rm('-rf', args.OUTPUT_DIR);

if (args.TARGET_BRANCH !== "gh-pages") {
if (args.TARGET_BRANCH !== 'gh-pages') {
const rawGitUrl =
GIT_URL.replace("github.com", "rawgit.com")
.replace(".git", "")
.replace(/\/$/, "") +
"/" +
GIT_URL.replace('github.com', 'rawgit.com')
.replace('.git', '')
.replace(/\/$/, '') +
'/' +
args.TARGET_BRANCH +
"/index.html";
console.log("=> Storybook deployed to: " + rawGitUrl);
'/index.html';
console.log('=> Storybook deployed to: ' + rawGitUrl);
} else {
console.log(
"=> Storybook deployed to: " + publishUtils.getGHPagesUrl(GIT_URL)
'=> Storybook deployed to: ' + publishUtils.getGHPagesUrl(GIT_URL)
);
}
19 changes: 19 additions & 0 deletions package.json
Expand Up @@ -28,5 +28,24 @@
"parse-repo": "^1.0.4",
"shelljs": "^0.8.1",
"yargs": "^11.0.0"
},
"devDependencies": {
"husky": "^1.2.0",
"lint-staged": "^8.1.0",
"prettier": "^1.15.3"
},
"prettier": {
"singleQuote": true
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,json,css,md}": [
"prettier --write",
"git add"
]
}
}
7 changes: 5 additions & 2 deletions src/build.js
@@ -1,6 +1,7 @@
const buildMonorepoIndex = require('./build-monorepo-index');
const glob = require('glob');
const path = require('path');
const fs = require('fs');
const publishUtils = require('./utils');
const shell = require('shelljs');

Expand Down Expand Up @@ -47,7 +48,7 @@ function buildSubPackage(origDir, dir, outputDirectory, npmScriptName) {
return subPackage;
}

module.exports = function (
module.exports = function(
skipBuild,
outputDirectory,
packageJson,
Expand All @@ -67,7 +68,9 @@ module.exports = function (
ignore: '**/node_modules/**'
})
.map(json => path.dirname(json))
.map(subPackage => buildSubPackage(origDir, subPackage, outputDirectory, npmScriptName))
.map(subPackage =>
buildSubPackage(origDir, subPackage, outputDirectory, npmScriptName)
)
.filter(subPackage => subPackage);

shell.cd(origDir);
Expand Down
61 changes: 32 additions & 29 deletions src/parse-args.js
@@ -1,32 +1,35 @@
const defaultConfig = {
gitUsername: "GH Pages Bot",
gitEmail: "hello@ghbot.com",
commitMessage: "Deploy Storybook to GitHub Pages"
gitUsername: 'GH Pages Bot',
gitEmail: 'hello@ghbot.com',
commitMessage: 'Deploy Storybook to GitHub Pages'
};

module.exports = (packageJson, argv) => ({
config: Object.assign(
{},
defaultConfig,
packageJson["storybook-deployer"] || defaultConfig
),
SKIP_BUILD: Boolean(argv["existing-output-dir"]),
OUTPUT_DIR:
argv.out ||
argv["existing-output-dir"] ||
"out" + Math.ceil(Math.random() * 9999),
PACKAGES_DIRECTORY: argv["packages"],
MONOREPO_INDEX_GENERATOR: argv["monorepo-index-generator"],
NPM_SCRIPT: argv["script"] || "build-storybook",
CI_DEPLOY: Boolean(argv["ci"]),
// Git Variables
GIT_REMOTE: argv["remote"] || "origin",
TARGET_BRANCH: argv["branch"] || "gh-pages",
SOURCE_BRANCH: argv["source-branch"] || "master",
HOST_TOKEN_ENV_VARIABLE: argv["host-token-env-variable"] || "GH_TOKEN",
HOST_TOKEN: process.env[HOST_TOKEN_ENV_VARIABLE],
// AWS Variables
AWS_PROFILE: argv["aws-profile"] || "default",
BUCKET_PATH: argv["bucket-path"],
S3_PATH: "s3://" + argv["bucket-path"]
});
module.exports = (packageJson, argv) => {
const HOST_TOKEN_ENV_VARIABLE = argv['host-token-env-variable'] || 'GH_TOKEN';

return {
config: Object.assign(
{},
defaultConfig,
packageJson['storybook-deployer'] || defaultConfig
),
SKIP_BUILD: Boolean(argv['existing-output-dir']),
OUTPUT_DIR:
argv.out ||
argv['existing-output-dir'] ||
'out' + Math.ceil(Math.random() * 9999),
PACKAGES_DIRECTORY: argv['packages'],
MONOREPO_INDEX_GENERATOR: argv['monorepo-index-generator'],
NPM_SCRIPT: argv['script'] || 'build-storybook',
CI_DEPLOY: Boolean(argv['ci']),
// Git Variables
GIT_REMOTE: argv['remote'] || 'origin',
TARGET_BRANCH: argv['branch'] || 'gh-pages',
SOURCE_BRANCH: argv['source-branch'] || 'master',
HOST_TOKEN: process.env[HOST_TOKEN_ENV_VARIABLE],
// AWS Variables
AWS_PROFILE: argv['aws-profile'] || 'default',
BUCKET_PATH: argv['bucket-path'],
S3_PATH: 's3://' + argv['bucket-path']
};
};
21 changes: 14 additions & 7 deletions src/utils.js
Expand Up @@ -2,15 +2,19 @@ var shell = require('shelljs');
var parseGitUrl = require('git-url-parse');

module.exports.exec = function exec(command) {
console.log(" executing: " + command);
console.log(' executing: ' + command);
var options = { silent: true };
var ref = shell.exec(command, options);
if (ref.code === 0) {
return ref.stdout.trim();
return ref.stdout.trim();
}

var message =
'Exec code(' + ref.code + ') on executing: ' + command + '\n' +
'Exec code(' +
ref.code +
') on executing: ' +
command +
'\n' +
shell.stderr;

throw new Error(message);
Expand All @@ -20,10 +24,13 @@ module.exports.getGHPagesUrl = function getGHPagesUrl(ghUrl) {
var parsedUrl = parseGitUrl(ghUrl);
var ghPagesUrl;
if (parsedUrl.resource === 'github.com') {
ghPagesUrl = 'https://' + parsedUrl.owner + '.github.io/' + parsedUrl.name + '/';
} else { // Github Enterprise
ghPagesUrl = 'https://' + parsedUrl.resource + '/pages/' + parsedUrl.full_name + '/';
ghPagesUrl =
'https://' + parsedUrl.owner + '.github.io/' + parsedUrl.name + '/';
} else {
// Github Enterprise
ghPagesUrl =
'https://' + parsedUrl.resource + '/pages/' + parsedUrl.full_name + '/';
}

return ghPagesUrl;
};

0 comments on commit 0014e98

Please sign in to comment.