This repository was archived by the owner on Aug 18, 2021. It is now read-only.
Repository navigation
Offline support #5
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
53d676b
Adds offline support
eba64ed
Updating deps
378c008
Think I got the path wrong
f4b99e8
First pass at offline downloader
0441335
Updating go-offline
738adec
Replacing rimraf with tmp
8c99317
Moving things
854e91c
Now using npm instead of GitHub
e2863e6
Moving dist to gitignore
2adc0c5
Reducing number of copies of d3
53a27a2
Will now work even if we upgrade to D3 v5
dfdf48e
Fixing 'offline:download-deps' script as per @tlfrd's comment.
8550051
Updating modules
49eb56b
Superfluous var
d617ee3
Merge branch 'master' into offline-support
bbae6be
Latest
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| offline/* | ||
| !offline/index.js | ||
| dist | ||
| node_modules | ||
| dist/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * @file | ||
| * Runtime for downloading appropriate deps for offline us | ||
| */ | ||
|
|
||
| const cheerio = require('cheerio'); | ||
| const Axios = require('axios'); | ||
| const execa = require('execa'); | ||
| const tmp = require('tmp'); | ||
| const npm = require('npm'); | ||
| const pacote = require('pacote'); | ||
| const { readFileSync, writeFileSync, createWriteStream } = require('fs'); | ||
|
|
||
| (async () => { | ||
| try { | ||
| console.info('Checking d3-bootloader version...'); | ||
| const index = readFileSync(`${process.cwd()}/index.html`, 'utf-8'); | ||
| const $ = cheerio.load(index); | ||
| const bootloaderTag = $( | ||
| $('script') | ||
| .toArray() | ||
| .find(d => /d3-bootloader/.test($(d).attr('src'))), | ||
| ); | ||
| let version; | ||
| const bootloaderUrl = bootloaderTag.attr('src'); | ||
|
|
||
| if (!bootloaderUrl) { | ||
| throw new Error( | ||
| 'Unable to locate D3 bootloader; have you run `offline` in a directory with an index.html file?', | ||
| ); | ||
| } else if (/unpkg.com\/@financial-times\/d3-bootloader@/.test(bootloaderUrl)) { | ||
| console.error('Using a specified version of d3-bootloader deps is current unsupported.'); | ||
| console.info('Falling back to using `latest` release.'); | ||
| version = 'latest'; | ||
| // @TODO write version handling logic | ||
| } else { | ||
| version = 'latest'; | ||
| console.info('Using `latest` release dependencies.'); | ||
| } | ||
|
|
||
| console.info('Downloading dependencies...'); | ||
|
|
||
| // Create a tmp file | ||
| const tmpFile = tmp.fileSync(); | ||
|
|
||
| // Download tarball to tmpfile | ||
| await pacote.tarball.toFile(`@financial-times/d3-bootloader@${version}`, tmpFile.name); | ||
|
|
||
| console.info('Download complete.'); | ||
| console.info('Extracting...'); | ||
|
|
||
| // Create tmp dir. | ||
| const tmpDir = tmp.dirSync(); | ||
|
|
||
| // Extract to tmp dir | ||
| await execa('tar', ['-C', tmpDir.name, '--strip=1', '-xzvf', tmpFile.name]); | ||
|
|
||
| // Move tmpDir/offline to $CWD | ||
| await execa('mv', [`${tmpDir.name}/offline`, process.cwd()]); | ||
|
|
||
| // Remove tmp dir. | ||
| tmpDir.removeCallback(); | ||
|
|
||
| // Remove the tarball | ||
| tmpFile.removeCallback(); | ||
|
|
||
| // Write updated index.html to $CWD | ||
| console.info('Updating index.html...'); | ||
| bootloaderTag.attr('src', './offline/index.js'); | ||
| writeFileSync(`${process.cwd()}/index.html`, $.html()); | ||
|
|
||
| console.info('Done!'); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * @file | ||
| * Offline support | ||
| */ | ||
|
|
||
| import Axios from "axios"; | ||
| import path from "path"; | ||
| import { createWriteStream } from "fs"; | ||
| import modules from "./modules"; | ||
|
|
||
| Promise.all(Object.entries(modules).map(item => downloadDependency(...item))) | ||
| .then(() => console.log("Done!")) | ||
| .catch(console.error); | ||
|
|
||
| async function downloadDependency(pkg, url) { | ||
| const pkgName = /d3js\.org\/d3\./.test(url) | ||
| ? "d3" | ||
| : /^@/.test(pkg) | ||
| ? pkg.slice(pkg.indexOf("/") + 1) | ||
| : pkg; | ||
| const outFile = path.resolve(__dirname, "offline", `${pkgName}.js`); | ||
| console.log(pkgName, url); | ||
|
|
||
| // axios image download with response type "stream" | ||
| const response = await Axios({ | ||
| method: "GET", | ||
| url: url, | ||
| responseType: "stream" | ||
| }); | ||
|
|
||
| // pipe the result stream into a file on disc | ||
| response.data.pipe(createWriteStream(outFile)); | ||
|
|
||
| // return a promise and resolve when download finishes | ||
| return new Promise((resolve, reject) => { | ||
| response.data.on("end", () => { | ||
| resolve(); | ||
| }); | ||
|
|
||
| response.data.on("error", () => { | ||
| reject(); | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| export default { | ||
| '@financial-times/load-data': | ||
| 'https://unpkg.com/@financial-times/load-data@^1.0.2/dist/loadData.dist.min.js', | ||
| 'bertha-client': 'https://unpkg.com/bertha-client@4.0.1/dist/index.js', | ||
| d3: 'https://d3js.org/d3.v4.js', | ||
| 'd3-array': 'https://d3js.org/d3.v4.js', | ||
| 'd3-axis': 'https://d3js.org/d3.v4.js', | ||
| 'd3-brush': 'https://d3js.org/d3.v4.js', | ||
| 'd3-chord': 'https://d3js.org/d3.v4.js', | ||
| 'd3-collection': 'https://d3js.org/d3.v4.js', | ||
| 'd3-color': 'https://d3js.org/d3.v4.js', | ||
| 'd3-dispatch': 'https://d3js.org/d3.v4.js', | ||
| 'd3-drag': 'https://d3js.org/d3.v4.js', | ||
| 'd3-dsv': 'https://d3js.org/d3.v4.js', | ||
| 'd3-ease': 'https://d3js.org/d3.v4.js', | ||
| 'd3-force': 'https://d3js.org/d3.v4.js', | ||
| 'd3-format': 'https://d3js.org/d3.v4.js', | ||
| 'd3-geo': 'https://d3js.org/d3.v4.js', | ||
| 'd3-hierarchy': 'https://d3js.org/d3.v4.js', | ||
| 'd3-interpolate': 'https://d3js.org/d3.v4.js', | ||
| 'd3-path': 'https://d3js.org/d3.v4.js', | ||
| 'd3-polygon': 'https://d3js.org/d3.v4.js', | ||
| 'd3-quadtree': 'https://d3js.org/d3.v4.js', | ||
| 'd3-queue': 'https://d3js.org/d3.v4.js', | ||
| 'd3-random': 'https://d3js.org/d3.v4.js', | ||
| 'd3-request': 'https://d3js.org/d3.v4.js', | ||
| 'd3-sankey': 'https://unpkg.com/d3-sankey@0.7.1/build/d3-sankey.js', | ||
| 'd3-scale': 'https://d3js.org/d3.v4.js', | ||
| 'd3-selection-multi': 'https://unpkg.com/d3-selection-multi@1.0.1/build/d3-selection-multi.js', | ||
| 'd3-selection': 'https://d3js.org/d3.v4.js', | ||
| 'd3-shape': 'https://d3js.org/d3.v4.js', | ||
| 'd3-time-format': 'https://d3js.org/d3.v4.js', | ||
| 'd3-time': 'https://d3js.org/d3.v4.js', | ||
| 'd3-timer': 'https://d3js.org/d3.v4.js', | ||
| 'd3-transition': 'https://d3js.org/d3.v4.js', | ||
| 'd3-voronoi': 'https://d3js.org/d3.v4.js', | ||
| 'd3-zoom': 'https://d3js.org/d3.v4.js', | ||
| 'g-annotations': 'https://unpkg.com/g-annotations@^1/build/g-annotations.js', | ||
| 'g-axis': 'https://unpkg.com/g-axis@^1/build/g-axis.js', | ||
| 'g-chartcolour': 'https://unpkg.com/g-chartcolour@0.8.26/build/g-chartcolour.js', | ||
| 'g-chartframe': 'https://unpkg.com/g-chartframe@^5/build/g-chartframe.js', | ||
| 'g-legend': 'https://unpkg.com/g-legend@^1/build/g-legend.js', | ||
| 'g-xaxisdate': 'https://unpkg.com/g-axis@^1/build/g-axis.js', | ||
| 'g-yaxislinear': 'https://unpkg.com/g-axis@^1/build/g-axis.js', | ||
| 'plugin-babel': 'https://unpkg.com/systemjs-plugin-babel/plugin-babel.js', | ||
| 'plugin-json': 'https://unpkg.com/systemjs-plugin-json@0.3.0/json.js', | ||
| 'save-svg-as-png': 'https://unpkg.com/save-svg-as-png@1.4.6/lib/saveSvgAsPng.js', | ||
| 'simple-statistics': 'https://unpkg.com/simple-statistics@5.4.0/dist/simple-statistics.js', | ||
| 'systemjs-babel-build': | ||
| 'https://unpkg.com/systemjs-plugin-babel@0.0.25/systemjs-babel-browser.js', | ||
| 'topojson-client': 'https://unpkg.com/topojson-client@3.0.0/dist/topojson-client.js', | ||
| chai: 'https://www.chaijs.com/chai.js', | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * d3-bootloader | ||
| * 2017 Ændrew Rininsland <@aendrew>, Financial Times | ||
| * | ||
| * Allows the use of D3 using ES6 modules via SystemJS. | ||
| * This does Babel transpilation in the browser. It's probably | ||
| * not very performant — at all — and is intended for scenarios | ||
| * such as with Visual Vocabulary (I.e., D3 is being used as an | ||
| * in-browser tool for creating static print visualisations). | ||
| */ | ||
|
|
||
| import SystemJS from "systemjs"; | ||
| import modules from "../modules.js"; | ||
|
|
||
| const offlineModules = Object.entries(modules).reduce( | ||
| (a, [k, v]) => | ||
| Object.assign({}, a, { | ||
| [k]: `./${ | ||
| /d3js\.org\/d3\./.test(v) | ||
| ? "d3" | ||
| : /^@/.test(pkg) | ||
| ? pkg.slice(pkg.indexOf("/") + 1) | ||
| : pkg | ||
| }.js` | ||
| }), | ||
| {} | ||
| ); | ||
|
|
||
| const config = { | ||
| map: offlineModules, | ||
| packages: { | ||
| "plugin-babel": { | ||
| main: "plugin-babel.js" | ||
| } | ||
| }, | ||
| meta: { "*.json": { loader: "plugin-json" } }, | ||
| transpiler: "plugin-babel" | ||
| }; | ||
|
|
||
| SystemJS.config(config); | ||
|
|
||
| export default SystemJS.import("../index.js"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this file called?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tlfrd It's set as
bin.offlinein package.json so users can either install@financial-times/d3-bootloaderglobally or runnpx @financial-times/d3-bootloaderand it will: