Skip to content

Commit

Permalink
style: format code consistently
Browse files Browse the repository at this point in the history
- add .editorconfig, .prettierrc and use shfmt
- reorder exports from utils.js
- use tabs instead of spaces in JS
- keep using 4-spaces in shell scripts
  • Loading branch information
sidvishnoi committed Aug 23, 2020
1 parent 782f1d8 commit 1063d38
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 112 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
indent_style = tab
tab_width = 2
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"arrowParens": "avoid",
"singleQuote": false,
"trailingComma": "all",
"tabWidth": 2,
"useTabs": true
}
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: W3C Deploy
author: 'Sid Vishnoi'
description: 'Build ReSpec/Bikeshed specs, validate output and publish to w3.org or GitHub pages'
author: "Sid Vishnoi"
description: "Build ReSpec/Bikeshed specs, validate output and publish to w3.org or GitHub pages"

inputs:
type:
Expand Down
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/env bash
source "./utils.sh";
source "./utils.sh"
require_env "INPUTS_TYPE"
require_env "INPUTS_SOURCE"

OUTFILE="tmp-output.html"

case $INPUTS_TYPE in
respec)
respec)
echo "Converting ReSpec document '$INPUTS_SOURCE' to HTML..."
respec -s "$INPUTS_SOURCE" -o "$OUTFILE"
;;
bikeshed)
bikeshed)
echo "Converting Bikeshed document '$INPUTS_SOURCE' to HTML..."
bikeshed spec "$INPUTS_SOURCE" "$OUTFILE"
;;
Expand Down
22 changes: 11 additions & 11 deletions deploy-gh-pages.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
source "./utils.sh";
source "./utils.sh"
require_env "INPUTS_GH_PAGES_BRANCH"
require_env "IN_GITHUB_EVENT_NAME"
require_env "IN_GITHUB_REPOSITORY"
Expand All @@ -9,8 +9,8 @@ require_env "IN_GITHUB_TOKEN"
require_env "OUTPUT_FILE"

if [ "$INPUTS_GH_PAGES_BRANCH" = "false" ]; then
echo "Skipped."
exit
echo "Skipped."
exit
fi

target_branch="$INPUTS_GH_PAGES_BRANCH"
Expand All @@ -23,12 +23,12 @@ mv "$OUTPUT_FILE" /tmp/output-build-action/
# If it exists, we do a pull, otherwise we create a new orphan branch.
repo_uri="https://github.com/${IN_GITHUB_REPOSITORY}.git/"
if [[ $(git ls-remote --exit-code --heads "$repo_uri" "$target_branch") ]]; then
echo "Remote branch \"${target_branch}\" exists."
git fetch origin "$target_branch"
git checkout "$target_branch"
echo "Remote branch \"${target_branch}\" exists."
git fetch origin "$target_branch"
git checkout "$target_branch"
else
echo "Remote branch \"${target_branch}\" does not exist."
git checkout --orphan "$target_branch"
echo "Remote branch \"${target_branch}\" does not exist."
git checkout --orphan "$target_branch"
fi

# Bring back the changed file. We'll be serving it as index.html
Expand All @@ -40,7 +40,7 @@ git add .
git config user.name "$IN_GITHUB_ACTOR"
git config user.email "$(git show -s --format='%ae' $IN_GITHUB_SHA)"
github_actions_bot="github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
read -r -d '' commit_message <<- EOT_COMMIT_MSG
read -r -d '' commit_message <<-EOT_COMMIT_MSG
chore(rebuild): $(git log --format=%B -n 1 $IN_GITHUB_SHA)
SHA: ${IN_GITHUB_SHA}
Expand All @@ -52,8 +52,8 @@ EOT_COMMIT_MSG
echo "$commit_message" | git commit -F -

if [ $? -ne 0 ]; then
echo "Nothing to commit. Skipping deploy."
exit 0
echo "Nothing to commit. Skipping deploy."
exit 0
fi

# Push it!
Expand Down
106 changes: 53 additions & 53 deletions prepare.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
// @ts-check
const { existsSync } = require('fs');
const { inspect } = require('util');
const { existsSync } = require("fs");
const { inspect } = require("util");

const { exit, setOutput, formatAsHeading, sh, yesOrNo } = require('./utils.js');
const { exit, setOutput, formatAsHeading, sh, yesOrNo } = require("./utils.js");

const inputs = JSON.parse(process.env.INPUTS || '{}');
console.log(formatAsHeading('Provided input'));
const inputs = JSON.parse(process.env.INPUTS || "{}");
console.log(formatAsHeading("Provided input"));
console.log(inspect(inputs, false, Infinity, true));

/**
* Figure out "type" and "source".
*/
if (inputs.type) {
switch (inputs.type) {
case 'respec':
inputs.source = inputs.source || 'index.html';
break;
case 'bikeshed':
inputs.source = inputs.source || 'index.bs';
break;
default:
exit(`Invalid input "type": ${inputs.type}`);
}
switch (inputs.type) {
case "respec":
inputs.source = inputs.source || "index.html";
break;
case "bikeshed":
inputs.source = inputs.source || "index.bs";
break;
default:
exit(`Invalid input "type": ${inputs.type}`);
}
} else if (!inputs.source) {
exit(`Either of "type" or "source" must be provided.`);
exit(`Either of "type" or "source" must be provided.`);
}

if (!existsSync(inputs.source)) {
exit(`"source" file "${inputs.source}" not found.`);
exit(`"source" file "${inputs.source}" not found.`);
}

if (!inputs.type) {
switch (inputs.source) {
case 'index.html':
inputs.type = 'respec';
break;
case 'index.bs':
inputs.type = 'bikeshed';
break;
default:
exit(
`Failed to figure out "type" from "source". Please specify the "type".`
);
}
switch (inputs.source) {
case "index.html":
inputs.type = "respec";
break;
case "index.bs":
inputs.type = "bikeshed";
break;
default:
exit(
`Failed to figure out "type" from "source". Please specify the "type".`,
);
}
}

/**
Expand All @@ -55,37 +55,37 @@ inputs.validateMarkup = yesOrNo(inputs.validateMarkup) || false;
* Figure out GitHub pages deployment.
*/
const event = process.env.IN_GITHUB_EVENT_NAME;
const shouldTryDeployToGitHubPages = event === 'push';
const shouldTryDeployToGitHubPages = event === "push";
if (shouldTryDeployToGitHubPages) {
if (inputs.ghPages) {
const askedNotToDeploy = yesOrNo(inputs.ghPages) === false;
if (askedNotToDeploy) {
inputs.ghPages = false;
} else {
if (yesOrNo(inputs.ghPages) === true) {
inputs.ghPages = 'gh-pages';
}
const currentBranch = sh('git branch --show-current');
if (currentBranch === inputs.ghPages) {
exit(`Current branch and "ghPages" cannot be same.`);
}
if (!inputs.GITHUB_TOKEN) {
console.log('Using default GITHUB_TOKEN.');
inputs.GITHUB_TOKEN = process.env.IN_GITHUB_TOKEN;
}
}
} else {
throw new Error('Unreachable');
}
if (inputs.ghPages) {
const askedNotToDeploy = yesOrNo(inputs.ghPages) === false;
if (askedNotToDeploy) {
inputs.ghPages = false;
} else {
if (yesOrNo(inputs.ghPages) === true) {
inputs.ghPages = "gh-pages";
}
const currentBranch = sh("git branch --show-current");
if (currentBranch === inputs.ghPages) {
exit(`Current branch and "ghPages" cannot be same.`);
}
if (!inputs.GITHUB_TOKEN) {
console.log("Using default GITHUB_TOKEN.");
inputs.GITHUB_TOKEN = process.env.IN_GITHUB_TOKEN;
}
}
} else {
throw new Error("Unreachable");
}
} else {
inputs.ghPages = false;
inputs.ghPages = false;
}

