Skip to content

Commit

Permalink
Setup eslint (#4)
Browse files Browse the repository at this point in the history
* Setup eslint

- Setup eslint under Airbnb standards
- Add CI action with linter
- Update code to fit linting standards
  • Loading branch information
rogebrd authored Oct 16, 2020
1 parent a4bcac4 commit eefb36e
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 85 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
},
extends: [
'airbnb-base',
],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
rules: {
'import/extensions': 0,
},
};
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: ':bug: Bug report'
about: Create a report to help us improve the SDK
about: Create a report to help us improve
title: ''
labels: bug
assignees: 'rogebrd'
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: ':rocket: Feature Request'
about: Suggest an idea for this SDK
about: Suggest an idea
title: ''
labels: enhancement
assignees: 'rogebrd'
Expand Down
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

**Is This a Code Change?**
- [ ] Non-code related change (markdown/git settings etc)
- [ ] SDK Code Change
- [ ] Core Code Change
- [ ] Example/Test Code Change

**Validation**
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
pull_request:

jobs:
ci:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
node: [10, 12, 14]
runs-on: ${{ matrix.os }}
continue-on-error: true
steps:
- name: Checkout Package 🛎️
uses: actions/checkout@v2
- name: Setup Node.JS 🎱
uses: actions/setup-node@v2.1.2
with:
node-version: ${{ matrix.node }}
- name: Install Package 📥
run: |
npm install
- name: Build Package 🏗️
run: |
npm run build
- name: Run Linter 🗞️
run: |
npm run lint
10 changes: 6 additions & 4 deletions .github/workflows/npm_upload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: ':bellhop_bell: Checkout Package'
- name: Checkout Package 🛎️
uses: actions/checkout@v2
- name: ':gear: Setup Node.JS'
- name: Setup Node.JS 🎱
uses: actions/setup-node@v2.1.2
with:
node-version: 14
- name: ':building_construction: Build Package'
- name: Install Package 📥
run: |
npm install
- name: Build Package 🏗️
run: |
npm run build
- name: ':rocket: Publish to NPM'
- name: Publish to NPM 📦
uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.NPM_TOKEN }}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
"browser": "dist/dropboxPopup.js",
"scripts": {
"test": "node test/index.js",
"build": "rollup -c"
"build": "rollup -c",
"lint": "eslint *.js .",
"lint-fix": "eslint *.js . --fix"
},
"author": "Brad Rogers <brad12rogers@gmail.com>",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.12.0",
"@babel/preset-env": "^7.12.0",
"eslint": "^7.11.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-plugin-import": "^2.22.1",
"express": "^4.17.1",
"rollup": "^2.30.0",
"rollup-plugin-babel": "^4.4.0",
Expand Down
58 changes: 29 additions & 29 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@ import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';

