UNPKG

17.6 kBMarkdownView Raw
1<h1 align="center">
2 data-visuals-create
3</h1>
4<p align="center">
5 <a href="https://www.npmjs.org/package/@data-visuals/create"><img src="https://img.shields.io/npm/v/@data-visuals/create.svg?style=flat" alt="npm"></a>
6 <a href="https://david-dm.org/texastribune/data-visuals-create"><img src="https://david-dm.org/texastribune/data-visuals-create/status.svg" alt="dependencies"></a>
7 <a href="https://david-dm.org/texastribune/data-visuals-create/?type=dev"><img src="https://david-dm.org/texastribune/data-visuals-create/dev-status.svg" alt="devDependencies"></a>
8</p>
9
10A tool for generating the scaffolding needed to create a graphic or feature the Data Visuals way.
11
12## Key features
13
14- 📐 **HTML templating with a familiar, easy Jinja2-esque format** via a modified instance of a [Nunjucks](https://github.com/mozilla/nunjucks) environment that comes with all the functionality of [`journalize`](https://github.com/rdmurphy/journalize) by default.
15- 🎨 **Supports SCSS syntax** for styles compiled with the super fast reference implementation of Sass via [`dart-sass`](https://github.com/sass/dart-sass). All CSS is passed through [`autoprefixer`](https://github.com/postcss/autoprefixer) and minified with [`clean-css`](https://github.com/jakubpawlowicz/clean-css) in production.
16- 📦 A **configured instance of Webpack ready to go** and optimized for a [two-path modern/legacy bundle approach](https://philipwalton.com/articles/deploying-es2015-code-in-production-today/). Ship lean ES2015+ code to modern browsers, and a functional polyfilled/transpiled bundle to the rest!
17- 📑 Full-support for **[ArchieML](http://archieml.org/) formatted Google Docs and key/value or table formatted Google Sheets**. Use data you've collaborated on with reporters and editors directly in your templates.
18- 🎊 And so, so, so much more!
19
20## Getting started
21
22```sh
23npx @data-visuals/create feature my-great-project
24cd my-great-project
25npm start
26```
27
28> While you can install `@data-visuals/create` globally and use the `data-visuals-create` command, we recommend using the `npx` method instead to ensure you are always using the latest version.
29
30## Table of contents
31
32<!-- START doctoc generated TOC please keep comment here to allow auto update -->
33<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
34
35
36- [Installation](#installation)
37- [Usage](#usage)
38- [Folder structure](#folder-structure)
39 - [config/](#config)
40 - [data/](#data)
41 - [workspace/](#workspace)
42 - [project.config.js](#projectconfigjs)
43 - [app/](#app)
44 - [app/index.html](#appindexhtml)
45 - [app/templates/](#apptemplates)
46 - [app/scripts/](#appscripts)
47 - [app/styles/](#appstyles)
48 - [app/assets/](#appassets)
49 - [Other directories you may see](#other-directories-you-may-see)
50 - [.tmp/](#tmp)
51 - [dist/](#dist)
52- [How to work with Google Doc and Google Sheet files](#how-to-work-with-google-doc-and-google-sheet-files)
53 - [Google Docs](#google-docs)
54 - [Google Sheets](#google-sheets)
55- [Supported browsers](#supported-browsers)
56- [How do JavaScript packs work?](#how-do-javascript-packs-work)
57 - [Creating a new entrypoint](#creating-a-new-entrypoint)
58 - [Connecting an entrypoint to an HTML file](#connecting-an-entrypoint-to-an-html-file)
59- [Available commands](#available-commands)
60 - [`npm start` or `npm run serve`](#npm-start-or-npm-run-serve)
61 - [`npm run deploy`](#npm-run-deploy)
62 - [`npm run data:fetch`](#npm-run-datafetch)
63 - [`npm run assets:push`](#npm-run-assetspush)
64 - [`npm run assets:pull`](#npm-run-assetspull)
65 - [`npm run workspace:push`](#npm-run-workspacepush)
66 - [`npm run workspace:pull`](#npm-run-workspacepull)
67- [Environment variables and authentication](#environment-variables-and-authentication)
68 - [AWS](#aws)
69 - [Google](#google)
70 - [CLIENT_SECRETS_FILE](#client_secrets_file)
71 - [GOOGLE_TOKEN_FILE](#google_token_file)
72- [License](#license)
73
74<!-- END doctoc generated TOC please keep comment here to allow auto update -->
75
76## Installation
77
78While we recommend using the `npx` method, you can also install the tool globally. If you do, replace all instances of `npx @data-visuals/create` you see with `data-visuals-create`.
79
80```sh
81npm install -g @data-visuals/create
82```
83
84## Usage
85
86```sh
87npx @data-visuals/create <project-type> <slug>
88```
89
90Currently there are two project types available — `graphic` and `feature`.
91
92```sh
93npx @data-visuals/create graphic school-funding
94```
95
96This will create a directory for you, copy in the files, install the dependencies, and do your first `git commit`.
97
98The directory name will be formatted like this:
99
100```
101<project-type>-<slug>-<year>-<month>
102
103Using the example command above, it would be the following:
104graphic-school-funding-2018-01
105```
106
107This is to ensure consistent naming of our directories!
108
109## Folder structure
110
111After creation, your project directory should look something like this:
112
113```
114your-project/
115 README.md
116 node_modules/
117 config/
118 data/
119 workspace/
120 package.json
121 project.config.js
122 app/
123 index.html
124 templates/
125 styles/
126 scripts/
127 assets/
128```
129
130Here are the highlights of what each file/directory represents:
131
132#### config/
133
134This is the directory of all the configuration and tasks that power the kit. You probably do not need to ever go in here! (And eventually this will be abstracted away.)
135
136#### data/
137
138Where data downloaded and processed with `npm run data:fetch` ends up. You are also free to manually (or via your own scripts!) put data files here - they will get pulled in too! Be aware that the only compatible data files that belong here are ones that [`quaff`](https://github.com/rdmurphy/quaff) knows how to consume, otherwise it will ignore them.
139
140#### workspace/
141
142The `workspace` directory is for storing all of your analysis, production and raw data files. It's important to use this directory for these files (instead of `app/assets/` or `data/`) so we can keep them out of GitHub and away from other parts of the kit. You interact with it using the `npm run workspace:push` and `npm run workspace:pull` commands.
143
144#### project.config.js
145
146Where all the configuration for a project belongs. This is where you can change the S3 deploy parameters, manage the Google Drive documents that sync with this project, set up a bespoke API or add custom filters to Nunjucks.
147
148#### app/
149
150Where you'll spend most of your time! Here are where all the assets that go into building your project live.
151
152#### app/index.html
153
154The starter HTML page that's provided by the kit. If your project is only a single page (or graphic), this will likely be where you do all your HTML work. No special configuration is required to create new HTML files - just creating a new `.html` file in in the `app` directory (but _not_ within `app/scripts/` or `/app/templates/` - HTML files have special meanings in those directories) is enough to tell the kit about new pages it should compile.
155
156#### app/templates/
157
158Where all the Nunjucks templates (including the `base.html` template that `app/index.html` inherits from), includes and macros live.
159
160#### app/scripts/
161
162Where all of our JavaScript files live. Within this folder there are a number of helpful utilities and scripts we've created across tons of projects. JavaScript imports work as you'd expect, but the `app/scripts/packs/` directory is special - learn more about it in the [How do JavaScript packs work?](#how-do-javascript-packs-work) section.
163
164#### app/styles/
165
166All the SCSS files that are used to compile the CSS files live here. This includes all of our house styles and variables (`app/styles/_variables.scss`). `app/styles/main.scss` is the primary entrypoint - any changes you make will either need to be in this file or be imported into it.
167
168#### app/assets/
169
170Where all other assets should live. This includes images, font files, any JSON or CSV files you want to directly interact with in your JavaScript - these files are post-processed and deployed along with the other production files. Be aware, **anything in this directory will technically be public on deploy**. Use `workspace/` or `data/` instead for things that shouldn't be public.
171
172### Other directories you may see
173
174#### .tmp/
175
176This is a temporary folder where files compiled during development will be placed. You can safely ignore it.
177
178#### dist/
179
180This is the compiled project and the result of running `npm run build`.
181
182## How to work with Google Doc and Google Sheet files
183
184`@data-visuals/create` projects support downloading ArchieML-formatted Google Docs and correctly-formatted Google Sheets directly from Google Drive for use within your templates. All files you want to use in your projects should be listed in `project.config.js` under the `files` key. You are not limited to one of each, either! (Our current record is **seven** Google Docs and **two** Google Sheets in a single project.)
185
186```js
187{ // ...
188 /**
189 * Any Google Doc and Google Sheet files to be synced with this project.
190 */
191 files: [
192 {
193 fileId: '<the-document-id-from-the-url>',
194 type: 'doc',
195 name: 'text',
196 },
197 {
198 fileId: '<the-sheet-id-from-the-url>',
199 type: 'sheet',
200 name: 'data',
201 },
202 // ...
203}
204```
205
206Each object representing a file needs three things:
207
208**fileId**
209
210The `fileId` key represents the ID of a Google Doc or Google Sheet. This is most easily found in the URL of a document when you have it open in your browser.
211
212**type**
213
214The `type` key is used to denote whether this is a Google Doc (`doc`) or a Google Sheet (`sheet`). This controls how it gets processed.
215
216**name**
217
218The `name` key controls what filename it will receive once it's put in the `data/` directory. So if the `name` is `hello`, it'll be saved to `data/hello.json`.
219
220### Google Docs
221
222ArchieML Google Docs work as documented on the [ArchieML](http://archieml.org/) site. This includes the automatic conversion of links to `<a>` tags!
223
224### Google Sheets
225
226Google Sheets processed by `@data-visuals/create` may potentially require some additional configuration. Each sheet (or tab) in a Google Sheet is converted separately by the kit, and keyed-off in the output object by the _name of the sheet_.
227
228By default it treats every sheet in a Google Sheet as being formatted as a `table`. In other words, every _row_ is considered an item, and the _header row_ determines the key of each value in a _column_.
229
230The Google Sheets processor also supports a `key-value` format as popularized by [`copytext`](https://github.com/nprapps/copytext) ([and its Node.js counterpart](https://github.com/rdmurphy/node-copytext)). This treats everything in the _first column_ as the key, and everything in the _second column_ as the value matched to its key. Every other column is _ignored_.
231
232To activate the `key-value` format, add `:kv` to the end of a sheet's filename. (For consistency you can also use `:table` to tell the processor to treat a sheet as a `table`, but it is not required due to it being the default.)
233
234If there are any sheets you want to exclude from being processed, you can do it via two ways: hide them using the native _hide_ mechanism in Google Sheets, or add `:skip` to the end of the sheet name.
235
236## Supported browsers
237
238`@data-visuals/create` projects use a two-prong JavaScript bundling method to ship a lean, modern bundle for evergreen browsers and and a polyfilled, larger bundle for legacy browsers. It uses the methods promoted in Philip Walton's [Deploying ES2015+ Code in Production Today](https://philipwalton.com/articles/deploying-es2015-code-in-production-today/) blog post and determines browser support based on whether a browser understands ES Module syntax. If a browser does, it gets the **modern** bundle. If it doesn't, it gets the **legacy** bundle.
239
240In practice this means you mostly do not have to worry about it - as long as you're using the [JavaScript packs correctly](#how-do-javascript-packs-work) everything should just work. In terms of actual browsers, while we do still currently do a courtesy check of how things look in _Internet Explorer 11_, it's not considered a dealbreaker if a complicated feature or graphic does not work there and would require extensive work to ensure compatibility.
241
242For CSS we currently pass the following to [`autoprefixer`](https://github.com/postcss/autoprefixer).
243
244```json
245"browserslist": ["> 0.5%", "last 2 versions", "Firefox ESR", "not dead"]
246```
247
248## How do JavaScript packs work?
249
250Projects created with `@data-visuals/create` borrow a Webpack approach from [`rails/webpacker`](https://github.com/rails/webpacker) to manage JavaScript entrypoints without configuration. To get the right scripts into the right pages, you have to do two things.
251
252### Creating a new entrypoint
253
254By default every project will come with an entrypoint file located at `app/scripts/packs/main.js`, but you're not required to only use that if it makes sense to have different sets of scripts for different pages. **Any** JavaScript file that exists within `app/scripts/packs/` is considered a Webpack entrypoint.
255
256```sh
257touch app/scripts/packs/maps.js
258# Now the build task will create a new entrypoint called `maps`! Don't forget to add your code.
259```
260
261### Connecting an entrypoint to an HTML file
262
263Because there's a lot more going on behind the scenes than just adding a `<script>` tag, you have to set a special variable in a template in order to get the right entrypoint into the right HTML file.
264
265Set `jsPackName` anywhere in the HTML file to the name of your entrypoint (__without__ the extension) to route the right JavaScript files to it.
266
267```html
268{% set jsPackName = 'map' %}
269{# This is now using the new entrypoint we created above #}
270```
271
272Pack entrypoints can be used multiple times across multiple pages, so if your code allows for it feel free to add an entrypoint to multiple pages. (You can also add `jsPackName` to the base `app/templates/base.html` file and have it inserted in every page that inherits from it).
273
274## Available commands
275
276All project templates share the same build commands.
277
278#### `npm start` or `npm run serve`
279
280The main command for development. This will build your HTML pages, prepare your SCSS files and compile your JavaScript. A local server is set up so you can view the project in your browser.
281
282#### `npm run deploy`
283
284The main command for deployment. It will always run `npm run build` first to ensure the compiled version is up-to-date. Use this when you want to put your project online. This will use the `bucket` and `folder` values in the `project.config.js` file to determine where it should be deployed on S3. Make sure those are set the appropriate values!
285
286#### `npm run data:fetch`
287
288This command uses the array of files listed under the `files` key in `project.config.js` to download data to the project. This data will be processed and made available in the `data` folder in the root of the project.
289
290You can also set `dataDir` in `project.config.js` to change the location of that directory if necessary.
291
292#### `npm run assets:push`
293
294This pushes all the raw files found in the `app/assets` directory to S3 to a `raw_assets` directory. This makes it possible for collaborators on the project to sync up with your assets when they run `npm run assets:pull`. This prevents potentially large assets like photos and audio clips from ending up in GitHub. This also runs automatically when `npm run deploy` is used.
295
296#### `npm run assets:pull`
297
298Pulls any raw assets that have been pushed to S3 back down to the project's `app/assets` directory. Good for ensuring you have the same files as anyone else who is working on the project.
299
300#### `npm run workspace:push`
301
302The `workspace` directory is for storing all of your analysis, production and raw data files. It's important to use this directory for these files (instead of `assets` or `data`) so we can keep them out of GitHub. This command will push the contents of the `workspace` directory to S3.
303
304#### `npm run workspace:pull`
305
306Pulls any `workspace` files that have been pushed to S3 back down to the project's local `workspace` directory. This is helpful for ensuring you're in sync with another developer.
307
308## Environment variables and authentication
309
310Any projects created with `data-visuals-create` assume you're working within a Texas Tribune environment, but it is possible to point AWS (used for deploying the project and assets to S3) and Google's API (used for interfacing with Google Drive) at your own credentials.
311
312### AWS
313
314Projects created with `data-visuals-create` support two of the built-in ways that `aws-sdk` can authenticate. If you are already set up with the [AWS shared credentials file](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html) (and those credentials are allowed to interact with your S3 buckets), you're good to go. `aws-sdk` will also recognize the [AWS credential environmental variables](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html).
315
316### Google
317
318The interface with Google Drive within `data-visuals-create` projects currently only supports using Oauth2 credentials to speak to the Google APIs. This requires a set of OAuth2 credentials that will be used to generate and save an access token to your computer. `data-visuals-create` projects have hardcoded locations for the credential file and token file, but you may override those with environmental variables.
319
320#### CLIENT_SECRETS_FILE
321
322**default**: `~/.tt_kit_google_client_secrets.json`
323
324#### GOOGLE_TOKEN_FILE
325
326**default**: `~/.google_drive_fetch_token`
327
328## License
329
330MIT