/**
* Make processed inputs available to next steps.
*/
console.log(`\n\n${formatAsHeading('Normalized input')}`);
console.log(`\n\n${formatAsHeading("Normalized input")}`);
console.log(inspect(inputs, false, Infinity, true));
for (const [key, val] of Object.entries(inputs)) {
setOutput(key, val);
setOutput(key, val);
}
6 changes: 3 additions & 3 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
source "./utils.sh";
source "./utils.sh"
require_env "INPUTS_TYPE"

export PATH="$(yarn global bin):$PATH"
Expand All @@ -10,12 +10,12 @@ export PATH=$HOME/.local/bin:$PATH
echo "::add-path::$HOME/.local/bin"

case $INPUTS_TYPE in
respec)
respec)
echo "Installing ReSpec..."
export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=1
yarn global add respec
;;
bikeshed)
bikeshed)
echo "Installing Bikeshed..."
python3 -m pip --quiet install bikeshed
bikeshed update
Expand Down
76 changes: 38 additions & 38 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
// @ts-check
const { execSync } = require('child_process');
const { execSync } = require("child_process");

/**
* @param {string} path
*/
function addPath(path) {
console.log(`::add-path::${path}`);
}

/**
* @param {string} message
* @param {number} [code]
*/
function exit(message, code = 1) {
if (code === 0) {
console.log(message);
} else {
console.error(message);
}
process.exit(code);
if (code === 0) {
console.log(message);
} else {
console.error(message);
}
process.exit(code);
}

/**
* @param {string} path
* @param {string} text
*/
function addPath(path) {
console.log(`::add-path::${path}`);
function formatAsHeading(text) {
const marker = "=".repeat(Math.max(50, text.length));
return `${marker}\n${text}:\n${marker}`;
}

/**
* @param {string} key
* @param {string} value
*/
function setEnv(key, value) {
console.log(`::set-env name=${key}::${value}`);
console.log(`::set-env name=${key}::${value}`);
}

/**
* @param {string} key
* @param {string} value
*/
function setOutput(key, value) {
console.log(`::set-output name=${key}::${value}`);
}

/**
* @param {string} text
*/
function formatAsHeading(text) {
const marker = '='.repeat(Math.max(50, text.length));
return `${marker}\n${text}:\n${marker}`;
}

function yesOrNo(value) {
const str = String(value).trim();
if (/^(?:y|yes|true|1|on)$/i.test(str)) {
return true;
}
if (/^(?:n|no|false|0|off)$/i.test(value)) {
return false;
}
console.log(`::set-output name=${key}::${value}`);
}

/**
* Synchronously run a shell command get its result.
* @param {string} command
*/
function sh(command, options = {}) {
return execSync(command, { encoding: 'utf-8', ...options }).trim();
return execSync(command, { encoding: "utf-8", ...options }).trim();
}

function yesOrNo(value) {
const str = String(value).trim();
if (/^(?:y|yes|true|1|on)$/i.test(str)) {
return true;
}
if (/^(?:n|no|false|0|off)$/i.test(value)) {
return false;
}
}

module.exports = {
exit,
addPath,
setEnv,
setOutput,
formatAsHeading,
yesOrNo,
sh,
addPath,
exit,
formatAsHeading,
setEnv,
setOutput,
sh,
yesOrNo,
};
2 changes: 1 addition & 1 deletion validate-links.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
source "./utils.sh";
source "./utils.sh"
require_env "INPUTS_VALIDATE_LINKS"
require_env "OUTPUT_FILE"

Expand Down
2 changes: 1 addition & 1 deletion validate-markup.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
source "./utils.sh";
source "./utils.sh"
require_env "INPUTS_VALIDATE_MARKUP"
require_env "OUTPUT_FILE"

Expand Down

0 comments on commit 1063d38

Please sign in to comment.