Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add Bundler and tighten Docker Image #17

Merged
merged 15 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ node_modules
.git
.gitignore
*.md
dist
dist
bin
.env
lh-config.js
urls.json
2 changes: 0 additions & 2 deletions .env.template

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ urls.json
lh-config.js

# TSC Output
dist
dist
bin
36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ Vigilo is a monitoring tool that automates Lighthouse audits and sends key metri

### Local Setup
1. Clone the repo
2. Run `pnpm install`
3. Run `cp lh-config.example.js lh-config.js`
4. Add the metrics you want to track to the `onlyAudits` property in the `lh-config.js` file
5. Run `pnpm run build`
6. Run `cp .env.template .env`
7. Add your Datadog API key and Datadog Application key to the `.env` file
8. Run `cp urls.example.json urls.json`
9. Add your URLs to the `urls.json` file
2. Run `pnpm run setup`
3. Add the metrics you want to track to the `onlyAudits` property in the `src/config/lh-config.js` file
4. Add your URLs to the `src/config/urls.json` file
5. Add your Datadog API key and Datadog Application key to the `.env` file
6. Run `pnpm run build`

⚠️ If you are setting this up on a Windows machine via WSL, then you will need to run the following commands to ensure the correct linux dependencies are installed:
**Make sure dependencies are up to date**
Expand Down Expand Up @@ -45,14 +42,19 @@ This is useful when there were changes to the config files (`lh-config.js`, and

### Docker Setup

1. Clone the repo
2. Run `cp .env.template .env`
3. Add your Datadog API key and Datadog Application key to the `.env` file
4. Run `cp urls.example.json urls.json`
5. Add your URLs to the `urls.json` file
6. Run `cp lh-config.example.js lh-config.js`
7. Add the metrics you want to track to the `onlyAudits` property in the `lh-config.js` file
8. Run `docker build -t vigilo .`
1. Similar [Local Setup](#local-setup) steps 1-5
2. Run `docker build -t vigilo .`

#### Docker Usage
1. After setup, run `docker run --rm vigilo` to run vigilo and send metrics to Datadog
1. After setup, run `pnpm start:docker` to run vigilo and send metrics to Datadog


### Building

- Run `pnpm run build` to build all packages

- Run `pnpm run build:core` to build the core package

- Run `pnpm run build:scripts` to build the standalone utility scripts to interact with Datadog

If you want additional options for the build process, you can use run `pnpm build --help` or `node build.js --help` to see the available options.
23 changes: 23 additions & 0 deletions build.config-alias-replacer.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* File must be named with a .cjs extension to be recognized by tsc-alias and
* work with the type: "module" setting in package.json.
* @see https://github.com/justkey007/tsc-alias/discussions/73#discussioncomment-4416038
*/
const fs = require('fs');

/**
* This script replaces '@config/' aliases with relative paths './' in the
* provided file.
* This is necessary for non-bundling scenarios like Docker, where we want to
* use files from the host machine.
* Without this, the referenced files would be bundled, which is not the
* desired behavior in this case.
*/
function configAliasReplace({ orig, file }) {
const fileContents = fs.readFileSync(file, 'utf8');
const newContents = fileContents.replace(/@config\//g, './');
fs.writeFileSync(file, newContents, 'utf8');
return orig;
}

exports.default = configAliasReplace;
95 changes: 95 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { build as tsupBuild } from 'tsup';
import { replaceTscAliasPaths } from 'tsc-alias';
import yargs from 'yargs';

const CORE_DIR = 'dist';
const SCRIPTS_DIR = 'bin';
const isDocker = process.env.DOCKER === 'true';

const argv = yargs(process.argv.slice(2)).options({
target: {
alias: 't',
type: 'string',
description: 'Build target',
choices: ['all', 'core', 'scripts'],
default: 'all'
},
silent: {
alias: 's',
type: 'boolean',
description: 'Silent mode',
default: false
},
clean: {
alias: 'c',
type: 'boolean',
description: 'Clean out files before build',
default: false
},
}).strict().parseSync();

const config = {
splitting: false, // don't split code into chunks
platform: 'node', // environment to build for
target: 'node20',
format: 'esm',
silent: argv.silent,
clean: argv.clean,
external: isDocker ? ['@config'] : [], // Don't resolve @config modules when building in docker
}

const CORE_BUILD = {
...config,
outDir: CORE_DIR,
entry: {
index: 'src/core/index.ts',
},
}

const SCRIPTS_BUILD = {
...config,
outDir: SCRIPTS_DIR,
entry: ['src/scripts/*'],
}

/**
* Resolve paths after the build process because ESBuild only supports path
* resolution natively during bundling.
*
* "@config/" paths are replaced with relative-path "./" to allow for
* non-bundling scenarios like Docker.
*/
async function replaceConfigAliasPaths(outDir) {
await replaceTscAliasPaths({
configFile: 'tsconfig.json',
outDir: outDir,
watch: false,
replacers: ['build.config-alias-replacer.cjs']
});
}

async function build(buildConfig, buildTarget) {
try {
await tsupBuild(buildConfig);

if (isDocker) {
await replaceConfigAliasPaths(buildConfig.outDir);
}
} catch (e) {
console.error(`Failed to build ${buildTarget}:`, e);
process.exit(1);
}
}

switch(argv.target) {
case 'all':
await build(CORE_BUILD, 'core');
await build(SCRIPTS_BUILD, 'scripts');
break;
case 'core':
await build(CORE_BUILD, 'core');
break;
case 'scripts':
await build(SCRIPTS_BUILD, 'scripts');
break;
}
3 changes: 3 additions & 0 deletions config-templates/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Do note wrap the values in quotes (single or double)
DD_API_KEY=
DD_APP_KEY=
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions dev-scripts/run-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

root_dir=$(git rev-parse --show-toplevel)
config_dir="$root_dir/src/config"

# Run the docker image
docker run --rm \
-v "$config_dir/lh-config.js":/app/dist/lh-config.js \
-v "$config_dir/urls.json":/app/dist/urls.json \
--env-file "$root_dir/.env" \
vigilo
15 changes: 15 additions & 0 deletions dev-scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -ex

root_dir=$(git rev-parse --show-toplevel)

config_templates_dir="$root_dir/config-templates"
config_dir="$root_dir/src/config"

cp $config_templates_dir/.env.template $root_dir/.env
batbattur marked this conversation as resolved.
Show resolved Hide resolved

cp $config_templates_dir/lh-config.template.js $config_dir/lh-config.js
cp $config_templates_dir/urls.template.json $config_dir/urls.json

pnpm install
21 changes: 13 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
{
"name": "vigilo",
"version": "1.0.0",
"version": "1.1.0",
"description": "Vigilo is a monitoring tool that automates Lighthouse audits and sends key metrics to Datadog. Designed to keep a vigilant eye on your web application's performance and accessibility.",
"main": "dist/src/index.js",
"main": "dist/index.js",
"repository": "https://github.com/iFixit/vigilo.git",
"type": "module",
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"start": "node dist/src/index.js",
"create-dashboard": "pnpm build && node dist/src/createDashboard.js",
"update-metric-metadata": "node dist/src/updateDatadogMetricMetadata.js",
"clean": "rm -rf dist",
"build": "pnpm clean && tsc",
"test": "echo \"Error: no test specified\" && exit 1"
"setup": "dev-scripts/setup.sh",
"start": "node dist/index.js",
"start:docker": "dev-scripts/run-docker.sh",
"create-dashboard": "pnpm build:scripts && node bin/createDashboard.js",
"update-metric-metadata": "pnpm build:scripts && node bin/updateDatadogMetricMetadata.js",
"clean": "rm -rf dist && rm -rf bin",
"build": "pnpm clean && node build.js",
"build:core": "node build.js --target=core",
"build:scripts": "node build.js --target=scripts"
},
"keywords": [],
"author": "Angel de la Torre <[email protected]>",
Expand All @@ -23,6 +26,8 @@
"@tsconfig/node18": "^18.2.1",
"@types/node": "^20.5.9",
"@types/yargs": "^17.0.28",
"tsc-alias": "^1.8.8",
"tsup": "^8.0.1",
"typescript": "^5.2.2"
},
"dependencies": {
Expand Down
Loading