From 4b7252c15f480a45d2e3b170e5885c5e5e72ade8 Mon Sep 17 00:00:00 2001 From: Pacharapol Withayasakpunt Date: Wed, 16 Dec 2020 14:13:51 +0700 Subject: [PATCH] deploy to gcloud --- .git-hooks/precommit.sh | 4 ++ packages/nuxt/.gcloudignore | 19 +++++++ packages/nuxt/app.yaml | 20 ++++++++ packages/nuxt/assets/global.ts | 72 --------------------------- packages/nuxt/nuxt.config.ts | 9 +++- packages/nuxt/package.json | 5 +- packages/nuxt/pages/blog.vue | 8 +-- packages/nuxt/pages/post/_.vue | 54 +++++++++----------- packages/nuxt/pages/tag/_tag.vue | 8 +-- packages/nuxt/plugins/axios-fix.ts | 31 ++++++++++++ packages/nuxt/tsconfig.json | 2 +- packages/nuxt/yarn.lock | 79 ++++++++++++++++++++++++++++++ 12 files changed, 190 insertions(+), 121 deletions(-) create mode 100644 .git-hooks/precommit.sh create mode 100644 packages/nuxt/.gcloudignore create mode 100644 packages/nuxt/app.yaml create mode 100644 packages/nuxt/plugins/axios-fix.ts diff --git a/.git-hooks/precommit.sh b/.git-hooks/precommit.sh new file mode 100644 index 0000000..a6b495d --- /dev/null +++ b/.git-hooks/precommit.sh @@ -0,0 +1,4 @@ +#!/usr/bin/zsh + +# current directory is always `.git/..` root +# echo $PWD diff --git a/packages/nuxt/.gcloudignore b/packages/nuxt/.gcloudignore new file mode 100644 index 0000000..2230487 --- /dev/null +++ b/packages/nuxt/.gcloudignore @@ -0,0 +1,19 @@ +# This file specifies files that are *not* uploaded to Google Cloud Platform +# using gcloud. It follows the same syntax as .gitignore, with the addition of +# "#!include" directives (which insert the entries of the given .gitignore-style +# file at that point). +# +# For more information, run: +# $ gcloud topic gcloudignore +# +.gcloudignore +# If you would like to upload your .git directory, .gitignore file or files +# from your .gitignore file, remove the corresponding line +# below: +.git +.gitignore + +# Node.js dependencies: +node_modules/ + +/scripts/ diff --git a/packages/nuxt/app.yaml b/packages/nuxt/app.yaml new file mode 100644 index 0000000..5b28d9f --- /dev/null +++ b/packages/nuxt/app.yaml @@ -0,0 +1,20 @@ +runtime: nodejs12 + +instance_class: F2 + +handlers: + - url: /_nuxt + static_dir: .nuxt/dist/client + secure: always + + - url: /(.*\.(gif|png|jpg|ico|txt))$ + static_files: static/\1 + upload: static/.*\.(gif|png|jpg|ico|txt)$ + secure: always + + - url: /.* + script: auto + secure: always + +env_variables: + HOST: '0.0.0.0' diff --git a/packages/nuxt/assets/global.ts b/packages/nuxt/assets/global.ts index f9e654c..6a5fcb2 100644 --- a/packages/nuxt/assets/global.ts +++ b/packages/nuxt/assets/global.ts @@ -1,75 +1,3 @@ import type { ITheme } from '~/server/theme' export const THEME: ITheme = JSON.parse(process.env.THEME!) - -export async function api( - url: string, - opts: RequestInit & { - params?: Record - } = {} -): Promise< - | T - | { - error: { - statusCode: number - message: unknown - } - } -> { - if (!url.startsWith('/api')) { - return { - error: { - statusCode: 500, - message: 'malformed API route', - }, - } - } - - const baseURL = new URL( - typeof location !== 'undefined' - ? location.origin - : `http://localhost:${process.env.PORT}` - ) - baseURL.pathname = url - Object.entries(opts.params || {}).map(([k, v]) => { - if (v) { - /** - * Arrays will be comma separated, and empty strings will be discarded. - */ - baseURL.searchParams.set(k, v) - } - }) - - const r = await fetch(baseURL.href, opts) - - try { - /** - * Does not rely on `r.ok`, but rather, errorhandler - */ - const data = await r.json() - - /** - * Transform to Nuxt-compatible style - */ - if (data.error) { - return { - error: { - statusCode: r.status, - message: data, - }, - } - } - - return data - } catch (e) { - // eslint-disable-next-line no-console - console.error(e) - } - - return { - error: { - statusCode: r.status, - message: r, - }, - } -} diff --git a/packages/nuxt/nuxt.config.ts b/packages/nuxt/nuxt.config.ts index ed2e079..2180adf 100644 --- a/packages/nuxt/nuxt.config.ts +++ b/packages/nuxt/nuxt.config.ts @@ -1,4 +1,5 @@ import { NuxtConfig } from '@nuxt/types' + import { getTheme } from './server/theme' const config = async (): Promise => { @@ -54,7 +55,11 @@ const config = async (): Promise => { css: [], // Plugins to run before rendering page (https://go.nuxtjs.dev/config-plugins) - plugins: ['~/plugins/format.ts', '~/plugins/remark42.client.js'], + plugins: [ + '~/plugins/axios-fix.ts', + '~/plugins/format.ts', + '~/plugins/remark42.client.js', + ], // Auto import components (https://go.nuxtjs.dev/config-components) components: true, @@ -66,7 +71,7 @@ const config = async (): Promise => { ], // Modules (https://go.nuxtjs.dev/config-modules) - modules: [], + modules: ['@nuxtjs/axios'], // Build Configuration (https://go.nuxtjs.dev/config-build) build: {}, diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index b3152f2..c1d19e3 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -14,6 +14,7 @@ }, "dependencies": { "@nuxt/typescript-runtime": "^2.0.0", + "@nuxtjs/axios": "^5.12.4", "@patarapolw/scope-css": "^1.3.0", "@types/express": "^4.17.9", "@types/js-yaml": "^3.12.5", @@ -25,6 +26,7 @@ "js-yaml": "^3.14.1", "morgan": "^1.10.0", "nuxt": "^2.14.6", + "nuxt-property-decorator": "^2.8.8", "showdown": "^1.9.1", "zod": "^1.11.11" }, @@ -49,7 +51,6 @@ "eslint-plugin-prettier": "^3.1.4", "import-sort-parser-typescript": "^6.0.0", "nodejieba": "^2.5.1", - "nuxt-property-decorator": "^2.8.8", "prettier": "^2.1.2", "ts-node": "^9.1.1", "typescript": "4.0.5" @@ -67,4 +68,4 @@ "pnpm": "please-use-yarn" }, "engineStrict": true -} \ No newline at end of file +} diff --git a/packages/nuxt/pages/blog.vue b/packages/nuxt/pages/blog.vue index b0ba9a7..43dd227 100644 --- a/packages/nuxt/pages/blog.vue +++ b/packages/nuxt/pages/blog.vue @@ -9,7 +9,6 @@ diff --git a/packages/nuxt/pages/tag/_tag.vue b/packages/nuxt/pages/tag/_tag.vue index 5a9dd6a..6c30c28 100644 --- a/packages/nuxt/pages/tag/_tag.vue +++ b/packages/nuxt/pages/tag/_tag.vue @@ -6,7 +6,6 @@