Skip to content

Commit

Permalink
feat: add 'platform' field to lib 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 25, 2022
1 parent 3536b9c commit e578084
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
31 changes: 27 additions & 4 deletions packages/nx/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('lib', () => {

describe('not nested', () => {
it('should update workspace.json', async () => {
await library(tree, { name: 'myLib' });
await library(tree, { name: 'myLib', platform: 'nativescript' });
const libName = `nativescript-my-lib`;

expect(tree.exists(`libs/${libName}/tsconfig.json`)).toBeTruthy();
Expand All @@ -33,8 +33,31 @@ describe('lib', () => {
});
});

it(`should update workspace.json having an omitted 'platform' segment in name`, async () => {
await library(tree, { name: 'myLib', platform: 'none' });
const libName = `my-lib`;

expect(tree.exists(`libs/${libName}/tsconfig.json`)).toBeTruthy();
expect(tree.exists(`libs/${libName}/references.d.ts`)).toBeTruthy();

const tsconfig = readJson(tree, `libs/${libName}/tsconfig.json`);
expect(tsconfig.files).toEqual(['./references.d.ts']);
expect(tsconfig.include).toEqual(['**/*.ts']);

const projectConfig = readProjectConfiguration(tree, libName);
expect(projectConfig.root).toEqual(`libs/${libName}`);
expect(projectConfig.targets.build).toBeUndefined();
expect(projectConfig.targets.lint).toEqual({
executor: '@nrwl/linter:eslint',
options: {
lintFilePatterns: [`libs/${libName}/**/*.ts`],
},
outputs: ['{options.outputFile}'],
});
});

it('groupByName: should update workspace.json', async () => {
await library(tree, { name: 'myLib', groupByName: true });
await library(tree, { name: 'myLib', groupByName: true, platform: 'nativescript' });
const libName = `my-lib-nativescript`;
const projectConfig = readProjectConfiguration(tree, libName);

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

it('should update root tsconfig.json', async () => {
await library(tree, { name: 'myLib' });
await library(tree, { name: 'myLib', platform: 'nativescript' });
const libName = `nativescript-my-lib`;
const tsconfigJson = readJson(tree, '/tsconfig.base.json');
expect(tsconfigJson.compilerOptions.paths[`@proj/${libName}`]).toEqual([`libs/${libName}/src/index.ts`]);
Expand All @@ -62,7 +85,7 @@ describe('lib', () => {
return json;
});

await library(tree, { name: 'myLib' });
await library(tree, { name: 'myLib', platform: 'nativescript' });
const libName = `nativescript-my-lib`;
const tsconfigJson = readJson(tree, '/tsconfig.base.json');
expect(tsconfigJson.compilerOptions.paths[`@proj/${libName}`]).toEqual([`libs/${libName}/src/index.ts`]);
Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { libraryGenerator } from '@nrwl/workspace';

export function library(tree: Tree, options: any) {

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

prerun(tree, options, true);
PluginHelpers.applyAppNamingConvention(tree, options, 'nativescript'),
PluginHelpers.applyAppNamingConvention(tree, options, options.platform),
libraryGenerator(tree, options)

// add extra files
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/generators/library/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { Linter } from '@nrwl/workspace/src/utils/lint';

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;
directory?: string;
skipTsConfig: boolean;
skipFormat: boolean;
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"x-prompt": "What name would you like to use for the library?",
"pattern": "^[a-zA-Z]{1}.*$"
},
"platform": {
"description": "An arbitrary value to be assigned to this library's folder name.",
"type": "string",
"default": "nativescript",
"x-prompt": "What platform does this NativeScript lib belong to?"
},
"directory": {
"type": "string",
"description": "A directory where the lib is placed"
Expand Down

0 comments on commit e578084

Please sign in to comment.