https://github.com/daeh/jspsych-template

jsPsych with Firestore (serverless) and ESLint flat config

https://github.com/daeh/jspsych-template

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.6%) to scientific vocabulary

Keywords

eslint eslint-config experiment firestore jspsych prettier psychology serverless template typescript
Last synced: 5 months ago · JSON representation

Repository

jsPsych with Firestore (serverless) and ESLint flat config

Basic Info
  • Host: GitHub
  • Owner: daeh
  • License: isc
  • Language: TypeScript
  • Default Branch: main
  • Homepage:
  • Size: 5.4 MB
Statistics
  • Stars: 2
  • Watchers: 1
  • Forks: 3
  • Open Issues: 0
  • Releases: 9
Topics
eslint eslint-config experiment firestore jspsych prettier psychology serverless template typescript
Created over 2 years ago · Last pushed 11 months ago
Metadata Files
Readme License

README.md

serverless jsPsych Template (jsPsych-Firebase-Firestore-Prolific-Vite)

This repository is an example of how to setup a development environment for building online psych experiments. Key aspects are:

Installation

  • Fork this repo: Use this template > Create a new repository
  • Git clone the forked repo (replace with your repo info): e.g. git clone --branch main https://github.com/daeh/jspsych-template.git jspsych-template
  • Enter the repo folder (replace with your repo info): e.g. cd jspsych-template

Install Node Dependencies

Install the dependencies using Yarn

```shell

From within the cloned repo folder

Install the Firebase dependencies

yarn install

Start the Vite server

yarn dev ```

If you're new to Yarn Yarn is a package manager, like `npm`, for Node. https://yarnpkg.com/getting-started/install https://yarnpkg.com/corepack ```shell npm install -g corepack corepack enable yarn set version stable yarn install ```

Usage

You can format, lint and build the project from the command line by calling the commands in package.json:

package.json scripts ```json { "scripts": { "dev": "vite --config vite.config.mts hosting", "build": "vite build --config vite.config.mts hosting", "lint": "ESLINT_USE_FLAT_CONFIG=true && prettier --config prettier.config.mjs --write . && eslint --config eslint.config.mjs --fix . && tsc --project tsconfig.json --noEmit" } } ```

To develop the website, run yarn dev, which will open a localhost Vite server that will update as you make modifications.

Sandbox

You don't need to set up Firebase, Firestore or Prolific to develop the experiment. This app comes built to start development immediately. There are primarily two toggles to help with sandboxed development, which are found in appConfig.ts.

  • Setting const debuggingMode = true will increase the verbosity of the console output.

    • Alternatively, you can force debugging mode by including debug as a URL Parameter, e.g. https://mysite.web.app/?debug=true
  • Setting const simulateMockDatabase = true will make the app use a serverless emulator so that you don't have to set up Firestore before beginning development.

    • Similarly, you can modulate the backend with a URL Parameter, e.g. https://mysite.web.app/?debug=false&mock=true

Configuration

Components

For developing the website, this project uses

The ESLint config integrates these configurations.

For bundling the website, this project uses

For serving the website, this project uses

ESLint

This project uses a future-looking configuration that implements the major developments from ESLint. The main config file in this repo is the flat ESLint config, eslint.config.mjs.

ES Module parsing This project is configured as an `ES Module`, so this config file could be named `eslint.config.js`, but I have given it the `mjs` extension to make this config work for `Common.js Module` development with minimal reconfiguration.

Flat Config System

Beginning in ESLint v9.0.0, the default will be the new flat config system. This will deprecate the Common.js Module config system (which uses .eslintrc.js), replacing it with the ES Module config system (which uses eslint.config.js).

Stylistic Plugin

ESLint is deprecating formatting rules, passing over maintenance and development to the community-run plugin ESLint Stylistic.

IDE

VS Code Settings

