Skip to content

Commit

Permalink
feat: de-coupled @angular-devkit from executors
Browse files Browse the repository at this point in the history
  • Loading branch information
bennymeg committed Nov 22, 2021
1 parent 0d72a3d commit f5998b6
Show file tree
Hide file tree
Showing 20 changed files with 440 additions and 555 deletions.
13 changes: 13 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
"files": ["*.js", "*.jsx"],
"extends": ["plugin:@nrwl/nx/javascript"],
"rules": {}
},
{
"files": ["**/*.ts"],
"excludedFiles": ["./src/migrations/**"],
"rules": {
"no-restricted-imports": [
"error",
"@nrwl/workspace",
"@angular-devkit/core",
"@angular-devkit/architect",
"@angular-devkit/schematics"
]
}
}
]
}
8 changes: 4 additions & 4 deletions packages/nx-electron/executors.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
"$schema": "http://json-schema.org/schema",
"executors": {
"build": {
"implementation": "./src/executors/build/executor",
"implementation": "./src/executors/build/executor.compat",
"schema": "./src/executors/build/schema.json",
"description": "Build an Electron application"
},
"execute": {
"implementation": "./src/executors/execute/executor",
"implementation": "./src/executors/execute/executor.compat",
"schema": "./src/executors/execute/schema.json",
"description": "Execute an Electron application"
},
"package": {
"implementation": "./src/executors/package/executor",
"implementation": "./src/executors/package/executor.compat",
"schema": "./src/executors/package/schema.json",
"description": "Package an Electron application"
},
"make": {
"implementation": "./src/executors/package/executor",
"implementation": "./src/executors/package/executor.compat",
"schema": "./src/executors/package/schema.json",
"description": "Make an Electron application"
}
Expand Down
79 changes: 72 additions & 7 deletions packages/nx-electron/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/nx-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"fork-ts-checker-webpack-plugin": "^6.1.0",
"license-webpack-plugin": "^2.3.11",
"rimraf": "^3.0.2",
"rxjs": "^7.4.0",
"rxjs-for-await": "^1.0.0",
"source-map-support": "^0.5.19",
"strip-json-comments": "^3.1.1",
"terser-webpack-plugin": "^4.2.3",
Expand Down
10 changes: 10 additions & 0 deletions packages/nx-electron/src/executors/build/executor.compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { convertNxExecutor, ExecutorContext } from '@nrwl/devkit';

import executor from './executor';

function executorAdapter(options: any, context: ExecutorContext):
Promise<{ success: boolean; }> | AsyncIterableIterator<{ success: boolean; }> {
return executor(options, context);
}

export default convertNxExecutor(executorAdapter);
110 changes: 51 additions & 59 deletions packages/nx-electron/src/executors/build/executor.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,86 @@
import { join, resolve } from 'path';
import { from, Observable } from 'rxjs';
import { concatMap, map, tap } from 'rxjs/operators';

import { BuilderContext, createBuilder } from '@angular-devkit/architect';
import { BuildResult, runWebpack } from '@angular-devkit/build-webpack';
import { JsonObject } from '@angular-devkit/core';
import { map, tap } from 'rxjs/operators';
import { eachValueFrom } from 'rxjs-for-await';

import { ExecutorContext } from '@nrwl/devkit';
import { runWebpack } from '@nrwl/workspace/src/utilities/run-webpack';
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import { calculateProjectDependencies, checkDependentProjectsHaveBeenBuilt, createTmpTsConfig } from '@nrwl/workspace/src/utils/buildable-libs-utils';
import { calculateProjectDependencies, checkDependentProjectsHaveBeenBuilt, createTmpTsConfig } from '@nrwl/workspace/src/utilities/buildable-libs-utils';

import { getElectronWebpackConfig } from '../../utils/electron.config';
import { normalizeBuildOptions } from '../../utils/normalize';
import { BuildBuilderOptions } from '../../utils/types';
import { getSourceRoot } from '../../utils/workspace';
import { MAIN_OUTPUT_FILENAME } from '../../utils/config';
import { generatePackageJson } from '../../utils/generate-package-json';
import webpack from 'webpack';

try {
require('dotenv').config();
} catch (e) {}

export type ElectronBuildEvent = {
outfile: string;
success: boolean;
};

export interface BuildElectronBuilderOptions extends BuildBuilderOptions {
optimization?: boolean;
sourceMap?: boolean;
buildLibsFromSource?: boolean;
generatePackageJson?: boolean;
implicitDependencies: Array<string>;
externalDependencies: 'all' | 'none' | Array<string>;
}

export type ElectronBuildEvent = BuildResult & {
outfile: string;
};
export interface NormalizedBuildElectronBuilderOptions extends BuildElectronBuilderOptions {
webpackConfig: string;
}

export default createBuilder<JsonObject & BuildElectronBuilderOptions>(run);

function run( options: JsonObject & BuildElectronBuilderOptions, context: BuilderContext): Observable<ElectronBuildEvent> {
export function executor(rawOptions: BuildElectronBuilderOptions, context: ExecutorContext): AsyncIterableIterator<ElectronBuildEvent> {
const { sourceRoot, projectRoot } = getSourceRoot(context);
const normalizedOptions = normalizeBuildOptions( rawOptions, context.root, sourceRoot, projectRoot);
const projGraph = readCachedProjectGraph();

if (!options.buildLibsFromSource) {
const { target, dependencies } = calculateProjectDependencies( projGraph, context);
if (!normalizedOptions.buildLibsFromSource) {
const { target, dependencies } =
calculateProjectDependencies(projGraph, context.root, context.projectName, context.targetName, context.configurationName);

options.tsConfig = createTmpTsConfig(
join(context.workspaceRoot, options.tsConfig),
context.workspaceRoot,
target.data.root,
dependencies
);
normalizedOptions.tsConfig = createTmpTsConfig(normalizedOptions.tsConfig, context.root, target.data.root, dependencies);

if (!checkDependentProjectsHaveBeenBuilt(context, dependencies)) {
if (!checkDependentProjectsHaveBeenBuilt(context.root, context.projectName, context.targetName, dependencies)) {
return { success: false } as any;
}
}

return from(getSourceRoot(context)).pipe(
map(({ sourceRoot, projectRoot }) =>
normalizeBuildOptions(options, context.workspaceRoot, sourceRoot, projectRoot)
),
tap((normalizedOptions) => {
if (normalizedOptions.generatePackageJson) {
generatePackageJson(
context.target.project,
projGraph,
normalizedOptions
);
}
}),
map(options => {
let config = getElectronWebpackConfig(options);
if (options.webpackConfig) {
config = require(options.webpackConfig)(config, {
options,
configuration: context.target.configuration
});
}
config.entry['preload'] = join(options.sourceRoot, 'app/api/preload.ts');
return config;
}),
concatMap(config =>
runWebpack(config, context, {
logging: stats => {
context.logger.info(stats.toString(config.stats));
}
if (normalizedOptions.generatePackageJson) {
generatePackageJson(context.projectName, projGraph, normalizedOptions);
}

let config = getElectronWebpackConfig(normalizedOptions);
if (normalizedOptions.webpackConfig) {
config = require(normalizedOptions.webpackConfig)(config, {
normalizedOptions,
configuration: context.configurationName,
});
}
config.entry['preload'] = join(normalizedOptions.sourceRoot, 'app/api/preload.ts');


return eachValueFrom(
runWebpack(config, webpack).pipe(
tap((stats) => {
console.info(stats.toString(config.stats));
}),
map((stats) => {
return {
success: !stats.hasErrors(),
outfile: resolve(context.root, normalizedOptions.outputPath, MAIN_OUTPUT_FILENAME)
} as ElectronBuildEvent;
})
),
map((buildEvent: BuildResult) => {
buildEvent.outfile = resolve(
context.workspaceRoot,
options.outputPath,
MAIN_OUTPUT_FILENAME
);
return buildEvent as ElectronBuildEvent;
})
)
);
}

export default executor;
1 change: 1 addition & 0 deletions packages/nx-electron/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"title": "Electron Application Build Target",
"description": "Electron application build target options for Build Facade",
"type": "object",
"cli": "nx",
"properties": {
"main": {
"type": "string",
Expand Down
5 changes: 5 additions & 0 deletions packages/nx-electron/src/executors/execute/executor.compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { convertNxExecutor } from '@nrwl/devkit';

import executor from './executor';

export default convertNxExecutor(executor);
Loading

0 comments on commit f5998b6

Please sign in to comment.