Skip to content

Commit

Permalink
Add publish.sh to publish to NPM-JS and Github Pages. (#429)
Browse files Browse the repository at this point in the history
Fixes #385 as a bonus.
  • Loading branch information
RomainMuller authored Jul 30, 2018
1 parent 44a4be9 commit 4689f7f
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ Set-ExecutionPolicy Unrestricted

### Install the command-line toolkit and docs

Install (or update) `aws-cdk` and `aws-cdk-docs` globally
Install (or update) `aws-cdk` globally

```shell
y-npm install --global aws-cdk aws-cdk-docs # sudo might be needed
y-npm install --global aws-cdk # sudo might be needed
```

> `y-npm` is an npm wrapper which allows installing npm modules from a local repository located at `~/.cdk/y/npm`. `y-npm` will fall back to the public npm repository if a module cannot be found locally.
Expand Down
10 changes: 6 additions & 4 deletions bundle-beta.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ echo "Staging: ${staging}"
# │ └ maven
# ├─ y
# │ └─ npm
# └─ node_modules
# ├─ node_modules
# └─ .version

# Creating a `y-npm` registry
echo "Preparing local NPM registry"
Expand All @@ -50,13 +51,14 @@ mkdir -p repo/maven
rsync -av ${root}/packages/aws-cdk-java/maven-repo/ repo/maven/
rsync -av ${root}/node_modules/jsii-java-runtime/maven-repo/ repo/maven/

# Symlink the docs website to docs
echo "Symlinking docs"
ln -s node_modules/aws-cdk-docs/dist/docs docs
# Copy the docs website to docs
echo "Copying docs"
cp -r ${root}/packages/aws-cdk-docs/dist/docs docs

# Create an archive under ./dist
echo "Creating ZIP bundle"
version="$(cat ${root}/lerna.json | grep version | cut -d '"' -f4)"
echo ${version} > .version
dist=${root}/dist
output=${dist}/aws-cdk-${version}.zip
rm -fr ${dist}
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk-docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "aws-cdk-docs",
"version": "0.7.4-beta",
"private": true,
"description": "AWS CDK Documentation",
"private": true,
"main": "lib/index.js",
Expand Down
26 changes: 11 additions & 15 deletions packages/aws-cdk/lib/commands/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import childProcess = require('child_process');
import colors = require('colors/safe');
import process = require('process');
import yargs = require('yargs');
import { debug, error, warning } from '../../lib/logging';
import { debug, print, warning } from '../../lib/logging';

export const command = 'docs';
export const describe = 'Opens the documentation in a browser';
Expand All @@ -20,22 +20,18 @@ export interface Arguments extends yargs.Arguments {
browser: string
}

export async function handler(argv: Arguments): Promise<number> {
let documentationIndexPath: string;
try {
// tslint:disable-next-line:no-var-require Taking an un-declared dep on aws-cdk-docs, to avoid a dependency circle
const docs = require('aws-cdk-docs');
documentationIndexPath = docs.documentationIndexPath;
} catch (err) {
error('Unable to open CDK documentation - the aws-cdk-docs package appears to be missing. Please run `npm install -g aws-cdk-docs`');
return -1;
}

const browserCommand = argv.browser.replace(/%u/g, documentationIndexPath);
export function handler(argv: Arguments): Promise<number> {
const docVersion = require('../../package.json').version;
const url = `https://awslabs.github.io/aws-cdk/versions/${docVersion}/`;
print(colors.green(url));
const browserCommand = argv.browser.replace(/%u/g, url);
debug(`Opening documentation ${colors.green(browserCommand)}`);
return await new Promise<number>((resolve, reject) => {
return new Promise<number>((resolve, _reject) => {
childProcess.exec(browserCommand, (err, stdout, stderr) => {
if (err) { return reject(err); }
if (err) {
debug(`An error occurred when trying to open a browser: ${err.stack || err.message}`);
return resolve(0);
}
if (stdout) { debug(stdout); }
if (stderr) { warning(stderr); }
resolve(0);
Expand Down
14 changes: 1 addition & 13 deletions packages/aws-cdk/lib/commands/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export async function handler(): Promise<number> {

const verifications: Array<() => boolean | Promise<boolean>> = [
displayVersionInformation,
displayAwsEnvironmentVariables,
checkDocumentationIsAvailable
displayAwsEnvironmentVariables
];

// ### Verifications ###
Expand All @@ -42,14 +41,3 @@ function displayAwsEnvironmentVariables() {
}
return true;
}

function checkDocumentationIsAvailable() {
try {
const version = require('aws-cdk-docs/package.json').version;
print(`✅ AWS CDK Documentation: ${version}`);
return true;
} catch (e) {
print(`❌ AWS CDK Documentation: install using ${colors.green('y-npm install --global aws-cdk-docs')}`);
return false;
}
}
11 changes: 8 additions & 3 deletions packages/aws-cdk/test/test.cdk-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = testCase({
mockery.registerMock('../../lib/logging', {
debug() { return; },
error() { return; },
print() { return; },
warning() { return; }
});
mockery.enable({ useCleanCache: true, warnOnReplace: true, warnOnUnregistered: false });
Expand All @@ -20,7 +21,6 @@ module.exports = testCase({
cb();
},
async 'exits with 0 when everything is OK'(test: Test) {
mockery.registerMock('aws-cdk-docs', { documentationIndexPath: 'index.html' });
try {
const result = await require('../lib/commands/docs').handler(argv);
test.equal(result, 0, 'exit status was 0');
Expand All @@ -30,10 +30,15 @@ module.exports = testCase({
test.done();
}
},
async 'exits with non-0 when documentation is missing'(test: Test) {
async 'exits with 0 when opening the browser fails'(test: Test) {
mockery.registerMock('child_process', {
exec(_: string, cb: (err: Error, stdout?: string, stderr?: string) => void) {
cb(new Error('TEST'));
}
});
try {
const result = await require('../lib/commands/docs').handler(argv);
test.notEqual(result, 0, 'exit status was non-0');
test.equal(result, 0, 'exit status was 0');
} catch (e) {
test.doesNotThrow(() => { throw e; });
} finally {
Expand Down
12 changes: 0 additions & 12 deletions packages/aws-cdk/test/test.cdk-doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ module.exports = testCase({
cb();
},
async 'exits with 0 when everything is OK'(test: Test) {
mockery.registerMock('aws-cdk-docs/package.json', { version: 'x.y.z' });

try {
const result = await require('../lib/commands/doctor').handler();
test.equal(result, 0, 'exit status was 0');
Expand All @@ -26,16 +24,6 @@ module.exports = testCase({
} finally {
test.done();
}
},
async 'exits with non-0 when documentation is missing'(test: Test) {
try {
const result = await require('../lib/commands/doctor').handler();
test.notEqual(result, 0, 'exit status was non-0');
} catch (e) {
test.doesNotThrow(() => e);
} finally {
test.done();
}
}
}
});
95 changes: 95 additions & 0 deletions publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash
set -euo pipefail

###
# Usage: ./publish.sh <release-zip>
#
# Publishes the content of a release bundle ZIP file to the standard package
# repositories for the various supported languages:
# * Javascript & TypeScript: NPM
# * Documentation: GitHub Pages
# * (More to come later)
###

if [ $# -ne 1 ]; then
echo "Missing release zip file argument"
echo "Usage: ./publish.sh <release-zip>"
exit 1
fi

RELEASE_BUNDLE=$1
if [ ! -f ${RELEASE_BUNDLE} ]; then
echo "${RELEASE_BUNDLE} is not a file!"
exit 127
fi

###############
# PREPARATION #
###############

declare -a CLEANUP=()
function cleanup() {
for ((i = 0; i < ${#CLEANUP[@]}; i++ ))
do
eval "${CLEANUP[$i]}"
done
echo '🍻 Done!'
}
trap cleanup 'EXIT'


WORK_DIR=$(mktemp -d)
CLEANUP+=("echo '🚮 Cleaning up working directory'" "rm -fr ${WORK_DIR}")
echo "💼 Working directory: ${WORK_DIR}"

echo "🗜 Unzipping release bundle (be patient - this may take a while)"
unzip -q ${RELEASE_BUNDLE} -d ${WORK_DIR}

PKG_VERSION=$(cat ${WORK_DIR}/.version)

#######
# NPM #
#######

echo "📦 Publishing to NPM"
REGISTRY='https://registry.npmjs.org/'
OLD_REGISTRY=$(npm config get registry)
if [ ${OLD_REGISTRY} != ${REGISTRY} ]; then
echo "👉 Switching to NPM registry ${REGISTRY}"
npm config set registry ${REGISTRY}
CLEANUP+=("echo '👈 Resetting NPM registry to ${OLD_REGISTRY}'" "npm config set registry ${OLD_REGISTRY}")
fi

TOKENS=$(npm token list 2>&1 || echo '')
if echo ${TOKENS} | grep 'EAUTHUNKNOWN' > /dev/null; then
echo "🔑 Can't list tokens - apparently missing authentication info"
npm login
fi

for TGZ in $(find ${WORK_DIR}/y/npm -iname '*.tgz'); do
npm publish $TGZ --access=public
done

################
# GitHub Pages #
################

echo "📖 Publishing to GitHub Pages"

GIT_REPO=$(mktemp -d)
CLEANUP+=("echo '🚮 Cleaning up GitHub Pages working copy'" "rm -fr ${GIT_REPO}")

git clone -b gh-pages --depth=1 ssh://github.com/awslabs/aws-cdk ${GIT_REPO}
mkdir -p ${GIT_REPO}/versions

rsync -ar --delete --exclude=/.git --exclude=/versions ${WORK_DIR}/docs/ ${GIT_REPO}/
rsync -ar --delete ${WORK_DIR}/docs/ ${GIT_REPO}/versions/${PKG_VERSION}/

(
cd ${GIT_REPO}
git add .
git commit -m "Release ${PKG_VERSION}"
git push
)

echo "✅ All OK!"

0 comments on commit 4689f7f

Please sign in to comment.