Skip to content

Commit

Permalink
Moving to a build process, big change
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancarter committed Oct 18, 2017
1 parent 2663db7 commit 393beb5
Show file tree
Hide file tree
Showing 18 changed files with 2,307 additions and 12 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ need a different toolkit.
Import the relevant files into your page. You can use a CDN, like so:
```html
<!-- The main file, essential: -->
<script src='https://cdn.jsdelivr.net/gh/lurchmath/cloud-storage@v1/cloud-storage.js'></script>
<script src='https://cdn.jsdelivr.net/gh/lurchmath/cloud-storage@v2/release/cloud-storage.js'></script>
<!-- If you want the Dropbox backend, which you probably do: -->
<script src='https://unpkg.com/dropbox/dist/Dropbox-sdk.min.js'></script>
<script src='https://cdn.jsdelivr.net/gh/lurchmath/cloud-storage@v1/dropbox-backend.js'></script>
<script src='https://cdn.jsdelivr.net/gh/lurchmath/cloud-storage@v2/release/dropbox-backend.js'></script>
<!-- If you want the LocalStorage backend also: -->
<script src='https://cdn.jsdelivr.net/gh/lurchmath/cloud-storage@v1/localstorage-backend.js'></script>
<script src='https://cdn.jsdelivr.net/gh/lurchmath/cloud-storage@v2/release/localstorage-backend.js'></script>
```