const umd = {
input: 'src/dropboxPopup.js',
output: {
file: __dirname + '/dist/dropboxPopup.js',
format: 'umd',
name: 'DropboxPopup',
sourcemap: true,
globals: {
'dropbox': 'Dropbox'
}
input: 'src/dropboxPopup.js',
output: {
file: `${__dirname}/dist/dropboxPopup.js`,
format: 'umd',
name: 'DropboxPopup',
sourcemap: true,
globals: {
dropbox: 'Dropbox',
},
plugins: [
babel(),
],
external: ['dropbox']
},
plugins: [
babel(),
],
external: ['dropbox'],
};

const umd_min = {
input: 'src/dropboxPopup.js',
output: {
file: __dirname + '/dist/dropboxPopup.min.js',
format: 'umd',
name: 'DropboxPopup',
sourcemap: false,
globals: {
'dropbox': 'Dropbox'
}
const umdMin = {
input: 'src/dropboxPopup.js',
output: {
file: `${__dirname}/dist/dropboxPopup.min.js`,
format: 'umd',
name: 'DropboxPopup',
sourcemap: false,
globals: {
dropbox: 'Dropbox',
},
plugins: [
babel(),
terser()
],
external: ['dropbox']
},
plugins: [
babel(),
terser(),
],
external: ['dropbox'],
};

export default [umd, umd_min];
export default [umd, umdMin];
91 changes: 45 additions & 46 deletions src/dropboxPopup.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
import { DropboxAuth } from 'dropbox';

function dispatchResult() {
const params = window.location.href;
if (window.opener) {
// send them to the opening window
window.opener.postMessage(params);
// close the popup
window.close();
}
}

window.addEventListener('load', dispatchResult);

const popupFeatures =
'toolbar=no, menubar=no, width=600, height=800, top=100, left=100';
const popupFeatures = 'toolbar=no, menubar=no, width=600, height=800, top=100, left=100';
const popupName = 'Dropbox OAuth';
export default class DropboxPopup {
constructor(options) {
this.clientId = options.clientId;
this.clientSecret = options.clientSecret;
this.authObject = new DropboxAuth({
clientId: this.clientId,
clientSecret: this.clientSecret
});
this.redirectUri = options.redirectUri;
this.codeOffset = this.redirectUri.length + 7 // format is `${redirectUri}/?code={code}
}
constructor(options) {
this.clientId = options.clientId;
this.clientSecret = options.clientSecret;
this.authObject = new DropboxAuth({
clientId: this.clientId,
clientSecret: this.clientSecret,
});
this.redirectUri = options.redirectUri;
this.codeOffset = this.redirectUri.length + 7; // format is `${redirectUri}/?code={code}
}

authUser(callback) {
window.removeEventListener('message', this.handleRedirect);
this.callback = callback;
this.callback.bind(this);
const authUrl = this.authObject.getAuthenticationUrl(this.redirectUri, '', 'code', 'offline');
const popupWindow = window.open(authUrl, popupName, popupFeatures);
popupWindow.focus();
authUser(callback) {
window.removeEventListener('message', this.handleRedirect);
this.callback = callback;
this.callback.bind(this);
const authUrl = this.authObject.getAuthenticationUrl(this.redirectUri, '', 'code', 'offline');
const popupWindow = window.open(authUrl, popupName, popupFeatures);
popupWindow.focus();

window.addEventListener('message', event => this.handleRedirect(event), false);
}
window.addEventListener('message', (event) => this.handleRedirect(event), false);
}

handleRedirect(event) {
const { data } = event;
const code = data.substring(this.codeOffset);
this.authObject.getAccessTokenFromCode(this.redirectUri, code)
.then((response) => {
const result = response.result;
this.authObject.setAccessToken(result.access_token);
this.authObject.setRefreshToken(result.refresh_token);
this.authObject.setAccessTokenExpiresAt(new Date(Date.now() + result.expires_in));
this.callback(this.authObject)
})
.catch((error) => {
console.error(error);
});
}
handleRedirect(event) {
const { data } = event;
const code = data.substring(this.codeOffset);
this.authObject.getAccessTokenFromCode(this.redirectUri, code)
.then((response) => {
const { result } = response;
this.authObject.setAccessToken(result.access_token);
this.authObject.setRefreshToken(result.refresh_token);
this.authObject.setAccessTokenExpiresAt(new Date(Date.now() + result.expires_in));
this.callback(this.authObject);
})
.catch((error) => {
throw error;
});
}
}

function dispatchResult() {
const params = window.location.href;
if (window.opener) {
// send them to the opening window
window.opener.postMessage(params);
// close the popup
window.close();
}
}
5 changes: 5 additions & 0 deletions test/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'no-console': 0,
},
};
5 changes: 3 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const express = require('express');

const app = express();
const port = 8080;

app.use('/', express.static('test/static'));
app.use('/src', express.static('dist'));

app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
console.log(`Listening at http://localhost:${port}`);
});

0 comments on commit eefb36e

Please sign in to comment.