diff --git a/packages/nx/src/generators/application/application.spec.ts b/packages/nx/src/generators/application/application.spec.ts index 35240a7cc..266705e2a 100644 --- a/packages/nx/src/generators/application/application.spec.ts +++ b/packages/nx/src/generators/application/application.spec.ts @@ -11,15 +11,24 @@ 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(); @@ -27,7 +36,7 @@ describe('app', () => { }); 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(); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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(); diff --git a/packages/nx/src/generators/application/application.ts b/packages/nx/src/generators/application/application.ts index dce97dacf..eeaecf218 100644 --- a/packages/nx/src/generators/application/application.ts +++ b/packages/nx/src/generators/application/application.ts @@ -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)) { @@ -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') { @@ -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, diff --git a/packages/nx/src/generators/application/schema.d.ts b/packages/nx/src/generators/application/schema.d.ts index fd26e6b8d..e6a07bff2 100644 --- a/packages/nx/src/generators/application/schema.d.ts +++ b/packages/nx/src/generators/application/schema.d.ts @@ -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; diff --git a/packages/nx/src/generators/application/schema.json b/packages/nx/src/generators/application/schema.json index 678776402..9d4f9f2a2 100644 --- a/packages/nx/src/generators/application/schema.json +++ b/packages/nx/src/generators/application/schema.json @@ -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.", diff --git a/packages/nx/src/utils/general.ts b/packages/nx/src/utils/general.ts index e7736d56d..d772659f5 100644 --- a/packages/nx/src/utils/general.ts +++ b/packages/nx/src/utils/general.ts @@ -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 = ''; @@ -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() { diff --git a/packages/nx/src/utils/helpers.ts b/packages/nx/src/utils/helpers.ts index 205a3729f..f44f56fd9 100644 --- a/packages/nx/src/utils/helpers.ts +++ b/packages/nx/src/utils/helpers.ts @@ -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; + } } /** @@ -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); @@ -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) { @@ -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) {