From cd45c212b1f0ebdfd53fc70626537e9664047151 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Thu, 1 Feb 2018 23:40:01 -0700 Subject: [PATCH] make shared work nicely... hopefully --- client/other/shared | 1 + client/package.json | 3 ++- client/test/client-test-utils.js | 2 +- cypress/utils.js | 2 +- package.json | 17 ++--------------- scripts/deploy.sh | 6 +++--- scripts/install.js | 3 ++- server/__tests__/auth.js | 3 +-- server/__tests__/posts.js | 3 +-- server/other/shared | 1 + server/package.json | 13 ++++++++++--- server/src/auth.js | 2 +- server/src/db.js | 6 +++--- server/test/server-test-utils.js | 13 ++++++------- {other => shared}/generate.js | 6 +----- shared/index.js | 4 ++++ shared/package.json | 11 +++++++++++ 17 files changed, 51 insertions(+), 45 deletions(-) create mode 120000 client/other/shared create mode 120000 server/other/shared rename {other => shared}/generate.js (87%) create mode 100644 shared/index.js create mode 100644 shared/package.json diff --git a/client/other/shared b/client/other/shared new file mode 120000 index 00000000..5b802bae --- /dev/null +++ b/client/other/shared @@ -0,0 +1 @@ +/Users/kdodds/Developer/testing-workshop/shared \ No newline at end of file diff --git a/client/package.json b/client/package.json index 97b35932..14260b8e 100644 --- a/client/package.json +++ b/client/package.json @@ -34,7 +34,8 @@ "enzyme-to-json": "^3.3.0", "lodash.pick": "^4.4.0", "react-scripts": "^2.0.0-next.9754a231", - "react-test-renderer": "^16.2.0" + "react-test-renderer": "^16.2.0", + "til-shared": "file:other/shared" }, "browserslist": { "development": [ diff --git a/client/test/client-test-utils.js b/client/test/client-test-utils.js index 73464264..7533324a 100644 --- a/client/test/client-test-utils.js +++ b/client/test/client-test-utils.js @@ -2,7 +2,7 @@ import React from 'react' import {Router} from 'react-router-dom' import {mount} from 'enzyme' import {createMemoryHistory} from 'history' -import * as generate from '../../other/generate' +import * as generate from 'til-shared/generate' function mountWithRouter(ui, {route = '/'} = {}) { const history = createMemoryHistory({initialEntries: [route]}) diff --git a/cypress/utils.js b/cypress/utils.js index 477fadba..338e9338 100644 --- a/cypress/utils.js +++ b/cypress/utils.js @@ -1,4 +1,4 @@ -import * as generate from '../other/generate' +import * as generate from '../shared/generate' function loginAsNewUser() { return createNewUser().then(user => { diff --git a/package.json b/package.json index 4058c0da..820c9f70 100644 --- a/package.json +++ b/package.json @@ -29,21 +29,8 @@ "lint": "eslint . --cache --cache-location node_modules/.cache/eslint", "validate": "concurrently --names \"lint,test:coverage,test:e2e\" --prefix \"[{name}]\" --prefix-colors \"bgGreen.reset.bold,bgBlue.reset.bold,bgRed.reset.bold\" \"npm run lint --silent\" \"npm run test:coverage --silent\" \"npm run test:e2e --silent\"", "precommit": "lint-staged && npm run --silent validate", - "clean": "rimraf node_modules ./client/node_modules ./server/node_modules", - "setup": "node ./scripts/verify && node ./scripts/install && npm run validate", - "now-build": "cd server && npm install", - "now-start": "node ./server" - }, - "now": { - "name": "til", - "alias": "til.now.sh", - "files": [ - "./server", - "./shared" - ] - }, - "dependencies": { - "jsonwebtoken": "^8.1.1" + "clean": "rimraf node_modules ./shared/node_modules ./client/node_modules ./server/node_modules", + "setup": "node ./scripts/verify && node ./scripts/install && npm run validate" }, "devDependencies": { "all-contributors-cli": "^4.10.1", diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 8528d6e4..2bc2aa9b 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,3 +1,3 @@ -npm i -g now@9 -now -e NODE_ENV=production --token $NOW_TOKEN --npm deploy --public -now alias --token=$NOW_TOKEN +cd server +npx --package now@9 --call \ +"now -e NODE_ENV=production --token $NOW_TOKEN --npm deploy --public && now alias --token=$NOW_TOKEN" diff --git a/scripts/install.js b/scripts/install.js index bca41b7c..3b260f54 100644 --- a/scripts/install.js +++ b/scripts/install.js @@ -4,9 +4,10 @@ var path = require('path') var installDeps = require('./workshop-setup').installDeps var main = path.resolve(__dirname, '..') +var shared = path.resolve(__dirname, '../shared') var server = path.resolve(__dirname, '../server') var client = path.resolve(__dirname, '../client') -installDeps([main, server, client]).then( +installDeps([main, shared, server, client]).then( function() { console.log('👍 all dependencies installed') }, diff --git a/server/__tests__/auth.js b/server/__tests__/auth.js index 13b44d4f..c77d04e8 100644 --- a/server/__tests__/auth.js +++ b/server/__tests__/auth.js @@ -1,4 +1,3 @@ -import faker from 'faker' import axios from 'axios' import db from '../src/db' // eslint-disable-next-line @@ -23,7 +22,7 @@ afterAll(async () => { }) beforeEach(async () => { - testUser = generate.userData({id: faker.random.uuid()}) + testUser = generate.userData({id: generate.id()}) await resetDb({testUser}) const token = getUserToken(testUser) authAPI = axios.create({baseURL}) diff --git a/server/__tests__/posts.js b/server/__tests__/posts.js index cec5a83e..d3a7f894 100644 --- a/server/__tests__/posts.js +++ b/server/__tests__/posts.js @@ -1,4 +1,3 @@ -import faker from 'faker' import axios from 'axios' // eslint-disable-next-line import {resetDb, generate} from 'server-test-utils' @@ -21,7 +20,7 @@ afterAll(async () => { }) beforeEach(async () => { - testUser = generate.userData({id: faker.random.uuid()}) + testUser = generate.userData({id: generate.id()}) mockData = await resetDb({testUser}) const token = getUserToken(testUser) authAPI = axios.create({baseURL}) diff --git a/server/other/shared b/server/other/shared new file mode 120000 index 00000000..5b802bae --- /dev/null +++ b/server/other/shared @@ -0,0 +1 @@ +/Users/kdodds/Developer/testing-workshop/shared \ No newline at end of file diff --git a/server/package.json b/server/package.json index 3cb523a6..f6f03166 100644 --- a/server/package.json +++ b/server/package.json @@ -7,7 +7,9 @@ "start": "cross-env NODE_ENV=production node .", "dev": "node .", "build": "babel --ignore __tests__,__mocks__ --out-dir dist src", - "test": "cd .. && npm run test:server --silent" + "test": "cd .. && npm run test:server --silent", + "now-build": "echo \"build happens before deploy\"", + "now-start": "node ." }, "author": "Kent C. Dodds (http://kentcdodds.com/)", "license": "MIT", @@ -19,10 +21,16 @@ "express": "^4.16.2", "express-async-errors": "^2.1.0", "express-jwt": "^5.3.0", + "jsonwebtoken": "^8.1.1", "lodash": "^4.17.4", "loglevel": "^1.6.1", "passport": "^0.4.0", - "passport-local": "^1.0.0" + "passport-local": "^1.0.0", + "til-shared": "file:./other/shared" + }, + "now": { + "name": "til", + "alias": "til.now.sh" }, "babel": { "presets": [ @@ -46,7 +54,6 @@ "babel-plugin-transform-object-rest-spread": "^6.26.0", "babel-preset-env": "^1.6.1", "babel-register": "^6.26.0", - "faker": "^4.1.0", "nodemon": "^1.14.11" } } diff --git a/server/src/auth.js b/server/src/auth.js index 5c1209a8..31365a27 100644 --- a/server/src/auth.js +++ b/server/src/auth.js @@ -6,7 +6,7 @@ import { isPasswordValid, secret, getUserToken, -} from '../../shared/auth' +} from 'til-shared/auth' import db from './db' const authMiddleware = { diff --git a/server/src/db.js b/server/src/db.js index 81c51fcf..b8f53e5f 100644 --- a/server/src/db.js +++ b/server/src/db.js @@ -1,4 +1,4 @@ -import faker from 'faker' +import * as generate from 'til-shared/generate' const db = { users: [], @@ -20,7 +20,7 @@ const db = { async function insertUser(user) { const newUser = { ...user, - id: faker.random.uuid(), + id: generate.id(), } db.users.push(newUser) return newUser @@ -49,7 +49,7 @@ async function deleteUser(id) { async function insertPost(post) { const newPost = { ...post, - id: faker.random.uuid(), + id: generate.id(), } db.posts.push(newPost) return newPost diff --git a/server/test/server-test-utils.js b/server/test/server-test-utils.js index 4703b088..5ed8cc83 100644 --- a/server/test/server-test-utils.js +++ b/server/test/server-test-utils.js @@ -1,20 +1,19 @@ -import faker from 'faker' +import * as generate from 'til-shared/generate' import db from '../src/db' -import * as generate from '../../other/generate' function initDb() { const users = Array.from({length: 10}, () => - generate.userData({id: faker.random.uuid()}), + generate.userData({id: generate.id()}), ) const posts = users.map(u => - generate.postData({authorId: u.id, id: faker.random.uuid()}), + generate.postData({authorId: u.id, id: generate.id()}), ) db.users = users db.posts = posts const testUser = generate.userData({ - id: faker.random.uuid(), + id: generate.id(), username: 'til', password: 'til', }) @@ -23,10 +22,10 @@ function initDb() { async function resetDb({testUser} = {}) { const users = Array.from({length: 10}, () => - generate.userData({id: faker.random.uuid()}), + generate.userData({id: generate.id()}), ) const posts = users.map(u => - generate.postData({authorId: u.id, id: faker.random.uuid()}), + generate.postData({authorId: u.id, id: generate.id()}), ) db.users = users db.posts = posts diff --git a/other/generate.js b/shared/generate.js similarity index 87% rename from other/generate.js rename to shared/generate.js index 82f47142..9c79485e 100644 --- a/other/generate.js +++ b/shared/generate.js @@ -1,10 +1,6 @@ -// this file is not transpiled -// we could probably make it work -// but meh... ¯\_(ツ)_/¯ - const omit = require('lodash.omit') const faker = require('faker') -const {getSaltAndHash, getUserToken} = require('../shared/auth') +const {getSaltAndHash, getUserToken} = require('./auth') function userData(overrides = {}) { const password = overrides.password || faker.internet.password() diff --git a/shared/index.js b/shared/index.js new file mode 100644 index 00000000..d0ecf573 --- /dev/null +++ b/shared/index.js @@ -0,0 +1,4 @@ +module.exports = { + auth: require('./auth'), + generate: require('./generate'), +} diff --git a/shared/package.json b/shared/package.json new file mode 100644 index 00000000..325d3417 --- /dev/null +++ b/shared/package.json @@ -0,0 +1,11 @@ +{ + "name": "til-shared", + "version": "1.0.0", + "private": true, + "main": "./index", + "dependencies": { + "faker": "^4.1.0", + "jsonwebtoken": "^8.1.1", + "lodash.omit": "^4.5.0" + } +}