Skip to content

Commit

Permalink
feat: de-coupled @angular-devkit from generators
Browse files Browse the repository at this point in the history
  • Loading branch information
bennymeg committed Nov 23, 2021
1 parent f5998b6 commit 86e0448
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 170 deletions.
13 changes: 0 additions & 13 deletions packages/nx-electron/package-lock.json

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

3 changes: 1 addition & 2 deletions packages/nx-electron/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nx-electron/nx-electron",
"version": "12.0.0-alpha.0",
"version": "12.0.0-alpha.1",
"main": "src/index.js",
"description": "Electron Plugin for Nx",
"author": "Benny Megidish",
Expand Down Expand Up @@ -67,7 +67,6 @@
"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",
Expand Down
70 changes: 42 additions & 28 deletions packages/nx-electron/src/generators/init/generator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Rule, chain } from '@angular-devkit/schematics';
import { addDepsToPackageJson, updateJsonInTree, addPackageWithInit, updateWorkspace, formatFiles } from '@nrwl/workspace';
import { addDependenciesToPackageJson, convertNxGenerator, formatFiles, GeneratorCallback, Tree, updateJson } from '@nrwl/devkit';
import { Schema } from './schema';
import { nxElectronVersion, electronVersion, electronBuilderVersion, rimrafVersion, exitZeroVersion } from '../../utils/versions';
import { JsonObject } from '@angular-devkit/core';
import { setDefaultCollection } from '@nrwl/workspace/src/utilities/set-default-collection';
import { jestInitGenerator } from '@nrwl/jest';

function addDependencies(): Rule {
return addDepsToPackageJson(

function addDependencies(tree: Tree) {
return addDependenciesToPackageJson(
tree,
{},
{
'nx-electron': nxElectronVersion,
Expand All @@ -17,8 +19,8 @@ function addDependencies(): Rule {
);
}

function moveDependency(): Rule {
return updateJsonInTree('package.json', json => {
function moveDependency(tree: Tree) {
return updateJson(tree, 'package.json', json => {
json.dependencies = json.dependencies || {};

delete json.dependencies['nx-electron'];
Expand All @@ -30,8 +32,8 @@ function moveDependency(): Rule {
});
}

function addScripts(): Rule {
return updateJsonInTree('package.json', json => {
function addScripts(tree: Tree) {
return updateJson(tree, 'package.json', json => {
json.scripts = json.scripts || {};

json.scripts["postinstall"] = "exitzero electron-builder install-app-deps";
Expand All @@ -40,27 +42,39 @@ function addScripts(): Rule {
});
}

function setDefault(): Rule {
return updateWorkspace(workspace => {
workspace.extensions.cli = workspace.extensions.cli || {};
function normalizeOptions(schema: Schema) {
return {
...schema,
unitTestRunner: schema.unitTestRunner ?? 'jest',
};
}

export async function initGenerator(tree: Tree, schema: Schema) {
const options = normalizeOptions(schema);

setDefaultCollection(tree, 'nx-electron');

let jestInstall: GeneratorCallback;
if (options.unitTestRunner === 'jest') {
jestInstall = await jestInitGenerator(tree, {});
}

const defaultCollection: string =
workspace.extensions.cli &&
((workspace.extensions.cli as JsonObject).defaultCollection as string);
const installTask = await addDependencies(tree);

if (!defaultCollection || defaultCollection === '@nrwl/workspace') {
(workspace.extensions.cli as JsonObject).defaultCollection = 'nx-electron';
if (!options.skipFormat) {
await formatFiles(tree);
}

return async () => {
if (jestInstall) {
await jestInstall();
}
});
}

export default function(schema: Schema) {
return chain([
setDefault(),
addPackageWithInit('@nrwl/jest'),
addScripts(),
addDependencies(),
moveDependency(),
formatFiles(schema)
]);
await addScripts(tree);
await installTask();
await moveDependency(tree);
};
}

export default initGenerator;
export const initSchematic = convertNxGenerator(initGenerator);
1 change: 1 addition & 0 deletions packages/nx-electron/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface Schema {
unitTestRunner?: 'jest' | 'none';
skipFormat: boolean;
}
6 changes: 6 additions & 0 deletions packages/nx-electron/src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"title": "Add Nx Electron Schematics",
"type": "object",
"properties": {
"unitTestRunner": {
"description": "Adds the specified unit test runner",
"type": "string",
"enum": ["jest", "none"],
"default": "jest"
},
"skipFormat": {
"description": "Skip formatting files",
"type": "boolean",
Expand Down

0 comments on commit 86e0448

Please sign in to comment.