Skip to content

Commit

Permalink
build: generate code without building
Browse files Browse the repository at this point in the history
Reduce the steps in running restful-react by generating the OpenAPI doc and running the converter as part of the build separately. As a result, the script doesn't have to be built by Parcel.

This change also ensures that the file is built and shared with the host correctly when running the development environment.

Refs #399
  • Loading branch information
thewilkybarkid committed Aug 20, 2021
1 parent 37a1d25 commit a3773cb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/deploy/
/dist/
/node_modules/
/src/frontend/hooks/api-hooks.tsx
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ RUN \



#
# Stage: Frontend hooks build
#
FROM builder AS frontend-hooks

COPY src/common/ src/common/
COPY src/backend/ src/backend/

RUN \
mkdir --parents dist src/frontend/hooks \
&& npm run build:hooks \
&& rm -rf .parcel-cache



#
# Stage: Frontend build
#
Expand All @@ -71,6 +86,7 @@ FROM builder AS frontend
COPY --from=scripts /app/dist/scripts/ dist/scripts/
COPY src/common/ src/common/
COPY src/frontend/ src/frontend/
COPY --from=frontend-hooks /app/src/frontend/hooks/ src/frontend/hooks/

RUN \
npm run build:frontend \
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
"lint": "eslint --ignore-path .gitignore ./src/backend",
"test": "jest --coverage --forceExit --passWithNoTests",
"doc": "jsdoc -d ./dist/doc/ -r ./src/backend ./src/frontend",
"build": "rimraf dist && npm run build:scripts && npm run build:backend && npm run build:frontend",
"build:dev": "rimraf dist && npm run build:scripts && npm run build:backend:dev && npm run build:frontend:dev",
"build": "rimraf dist && npm run build:scripts && npm run build:backend && npm run build:hooks && npm run build:frontend",
"build:dev": "rimraf dist && npm run build:scripts && npm run build:backend:dev && npm run build:hooks && npm run build:frontend:dev",
"build:backend": "parcel build --target=backend --no-optimize src/backend/index.js && npm run build:templates",
"build:backend:dev": "parcel build --target=backend --no-optimize src/backend/index.js && npm run build:templates",
"watch:backend:dev": "npm-watch build:backend:dev",
"build:frontend": "npm run build:hooks && parcel build --no-scope-hoist --target=frontend src/frontend/index.html",
"build:frontend:dev": "npm run build:hooks && parcel build --target=frontend src/frontend/index.html",
"build:frontend": "parcel build --no-scope-hoist --target=frontend src/frontend/index.html",
"build:frontend:dev": "parcel build --target=frontend src/frontend/index.html",
"build:scripts:migrations": "npm run scan:migrations && parcel build --target=migrations-script src/backend/scripts/dbMigrations.ts",
"build:scripts:seeds": "parcel build --target=seeds-script src/backend/scripts/dbSeeds.ts",
"build:scripts:init": "parcel build --target=init-script --no-optimize src/backend/scripts/dbInit.ts",
"build:scripts:spec": "parcel build --target=spec-script src/backend/scripts/genSpec.js",
"build:scripts": "rimraf dist/scripts && npm run build:scripts:migrations && npm run build:scripts:init && npm run build:scripts:spec",
"build:hooks:spec": "node dist/scripts/genSpec.js",
"build:hooks:spec": "ts-node --transpile-only src/backend/scripts/genSpec.js > ./dist/openapi.json",
"build:hooks:import": "restful-react import --file ./dist/openapi.json --output ./src/frontend/hooks/api-hooks.tsx",
"build:hooks": "npm run build:hooks:spec && npm run build:hooks:import && rimraf dist/openapi.json",
"build:templates": "copyfiles -u 2 src/backend/templates/email/**/*.hbs dist/backend",
Expand All @@ -38,7 +38,7 @@
"start:backend": "node dist/backend/index.js",
"start:backend:dev": "nodemon -w dist/backend --enable-source-maps dist/backend/index.js -p 3001",
"start": "npm run start:backend",
"start:frontend:dev": "parcel serve -p 3000 src/frontend/index.html",
"start:frontend:dev": "npm run build:hooks && parcel serve -p 3000 src/frontend/index.html",
"start:dev": "concurrently -k -p \"[{name}]\" -n \"BACKEND,FRONTEND,WATCH\" -c \"bgBlue.bold,bgMagenta.bold,bgGreen.bold\" \"npm run start:backend:dev\" \"npm run start:frontend:dev\" \"npm run watch:backend:dev\""
},
"targets": {
Expand Down
7 changes: 1 addition & 6 deletions src/backend/scripts/genSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'fs';
import path from 'path';
import { SwaggerAPI } from 'koa-joi-router-docs';
import preprintRoutes from '../controllers/preprint.js';
import authWrapper from '../middleware/auth.js';
Expand All @@ -19,9 +17,6 @@ import tagRoutes from '../controllers/tag.js';
import templateRoutes from '../controllers/template.js';
import notificationRoutes from '../controllers/notification.js';

const __dirname = path.resolve();
const OUT_FILE = path.resolve(__dirname, 'dist', 'openapi.json');

function docs() {
const generator = new SwaggerAPI();
const authz = authWrapper({}); // authorization, not authentication
Expand Down Expand Up @@ -65,7 +60,7 @@ function docs() {
);

const specJson = JSON.stringify(spec, null, 2);
fs.writeFileSync(OUT_FILE, specJson);
process.stdout.write(specJson);
}

docs();

0 comments on commit a3773cb

Please sign in to comment.