Skip to content

Commit

Permalink
feat: add 'platform' field to app schematic
Browse files Browse the repository at this point in the history
alternative to discussion in issue NativeScript#6
  • Loading branch information
marckassay committed Feb 24, 2022
1 parent b8e2c11 commit 3536b9c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 31 deletions.
29 changes: 19 additions & 10 deletions packages/nx/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@ describe('app', () => {
appTree = createTreeWithEmptyWorkspace(2);
});

it('should update workspace.json', async () => {
await applicationGenerator(appTree, { name: 'myApp' });
it(`should update workspace.json having a 'platform-name' format`, async () => {
await applicationGenerator(appTree, { name: 'myApp', platform: 'nativescript' });
const config = readProjectConfiguration(appTree, 'nativescript-my-app');
const appPath = 'apps/nativescript-my-app/';

expect(config.root).toEqual('apps/nativescript-my-app/');
expect(config.root).toEqual(appPath);
});

it(`should update workspace.json having a 'name' only format`, async () => {
await applicationGenerator(appTree, { name: 'myApp', platform: 'none' });
const config = readProjectConfiguration(appTree, 'my-app');
const appPath = 'apps/my-app/';

expect(config.root).toEqual(appPath);
});

it('should generate files', async () => {
await applicationGenerator(appTree, { name: 'myApp', framework: 'vanilla' });
await applicationGenerator(appTree, { name: 'myApp', framework: 'vanilla', platform: 'nativescript' });
const appPath = 'apps/nativescript-my-app';
expect(appTree.exists(`${appPath}/src/app-root.xml`)).toBeTruthy();
expect(appTree.exists(`${appPath}/src/main-page.ts`)).toBeTruthy();
checkFiles(appTree, appPath);
});

it('nested in directory: should generate files', async () => {
await applicationGenerator(appTree, { name: 'myApp', directory: 'mobile', framework: 'vanilla' });
await applicationGenerator(appTree, { name: 'myApp', directory: 'mobile', framework: 'vanilla', platform: 'nativescript' });
const appPath = 'apps/mobile/nativescript-my-app';
expect(appTree.exists(`${appPath}/src/app-root.xml`)).toBeTruthy();
expect(appTree.exists(`${appPath}/src/main-page.ts`)).toBeTruthy();
Expand All @@ -36,7 +45,7 @@ describe('app', () => {
});

it('Angular with Routing: should generate files', async () => {
await applicationGenerator(appTree, { name: 'myApp', framework: 'angular', routing: true });
await applicationGenerator(appTree, { name: 'myApp', framework: 'angular', routing: true, platform: 'nativescript' });
const appPath = 'apps/nativescript-my-app';
checkAngularFiles(appTree, appPath);

Expand All @@ -51,7 +60,7 @@ describe('app', () => {
});

it('Angular without Routing: should generate files', async () => {
await applicationGenerator(appTree, { name: 'myApp', framework: 'angular', routing: false });
await applicationGenerator(appTree, { name: 'myApp', framework: 'angular', routing: false, platform: 'nativescript' });
const appPath = 'apps/nativescript-my-app';
checkAngularFiles(appTree, appPath);

Expand All @@ -62,7 +71,7 @@ describe('app', () => {
});

it('Angular nested in directory: should generate files', async () => {
await applicationGenerator(appTree, { name: 'myApp', framework: 'angular', directory: 'mobile', routing: true });
await applicationGenerator(appTree, { name: 'myApp', framework: 'angular', directory: 'mobile', routing: true, platform: 'nativescript' });
const appPath = 'apps/mobile/nativescript-my-app';
checkAngularFiles(appTree, appPath);

Expand All @@ -73,7 +82,7 @@ describe('app', () => {
});

it('should add angular dependencies when framework is angular', async () => {
await applicationGenerator(appTree, { name: 'myApp', framework: 'angular' });
await applicationGenerator(appTree, { name: 'myApp', framework: 'angular', platform: 'nativescript' });
const packageJson = readJson(appTree, `package.json`);

expect(packageJson['dependencies']['@angular/animations']).toEqual(angularVersion);
Expand All @@ -90,7 +99,7 @@ describe('app', () => {
});

it('should not add angular dependencies when framework is not angular', async () => {
await applicationGenerator(appTree, { name: 'myApp', framework: '' });
await applicationGenerator(appTree, { name: 'myApp', framework: '', platform: 'nativescript' });
const packageJson = readJson(appTree, `package.json`);

expect(packageJson['dependencies']['@angular/animations']).toBeFalsy();
Expand Down
8 changes: 5 additions & 3 deletions packages/nx/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export async function applicationGenerator(tree: Tree, options: Schema) {
throw new Error(missingArgument('name', 'Provide a name for your NativeScript app.', 'nx g @nativescript/nx:app name'));
}

options.platform = options.platform === 'none' ? undefined : options.platform;

prerun(tree, options, true);
PluginHelpers.applyAppNamingConvention(tree, options, 'nativescript');
PluginHelpers.applyAppNamingConvention(tree, options, options.platform);
addAppFiles(tree, options, options.name);
// add extra files per options
if (options.routing && ['angular'].includes(options.framework)) {
Expand Down Expand Up @@ -108,7 +110,7 @@ export async function applicationGenerator(tree: Tree, options: Schema) {
}

function addAppFiles(tree: Tree, options: Schema, appName: string, extra: string = '') {
const appname = getAppName(options, 'nativescript');
const appname = getAppName(options, options.platform);
const directory = options.directory ? `${options.directory}/` : '';
const framework = options.framework || getFrontendFramework() || 'angular';
if (typeof options.routing === 'undefined') {
Expand All @@ -121,7 +123,7 @@ function addAppFiles(tree: Tree, options: Schema, appName: string, extra: string
appname,
directoryAppPath: `${directory}${options.name}`,
pathOffset: directory ? '../../../' : '../../',
libFolderName: PluginHelpers.getLibFoldername('nativescript'),
libFolderName: PluginHelpers.getLibFoldername('nativescript'), // unsure why this is needed if adding app files.
angularVersion,
nsAngularVersion,
nsCoreVersion,
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export interface Schema {
name: string;

/**
* Should always be parsed with a string value, defaults to `'nativescript'`. However,
* if value is `'none'` value will be replaced with `undefined` after being parsed.
*/
platform: string | undefined;
framework?: string;
routing?: boolean;
skipFormat?: boolean;
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
]
}
},
"platform": {
"description": "An arbitrary value to be assigned to this application's folder name.",
"type": "string",
"default": "nativescript",
"x-prompt": "What platform does this NativeScript app belong to?"
},
"routing": {
"type": "boolean",
"description": "Use root routing module.",
Expand Down
40 changes: 30 additions & 10 deletions packages/nx/src/utils/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,36 @@ export function getGroupByName() {
return groupByName;
}

export function getAppName(options: any, platform: PlatformTypes) {
return groupByName
? options.name.replace(`-${platform}`, '')
: options.name.replace(`${platform}-`, '');
export function getAppName(options: any, platform?: string) {

if(platform) {
return groupByName
? options.name.replace(`-${platform}`, '')
: options.name.replace(`${platform}-`, '');
} else {
return options.name;
}
}


export function isXplatWorkspace() {
return usingXplatWorkspace;
}

// TODO: this is a duplicate function. see other in helpers.ts
export function applyAppNamingConvention(
options: any,
platform: PlatformTypes
platform?: string
) {
const { name, directory } = getAppNamingConvention(options, platform);
options.name = name;
options.directory = directory;
}

// TODO: this is a duplicate function. see other in helpers.ts
export function getAppNamingConvention(
options: any,
platform: PlatformTypes
platform?: string
) {
let name = '';
let directory = '';
Expand All @@ -95,11 +103,23 @@ export function getAppNamingConvention(
};
}

export function getPlatformName(name: string, platform: PlatformTypes) {
// TODO: this is a duplicate function. see other in helpers.ts
/**
* Returns a sanitized name value with the platform (if platform param is defined).
*
* @example (app, nativescript) => nativescript-app or app-nativescript
* @example (app, undefined) => app
* @param name
* @param platform
*/
export function getPlatformName(name: string, platform?: string) {
const nameSanitized = toFileName(name);
return getGroupByName()
? `${nameSanitized}-${platform}`
: `${platform}-${nameSanitized}`;

if(platform) {
return getGroupByName() ? `${nameSanitized}-${platform}` : `${platform}-${nameSanitized}`;
} else {
return nameSanitized;
}
}

export function getDefaultTemplateOptions() {
Expand Down
33 changes: 25 additions & 8 deletions packages/nx/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,23 @@ export namespace PluginHelpers {
return pluginSettings;
}

// TODO: this is a duplicate function. see other in general.ts
/**
* Returns a name with the platform.
* Returns a sanitized name value with the platform (if platform param is defined).
*
* @example (app, nativescript) => nativescript-app or app-nativescript
* @example (app, undefined) => app
* @param name
* @param platform
*/
export function getPlatformName(name: string, platform: PlatformTypes) {
export function getPlatformName(name: string, platform?: string) {
const nameSanitized = toFileName(name);
return getGroupByName() ? `${nameSanitized}-${platform}` : `${platform}-${nameSanitized}`;

if(platform) {
return getGroupByName() ? `${nameSanitized}-${platform}` : `${platform}-${nameSanitized}`;
} else {
return nameSanitized;
}
}

/**
Expand All @@ -183,7 +190,7 @@ export namespace PluginHelpers {
* @param platform
* @param framework
*/
export function getLibFoldername(platform: PlatformTypes, framework?: FrameworkTypes) {
export function getLibFoldername(platform?: string, framework?: FrameworkTypes) {
const frontendFramework = getFrontendFramework();
// console.log('getLibFoldername frontendFramework:', frontendFramework);
// console.log('framework:', framework);
Expand All @@ -192,9 +199,17 @@ export namespace PluginHelpers {
// user had a default framework set
// however an explicit framework is being requested
// if they differ, use suffix to distinguish
frameworkSuffix = `-${framework}`;
frameworkSuffix = framework;
}

// TODO: unsure if when platform is undefined, will this suffice.
if(platform) {
return `${platform}-${frameworkSuffix}`;
} else if (frameworkSuffix) {
return frameworkSuffix;
} else {
return '';
}
return `${platform}${frameworkSuffix}`;
}

export function getExternalChainsForGenerator(options: Schema, generator: string, packagesToRunXplat: Array<string>) {
Expand Down Expand Up @@ -350,14 +365,16 @@ export namespace PluginHelpers {
return externalChains;
}

export function applyAppNamingConvention(tree: Tree, options: any, platform: PlatformTypes) {
// TODO: this is a duplicate function. see other in general.ts
export function applyAppNamingConvention(tree: Tree, options: any, platform?: string) {
const { name, directory } = getAppNamingConvention(options, platform);
options.name = name;
options.directory = directory;
// console.log('applyAppNamingConvention:', options);
}

export function getAppNamingConvention(options: any, platform: PlatformTypes) {
// TODO: this is a duplicate function. see other in general.ts
export function getAppNamingConvention(options: any, platform?: string) {
let name = '';
let directory = '';
if (options.directory) {
Expand Down

0 comments on commit 3536b9c

Please sign in to comment.