Call `openFile()` or `saveFile()`. That's it! Obviously, you'll want to
pass parameters to those functions to customize what to save or what to do
with opened files, so you should see the example in the next section, or the
documented source code for those two functions, about half way through
[cloud-storage.js](https://github.com/lurchmath/cloud-storage/blob/master/cloud-storage.js).
[cloud-storage.js](https://github.com/lurchmath/cloud-storage/blob/master/source/cloud-storage.js).

## Example

See the [simple test
page](http://lurchmath.github.io/cloud-storage/test.html) for an example of
how the various storage options and functions work.
See the [simple example
page](http://lurchmath.github.io/cloud-storage/example/example.html) for an
example of how the various storage options and functions work.

The source code for that page [is documented with
comments](https://github.com/lurchmath/cloud-storage/blob/master/test.html).
comments](https://github.com/lurchmath/cloud-storage/blob/master/example/example.html).

## Status

Expand All @@ -51,7 +51,7 @@ Right now three data storage backends are supported.
keeping the API here very simple.)

To add a new backend, mimic the work done in
[dropbox-backend.js](https://github.com/lurchmath/cloud-storage/blob/master/dropbox-backend.js) and create your own backend.
[dropbox-backend.js](https://github.com/lurchmath/cloud-storage/blob/master/source/dropbox-backend.js) and create your own backend.
Cloud storage providers besides Dropbox are welcome!

There are other things that could be improved about this project.
Expand Down
1 change: 1 addition & 0 deletions example/dropbox-login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><script src="https://unpkg.com/dropbox/dist/Dropbox-sdk.min.js"></script><script>function parseQueryString(e){var a=Object.create(null);return"string"!=typeof e?a:(e=e.trim().replace(/^(\?|#|&)/,""))?(e.split("&").forEach(function(e){var t=e.replace(/\+/g," ").split("="),n=t.shift(),o=t.length>0?t.join("="):void 0;n=decodeURIComponent(n),o=void 0===o?null:decodeURIComponent(o),void 0===a[n]?a[n]=o:Array.isArray(a[n])?a[n].push(o):a[n]=[a[n],o]}),a):a}window.addEventListener("message",function(e){if("setClientID"==e.data[0]){var a=new Dropbox({clientId:e.data[1]});window.location.href=a.getAuthenticationUrl(window.location.href)}});var hashData=parseQueryString(window.location.hash);(hashData.access_token||hashData.error)&&window.opener.postMessage(JSON.stringify(["dialogLogin",hashData]),"*")</script></head><body></body></html>
6 changes: 3 additions & 3 deletions test.html → example/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<html>
<head>
<!-- Import the cloud storage module. -->
<script src='./cloud-storage.js'></script>
<script src='https://cdn.jsdelivr.net/gh/lurchmath/cloud-storage@v2/release/cloud-storage.js'></script>
<!-- We aim to test the Dropbox backend, so we need its SDK. -->
<script src='https://unpkg.com/dropbox/dist/Dropbox-sdk.min.js'>
</script>
<!-- Now that Dropbox is defined, import the Dropbox back end. -->
<script src='./dropbox-backend.js'></script>
<script src='https://cdn.jsdelivr.net/gh/lurchmath/cloud-storage@v2/release/dropbox-backend.js'></script>
<!-- Import the LocalStorage back end. -->
<script src='./localstorage-backend.js'></script>
<script src='https://cdn.jsdelivr.net/gh/lurchmath/cloud-storage@v2/release/localstorage-backend.js'></script>
</head>
<body>
<!--
Expand Down
103 changes: 103 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

/*
* This file is the build process for this repository.
*
* It begins by importing all the gulp modules needed to do the tasks below.
*/
var gulp = require( 'gulp' );
var htmlmin = require( 'gulp-htmlmin' );
var embed = require( 'gulp-embed' );
var uglify = require( 'gulp-uglify' );
var each = require( 'gulp-each' );
var rename = require( 'gulp-rename' );
var concat = require( 'gulp-concat' );
var addsrc = require( 'gulp-add-src' );

/*
* Any client will need to import our scripts. The main one is the
* cloud-storage.js file itself, which we compile here, from the source
* folder to the release folder.
*
* This is a simple minification of the source file, with one additional
* step. We embed the entire dialog.{html,css,js} content as a single,
* base64-encoded string at the top of the compiled file, so that it can be
* used programmatically to fill an iframe, without the need for those three
* files to be present in the same folder.
*/
gulp.task( 'build-main-script', function () {
return gulp.src( 'source/dialog.html' )
// embed dialog.{js,css} into dialog.html, then minify
.pipe( embed() )
.pipe( htmlmin( { collapseWhitespace : true,
removeComments : true,
minifyJS : true,
minifyCSS : true } ) )
// manually base64 encode the result as a JS string variable
.pipe( each( function ( content, file, callback ) {
callback( null,
'var dialogHTML = "data:text/html;base64,' +
( new Buffer( content ).toString( 'base64' ) ) +
'";' );
} ) )
// prepend this onto cloud-storage.js and make it use the var
.pipe( addsrc.append( 'source/cloud-storage.js' ) )
.pipe( concat( 'cloud-storage.js' ) )
.pipe( each( function ( content, file, callback ) {
callback( null,
content.replace( "'./dialog.html'", "dialogHTML" ) );
} ) )
// minify and output
.pipe( uglify() )
.pipe( gulp.dest( 'release/' ) );
} );

/*
* Any client will need to import our scripts. In addition to the
* cloud-storage.js file built by the process above, there are also a
* handful of "backends" that reside in their own .js files, so that clients
* can import exactly the subset they need, and no more. We compile those
* here, also from the source folder to the release folder.
*/
gulp.task( 'minify-backends', function () {
return gulp.src( 'source/*-backend.js' )
.pipe( uglify() )
.pipe( gulp.dest( 'release/' ) );
} );

/*
* A client who chooses to use the Dropbox backend will need a page in their
* own web space to which to direct the Dropbox login procedure. We provide
* the file dropbox-login.html that receives that redirection from Dropbox
* and emits the messages that the scripts in dropbox-backend.js need to
* access the user's Dropbox. The user will need a copy of thta file in
* the same folder as the page importing cloud-storage.js.
*
* This build task just copies the dropbox-login.html page to the release
* folder, from which clients who need it can download it. But while we're
* at it, we might as well minify its contents.
*/
gulp.task( 'minify-login-page', function () {
return gulp.src( 'source/dropbox-login.html' )
.pipe( htmlmin( { collapseWhitespace : true,
removeComments : true,
minifyJS : true } ) )
.pipe( gulp.dest( 'release/' ) );
} );

/*
* This repository contains an example/ folder to show how to use the tools
* provided. This task just copies the minified login page from the release
* folder to the example folder.
*/
gulp.task( 'build-example', [ 'minify-login-page' ], function () {
return gulp.src( 'release/dropbox-login.html' )
.pipe( gulp.dest( 'example/' ) );
} );

/*
* The default build task is to do everything.
*/
gulp.task( 'default', [ 'build-main-script',
'minify-backends',
'minify-login-page',
'build-example' ] );
Loading

0 comments on commit 393beb5

Please sign in to comment.