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
Next Next commit
Re-factor scripts for building, development, linting, and cleaning to…
… TypeScript
  • Loading branch information
smithki committed Sep 2, 2021
commit d7c26fd88c4bb2eded661b215752fc7708cac5d5
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
**/dist
**/.eslintrc.js
**/jest.config.ts
**/babel.config.js
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"version": "0.0.0",
"scripts": {
"bootstrap": "yarn install && lerna link",
"wsrun": "wsrun --bin $INIT_CWD/scripts/wsrun/runner.sh -p $PKG",
"dev": "$INIT_CWD/scripts/clean.sh && $INIT_CWD/scripts/dev.sh",
"build": "$INIT_CWD/scripts/clean.sh && $INIT_CWD/scripts/build.sh",
"clean": "$INIT_CWD/scripts/clean.sh",
"lint": "$INIT_CWD/scripts/lint.sh",
"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.sh",
"paths": "yarn --silent wsrun --parallel --no-prefix -r -c $INIT_CWD/scripts/wsrun/resolve-paths.ts",
"postinstall": "husky install"
Expand All @@ -22,32 +22,37 @@
"@types/jsdom": "~12.2.4",
"@types/react": "^16.9.34",
"@types/react-native": "^0.62.2",
"@types/rimraf": "^3.0.2",
"@types/whatwg-url": "^6.4.0",
"@typescript-eslint/eslint-plugin": "~3.4.0",
"auto": "^10.31.0",
"ava": "3.11.0",
"boxen-cli": "^1.0.0",
"chalk": "~4.1.2",
"eslint": "~7.3.1",
"eslint-import-resolver-typescript": "~2.0.0",
"eslint-plugin-import": "~2.21.0",
"eslint-plugin-jsx-a11y": "~6.3.1",
"eslint-plugin-prettier": "~3.1.4",
"eslint-plugin-react": "~7.20.0",
"eslint-plugin-react-hooks": "~4.0.4",
"execa": "~5.1.1",
"find-glob": "^2.1.0",
"glob": "^7.1.6",
"husky": "^7.0.1",
"jest": "^27.0.6",
"lerna": "^3.21.0",
"lint-staged": "^10.0.7",
"meow": "9.0.0",
"microbundle": "0.12.0",
"npm-run-all": "^4.1.5",
"nyc": "13.1.0",
"ora": "~5.4.1",
"prettier": "~2.0.5",
"react": "^16.13.1",
"react-native": "^0.62.2",
"replace-in-file": "^6.1.0",
"rimraf": "~3.0.0",
"rimraf": "~3.0.2",
"ts-jest": "^27.0.4",
"ts-node": "^10.2.0",
"tsc-watch": "^4.2.9",
Expand Down
1 change: 1 addition & 0 deletions packages/@magic-sdk/react-native/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/dist
/.eslintrc.js
/jest.config.ts
/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ beforeEach(() => {
test('SDKBaseReactNative.Relayer aliases to ReactNativeWebViewController.Relayer', () => {
const magic = createMagicSDK();

(magic as any).overlay.Relayer = 'hello world';
magic.overlay.Relayer = 'hello world';
expect(magic.Relayer).toBe('hello world' as any);
});
34 changes: 0 additions & 34 deletions scripts/build.sh

This file was deleted.

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

import ora from 'ora';
import execa from 'execa';
import chalk from 'chalk';

type CatchFn<TResult = never> = (reason: any) => TResult | PromiseLike<TResult>;

function handleExecaError<TResult = never>(spinner?: ora.Ora, message?: string): CatchFn<TResult>;
function handleExecaError<TResult = never>(message?: string): CatchFn<TResult>;
function handleExecaError<TResult = never>(messageOrSpinner?: ora.Ora | string, message?: string): CatchFn<TResult> {
return (err: any) => {
if (messageOrSpinner) {
if (typeof messageOrSpinner === 'string') {
console.error(messageOrSpinner);
} else {
messageOrSpinner.fail(message);
}
}

if (err) console.error(err);
process.exit(1);
};
}

async function getTSConfigs() {
const spinner = ora('Determining TypeScript projects to build...').start();

const tsconfigs = await execa('yarn', ['--silent', 'paths', 'tsconfig.json', 'tsconfig.cjs.json'])
.then((subprocess) => {
spinner.succeed('Found TypeScript projects to build!');
return subprocess.stdout.split('\n');
})
.catch(handleExecaError(spinner, 'Failed to discover TypeScript projects to build.'));

return tsconfigs;
}

async function compileTypeScripts(tsconfigs: string[]) {
const spinner = ora('Compiling TypeScripts...').start();
await execa('tsc', ['-b', ...tsconfigs])
.then(() => {
spinner.succeed('TypeScripts successfully compiled!');
})
.catch(handleExecaError(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/wsrun/build:cdn.ts`], {
stdio: 'inherit',
})
.then(() => console.log())
.catch(handleExecaError('CDN bundles failed to build.'));
}

async function injectENV() {
const spinner = ora('Injecting environment variables...').start();
const onCatch = handleExecaError(spinner, 'Failed to inject environment variables.');
const allPkgs = await execa('yarn', ['--silent', 'paths'])
.then((subprocess) => subprocess.stdout.split('\n'))
.catch(onCatch);

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

async function main() {
const tsconfigs = await getTSConfigs();

console.log(
tsconfigs.reduce((prev, next) => `${prev}\n - ${next}`, ''),
'\n',
);

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

main();
73 changes: 0 additions & 73 deletions scripts/clean.sh

This file was deleted.

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

import execa from 'execa';
import meow from 'meow';
import ora from 'ora';
import chalk from 'chalk';

const spinner = ora();

async function abstractCleanFn(label: string, patterns: string[]) {
await Promise.all(
patterns.map(async (pattern) => {
await execa('yarn', ['wsrun', '--parallel', '-r', 'rimraf', pattern]);
}),
);
}

async function cleanDist() {
await abstractCleanFn('dist files', ['dist']);
}

async function cleanCache() {
await abstractCleanFn('cache files', ['node_modules/.cache']);
}

async function cleanTestArtifacts() {
await abstractCleanFn('test artifacts', ['coverage', '.nyc_output']);
}

async function cleanNodeModules() {
await Promise.all([
execa('yarn', ['wsrun', '--parallel', '-r', 'rimraf', 'node_modules']),
execa('rimraf', ['node_modules']),
]);
}

const helpText = `
Usage: yarn clean [OPTIONS]

Options:
--cache Remove cache files.

--test-artifacts Remove coverage reports.

--deps Remove node_modules (from all packages).

-h, --help Show this message. No cleaning happens if
this flag is present.
`;

async function main() {
const cli = meow(helpText, {
flags: {
cache: {
type: 'boolean',
default: false,
},

testArtifacts: {
type: 'boolean',
default: false,
},

deps: {
type: 'boolean',
default: false,
},

help: {
type: 'boolean',
default: false,
alias: 'h',
},
},
});

if (cli.flags.help) {
cli.showHelp();
}

const enabledCleaners: string[] = [chalk.rgb(0, 255, 255)('dist')];
const promises: (() => Promise<any>)[] = [cleanDist];

if (cli.flags.cache) {
enabledCleaners.push(chalk.rgb(0, 255, 255)('cache'));
promises.push(cleanCache);
}

if (cli.flags.testArtifacts) {
enabledCleaners.push(chalk.rgb(0, 255, 255)('test artifacts'));
promises.push(cleanTestArtifacts);
}

if (cli.flags.deps) {
enabledCleaners.push(chalk.rgb(0, 255, 255)('node_modules'));
promises.push(cleanNodeModules);
}

spinner.start(`Cleaning ${enabledCleaners.join(', ')}...`);

Promise.all(promises.map((p) => p()))
.then(() => {
spinner.succeed(`Cleaned ${enabledCleaners.join(', ')}!`);
})
.catch((err) => {
spinner.fail('Failed!');
if (err) console.error(err);
process.exit(1);
});
}

main();
18 changes: 0 additions & 18 deletions scripts/dev.sh

This file was deleted.

Loading