Skip to content

Commit

Permalink
scripts in js
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed May 12, 2024
1 parent 634acb9 commit cd5ffc5
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 113 deletions.
22 changes: 5 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,11 @@
"access": "public"
},
"scripts": {
"prebuild": "yarn clean && node scripts/update-version.js",
"build": "rollup --config rollup.config.mjs",
"postbuild": "node scripts/check-exports.mjs",
"clean": "rimraf --glob coverage common esm main preload renderer index.* sentry-electron*.tgz .eslintcache",
"prelint": "node scripts/update-version.js",
"lint": "run-s lint:prettier lint:eslint",
"lint:prettier": "prettier --check \"{src,test}/**/*.ts\"",
"lint:eslint": "eslint . --cache --format stylish",
"fix": "run-s fix:eslint fix:prettier",
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
"fix:eslint": "eslint . --format stylish --fix",
"update-electron-versions": "electron-latest-versions --start 15 --beta > ./test/e2e/versions.json",
"update-sdk-versions": "node ./scripts/update-sdk-versions.mjs",
"pretest": "yarn build",
"test": "vitest run --root=./test/unit",
"pree2e": "rimraf --glob test/e2e/dist/**/node_modules/@sentry/** test/e2e/dist/**/yarn.lock test/e2e/dist/**/package-lock.json && node scripts/clean-cache.js && yarn build && npm pack",
"e2e": "xvfb-maybe vitest run --root=./test/e2e --silent=false --disable-console-intercept"
"start": "node scripts/start.mjs",
"build": "node scripts/start.mjs build",
"test": "node scripts/start.mjs test",
"lint": "node scripts/start.mjs lint",
"e2e": "node scripts/start.mjs e2e"
},
"dependencies": {
"@sentry/browser": "8.0.0-beta.5",
Expand Down
93 changes: 47 additions & 46 deletions scripts/check-exports.mjs
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
import * as browser from '@sentry/browser';
import * as renderer from '../esm/renderer/index.js';

import * as node from '@sentry/node';

// We need to shim electron to avoid errors when importing the main process code into plain-old-node that doesn't have
// the electron module built-in.
import './electron-shim.mjs';

const main = await import('../esm/main/index.js');

const browserExports = Object.keys(browser);
const rendererExports = Object.keys(renderer);
const nodeExports = Object.keys(node);
const mainExports = Object.keys(main);

const ignoredBrowser = [
'SDK_VERSION',
'WINDOW',
'Integrations',
'close',
'flush',
'defaultStackLineParsers',
// These wont ever be used
'geckoStackLineParser',
'opera10StackLineParser',
'opera11StackLineParser',
'winjsStackLineParser',
// If you use the browser transports, just use the browser SDK
'makeBrowserOfflineTransport',
'makeFetchTransport',
'makeMultiplexedTransport',
'lazyLoadIntegration',
];

const ignoredNode = ['SDK_VERSION', 'makeNodeTransport', 'getSentryRelease'];

const missingRenderer = browserExports.filter((key) => !rendererExports.includes(key) && !ignoredBrowser.includes(key));
const missingMain = nodeExports.filter((key) => !mainExports.includes(key) && !ignoredNode.includes(key));

if (missingRenderer.length || missingMain.length) {
if (missingRenderer.length) {
console.error('Missing renderer exports:', missingRenderer);
}

if (missingMain.length) {
console.error('Missing main exports:', missingMain);
export default async function () {
// We need to shim electron to avoid errors when importing the main process code into plain-old-node that doesn't have
// the electron module built-in.
await import('./electron-shim.mjs');

const renderer = await import('../esm/renderer/index.js');
const main = await import('../esm/main/index.js');

const browserExports = Object.keys(browser);
const rendererExports = Object.keys(renderer);
const nodeExports = Object.keys(node);
const mainExports = Object.keys(main);

const ignoredBrowser = [
'SDK_VERSION',
'WINDOW',
'Integrations',
'close',
'flush',
'defaultStackLineParsers',
// These wont ever be used
'geckoStackLineParser',
'opera10StackLineParser',
'opera11StackLineParser',
'winjsStackLineParser',
// If you use the browser transports, just use the browser SDK
'makeBrowserOfflineTransport',
'makeFetchTransport',
'makeMultiplexedTransport',
'lazyLoadIntegration',
];

const ignoredNode = ['SDK_VERSION', 'makeNodeTransport', 'getSentryRelease'];

const missingRenderer = browserExports.filter(
(key) => !rendererExports.includes(key) && !ignoredBrowser.includes(key),
);
const missingMain = nodeExports.filter((key) => !mainExports.includes(key) && !ignoredNode.includes(key));

if (missingRenderer.length || missingMain.length) {
if (missingRenderer.length) {
console.error('⚠️ Missing renderer exports ⚠️\n', missingRenderer);
}

if (missingMain.length) {
console.error('⚠️ Missing main exports ⚠️\n', missingMain);
}
}

process.exit(1);
}
14 changes: 0 additions & 14 deletions scripts/clean-cache.js

This file was deleted.

49 changes: 49 additions & 0 deletions scripts/start.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import updateVersion from './update-version.mjs';
import updateSdkVersions from './update-sdk-versions.mjs';
import checkPackageExports from './check-exports.mjs';

/**
* Each script is made up of one or more commands.
*
* Commands can be:
* - String commands to be executed in the shell
* - String that refers to another script
* - JavaScript functions (sync or async)
*/
scripts({
clean: 'rimraf --glob coverage common esm main preload renderer index.* sentry-electron*.tgz .eslintcache',
build: ['clean', updateVersion, 'rollup --config rollup.config.mjs', checkPackageExports],
lint: [updateVersion, 'lint:prettier', 'lint:eslint'],
'lint:prettier': 'prettier --check "{src,test}/**/*.ts"',
'lint:eslint': 'eslint . --cache --format stylish',
fix: [updateVersion, 'fix:prettier', 'fix:eslint'],
'fix:prettier': 'prettier --write "{src,test}/**/*.ts"',
'fix:eslint': 'eslint . --cache --format --fix',
'update-electron-versions': 'electron-latest-versions --start 15 --beta > ./test/e2e/versions.json',
'update-sdk-versions': updateSdkVersions,
test: ['build', 'vitest run --root=./test/unit'],
e2e: [
'rimraf --glob test/e2e/dist/**/node_modules/@sentry/** test/e2e/dist/**/yarn.lock test/e2e/dist/**/package-lock.json',
'yarn cache clean',
'build',
'npm pack',
'xvfb-maybe vitest run --root=./test/e2e --silent=false --disable-console-intercept',
],
});

import { execSync } from 'child_process';

function scripts(scripts) {
async function run(cmd) {
for (const next of Array.isArray(scripts[cmd]) ? scripts[cmd] : [scripts[cmd]]) {
if (typeof next === 'function') await next();
else if (next in scripts) await run(next);
else {
console.log(`\n> ${next}`);
execSync(next, { stdio: 'inherit' });
}
}
}

run(process.argv[2]);
}
42 changes: 22 additions & 20 deletions scripts/update-sdk-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@ import { fileURLToPath } from 'url';

const __dirname = fileURLToPath(new URL('.', import.meta.url));

const latest = await latestVersion('@sentry/core');
const packageJsonPath = join(__dirname, '..', 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, { encoding: 'utf8' }));
const current = packageJson.dependencies['@sentry/core'];
export default async function () {
const latest = await latestVersion('@sentry/core');
const packageJsonPath = join(__dirname, '..', 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, { encoding: 'utf8' }));
const current = packageJson.dependencies['@sentry/core'];

if (current !== latest) {
console.log(`Updating Sentry deps from ${current} to ${latest}`);
if (current !== latest) {
console.log(`Updating Sentry deps from ${current} to ${latest}`);

const re = /^@sentry(-internal)?\//;
const re = /^@sentry(-internal)?\//;

for (const dep of Object.keys(packageJson.dependencies)) {
if (dep.match(re)) {
packageJson.dependencies[dep] = latest;
for (const dep of Object.keys(packageJson.dependencies)) {
if (dep.match(re)) {
packageJson.dependencies[dep] = latest;
}
}
}

for (const dep of Object.keys(packageJson.devDependencies)) {
if (dep.match(re)) {
packageJson.devDependencies[dep] = latest;
for (const dep of Object.keys(packageJson.devDependencies)) {
if (dep.match(re)) {
packageJson.devDependencies[dep] = latest;
}
}
}

writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));

// Update yarn.lock
spawnSync('yarn', ['install'], { stdio: 'inherit' });
// Update parameter that has the version in it
spawnSync('yarn', ['build'], { stdio: 'inherit' });
// Update lock file
spawnSync('npm', ['install'], { stdio: 'inherit' });
// Update parameter that has the version in it
spawnSync('npm', ['run', 'build'], { stdio: 'inherit' });
}
}
16 changes: 0 additions & 16 deletions scripts/update-version.js

This file was deleted.

21 changes: 21 additions & 0 deletions scripts/update-version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { join } from 'path';
import { readFileSync, writeFileSync } from 'fs';
import { fileURLToPath } from 'url';

const __dirname = fileURLToPath(new URL('.', import.meta.url));

export default function () {
const packageJson = JSON.parse(readFileSync(join(__dirname, '..', 'package.json')));

// SDK_VERSION to 'src/main/version.ts'
const versionPath = join(__dirname, '../src/main/version.ts');
writeFileSync(versionPath, `export const SDK_VERSION = '${packageJson.version}';\n`);

// Write @sentry/core version into options variable name so TypeScript error includes useful hint
const coreVersion = packageJson.dependencies['@sentry/core'];
const coreVersionVar = coreVersion.replace(/[\.-]/g, '_');
const rendererSdkPath = join(__dirname, '../src/renderer/sdk.ts');
let rendererSdk = readFileSync(rendererSdkPath, { encoding: 'utf8' });
rendererSdk = rendererSdk.replace(/version_v\d+_\d+_\d+[a-z_0-9]*/, `version_v${coreVersionVar}`);
writeFileSync(rendererSdkPath, rendererSdk);
}
5 changes: 5 additions & 0 deletions test/e2e/recipe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ export class RecipeRunner {
writeFileSync(path, content);
}

// Yarn v4 requires an empty yarn.lock file otherwise it complains that this is not part of the parent workspace
if (!this._recipe.files['yarn.lock']) {
writeFileSync(join(appPath, 'yarn.lock'), '');
}

if (this._recipe.metadata.command) {
log(`Running command '${this._recipe.metadata.command}'`);

Expand Down

0 comments on commit cd5ffc5

Please sign in to comment.