For VS Code to respect the configuration, you need to specify the formatter for the relevant files. This is done for you in VSCodeProject.code-workspace and in .vscode/settings.json (these are redundant, you only need one). This configures the ESLint extension to use the flat config system, makes VS Code use the Prettier - Code Formatter extensions for formatting filetypes not covered by ESLint, and enables Tailwind CSS IntelliSense. This obviously requires that you have these extensions enabled for the workspace. Activate the VSCodeProject.code-workspace via File > Open Workspace from File... (or by double-clicking it), or activate .vscode via File > Open Folder... in VS Code.

The relevant settings are:

VSCode Settings ```json { "editor.formatOnSave": true, "eslint.useFlatConfig": true, "editor.codeActionsOnSave": { "source.fixAll.eslint": "explicit" }, "files.associations": { "*.css": "tailwindcss" }, "[javascript][javascriptreact][typescript]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" }, "[html]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[json][jsonc]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[css][scss][less]": { "editor.defaultFormatter": "esbenp.prettier-vscode" } } ```

IntelliJ IDEA Settings

For IntelliJ IDEA / WebStorm to respect the configuration, you need to enable ESLint and Prettier for the relevant filetypes. There is an example config in .idea. To enable ESLint and Prettier manually:

IntelliJ Setup - `Settings... > Languages & Frameworks > JavaScript > Code Quality Tools > ESLint` - Enable `Automatic ESLint configuration` - Enable `Run eslint --fix on save` - Add the additional filetypes to the `Run for files` field: - `{**/*,*}.{ts,mts,cts,tsx,mtsx,js,mjs,cjs,jsx,mjsx,html,vue}` - `Settings... > Languages & Frameworks > JavaScript > Prettier` - Enable `Automatic Prettier configuration` - Enable `Run on save` - Add the additional filetypes to the `Run for files` field: - `{**/*,*}.{ts,mts,cts,tsx,mtsx,js,mjs,cjs,jsx,mjsx,json,html,css,scss,vue,astro}` If you change the project from an `ES Module` to a `Common.js Module`, or if ESLint isn't working, try [this fix from Ditlef Diseth](https://youtrack.jetbrains.com/issue/WEB-61117/ESLint-flat-config-doesnt-work-with-non-default-custom-path-to-the-config-file#focus=Comments-27-8196242.0-0): - `Settings... > Languages & Frameworks > JavaScript > Code Quality Tools > ESLint` - Switch to `Manual ESLint configuration` - Add this string to the `Extra ESLint options` field: ```shell ESLINT_USE_FLAT_CONFIG=true yarn eslint --config eslint.config.mjs ```

Integrations

Hosting and Database

Firebase and Firestore Configuration

Firebase Setup TODO: describe how to setup hosting and database Here's a [useful guide](https://firebase.google.com/docs/hosting/quickstart) After you setup your Firebase and Firestore accounts and services, add your configurations to - [`.firebaserc`](.firebaserc) - [`creds.ts`](hosting/src/creds.ts) ### Firestore Push the Firestore rules (defined in firestore.rules) ```sh yarn deploy-rules ``` Once you have the rules deployed, you can switch from using the mock database (a simple database emulator) to Firestore. Set `const simulateMockDatabase = false` in [`appConfig.ts`](hosting/src/appConfig.ts). This will cause the application to use [`creds.ts`](hosting/src/creds.ts) and store the input in Firestore. In production, the app writes to `exptData/:uid`, but will instead write to `exptData-dbug/:uid` when it is in debugging mode or running locally. This is so that there's no confusion about what data was generated locally during development. ### Firebase When you're ready to deploy the website, push it to Firebase ```sh yarn deploy ```

Data Collection

Prolific Configuration

Prolific URL Search Params ### Prolific URL Search Params The project is looks for Prolific URL parameters and stores them. Make sure that you've set up Prolific to use URL Search Params. ![Prolific_param](assets-readme/Prolific_param.png) ### Prolific Completion Code In order to register that Prolific users have completed the experiment, add the study's **Completion Code** to `const prolificCompletionCode = ...` in [`creds.ts`](hosting/src/creds.ts).

