Initial commit

This commit is contained in:
James Jia - Test
2023-09-08 13:52:13 -07:00
commit 4b86068d8f
584 changed files with 59963 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const AMPLITUDE_API_KEY = process.env.AMPLITUDE_API_KEY;
const currentPath = fileURLToPath(import.meta.url);
const projectRoot = path.dirname(currentPath);
const htmlFilePath = path.resolve(projectRoot, '../dist/index.html');
if(AMPLITUDE_API_KEY){
try {
const html = await fs.readFile(htmlFilePath, 'utf-8');
const amplitudeCdnScript = `<script type="text/javascript">
!function(){"use strict";!function(e,t){var n=e.amplitude||{_q:[],_iq:{}};if(n.invoked)e.console&&console.error&&console.error("Amplitude snippet has been loaded.");else{var r=function(e,t){e.prototype[t]=function(){return this._q.push({name:t,args:Array.prototype.slice.call(arguments,0)}),this}},s=function(e,t,n){return function(r){e._q.push({name:t,args:Array.prototype.slice.call(n,0),resolve:r})}},o=function(e,t,n){e[t]=function(){if(n)return{promise:new Promise(s(e,t,Array.prototype.slice.call(arguments)))}}},i=function(e){for(var t=0;t<m.length;t++)o(e,m[t],!1);for(var n=0;n<g.length;n++)o(e,g[n],!0)};n.invoked=!0;var u=t.createElement("script");u.type="text/javascript",u.integrity="sha384-x0ik2D45ZDEEEpYpEuDpmj05fY91P7EOZkgdKmq4dKL/ZAVcufJ+nULFtGn0HIZE",u.crossOrigin="anonymous",u.async=!0,u.src="https://cdn.amplitude.com/libs/analytics-browser-2.0.0-min.js.gz",u.onload=function(){e.amplitude.runQueuedFunctions||console.log("[Amplitude] Error: could not load SDK")};var a=t.getElementsByTagName("script")[0];a.parentNode.insertBefore(u,a);for(var c=function(){return this._q=[],this},p=["add","append","clearAll","prepend","set","setOnce","unset","preInsert","postInsert","remove","getUserProperties"],l=0;l<p.length;l++)r(c,p[l]);n.Identify=c;for(var d=function(){return this._q=[],this},f=["getEventProperties","setProductId","setQuantity","setPrice","setRevenue","setRevenueType","setEventProperties"],v=0;v<f.length;v++)r(d,f[v]);n.Revenue=d;var m=["getDeviceId","setDeviceId","getSessionId","setSessionId","getUserId","setUserId","setOptOut","setTransport","reset","extendSession"],g=["init","add","remove","track","logEvent","identify","groupIdentify","setGroup","revenue","flush"];i(n),n.createInstance=function(e){return n._iq[e]={_q:[]},i(n._iq[e]),n._iq[e]},e.amplitude=n}}(window,document)}();
</script>
`;
const amplitudeListenerScript = `<script type="module">
!function(){var e="${AMPLITUDE_API_KEY}";e&&(globalThis.amplitude.init(e),globalThis.amplitude.setOptOut(!1),globalThis.addEventListener("dydx:track",function(e){var t=e.detail.eventType,d=e.detail.eventData;globalThis.amplitude.track(t,d)}),globalThis.addEventListener("dydx:identify",function(e){var t=e.detail.property,d=e.detail.propertyValue;if("walletAddress"===t)globalThis.amplitude.setUserId(d);else{var i=new globalThis.amplitude.Identify;i.set(t,d),globalThis.amplitude.identify(i)}}),console.log("Amplitude enabled."))}();
</script>`;
const injectedHtml = html.replace(
'<div id="root"></div>',
`<div id="root"></div>\n${amplitudeCdnScript}\n${amplitudeListenerScript}`
);
await fs.writeFile(htmlFilePath, injectedHtml, 'utf-8');
console.log('Amplitude scripts successfully injected.');
} catch (err) {
console.error('Error injecting Amplitude scripts:', err);
}
}
+81
View File
@@ -0,0 +1,81 @@
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const BUGSNAG_API_KEY = process.env.BUGSNAG_API_KEY;
const currentPath = fileURLToPath(import.meta.url);
const projectRoot = path.dirname(currentPath);
const htmlFilePath = path.resolve(projectRoot, '../dist/index.html');
try {
const html = await fs.readFile(htmlFilePath, 'utf-8');
const scripts = `
<script src="//d2wy8f7a9ursnm.cloudfront.net/v7/bugsnag.min.js"></script>
<script type="module">
(function() {
var BUGSNAG_API_KEY = '${BUGSNAG_API_KEY}';
var walletType;
if (BUGSNAG_API_KEY) {
Bugsnag.start(BUGSNAG_API_KEY);
}
globalThis.addEventListener('dydx:identify', function (event) {
var property = event.detail.property;
var value = event.detail.propertyValue;
switch (property) {
case 'walletType':
walletType = value;
break;
default:
break;
}
});
globalThis.addEventListener('dydx:log', function (event) {
var error = event.detail.error;
var metadata = event.detail.metadata;
var location = event.detail.location;
if (BUGSNAG_API_KEY && Bugsnag.isStarted()) {
Bugsnag.notify(error, function (event) {
event.context = location;
if (metadata) {
event.addMetadata('metadata', metadata);
}
if (walletType) {
event.addMetadata('walletType', walletType);
}
});
} else {
console.warn(location, error, metadata);
}
});
})();
</script>
<script type="module">
import BugsnagPerformance from '//d2wy8f7a9ursnm.cloudfront.net/v1.0.0/bugsnag-performance.min.js'
BugsnagPerformance.start({
apiKey: '${BUGSNAG_API_KEY}',
appVersion: '4.10.0',
enabledReleaseStages: ['production', 'development', 'testing']
})
</script>`;
const injectedHtml = html.replace(
'<div id="root"></div>',
`<div id="root"></div>\n${scripts}\n`
);
await fs.writeFile(htmlFilePath, injectedHtml, 'utf-8');
console.log('Bugsnag scripts successfully injected.');
} catch (err) {
console.error('Error injecting Bugsnag scripts:', err);
}
+28
View File
@@ -0,0 +1,28 @@
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const STATUS_PAGE_SCRIPT_URI = process.env.STATUS_PAGE_SCRIPT_URI;
const currentPath = fileURLToPath(import.meta.url);
const projectRoot = path.dirname(currentPath);
const htmlFilePath = path.resolve(projectRoot, '../dist/index.html');
if (STATUS_PAGE_SCRIPT_URI) {
try {
const html = await fs.readFile(htmlFilePath, 'utf-8');
const statusPageScript = `<script defer src="${STATUS_PAGE_SCRIPT_URI}"></script>`;
const injectedHtml = html.replace(
'<div id="root"></div>',
`<div id="root"></div>\n${statusPageScript}\n`
);
await fs.writeFile(htmlFilePath, injectedHtml, 'utf-8');
console.log('StatusPage script successfully injected.');
} catch (err) {
console.error('Error injecting StatusPage scripts:', err);
}
}
+72
View File
@@ -0,0 +1,72 @@
/**
* Updates an existing IPNS record or creates a new IPNS record using w3name.
* @param {string} cid The CID to publish to IPNS
* @param {string} keyPath The path to the signing key
* @param {boolean} newIpns Whether to create a new IPNS record
* @param {boolean} verbose Whether to print verbose output
*/
import * as Name from 'w3name';
import fs from 'fs';
import minimist from 'minimist';
import process from 'process';
const SIGNING_KEY_PATH = '.web3name.key';
async function saveSigningKey(name, outputFilename = SIGNING_KEY_PATH) {
const bytes = name.key.bytes;
await fs.promises.writeFile(outputFilename, bytes);
}
async function loadSigningKey(filename) {
const bytes = await fs.promises.readFile(filename);
const name = await Name.from(bytes);
return name;
}
const {
cid,
key: keyPath,
newIpns,
verbose,
} = minimist(process.argv.slice(2), {
string: ['cid', 'key'],
boolean: ['newIpns', 'verbose'],
default: { verbose: false },
alias: { c: 'cid', k: 'key' },
});
if (!cid) {
console.error('Error: Provide the CID with the --cid flag.');
process.exit(1);
}
if (!keyPath && !newIpns) {
console.error(
'Error: To update an existing IPNS record, provide the path to the key file with the --key flag or create a new IPNS record with the --newIpns flag.'
);
process.exit(1);
}
let name;
let newRevision;
if (newIpns) {
if (verbose) console.log(`Creating new IPNS record with cid ${cid}...`);
name = await Name.create();
newRevision = await Name.v0(name, `/ipfs/${cid}`);
await saveSigningKey(name, keyPath);
if (verbose)
console.log(`The associated signing key is saved to ${keyPath ?? SIGNING_KEY_PATH}`);
} else {
if (verbose) console.log(`Updating existing IPNS record with cid ${cid}...`);
name = await loadSigningKey(keyPath);
const latestRevision = await Name.resolve(name);
newRevision = await Name.increment(latestRevision, `/ipfs/${cid}`);
}
if (verbose) console.log('Publishing...');
await Name.publish(newRevision, name.key);
console.log(`ipns://${name.toString()}`);
+48
View File
@@ -0,0 +1,48 @@
/**
* Uploads contents of the build directory to web3.storage,
* and returns the CID, which is available over the IPFS network.
* @param {boolean} rebuild Whether to rebuild the site before uploading, defaults to true
* @param {string} env The environment to build for, defaults to 'staging'
* @param {boolean} verbose Whether to print verbose output
*/
import fs from 'fs';
import minimist from 'minimist';
import process from 'process';
import { Web3Storage, getFilesFromPath } from 'web3.storage';
import { execSync } from 'child_process';
const BUILD_DIR_PATH = 'dist';
const API_TOKEN = process.env.WEB3_STORAGE_TOKEN;
const { rebuild, env, verbose } = minimist(process.argv.slice(2), {
string: ['env'],
boolean: ['rebuild', 'verbose'],
default: { env: 'staging', rebuild: true, verbose: false },
alias: { e: 'env' },
});
if (!API_TOKEN) {
console.error(
'Error: An API token is required. Create one at https://web3.storage and set the WEB3_STORAGE_TOKEN environment variable.'
);
process.exit(1);
}
if (rebuild || !fs.existsSync(BUILD_DIR_PATH)) {
if (verbose) console.log(`Building ${env}...`);
execSync(`pnpm run build --mode ${env} > /dev/null 2>&1`, { stdio: 'inherit' });
}
const client = new Web3Storage({ token: API_TOKEN });
const files = await getFilesFromPath(BUILD_DIR_PATH);
if (verbose) console.log(`Uploading ${files.length} files to web3.storage...`);
const cid = await client.put(files, { wrapWithDirectory: false });
if (verbose) {
console.log('Content added with CID:', cid);
console.log(`https://dweb.link/ipfs/${cid}`);
} else {
console.log(cid);
}