Skip to content

Commit

Permalink
feat(lint): add eslint / prettier and action job
Browse files Browse the repository at this point in the history
  • Loading branch information
matbour committed Mar 19, 2020
1 parent 028c3f4 commit 186bb88
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 112 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ on:
push:
branches: ['**']
jobs:
lint:
name: Lint with ESLint / Prettier
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: '12'
- name: Run ESLint
run: |
npm install
npm run lint
test:
strategy:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion .huskyrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ const tasks = commands => commands.join(' && ');

module.exports = {
hooks: {
'pre-commit': tasks(['npm run build', 'npm run format', 'git add dist/']),
'pre-commit': tasks(['npm run build', 'npm run lint', 'git add dist/']),
},
};
6 changes: 6 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
module.exports = {
printWidth: 120,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: true,
quoteProps: 'as-needed',
trailingComma: 'all',
bracketSpacing: true,
arrowParens: 'avoid',
endOfLine: 'lf'
};
60 changes: 27 additions & 33 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4626,14 +4626,37 @@ async function gcloud(args, options = undefined) {



/**
* Configure the default Google Cloud project.
* @param projectId
*/
function configureProject(projectId) {
return gcloud(['config', 'set', 'project', projectId]);
}
/**
* Configure the default Google Cloud project from the GitHub inputs.
*/
async function configureProjectFromInput() {
if (!['', 'none', 'auto'].includes(Object(core.getInput)('project'))) {
return await configureProject(Object(core.getInput)('project'));
}
else {
return;
}
}
/**
* Configure Docker credentials for Google Cloud Registry
*/
async function configureDocker() {
await gcloud(['auth', 'configure-docker']);
}
/**
* Authenticate the Google Cloud SDK.
*/
async function authenticate() {
// If service account key is not provided, skip the authentication
if (!Object(core.getInput)('service-account-key')) {
Object(core.warning)('No service-account-key input was passed. If it is intentional, you can ' +
'safely ignore this warning.');
Object(core.warning)('No service-account-key input was passed. If it is intentional, you can safely ignore this warning.');
await configureProjectFromInput();
}
// Write the service account key
Expand All @@ -4642,16 +4665,11 @@ async function authenticate() {
const serviceAccountKeyPath = Object(external_path_.resolve)(process.cwd(), 'gcloud.json');
Object(external_fs_.writeFileSync)(serviceAccountKeyPath, serviceAccountKeyJson);
// Activate the service account
await gcloud([
'auth',
'activate-service-account',
`--key-file=${serviceAccountKeyPath}`,
]);
await gcloud(['auth', 'activate-service-account', `--key-file=${serviceAccountKeyPath}`]);
// Remove the service account key
Object(external_fs_.unlinkSync)(serviceAccountKeyPath);
// Configure the default project
if (Object(core.getInput)('project') === 'auto' &&
Object(core.getInput)('service-account-key') !== '') {
if (Object(core.getInput)('project') === 'auto' && Object(core.getInput)('service-account-key') !== '') {
// Project will be read from the service account key
const serviceAccountKey = JSON.parse(serviceAccountKeyJson.toString());
if (serviceAccountKey.hasOwnProperty('project_id')) {
Expand All @@ -4671,30 +4689,6 @@ async function authenticate() {
await configureDocker();
}
}
/**
* Configure the default Google Cloud project.
* @param projectId
*/
function configureProject(projectId) {
return gcloud(['config', 'set', 'project', projectId]);
}
/**
* Configure the default Google Cloud project from the GitHub inputs.
*/
async function configureProjectFromInput() {
if (!['', 'none', 'auto'].includes(Object(core.getInput)('project'))) {
return await configureProject(Object(core.getInput)('project'));
}
else {
return;
}
}
/**
* Configure Docker credentials for Google Cloud Registry
*/
async function configureDocker() {
await gcloud(['auth', 'configure-docker']);
}

// EXTERNAL MODULE: ./node_modules/@actions/tool-cache/lib/tool-cache.js
var tool_cache = __webpack_require__(533);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"url": "git+https://github.com/mathrix-education/setup-gcloud.git"
},
"scripts": {
"format": "prettier --write \"src/**/*.ts\" \"*.js\"",
"lint": "eslint \"src/**\"",
"lint:fix": "eslint --fix \"src/**\"",
"build": "ncc build src/install.ts"
},
"dependencies": {
Expand Down
77 changes: 31 additions & 46 deletions src/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,59 @@ import { unlinkSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { gcloud } from './utils';

/**
* Configure the default Google Cloud project.
* @param projectId
*/
function configureProject(projectId: string): Promise<void> {
return gcloud(['config', 'set', 'project', projectId]);
}

/**
* Configure the default Google Cloud project from the GitHub inputs.
*/
async function configureProjectFromInput(): Promise<void> {
if (!['', 'none', 'auto'].includes(core.getInput('project'))) {
return await configureProject(core.getInput('project'));
} else {
return;
}
}

/**
* Configure Docker credentials for Google Cloud Registry
*/
async function configureDocker(): Promise<void> {
await gcloud(['auth', 'configure-docker']);
}

/**
* Authenticate the Google Cloud SDK.
*/
export async function authenticate(): Promise<void> {
// If service account key is not provided, skip the authentication
if (!core.getInput('service-account-key')) {
core.warning(
'No service-account-key input was passed. If it is intentional, you can ' +
'safely ignore this warning.',
);
core.warning('No service-account-key input was passed. If it is intentional, you can safely ignore this warning.');

await configureProjectFromInput();
}

// Write the service account key
const serviceAccountKeyBase64 = core.getInput('service-account-key');
const serviceAccountKeyJson = Buffer.from(
serviceAccountKeyBase64,
'base64',
).toString();
const serviceAccountKeyJson = Buffer.from(serviceAccountKeyBase64, 'base64').toString();
const serviceAccountKeyPath = resolve(process.cwd(), 'gcloud.json');
writeFileSync(serviceAccountKeyPath, serviceAccountKeyJson);

// Activate the service account
await gcloud([
'auth',
'activate-service-account',
`--key-file=${serviceAccountKeyPath}`,
]);
await gcloud(['auth', 'activate-service-account', `--key-file=${serviceAccountKeyPath}`]);

// Remove the service account key
unlinkSync(serviceAccountKeyPath);

// Configure the default project
if (
core.getInput('project') === 'auto' &&
core.getInput('service-account-key') !== ''
) {
if (core.getInput('project') === 'auto' && core.getInput('service-account-key') !== '') {
// Project will be read from the service account key
const serviceAccountKey: { project_id: string } = JSON.parse(
serviceAccountKeyJson.toString(),
);
const serviceAccountKey: { project_id: string } = JSON.parse(serviceAccountKeyJson.toString());

if (serviceAccountKey.hasOwnProperty('project_id')) {
// If key has a project_id field, use it to set the default project
Expand All @@ -64,29 +75,3 @@ export async function authenticate(): Promise<void> {
await configureDocker();
}
}

/**
* Configure the default Google Cloud project.
* @param projectId
*/
function configureProject(projectId: string): Promise<void> {
return gcloud(['config', 'set', 'project', projectId]);
}

/**
* Configure the default Google Cloud project from the GitHub inputs.
*/
async function configureProjectFromInput(): Promise<void> {
if (!['', 'none', 'auto'].includes(core.getInput('project'))) {
return await configureProject(core.getInput('project'));
} else {
return;
}
}

/**
* Configure Docker credentials for Google Cloud Registry
*/
async function configureDocker() {
await gcloud(['auth', 'configure-docker']);
}
5 changes: 1 addition & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ import { resolve } from 'path';

export const INSTALL_DIRECTORY = 'google-cloud-sdk';
export const UBUNTU_INSTALL_PATH = `/usr/lib/${INSTALL_DIRECTORY}`;
export const MACOS_INSTALL_PATH = resolve(
process.env.HOME ?? process.cwd(),
INSTALL_DIRECTORY,
);
export const MACOS_INSTALL_PATH = resolve(process.env.HOME ?? process.cwd(), INSTALL_DIRECTORY);
export const WINDOWS_INSTALL_PATH = `C:\\Program Files\\${INSTALL_DIRECTORY}`;
4 changes: 1 addition & 3 deletions src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export async function download(): Promise<void> {
}
} else {
// Should never be reached
core.setFailed(
`Unexpected extension (expected zip or tar.gz), but got ${downloadLink}`,
);
core.setFailed(`Unexpected extension (expected zip or tar.gz), but got ${downloadLink}`);
}
}
2 changes: 1 addition & 1 deletion src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { setup } from './setup';
* Install the Google Cloud SDK.
*/
try {
(async () => {
(async (): Promise<void> => {
await download();
await setup();
await authenticate();
Expand Down
5 changes: 1 addition & 4 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import { getCloudSDKDirectory, isMacOS, isUbuntu, isWindows } from './utils';
*/
export async function setup(): Promise<void> {
const installScriptExtension = isWindows() ? 'bat' : 'sh';
const installScript = resolve(
getCloudSDKDirectory(),
`install.${installScriptExtension}`,
);
const installScript = resolve(getCloudSDKDirectory(), `install.${installScriptExtension}`);

const args = [
'--usage-reporting=false',
Expand Down
23 changes: 4 additions & 19 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import * as core from '@actions/core';
import * as exec from '@actions/exec';
import { ExecOptions } from '@actions/exec/lib/interfaces';
import { resolve } from 'path';
import {
INSTALL_DIRECTORY,
MACOS_INSTALL_PATH,
UBUNTU_INSTALL_PATH,
WINDOWS_INSTALL_PATH,
} from './constants';
import { MACOS_INSTALL_PATH, UBUNTU_INSTALL_PATH, WINDOWS_INSTALL_PATH } from './constants';

/**
* Check if the runner is Windows-based.
Expand Down Expand Up @@ -72,22 +67,12 @@ export function getDownloadLink(): string {
* @param args The gcloud args
* @param options The command options
*/
export async function gcloud(
args: string[],
options: ExecOptions | undefined = undefined,
): Promise<void> {
let gcloudPath = resolve(
getCloudSDKDirectory(),
'bin',
'gcloud' + (isWindows() ? '.cmd' : ''),
);
export async function gcloud(args: string[], options: ExecOptions | undefined = undefined): Promise<void> {
let gcloudPath = resolve(getCloudSDKDirectory(), 'bin', 'gcloud' + (isWindows() ? '.cmd' : ''));

if (isWindows()) {
// Windows installation directory is C:\Program Files and thus need to be escaped
gcloudPath = gcloudPath.replace(
getCloudSDKDirectory(),
`"${getCloudSDKDirectory()}"`,
);
gcloudPath = gcloudPath.replace(getCloudSDKDirectory(), `"${getCloudSDKDirectory()}"`);
}

args.unshift('--quiet'); // Add quiet to all commands
Expand Down

0 comments on commit 186bb88

Please sign in to comment.