Skip to content

Commit

Permalink
feat(core): focus improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWalker committed Aug 19, 2020
1 parent b930d95 commit 38418ae
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 97 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nstudio/xplat-source",
"version": "10.0.0",
"version": "10.0.2",
"description": "Cross-platform (xplat) tools for Nx workspaces.",
"homepage": "https://nstudio.io/xplat",
"private": true,
Expand Down Expand Up @@ -70,6 +70,7 @@
"@types/react-dom": "~16.9.4",
"@types/react-router-dom": "~5.1.3",
"@types/webpack": "~4.41.21",
"@types/xml2js": "^0.4.5",
"@types/yargs": "~15.0.5",
"app-root-path": "~3.0.0",
"circular-dependency-plugin": "~5.2.0",
Expand Down
81 changes: 56 additions & 25 deletions packages/focus/src/schematics/mode/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chain, Tree, SchematicContext } from '@angular-devkit/schematics';
import { chain, Tree, SchematicContext, noop } from '@angular-devkit/schematics';

import { updateTsConfig, XplatHelpers } from '@nstudio/xplat';
import {
Expand All @@ -8,6 +8,8 @@ import {
getGroupByName,
PlatformModes,
supportedPlatformsWithNx,
getJsonFromFile,
isXplatWorkspace,
} from '@nstudio/xplat-utils';
import { Schema } from './schema';
import { FocusHelpers } from '../../utils';
Expand All @@ -27,49 +29,78 @@ export default function (options: Schema) {
// init xplat settings
prerun(),
// update tsconfig based on mode
(tree: Tree) => updateExcludes(name)(tree),
(tree: Tree) => {
return isXplatWorkspace() ? updateExcludes(name)(tree) : noop()
},
// update IDE settings
(tree: Tree, context: SchematicContext) => {
// apps
const appsDir = tree.getDir('apps');
const appFolders = appsDir.subdirs;
const allApps = [];
for (const dir of appFolders) {
allApps.push(`**${appsDir.path}/${dir}`);
if (appsDir && appsDir.subdirs) {
const appFolders = appsDir.subdirs;
for (const dir of appFolders) {
allApps.push(`**${appsDir.path}/${dir}`);
}
}

// libs
const libsDir = tree.getDir('libs');
const allLibs = [];
if (libsDir && libsDir.subdirs) {
const libsFolders = libsDir.subdirs;
for (const dir of libsFolders) {
allLibs.push(`**${libsDir.path}/${dir}`);
}
}

// packages
const packagesDir = tree.getDir('packages');
const allPackages = [];
if (packagesDir && packagesDir.subdirs) {
const packagesFolders = packagesDir.subdirs;
for (const dir of packagesFolders) {
allPackages.push(`**${packagesDir.path}/${dir}`);
}
}

// project handling
let focusOnApps: string[] = [];
if (name !== 'fullstack' && options.projects) {
focusOnApps = options.projects.split(',');
for (let i = 0; i < focusOnApps.length; i++) {
const projectName = focusOnApps[i];
const nameParts = <Array<PlatformTypes>>(
(<unknown>projectName.split('-'))
);
let containsPlatform = false;
for (const n of nameParts) {
if (supportedPlatformsWithNx.includes(n)) {
containsPlatform = true;
if (isXplatWorkspace()) {
// allows for shorthand project/app names omitting platform from the app name
// just add platform to the name to be specific
for (let i = 0; i < focusOnApps.length; i++) {
const projectName = focusOnApps[i];
const nameParts = <Array<PlatformTypes>>(
(<unknown>projectName.split('-'))
);
let containsPlatform = false;
for (const n of nameParts) {
if (supportedPlatformsWithNx.includes(n)) {
containsPlatform = true;
}
}
if (!containsPlatform) {
const appName = getGroupByName()
? `${nameParts.join('-')}-${name}`
: `${name}-${nameParts.join('-')}`;
focusOnApps[i] = `**/apps/${appName}`;
}
}
if (!containsPlatform) {
// allows for shorthand project/app names omitting platform
// just add platform to the name
const appName = getGroupByName()
? `${nameParts.join('-')}-${name}`
: `${name}-${nameParts.join('-')}`;
focusOnApps[i] = `**/apps/${appName}`;
}
}
}
// targets and mode should be the same
return FocusHelpers.updateIDESettings(
{
platforms: name,
devMode: name,
allApps,
focusOnApps,
allLibs,
allPackages
},
name,
allApps,
focusOnApps
)(tree, context);
},
]);
Expand Down
112 changes: 74 additions & 38 deletions packages/focus/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ import {
supportedPlatformsWithNx,
sanitizeCommaDelimitedArg,
jsonParse,
isXplatWorkspace,
} from '@nstudio/xplat-utils';
import { join } from 'path';
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
import { serializeJson } from '@nrwl/workspace';
const xml2js = require('xml2js');
import * as xml2js from 'xml2js';

export namespace FocusHelpers {
export function updateIDESettings(
options: {
platforms?: string;
},
devMode?: PlatformModes,
allApps?: string[],
focusOnApps?: string[]
devMode?: PlatformModes;
allApps?: string[];
focusOnApps?: string[];
allLibs?: string[];
allPackages?: string[];
}
) {
return (tree: Tree, context: SchematicContext) => {
if (isTesting()) {
Expand All @@ -52,40 +55,65 @@ export namespace FocusHelpers {
let isExcluding = false;
let appWildcards: Array<string> = [];
const userUpdates: any = {};
if (!devMode || devMode === 'fullstack') {
if (!options.devMode || options.devMode === 'fullstack') {
// show all
isFullstack = true;
for (const p of supportedPlatformsWithNx) {
const appFilter = groupByName ? `*-${p}` : `${p}*`;
userUpdates[`**/apps/${appFilter}`] = false;
userUpdates[`**/xplat/${p}`] = false;
if (frameworkSuffix) {
userUpdates[`**/xplat/${p}${frameworkSuffix}`] = false;
if (isXplatWorkspace()) {
for (const p of supportedPlatformsWithNx) {
const appFilter = groupByName ? `*-${p}` : `${p}*`;
userUpdates[`**/apps/${appFilter}`] = false;
userUpdates[`**/xplat/${p}`] = false;
if (frameworkSuffix) {
userUpdates[`**/xplat/${p}${frameworkSuffix}`] = false;
}
}
} else {
for (const p of options.allApps) {
userUpdates[`**/apps/${p}`] = false;
}
for (const p of options.allLibs) {
userUpdates[`**/libs/${p}`] = false;
}
for (const p of options.allPackages) {
userUpdates[`**/packages/${p}`] = false;
}
}
} else if (options.platforms) {
const platforms = sanitizeCommaDelimitedArg(options.platforms);
// switch on/off platforms
for (const p of supportedPlatformsWithNx) {
const excluded = platforms.includes(p) ? false : true;
const appFilter = groupByName ? `*-${p}` : `${p}*`;
if (focusOnApps.length) {
// focusing on apps
// fill up wildcards to use below (we will clear all app wildcards when focusing on apps)
appWildcards.push(`**/apps/${appFilter}`);
} else {
// use wildcards for apps only if no project names were specified
userUpdates[`**/apps/${appFilter}`] = excluded;
if (isXplatWorkspace()) {
// switch on/off platforms
for (const p of supportedPlatformsWithNx) {
const excluded = platforms.includes(p) ? false : true;
const appFilter = groupByName ? `*-${p}` : `${p}*`;
if (options.focusOnApps && options.focusOnApps.length) {
// focusing on apps
// fill up wildcards to use below (we will clear all app wildcards when focusing on apps)
appWildcards.push(`**/apps/${appFilter}`);
} else {
// use wildcards for apps only if no project names were specified
userUpdates[`**/apps/${appFilter}`] = excluded;
}
userUpdates[`**/xplat/${p}`] = excluded;
if (frameworkSuffix) {
userUpdates[`**/xplat/${p}${frameworkSuffix}`] = excluded;
}

if (excluded) {
// if excluding any platform at all, set the flag
// this is used for WebStorm support below
isExcluding = true;
}
}
userUpdates[`**/xplat/${p}`] = excluded;
if (frameworkSuffix) {
userUpdates[`**/xplat/${p}${frameworkSuffix}`] = excluded;
} else {
// switch on/off apps/libs/packages
for (const p of options.allApps) {
userUpdates[`**/apps/${p}`] = options.focusOnApps && options.focusOnApps.length && options.focusOnApps.includes(p) ? false : true;
}

if (excluded) {
// if excluding any platform at all, set the flag
// this is used for WebStorm support below
isExcluding = true;
for (const p of options.allLibs) {
userUpdates[`**/libs/${p}`] = platforms.includes(p) ? false : true;
}
for (const p of options.allPackages) {
userUpdates[`**/packages/${p}`] = platforms.includes(p) ? false : true;
}
}
}
Expand All @@ -96,22 +124,26 @@ export namespace FocusHelpers {
// VS Code
const isVsCode = updateVSCode({
userUpdates,
allApps,
focusOnApps,
allApps: options.allApps,
focusOnApps: options.focusOnApps,
appWildcards,
allLibs: options.allLibs,
allPackages: options.allPackages,
isFullstack,
});

// WebStorm
let isWebStorm = updateWebStorem({
let isWebStorm = updateWebStorm({
userUpdates,
allApps,
focusOnApps,
allApps: options.allApps,
focusOnApps: options.focusOnApps,
appWildcards,
allLibs: options.allLibs,
allPackages: options.allPackages,
isFullstack,
});

if (!devMode) {
if (!options.devMode) {
// only when not specifying a dev mode
const workspaceUpdates: any = {
'**/node_modules': true,
Expand Down Expand Up @@ -180,6 +212,8 @@ export namespace FocusHelpers {
allApps?: string[];
focusOnApps?: string[];
appWildcards?: string[];
allLibs?: string[];
allPackages?: string[];
isFullstack?: boolean;
}) {
// VS Code support
Expand Down Expand Up @@ -307,12 +341,14 @@ export namespace FocusHelpers {
return isVsCode;
}

export function updateWebStorem(options: {
export function updateWebStorm(options: {
userUpdates?: any;
workspaceUpdates?: any;
allApps?: string[];
focusOnApps?: string[];
appWildcards?: string[];
allLibs?: string[];
allPackages?: string[];
isFullstack?: boolean;
isExcluding?: boolean;
}) {
Expand Down
1 change: 1 addition & 0 deletions packages/xplat-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@angular-devkit/core": "~10.0.0",
"@angular-devkit/schematics": "~10.0.0",
"@nrwl/workspace": "~10.0.0",
"chalk": "~4.1.0",
"strip-json-comments": "~3.1.1"
},
"author": "Nathan Walker",
Expand Down
8 changes: 7 additions & 1 deletion packages/xplat-utils/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SchematicsException,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import stripJsonComments = require('strip-json-comments');
import * as stripJsonComments from 'strip-json-comments';
import { serializeJson } from '@nrwl/workspace';

export const supportedPlatforms: Array<PlatformTypes> = [
Expand Down Expand Up @@ -42,6 +42,7 @@ let frontendFramework: FrameworkTypes;
// Group by app name (appname-platform) instead of the default (platform-appname)
let groupByName = false;
let isTest = false;
let usingXplatWorkspace = false;

export function getNpmScope() {
return npmScope;
Expand All @@ -65,6 +66,10 @@ export function getAppName(options: any, platform: PlatformTypes) {
: options.name.replace(`${platform}-`, '');
}

export function isXplatWorkspace() {
return usingXplatWorkspace;
}

export function setTest() {
isTest = true;
}
Expand Down Expand Up @@ -166,6 +171,7 @@ export function prerun(options?: any, init?: boolean) {
if (packageJson) {
prefix = '';
if (packageJson.xplat) {
usingXplatWorkspace = true;
const xplatSettings = packageJson.xplat; //<IXplatSettings>packageJson.xplat;
// use persisted xplat settings
prefix = xplatSettings.prefix || npmScope; // (if not prefix, default to npmScope)
Expand Down
Loading

0 comments on commit 38418ae

Please sign in to comment.