Deploying an experiment

The script scripts/releaseScript.mjs automates deployment of the experiments. You can run it from the root directory with:

shell yarn release

The script will walk you through committing your changes to the git repo that you forked.

A key idea here is that there should never be ambiguity about what code was served to a participant.

The releaseScript.mjs prompts you to increment the version number, generates a new git commit, injects the version number and the git commit's SHA hash variables into the static website, and then deploys the website to Firebase. The version and hash variables are stored alongside a user's responses.

Deployment TODO: elaborate

Retrieving Data

Retrieve the data from Firestore using the Firebase Admin SDK, which you must authorize with credentials from your Firebase project.

Generate credentials to access the firestore database

  1. Go to the Firebase Console.
  2. Select your project.
  3. Navigate to "Project settings" > "Service accounts".
  4. Click "Generate new private key" and save the JSON file.

Save credential in an encrypted disk image (NOT IN YOUR GIT REPO)

Encryption Setup TODO: describe sparse image strategy

Install Firebase Admin SDK

shell pip install firebase-admin

Python Setup TODO: give environment requirements for python script

Download the data

Run the script in scripts/retrieve_data.py

shell python retrieve_data.py \ --cred "~/Desktop/myproject-firebase-adminsdk.json" \ --out "~/Desktop/dataout" \ --collection 'exptData' 'sharedData'

  • --cred the path to the private key you downloaded from Firebase.
  • --out the path to a directory where the files will be saved (the directory will be created; the path cannot exist yet)
  • --collection the collections to download (during development, these are exptData-dbug and sharedData-dbug)

Author

Personal Website BlueSky Twitter

Owner

  • Name: Dae
  • Login: daeh
  • Kind: user
  • Location: Cambridge, MA
  • Company: MIT

Neukom Computational Science Postdoc Fellow at Dartmouth. PhD from MIT Brain and Cognitive Sciences.

GitHub Events

Total
  • Release event: 6
  • Push event: 17
  • Gollum event: 13
  • Create event: 6
Last Year
  • Release event: 6
  • Push event: 17
  • Gollum event: 13
  • Create event: 6

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Dependencies

hosting/package.json npm
  • @jspsych/plugin-html-keyboard-response ^1.1.3 development
  • @jspsych/plugin-image-keyboard-response ^1.1.3 development
  • @jspsych/plugin-preload ^1.1.3 development
  • @stylistic/eslint-plugin ^1.4.1 development
  • @stylistic/eslint-plugin-js ^1.4.1 development
  • @stylistic/eslint-plugin-ts ^1.4.1 development
  • @types/eslint-config-prettier ^6.11.3 development
  • @typescript-eslint/eslint-plugin ^6.12.0 development
  • @typescript-eslint/parser ^6.12.0 development
  • autoprefixer ^10.4.16 development
  • browserslist-to-esbuild ^1.2.0 development
  • eslint ^8.54.0 development
  • eslint-config-prettier ^9.0.0 development
  • eslint-import-resolver-typescript ^3.6.1 development
  • eslint-plugin-import ^2.29.0 development
  • eslint-plugin-prettier ^5.0.1 development
  • globals ^13.23.0 development
  • postcss ^8.4.31 development
  • prettier ^3.1.0 development
  • prettier-plugin-tailwindcss ^0.5.7 development
  • tailwindcss ^3.3.5 development
  • typescript ^5.3.2 development
  • vite ^5.0.2 development
  • vite-plugin-html ^3.2.0 development
  • firebase ^10.6.0
  • jspsych ^7.3.4
hosting/yarn.lock npm
  • 487 dependencies
package.json npm
  • firebase-tools ^12.9.1 development
yarn.lock npm
  • 615 dependencies