Skip to content

Commit

Permalink
Add initial functionality (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
namoscato authored Jun 7, 2020
1 parent 692c824 commit a3f5b17
Show file tree
Hide file tree
Showing 29 changed files with 17,122 additions and 427 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
17 changes: 9 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
name: "build-test"
on: # rebuild any PRs and main branch changes
name: ci
on:
pull_request:
push:
branches:
- master
- 'releases/*'

jobs:
build: # make sure build/ci work properly
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- run: |
npm install
npm run all
test: # make sure the action works on a clean machine without building
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- uses: ./
with:
milliseconds: 1000
with:
api_key: abc123
github_token: ${{ secrets.GITHUB_TOKEN }}
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/action-tinify.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.17.0
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:lts AS builder
WORKDIR /usr/app
COPY package*.json ./
RUN npm ci
COPY tsconfig.json .
COPY src ./src
RUN npm run build

FROM node:lts
WORKDIR /usr/app
COPY package*.json ./
RUN npm ci --only=production
COPY --from=builder /usr/app/lib ./lib
CMD ["node", "/usr/app/lib/main.js"]
7 changes: 3 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
MIT License

The MIT License (MIT)

Copyright (c) 2018 GitHub, Inc. and contributors
Copyright (c) 2020 Nick Amoscato (namoscato)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
114 changes: 19 additions & 95 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,25 @@
<p align="center">
<a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a>
</p>
# Tinify Action

# Create a JavaScript Action using TypeScript
[GitHub Action](https://github.com/features/actions) to compress images with the [Tinify API](https://tinypng.com/developers).

Use this template to bootstrap the creation of a JavaScript action.:rocket:
![Example commit](https://i.imgur.com/FWOosON.png)

This template includes compilication support, tests, a validation workflow, publishing, and versioning guidance.

If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action)

## Create an action from this template

Click the `Use this Template` and provide the new repo details for your action

## Code in Master

Install the dependencies
```bash
$ npm install
```

Build the typescript and package it for distribution
```bash
$ npm run build && npm run pack
```

Run the tests :heavy_check_mark:
```bash
$ npm test

PASS ./index.test.js
✓ throws invalid number (3ms)
wait 500 ms (504ms)
test runs (95ms)

...
```

## Change action.yml

The action.yml contains defines the inputs and output for your action.

Update the action.yml with your name, description, inputs and outputs for your action.

See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions)

## Change the Code

Most toolkit and CI/CD operations involve async operations so the action is run in an async function.

```javascript
import * as core from '@actions/core';
...

async function run() {
try {
...
}
catch (error) {
core.setFailed(error.message);
}
}

run()
```

See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages.

## Publish to a distribution branch

Actions are run from GitHub repos so we will checkin the packed dist folder.

Then run [ncc](https://github.com/zeit/ncc) and push the results:
```bash
$ npm run pack
$ git add dist
$ git commit -a -m "prod dependencies"
$ git push origin releases/v1
```

Your action is now published! :rocket:

See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)

## Validate

You can now validate the action by referencing `./` in a workflow in your repo (see [test.yml](.github/workflows/test.yml)])
## Usage

```yaml
uses: ./
with:
milliseconds: 1000
name: image
on:
pull_request:
types: [opened]
jobs:
compress:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}
- uses: namoscato/action-tinify@v1
with:
api_key: ${{ secrets.TINIFY_API_KEY }}
github_token: ${{ secrets.GITHUB_TOKEN }}
```
See the [actions tab](https://github.com/actions/javascript-action/actions) for runs of this action! :rocket:
## Usage:
After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and latest V1 action
23 changes: 23 additions & 0 deletions __tests__/git-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {getCommitMessage} from '../src/git-utils'
import {Commit} from '../src/git'

describe('getCommitMessage', () => {
test('defined message', () => {
const message = getCommitMessage({message: 'MESSAGE'} as Commit)
expect(message).toEqual('MESSAGE')
})

test('one file', () => {
const message = getCommitMessage({
files: ['FILE']
} as any)
expect(message).toEqual('Compress image')
})

test('multiple files', () => {
const message = getCommitMessage({
files: ['FILE1', 'FILE2']
} as any)
expect(message).toEqual('Compress images')
})
})
15 changes: 15 additions & 0 deletions __tests__/image.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Image from '../src/image'

let image: Image

beforeEach(() => {
image = new Image('FILE', [20000, 9876])
})

test('getFilename', () => {
expect(image.getFilename()).toEqual('FILE')
})

test('getCompressionSummary', () => {
expect(image.getCompressionSummary()).toEqual('-9.89KB (-50%)')
})
Loading

0 comments on commit a3f5b17

Please sign in to comment.