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

Re-factor build scripts to TypeScript #202

Merged
merged 23 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d7c26fd
Re-factor scripts for building, development, linting, and cleaning to…
smithki Sep 1, 2021
7fa7da1
Remove remnants of old test runner (ava)
smithki Sep 1, 2021
a210f1b
Re-factor scripts for unit testing to TypeScript
smithki Sep 1, 2021
e2ccc67
Small cleanups
smithki Sep 1, 2021
d258eeb
Refactor to merge `ViewController` and `PayloadTransport` classes (#203)
smithki Sep 2, 2021
9a59d3f
Make script code more DRY
smithki Sep 2, 2021
d12760f
Update TypeScript & make scripts more DRY
smithki Sep 2, 2021
94efcb4
Fix typo and add 'printSeparator' script utility
smithki Sep 2, 2021
a2325a2
Fix 'inject-env.ts' script following re-organization of code
smithki Sep 2, 2021
d5e2f35
Remove outdated / unused dependencies from root PackageJSON
smithki Sep 8, 2021
a858d16
Progress towards integrating microbundle
smithki Sep 9, 2021
69d343f
Update build scripts to bundle all packages via microbundle
smithki Sep 10, 2021
50a0a03
Remove unnecessary console statement
smithki Sep 10, 2021
e8c7fde
Run pre-commit hooks against all packages
smithki Sep 10, 2021
2e0e32c
Add more memory for tasks spawned via 'wsrun'
smithki Sep 10, 2021
3e28ee0
Raise the resource class in CI
smithki Sep 10, 2021
66a3346
Try to fix ENOMEM errors in CI by limiting concurrency
smithki Sep 10, 2021
89a4660
Replace 'p-limit' dependency with prior version (new version is ESM-o…
smithki Sep 10, 2021
b529dd4
Better caching in CircleCI
smithki Sep 10, 2021
f80e463
Update test script to work with Yarn 2
smithki Sep 10, 2021
494fe08
Fix tests related to ViewController refactor
smithki Sep 10, 2021
f1066f4
Reduce the CircleCI resource class back to medium
smithki Sep 10, 2021
62b4874
Fix tests related to ViewController refactor
smithki Sep 10, 2021
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
Prev Previous commit
Next Next commit
Update TypeScript & make scripts more DRY
  • Loading branch information
smithki committed Sep 2, 2021
commit d12760f1c7786f7a34e724f882f0d0d5a3f3566a
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ To ensure ESLint is able to properly lint source files in your VS Code developme
| ---------- | ----- | ----------- |
| `bootstrap` | `yarn bootstrap` | Install dependencies/set up a local development environment. |
| `wsrun` | `PKG=$PACKAGE_TARGET yarn wsrun` | Execute arbitrary scripts via `wsrun` for the specified package. |
| `wsrun:paths` | `PKG=$PACKAGE_TARGET yarn paths` | Print the relative paths to each project based on the value of `$PKG`. |
| `dev` | `PKG=$PACKAGE_TARGET yarn dev` | Start the specified package in development mode. |
| `build` | `PKG=$PACKAGE_TARGET yarn build` | Build the specified package for production, or all packages if `$PKG` is omitted. |
| `clean` | `PKG=$PACKAGE_TARGET yarn clean` | Run cleaning scripts for the specified package, or all packages if `$PKG` is omitted. Available flags: (`--cache`, `--test-artifacts`, `--deps`) |
| `lint` | `PKG=$PACKAGE_TARGET yarn lint` | Run the linter for the specified package, or all packages if `$PKG` is omitted. |
| `test` | `PKG=$PACKAGE_TARGET yarn test` | Run tests for the specified package, or all packages if `$PKG` is omitted. |
| `paths` | `PKG=$PACKAGE_TARGET yarn paths` | Print the relative paths to each project based on the value of `$PKG`. |

## Opening a Pull Request

Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
"version": "0.0.0",
"scripts": {
"bootstrap": "yarn install && lerna link",
"wsrun": "wsrun --bin $INIT_CWD/scripts/wsrun/bin.ts -p $PKG",
"dev": "$INIT_CWD/scripts/clean.ts && $INIT_CWD/scripts/dev.ts",
"build": "$INIT_CWD/scripts/clean.ts && $INIT_CWD/scripts/build.ts",
"clean": "$INIT_CWD/scripts/clean.ts",
"lint": "$INIT_CWD/scripts/lint.ts",
"test": "$INIT_CWD/scripts/test.ts",
"paths": "yarn --silent wsrun --parallel --no-prefix -r -c $INIT_CWD/scripts/wsrun/resolve-paths.ts",
"wsrun": "wsrun --bin $INIT_CWD/scripts/bin/wsrun/bin.ts -p $PKG",
"wsrun:paths": "yarn --silent wsrun --parallel --no-prefix -r -c $INIT_CWD/scripts/bin/wsrun/resolve-paths.ts",
"dev": "$INIT_CWD/scripts/bin/clean.ts && $INIT_CWD/scripts/bin/dev.ts",
"build": "$INIT_CWD/scripts/bin/clean.ts && $INIT_CWD/scripts/bin/build.ts",
"clean": "$INIT_CWD/scripts/bin/clean.ts",
"lint": "$INIT_CWD/scripts/bin/lint.ts",
"test": "$INIT_CWD/scripts/bin/test.ts",
"postinstall": "husky install"
},
"devDependencies": {
"@ikscodes/browser-env": "~0.3.1",
"@ikscodes/eslint-config": "~7.0.0",
"@ikscodes/prettier-config": "~2.0.0",
"@ikscodes/eslint-config": "~7.0.2",
"@ikscodes/prettier-config": "~2.0.1",
"@istanbuljs/nyc-config-typescript": "~0.1.3",
"@types/jest": "^27.0.0",
"@types/jsdom": "~12.2.4",
Expand Down Expand Up @@ -47,7 +47,7 @@
"npm-run-all": "^4.1.5",
"nyc": "13.1.0",
"ora": "~5.4.1",
"prettier": "~2.0.5",
"prettier": "~2.3.2",
"react": "^16.13.1",
"react-native": "^0.62.2",
"replace-in-file": "^6.1.0",
Expand All @@ -56,7 +56,7 @@
"ts-node": "^10.2.0",
"tsc-watch": "^4.2.9",
"typed-emitter": "^1.0.0",
"typescript": "~3.8.3",
"typescript": "~4.4.2",
"wsrun": "^5.2.1"
},
"workspaces": {
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-sdk/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"peerDependencies": {
"@magic-sdk/provider": ">=4.3.0",
"@magic-sdk/types": ">=3.1.1",
"tslib": "^2.0.3"
"tslib": "^2.3.1"
},
"gitHead": "1ef062ea699d48d5e9a9375a93b7c147632b05ca"
}
2 changes: 1 addition & 1 deletion packages/@magic-sdk/provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@magic-sdk/types": "^4.0.0",
"eventemitter3": "^4.0.4",
"semver": "^7.3.2",
"tslib": "^2.0.3",
"tslib": "^2.3.1",
"web3-core": "1.5.2"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-sdk/provider/src/core/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function prepareExtensions(this: SDKBase, options?: MagicSDKAdditionalConfigurat

export interface MagicSDKAdditionalConfiguration<
TCustomExtName extends string = string,
TExt extends Extension<string>[] | { [P in TCustomExtName]: Extension<string> } = any
TExt extends Extension<string>[] | { [P in TCustomExtName]: Extension<string> } = any,
> {
endpoint?: string;
locale?: SupportedLocale;
Expand Down
8 changes: 2 additions & 6 deletions packages/@magic-sdk/provider/src/modules/base-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ type UnwrapArray<T extends any[]> = T extends Array<infer P> ? P : never;
* Create a union type of Extension names from an
* array of Extension types given by `TExt`.
*/
type ExtensionNames<TExt extends Extension<string>[]> = UnwrapArray<
{
[P in keyof TExt]: TExt[P] extends Extension<infer K> ? K : never;
}
>;
type ExtensionNames<TExt extends Extension<string>[]> = UnwrapArray<TExt> extends Extension<infer R> ? R : never;

/**
* From the literal Extension name type given by `TExtName`,
Expand All @@ -203,7 +199,7 @@ export type WithExtensions<SDK extends SDKBase> = {
new <
TCustomExtName extends string,
TExt extends Extension<string>[] | { [P in TCustomExtName]: Extension<string> },
TExtName extends string = TExt extends Extension<string>[] ? ExtensionNames<TExt> : keyof TExt
TExtName extends string = TExt extends Extension<string>[] ? ExtensionNames<TExt> : keyof TExt,
>(
apiKey: string,
options?: MagicSDKAdditionalConfiguration<TCustomExtName, TExt>,
Expand Down
15 changes: 7 additions & 8 deletions packages/@magic-sdk/provider/src/util/promise-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type DefaultEvents<TResult> = {
* A `Promise` executor with can be optionally asynchronous.
*/
type AsyncPromiseExecutor<TResult> = (
resolve: (value?: TResult | PromiseLike<TResult>) => void,
resolve: (value: TResult | PromiseLike<TResult>) => void,
reject: (reason?: any) => void,
) => void | Promise<void>;

Expand Down Expand Up @@ -71,13 +71,12 @@ export function createPromiEvent<TResult, TEvents extends EventsDefinition = voi
* Ensures the next object in the `PromiEvent` chain is overloaded with
* `EventEmitter` methods.
*/
const createChainingPromiseMethod = (
method: typeof thenSymbol | typeof catchSymbol | typeof finallySymbol,
source: Promise<any>,
) => (...args: any[]) => {
const nextPromise = (source as any)[method].apply(source, args);
return promiEvent(nextPromise);
};
const createChainingPromiseMethod =
(method: typeof thenSymbol | typeof catchSymbol | typeof finallySymbol, source: Promise<any>) =>
(...args: any[]) => {
const nextPromise = (source as any)[method].apply(source, args);
return promiEvent(nextPromise);
};

/**
* Builds a `PromiEvent` by assigning `EventEmitter` methods to a native
Expand Down
4 changes: 2 additions & 2 deletions packages/@magic-sdk/provider/src/util/web-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ async function generateWCKP() {
);

// export keys so we can send the public key.
const jwkPrivateKey = await subtle.exportKey('jwk', kp.privateKey);
const jwkPublicKey = await subtle.exportKey('jwk', kp.publicKey);
const jwkPrivateKey = await subtle.exportKey('jwk', kp.privateKey!);
const jwkPublicKey = await subtle.exportKey('jwk', kp.publicKey!);

// reimport the private key so it becomes non exportable when persisting.
const nonExportPrivateKey = await subtle.importKey('jwk', jwkPrivateKey, EC_IMPORT_PARAMS, false, ['sign']);
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-sdk/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"localforage-driver-memory": "^1.0.5",
"lodash": "^4.17.19",
"process": "~0.11.10",
"tslib": "^2.0.3",
"tslib": "^2.3.1",
"whatwg-url": "~8.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-sdk/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"module": "dist/es/index.js",
"types": "dist/es/index.d.ts",
"dependencies": {
"tslib": "^2.0.3"
"tslib": "^2.3.1"
},
"gitHead": "1ef062ea699d48d5e9a9375a93b7c147632b05ca"
}
2 changes: 1 addition & 1 deletion packages/magic-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"localforage": "^1.7.4",
"localforage-driver-memory": "^1.0.5",
"regenerator-runtime": "0.13.5",
"tslib": "^2.0.3"
"tslib": "^2.3.1"
},
"gitHead": "1ef062ea699d48d5e9a9375a93b7c147632b05ca"
}
49 changes: 49 additions & 0 deletions scripts/bin/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env ts-node-script

import ora from 'ora';
import execa from 'execa';
import chalk from 'chalk';
import path from 'path';
import { runAsyncProcess } from '../utils/run-async-process';
import { handleError } from '../utils/handle-script-error';
import { getTSConfigs, logTSConfigs } from '../utils/get-tsconfigs';

async function compileTypeScripts(tsconfigs: string[]) {
const spinner = ora('Compiling TypeScripts...').start();
await execa('tsc', ['-b', ...tsconfigs])
.then(() => {
spinner.succeed('TypeScripts successfully compiled!');
})
.catch(handleError(spinner, 'TypeScripts failed to compile.'));
}

async function bundleForCDN() {
console.log(chalk`\n{dim ❮❮❮} Building CDN bundles {dim ❯❯❯}\n`);
await execa('yarn', ['--silent', 'wsrun', '--serial', `${process.env.INIT_CWD}/scripts/bin/wsrun/build:cdn.ts`], {
stdio: 'inherit',
})
.then(() => console.log())
.catch(handleError('CDN bundles failed to build.'));
}

async function injectENV(allPkgs: string[]) {
const spinner = ora('Injecting environment variables...').start();
const onCatch = handleError(spinner, 'Failed to inject environment variables.');

await execa(`${process.env.INIT_CWD}/scripts/bin/inject-env.ts`, [...allPkgs])
.then(() => {
spinner.succeed('Environment variables successfully injected!');
})
.catch(onCatch);
}

async function main() {
const { tsconfigs, packages } = await getTSConfigs(['tsconfig.json', 'tsconfig.cjs.json']);

logTSConfigs(tsconfigs);
await compileTypeScripts(tsconfigs);
await bundleForCDN();
await injectENV(packages);
}

runAsyncProcess(main);
2 changes: 1 addition & 1 deletion scripts/clean.ts → scripts/bin/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import execa from 'execa';
import meow from 'meow';
import ora from 'ora';
import chalk from 'chalk';
import { runAsyncProcess } from './utils/run-async-process';
import { runAsyncProcess } from '../utils/run-async-process';

const spinner = ora();

Expand Down
50 changes: 50 additions & 0 deletions scripts/bin/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env ts-node-script

import ora from 'ora';
import execa from 'execa';
import TscWatchClient from 'tsc-watch/client';
import chalk from 'chalk';
import path from 'path';
import { runAsyncProcess } from '../utils/run-async-process';
import { getTSConfigs, logTSConfigs } from '../utils/get-tsconfigs';

async function injectENV(allPkgs: string[]) {
await execa(`${process.env.INIT_CWD}/scripts/bin/inject-env.ts`, [...allPkgs]).then(() => {
console.log('Environment variables successfully injected!');
});
}

async function runDevServer(tsconfigs: string[], allPkgs: string[]) {
const spinner = ora();

[
[5, 0],
[4, 1000],
[3, 2000],
[2, 3000],
[1, 4000],
].forEach(([countdown, timeout]) => {
if (!spinner.isSpinning) spinner.start();

setTimeout(() => {
spinner.text = `Starting TypeScript development server in ${countdown}...`;
}, timeout);
});

setTimeout(() => {
spinner.succeed('Starting TypeScript development server...');

const watch = new TscWatchClient();
watch.on('success', async () => injectENV(allPkgs));
watch.start('-b', ...tsconfigs);
}, 5000);
}

async function main() {
const { tsconfigs, packages } = await getTSConfigs(['tsconfig.json', 'tsconfig.cjs.json']);

logTSConfigs(tsconfigs);
await runDevServer(tsconfigs, packages);
}

runAsyncProcess(main);
6 changes: 3 additions & 3 deletions scripts/inject-env.ts → scripts/bin/inject-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { replaceInFile } from 'replace-in-file';
import path from 'path';
import { runAsyncProcess } from './utils/run-async-process';
import { runAsyncProcess } from '../utils/run-async-process';

/**
* Environment variables to be interpolated into the built files. Interpolations
Expand All @@ -15,8 +15,8 @@ import { runAsyncProcess } from './utils/run-async-process';
* (FYI: it's best to encapsulate the interpolation as a string, i.e.: `"%VARIABLE_NAME%"`).
*/
const environment = {
WEB_VERSION: require(path.resolve(__dirname, '../packages/magic-sdk/package.json')).version,
REACT_NATIVE_VERSION: require(path.resolve(__dirname, '../packages/@magic-sdk/react-native/package.json')).version,
WEB_VERSION: require(path.resolve(__dirname, '../../packages/magic-sdk/package.json')).version,
REACT_NATIVE_VERSION: require(path.resolve(__dirname, '../../packages/@magic-sdk/react-native/package.json')).version,
};

async function main() {
Expand Down
2 changes: 1 addition & 1 deletion scripts/lint.ts → scripts/bin/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import chalk from 'chalk';
import execa from 'execa';
import { runAsyncProcess } from './utils/run-async-process';
import { runAsyncProcess } from '../utils/run-async-process';

async function main() {
console.log(chalk`\n{dim ❮❮❮} Linting TypeScripts {dim ❯❯❯}\n`);
Expand Down
4 changes: 2 additions & 2 deletions scripts/test.ts → scripts/bin/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import chalk from 'chalk';
import execa from 'execa';
import { runAsyncProcess } from './utils/run-async-process';
import { runAsyncProcess } from '../utils/run-async-process';

async function main() {
console.log(chalk`\n{dim ❮❮❮} Running tests {dim ❯❯❯}\n`);
const args = process.argv.slice(2);

await execa(
'yarn',
['--silent', 'wsrun', '--serial', `${process.env.INIT_CWD}/scripts/wsrun/test:unit.ts`, ...args],
['--silent', 'wsrun', '--serial', `${process.env.INIT_CWD}/scripts/bin/wsrun/test:unit.ts`, ...args],
{
stdio: 'inherit',
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/wsrun/bin.ts → scripts/bin/wsrun/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* eslint-disable import/no-dynamic-require */

import execa from 'execa';
import { runAsyncProcess } from '../utils/run-async-process';
import { runAsyncProcess } from '../../utils/run-async-process';

/**
* The binary which `wsrun` uses to consume inputs and run actions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import execa from 'execa';
import fs from 'fs';
import { runAsyncProcess } from '../utils/run-async-process';
import { runAsyncProcess } from '../../utils/run-async-process';

function existsAsync(path: string) {
return new Promise((resolve) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import path from 'path';
import { existsSync } from 'fs';
import { runAsyncProcess } from '../utils/run-async-process';
import { runAsyncProcess } from '../../utils/run-async-process';

async function main() {
const args = process.argv.slice(2);
if (!args.length) args.push(''); // We need a minimum of one `args` element

const root = path.join(__dirname, '..', '..');
const root = path.join(__dirname, '..', '..', '..');
const absResults = args.map((a) => path.join(process.cwd(), a ?? ''));
const relResults = absResults.map((a) => path.relative(root, a));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import execa from 'execa';
import fs from 'fs';
import { runAsyncProcess } from '../utils/run-async-process';
import { runAsyncProcess } from '../../utils/run-async-process';

function existsAsync(path: string) {
return new Promise((resolve) => {
Expand Down
Loading