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

Replace auto type guard with struct #911

Merged
merged 1 commit into from Nov 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 1 addition & 6 deletions .eslintrc.js
Expand Up @@ -29,10 +29,5 @@ module.exports = {
},
],

ignorePatterns: [
'!.prettierrc.js',
'**/!.eslintrc.js',
'**/dist*/',
'**/*__GENERATED__*',
],
ignorePatterns: ['!.prettierrc.js', '**/!.eslintrc.js', '**/dist*/'],
};
1 change: 0 additions & 1 deletion .gitignore
@@ -1,5 +1,4 @@
**/dist
**/src/**/*__GENERATED__*

# sed backup files
*.*-e
Expand Down
1 change: 0 additions & 1 deletion packages/cli/.eslintrc.js
@@ -1,6 +1,5 @@
module.exports = {
extends: ['../../.eslintrc.js'],
ignorePatterns: ['**/*__GENERATED__*'],
overrides: [
{
files: ['**/*.ts'],
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/package.json
Expand Up @@ -16,16 +16,15 @@
"scripts": {
"shasum": "node ./scripts/computeSnapShasum.js",
"build:init-template": "node ./scripts/createInitTemplate.js && yarn prettier --check src/cmds/init/init-template.json",
"build:guards": "ts-auto-guard --guard-file-name=__GENERATED__ ./src/utils/snap-config.ts",
"build:tsc": "tsc --project ./tsconfig.local.json",
"build:chmod": "chmod +x ./dist/main.js",
"build:readme": "node ./scripts/updateReadme.js",
"build": "yarn build:pre-tsc && yarn build:tsc && yarn build:post-tsc",
"build:pre-tsc": "yarn build:init-template && yarn build:guards",
"build:pre-tsc": "yarn build:init-template",
"build:post-tsc": "yarn build:chmod && yarn build:readme",
"build:clean": "yarn clean && yarn build",
"build:watch": "tsc-watch --onSuccess 'yarn build:chmod'",
"clean": "rimraf '*.tsbuildinfo' 'dist/*' 'src/**/*__GENERATED__*'",
"clean": "rimraf '*.tsbuildinfo' 'dist/*'",
"test": "yarn build:init-template && jest",
"posttest": "jest-it-up",
"test:watch": "yarn test --watch",
Expand Down Expand Up @@ -58,6 +57,7 @@
"serve-handler": "^6.1.1",
"ses": "^0.15.15",
"slash": "^3.0.0",
"superstruct": "^0.16.7",
"yargs": "^16.2.0",
"yargs-parser": "^20.2.2"
},
Expand Down Expand Up @@ -93,7 +93,6 @@
"prettier": "^2.3.2",
"prettier-plugin-packagejson": "^2.2.11",
"rimraf": "^3.0.2",
"ts-auto-guard": "^2.3.0",
"ts-jest": "^29.0.0",
"ts-node": "^10.7.0",
"tsc-watch": "^4.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/tsconfig.json
@@ -1,5 +1,5 @@
{
"extends": "../tsconfig.test.json",
"extends": "../tsconfig.json",
"references": [
{
"path": "../utils"
Expand Down
31 changes: 26 additions & 5 deletions packages/cli/src/utils/snap-config.ts
Expand Up @@ -4,16 +4,37 @@ import type browserify from 'browserify';
import { Arguments } from 'yargs';
import yargsParse from 'yargs-parser';
import yargs from 'yargs/yargs';
import { object, optional, func, Infer, is } from 'superstruct';
import builders from '../builders';
import { CONFIG_FILE, logError } from './misc';
import { isSnapConfig } from './snap-config.__GENERATED__';

/** @see {isSnapConfig} ts-auto-guard:type-guard */
export type SnapConfig = {
cliOptions?: Record<string, unknown>;
bundlerCustomizer?: (bundler: browserify.BrowserifyObject) => void;
export type BundleCustomizer = (bundler: browserify.BrowserifyObject) => void;

export const SnapConfigStruct = object({
cliOptions: optional(object()),
bundlerCustomizer: optional(func()),
});

export type SnapConfig = Omit<
Infer<typeof SnapConfigStruct>,
'bundlerCustomizer'
> & {
bundlerCustomizer?: BundleCustomizer;
};

/**
* Check if the given value is a {@link SnapConfig} object. Note that this
* function does not check the validity of the `bundleCustomizer` property, as
* it is not possible to check the validity of a function in JavaScript.
*
* @param value - The value to check.
* @returns `true` if the value is a valid {@link SnapConfig} object, `false`
* otherwise.
*/
export function isSnapConfig(value: unknown): value is SnapConfig {
return is(value, SnapConfigStruct);
}

let snapConfigCache: SnapConfig | undefined;

/**
Expand Down