From 0f0b1e39e76032f454a1c6d19d389d0d2a0876de Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Fri, 24 Jul 2020 17:00:56 -0700 Subject: [PATCH] Build cli using rollup. --- cli.js | 68 ++++++++++++++++++++ cli/create-account.js | 6 +- cli/index.js | 14 ++-- lib/db.js | 13 +++- package.json | 14 ++-- prisma/schema.prisma | 2 +- rollup.cli.config.js | 18 ++++++ rollup.config.js => rollup.tracker.config.js | 4 +- sql/schema.postgresql.sql | 2 +- yarn.lock | 57 +++++++++++++++- 10 files changed, 175 insertions(+), 23 deletions(-) create mode 100644 cli.js create mode 100644 rollup.cli.config.js rename rollup.config.js => rollup.tracker.config.js (62%) diff --git a/cli.js b/cli.js new file mode 100644 index 00000000..8a3b9b80 --- /dev/null +++ b/cli.js @@ -0,0 +1,68 @@ +#!/usr/bin/env node +'use strict'; + +function _interopDefault(ex) { + return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex; +} + +require('dotenv/config'); +var yargs = _interopDefault(require('yargs')); +var chalk = _interopDefault(require('chalk')); +var client = require('@prisma/client'); + +const prisma = new client.PrismaClient({ + log: [ + { + emit: 'event', + level: 'query', + }, + ], +}); + +prisma.on('query', e => { + if (process.env.LOG_QUERY) { + console.log(`${e.query} (${e.duration}ms)`); + } +}); + +var createAccount = async () => { + const account = await prisma.account.findOne({ + where: { + username: 'admin', + }, + }); + + if (!account) { + await prisma.account.create({ + data: { + username: 'admin', + password: '$2a$10$BXHPV7APlV1I6WrKJt1igeJAyVsvbhMTaTAi3nHkUJFGPsYmfZq3y', + is_admin: true, + }, + }); + console.log('Account succesfully created.'); + } else { + console.log('Account already exists.'); + } +}; + +const cmd = yargs.usage('Usage: umami [arguments]').help('h').alias('h', 'help'); + +const { argv } = cmd; +const { + _: [action, ...params], +} = argv; + +const exec = async () => { + if (action === 'create') { + await createAccount(); + } else { + cmd.showHelp(); + } + + console.log(chalk.green('Finished.')); +}; + +exec().then(() => { + process.exit(0); +}); diff --git a/cli/create-account.js b/cli/create-account.js index 44de9d73..f689fe8c 100644 --- a/cli/create-account.js +++ b/cli/create-account.js @@ -1,8 +1,6 @@ -const { PrismaClient } = require('@prisma/client'); +import { prisma } from '../lib/db'; -const prisma = new PrismaClient(); - -module.exports = async () => { +export default async () => { const account = await prisma.account.findOne({ where: { username: 'admin', diff --git a/cli/index.js b/cli/index.js index 77457f42..0005f853 100644 --- a/cli/index.js +++ b/cli/index.js @@ -1,9 +1,9 @@ #!/usr/bin/env node -require('dotenv').config(); -const yargs = require('yargs'); -const chalk = require('chalk'); -const createAccount = require('./create-account'); +import 'dotenv/config'; +import yargs from 'yargs'; +import chalk from 'chalk'; +import createAccount from './create-account'; const cmd = yargs.usage('Usage: umami [arguments]').help('h').alias('h', 'help'); @@ -19,7 +19,9 @@ const exec = async () => { cmd.showHelp(); } - process.exit(0); + console.log(chalk.green('Finished.')); }; -exec(); +exec().then(() => { + process.exit(0); +}); diff --git a/lib/db.js b/lib/db.js index 6c107edb..6fa92ce8 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,7 +1,18 @@ import { PrismaClient } from '@prisma/client'; export const prisma = new PrismaClient({ - log: [process.env.NODE_ENV !== 'production' && 'query'], + log: [ + { + emit: 'event', + level: 'query', + }, + ], +}); + +prisma.on('query', e => { + if (process.env.LOG_QUERY) { + console.log(`${e.query} (${e.duration}ms)`); + } }); export async function runQuery(query) { diff --git a/package.json b/package.json index ad3280a3..502ab1f7 100644 --- a/package.json +++ b/package.json @@ -11,15 +11,16 @@ }, "main": "index.js", "bin": { - "umami": "cli/index.js" + "umami": "cli.js" }, "scripts": { - "dev": "next dev -p 8000", + "dev": "cross-env LOG_QUERY=1 next dev -p 8000", "build": "next build", "start": "next start", - "build-tracker": "rollup -c", - "build-schema": "dotenv prisma introspect", - "build-client": "dotenv prisma generate", + "build-cli": "rollup -c rollup.cli.config.js", + "build-tracker": "rollup -c rollup.tracker.config.js", + "build-db-schema": "dotenv prisma introspect", + "build-db-client": "dotenv prisma generate", "create-account": "node cli/create-account.js" }, "lint-staged": { @@ -66,9 +67,11 @@ "devDependencies": { "@prisma/cli": "2.2.2", "@rollup/plugin-buble": "^0.21.3", + "@rollup/plugin-commonjs": "^14.0.0", "@rollup/plugin-node-resolve": "^8.4.0", "@rollup/plugin-replace": "^2.3.3", "@svgr/webpack": "^5.4.0", + "cross-env": "^7.0.2", "dotenv-cli": "^3.2.0", "eslint": "^7.2.0", "eslint-config-prettier": "^6.11.0", @@ -84,6 +87,7 @@ "prettier": "^2.0.5", "prettier-eslint": "^10.1.1", "rollup": "^2.21.0", + "rollup-plugin-hashbang": "^2.2.2", "rollup-plugin-terser": "^6.1.0", "stylelint": "^13.6.0", "stylelint-config-css-modules": "^2.2.0", diff --git a/prisma/schema.prisma b/prisma/schema.prisma index b3cd714d..5a0a74b7 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -69,7 +69,7 @@ model session { model website { created_at DateTime? @default(now()) - hostname String + label String user_id Int website_id Int @default(autoincrement()) @id website_uuid String @unique diff --git a/rollup.cli.config.js b/rollup.cli.config.js new file mode 100644 index 00000000..0a63d131 --- /dev/null +++ b/rollup.cli.config.js @@ -0,0 +1,18 @@ +import 'dotenv/config'; +import hashbang from 'rollup-plugin-hashbang'; +import commonjs from '@rollup/plugin-commonjs'; + +export default { + input: 'cli/index.js', + output: { + file: 'cli.js', + format: 'cjs', + }, + plugins: [ + hashbang(), + commonjs({ + include: 'node_modules/**', + }), + ], + external: ['yargs', 'chalk', 'dotenv/config', '@prisma/client'], +}; diff --git a/rollup.config.js b/rollup.tracker.config.js similarity index 62% rename from rollup.config.js rename to rollup.tracker.config.js index 80bda3f0..e836955f 100644 --- a/rollup.config.js +++ b/rollup.tracker.config.js @@ -1,6 +1,6 @@ import 'dotenv/config'; import buble from '@rollup/plugin-buble'; -import { nodeResolve } from '@rollup/plugin-node-resolve'; +import resolve from '@rollup/plugin-node-resolve'; import { terser } from 'rollup-plugin-terser'; export default { @@ -9,5 +9,5 @@ export default { file: 'public/umami.js', format: 'iife', }, - plugins: [nodeResolve(), buble(), terser({ compress: { evaluate: false } })], + plugins: [resolve(), buble(), terser({ compress: { evaluate: false } })], }; diff --git a/sql/schema.postgresql.sql b/sql/schema.postgresql.sql index b8fa83f4..6f43e90b 100644 --- a/sql/schema.postgresql.sql +++ b/sql/schema.postgresql.sql @@ -11,7 +11,7 @@ create table website ( website_id serial primary key, website_uuid uuid unique not null, user_id int not null references account(user_id) on delete cascade, - hostname varchar(100) not null, + label varchar(100) not null, created_at timestamp with time zone default current_timestamp ); diff --git a/yarn.lock b/yarn.lock index ede20a5f..c3072b95 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1201,6 +1201,19 @@ "@types/buble" "^0.19.2" buble "^0.20.0" +"@rollup/plugin-commonjs@^14.0.0": + version "14.0.0" + resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-14.0.0.tgz#4285f9ec2db686a31129e5a2b415c94aa1f836f0" + integrity sha512-+PSmD9ePwTAeU106i9FRdc+Zb3XUWyW26mo5Atr2mk82hor8+nPwkztEjFo8/B1fJKfaQDg9aM2bzQkjhi7zOw== + dependencies: + "@rollup/pluginutils" "^3.0.8" + commondir "^1.0.1" + estree-walker "^1.0.1" + glob "^7.1.2" + is-reference "^1.1.2" + magic-string "^0.25.2" + resolve "^1.11.0" + "@rollup/plugin-node-resolve@^8.4.0": version "8.4.0" resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-8.4.0.tgz#261d79a680e9dc3d86761c14462f24126ba83575" @@ -1371,6 +1384,11 @@ resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d" integrity sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag== +"@types/estree@*": + version "0.0.45" + resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.45.tgz#e9387572998e5ecdac221950dab3e8c3b16af884" + integrity sha512-jnqIUKDUqJbDIUxm0Uj7bnlMnRm1T/eZ9N+AVMqhPgzrba2GhGG5o/jCTwmdPK709nEZsGoMzXEDUjcXHa3W0g== + "@types/estree@0.0.39": version "0.0.39" resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" @@ -2739,6 +2757,13 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7: safe-buffer "^5.0.1" sha.js "^2.4.8" +cross-env@^7.0.2: + version "7.0.2" + resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.2.tgz#bd5ed31339a93a3418ac4f3ca9ca3403082ae5f9" + integrity sha512-KZP/bMEOJEDCkDQAyRhu3RL2ZO/SUVrxQVI0G3YEQ+OLbRA3c6zgixe8Mq8a/z7+HKlNEjo8oiLUs8iRijY2Rw== + dependencies: + cross-spawn "^7.0.1" + cross-fetch@3.0.5: version "3.0.5" resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.5.tgz#2739d2981892e7ab488a7ad03b92df2816e03f4c" @@ -4042,7 +4067,7 @@ glob-to-regexp@^0.4.1: resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e" integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw== -glob@^7.1.3, glob@^7.1.4, glob@^7.1.6: +glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6: version "7.1.6" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6" integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA== @@ -4679,6 +4704,13 @@ is-plain-object@^2.0.1, is-plain-object@^2.0.3, is-plain-object@^2.0.4: dependencies: isobject "^3.0.1" +is-reference@^1.1.2: + version "1.2.1" + resolved "https://registry.yarnpkg.com/is-reference/-/is-reference-1.2.1.tgz#8b2dac0b371f4bc994fdeaba9eb542d03002d0b7" + integrity sha512-U82MsXXiFIrjCK4otLT+o2NA2Cd2g5MLoOVXUZjIOhLurrRxpEXzI8O0KZHr3IjLvlAH1kTPYSuqer5T9ZVBKQ== + dependencies: + "@types/estree" "*" + is-regex@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.1.0.tgz#ece38e389e490df0dc21caea2bd596f987f767ff" @@ -5147,7 +5179,14 @@ lru-cache@5.1.1, lru-cache@^5.1.1: dependencies: yallist "^3.0.2" -magic-string@^0.25.0, magic-string@^0.25.5, magic-string@^0.25.7: +magic-string@^0.22.4: + version "0.22.5" + resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.22.5.tgz#8e9cf5afddf44385c1da5bc2a6a0dbd10b03657e" + integrity sha512-oreip9rJZkzvA8Qzk9HFs8fZGF/u7H/gtrE8EN6RjKJ9kh2HlC+yQ2QezifqTZfGyiuAV0dRv5a+y/8gBb1m9w== + dependencies: + vlq "^0.2.2" + +magic-string@^0.25.0, magic-string@^0.25.2, magic-string@^0.25.5, magic-string@^0.25.7: version "0.25.7" resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051" integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA== @@ -7405,7 +7444,7 @@ resolve-url@^0.2.1: resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a" integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo= -resolve@^1.1.7, resolve@^1.10.0, resolve@^1.17.0, resolve@^1.3.2, resolve@^1.8.1: +resolve@^1.1.7, resolve@^1.10.0, resolve@^1.11.0, resolve@^1.17.0, resolve@^1.3.2, resolve@^1.8.1: version "1.17.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.17.0.tgz#b25941b54968231cc2d1bb76a79cb7f2c0bf8444" integrity sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w== @@ -7482,6 +7521,13 @@ ripemd160@^2.0.0, ripemd160@^2.0.1: hash-base "^3.0.0" inherits "^2.0.1" +rollup-plugin-hashbang@^2.2.2: + version "2.2.2" + resolved "https://registry.yarnpkg.com/rollup-plugin-hashbang/-/rollup-plugin-hashbang-2.2.2.tgz#971fc49b452e63f9dfdc75f79ae7256b3485e750" + integrity sha512-Yxw9ogeK3KncG1e4CvK0I0IKVBNeJP+DTZS3bExGTfGjw0WP1C7xxbY7LtRd8IKx4fFf53hz7XR1XG7UV6xqCw== + dependencies: + magic-string "^0.22.4" + rollup-plugin-terser@^6.1.0: version "6.1.0" resolved "https://registry.yarnpkg.com/rollup-plugin-terser/-/rollup-plugin-terser-6.1.0.tgz#071866585aea104bfbb9dd1019ac523e63c81e45" @@ -8812,6 +8858,11 @@ vfile@^4.0.0: unist-util-stringify-position "^2.0.0" vfile-message "^2.0.0" +vlq@^0.2.2: + version "0.2.3" + resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26" + integrity sha512-DRibZL6DsNhIgYQ+wNdWDL2SL3bKPlVrRiBqV5yuMm++op8W4kGFtaQfCs4KEJn0wBZcHVHJ3eoywX8983k1ow== + vm-browserify@^1.0.1: version "1.1.2" resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0"