Skip to content

Commit

Permalink
make shared work nicely... hopefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Feb 2, 2018
1 parent 71a43b5 commit cd45c21
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 45 deletions.
1 change: 1 addition & 0 deletions client/other/shared
3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion client/test/client-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down
2 changes: 1 addition & 1 deletion cypress/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as generate from '../other/generate'
import * as generate from '../shared/generate'

function loginAsNewUser() {
return createNewUser().then(user => {
Expand Down
17 changes: 2 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 2 additions & 1 deletion scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
},
Expand Down
3 changes: 1 addition & 2 deletions server/__tests__/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import faker from 'faker'
import axios from 'axios'
import db from '../src/db'
// eslint-disable-next-line
Expand All @@ -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})
Expand Down
3 changes: 1 addition & 2 deletions server/__tests__/posts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import faker from 'faker'
import axios from 'axios'
// eslint-disable-next-line
import {resetDb, generate} from 'server-test-utils'
Expand All @@ -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})
Expand Down
1 change: 1 addition & 0 deletions server/other/shared
13 changes: 10 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]> (http://kentcdodds.com/)",
"license": "MIT",
Expand All @@ -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": [
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion server/src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
isPasswordValid,
secret,
getUserToken,
} from '../../shared/auth'
} from 'til-shared/auth'
import db from './db'

const authMiddleware = {
Expand Down
6 changes: 3 additions & 3 deletions server/src/db.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import faker from 'faker'
import * as generate from 'til-shared/generate'

const db = {
users: [],
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions server/test/server-test-utils.js
Original file line number Diff line number Diff line change
@@ -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',
})
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions other/generate.js → shared/generate.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
4 changes: 4 additions & 0 deletions shared/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
auth: require('./auth'),
generate: require('./generate'),
}
11 changes: 11 additions & 0 deletions shared/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit cd45c21

Please sign in to comment.