Skip to content

Commit

Permalink
handle required options with JSON schemas and disallow unknown options
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckyMaler authored and ZachJW34 committed Jun 22, 2020
1 parent 7a34196 commit cb1a0fa
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 36 deletions.
50 changes: 22 additions & 28 deletions libs/vue-plugin/src/builders/browser/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
virtualFs
} from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import { from, Observable, of } from 'rxjs';
import { from, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { BrowserBuilderSchema } from './schema';
import { getProjectRoot, getProjectSourceRoot } from '../../utils';
Expand All @@ -34,16 +34,6 @@ export function runBuilder(
options: BrowserBuilderSchema,
context: BuilderContext
): Observable<BuilderOutput> {
const requiredOptions = ['outputPath', 'index', 'main', 'tsConfig'];
const missingOption = requiredOptions.find(
requiredOption => !options[requiredOption]
);

if (missingOption) {
context.logger.error(`Invalid options, "${missingOption}" is required.`);
return of({ success: false });
}

async function setup(): Promise<{
projectRoot: string;
inlineOptions;
Expand Down Expand Up @@ -92,24 +82,28 @@ export function runBuilder(
pkg: resolvePkg(context.workspaceRoot),
inlineOptions
});
const buildOptions = {
mode: options.mode,
dest: undefined,
modern: options.modern,
'no-unsafe-inline': options.skipUnsafeInline,
'no-clean': options.skipClean,
report: options.report,
'report-json': options.reportJson,
'skip-plugins': options.skipPlugins,
watch: options.watch
};

if (options.watch) {
return new Observable(obs => {
service
.run('build', buildOptions, ['build'])
.then(success => obs.next(success))
.catch(err => obs.error(err));
});
}

return from(
service.run(
'build',
{
mode: options.mode,
dest: undefined,
modern: options.modern,
'no-unsafe-inline': options.skipUnsafeInline,
'no-clean': options.skipClean,
report: options.report,
'report-json': options.reportJson,
'skip-plugins': options.skipPlugins,
watch: options.watch
},
['build']
)
);
return from(service.run('build', buildOptions, ['build']));
}),
map(() => ({ success: true }))
);
Expand Down
2 changes: 1 addition & 1 deletion libs/vue-plugin/src/builders/browser/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface BrowserBuilderSchema extends JsonObject {
skipClean: boolean;
report: boolean;
reportJson: boolean;
skipPlugins: string;
skipPlugins?: string;
watch: boolean;
index: string;
main: string;
Expand Down
4 changes: 2 additions & 2 deletions libs/vue-plugin/src/builders/browser/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"$id": "https://json-schema.org/draft-07/schema",
"title": "Browser builder",
"description": "Build app",
Expand Down Expand Up @@ -94,7 +93,8 @@
"enum": ["none", "all", "media", "bundles"]
}
},
"required": [],
"required": ["outputPath", "index", "main", "tsConfig"],
"additionalProperties": false,
"definitions": {
"assetPattern": {
"oneOf": [
Expand Down
13 changes: 13 additions & 0 deletions libs/vue-plugin/src/builders/dev-server/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ export function runBuilder(
modifyCachePaths(config, context);
addFileReplacements(config, browserOptions, context);
modifyFilenameHashing(config, browserOptions);

if (!options.watch) {
// There is no option to disable file watching in `webpack-dev-server`,
// but webpack's file watcher can be overriden.
config.plugin('vue-cli').use({
apply: compiler => {
compiler.hooks.afterEnvironment.tap('vue-cli', () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
compiler.watchFileSystem = { watch: () => {} };
});
}
});
}
}
};

Expand Down
7 changes: 4 additions & 3 deletions libs/vue-plugin/src/builders/dev-server/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ export interface DevServerBuilderSchema extends JsonObject {
open: boolean;
copy: boolean;
stdin: boolean;
mode: 'development' | 'production';
mode?: 'development' | 'production';
host: string;
port: number;
https: boolean;
public: string;
skipPlugins: string;
public?: string;
skipPlugins?: string;
buildTarget: string;
watch: boolean;
}
9 changes: 7 additions & 2 deletions libs/vue-plugin/src/builders/dev-server/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"$id": "https://json-schema.org/draft-07/schema",
"title": "DevServer builder",
"description": "Start development server",
Expand Down Expand Up @@ -52,7 +51,13 @@
"type": "string",
"description": "Target to serve.",
"pattern": "^[^:\\s]+:[^:\\s]+(:[^\\s]+)?$"
},
"watch": {
"type": "boolean",
"description": "Watch for changes.",
"default": true
}
},
"required": []
"required": ["buildTarget"],
"additionalProperties": false
}

0 comments on commit cb1a0fa

Please sign in to comment.