Skip to content

Commit a9b6abb

Browse files
chore: migrate the generators and clean up the code
1 parent 3bca023 commit a9b6abb

38 files changed

+609
-1041
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ indent_style = space
77
indent_size = 2
88
insert_final_newline = true
99
trim_trailing_whitespace = true
10+
max_line_length = 160
1011

1112
[*.md]
1213
max_line_length = off

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"singleQuote": true,
3-
"printWidth": 300
3+
"printWidth": 160
44
}

e2e/nx-e2e/jest.config.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module.exports = {
22
displayName: 'nx-e2e',
33
preset: '../../jest.preset.js',
4-
globals: {
5-
'ts-jest': {
6-
tsConfig: '<rootDir>/tsconfig.spec.json',
7-
},
8-
},
94
transform: {
10-
'^.+\\.[tj]s$': 'ts-jest',
5+
'^.+\\.[tj]s$': [
6+
'ts-jest',
7+
{
8+
tsConfig: '<rootDir>/tsconfig.spec.json',
9+
},
10+
],
1111
},
1212
moduleFileExtensions: ['ts', 'js', 'html'],
1313
coverageDirectory: '../../coverage/e2e/nx-e2e',

packages/nx/jest.config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
export default {
33
displayName: 'nx',
44
preset: '../../jest.preset.js',
5-
globals: {
6-
'ts-jest': { tsconfig: '<rootDir>/tsconfig.spec.json' },
7-
},
85
transform: {
9-
'^.+\\.[tj]sx?$': 'ts-jest',
6+
'^.+\\.[tj]sx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
107
},
118
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
129
coverageDirectory: '../../coverage/packages/nx',

packages/nx/src/generators/app-resources/app-resources.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { Tree, generateFiles, joinPathFragments } from '@nx/devkit';
2-
import { PluginHelpers, getDefaultTemplateOptions } from '../../utils';
2+
import { getDefaultTemplateOptions } from '../../utils';
33

44
import { Schema as AppResourcesSchema } from './schema';
55

66
export function appResources(tree: Tree, options: AppResourcesSchema) {
77
generateFiles(tree, joinPathFragments(__dirname, 'files'), options.path, {
88
...getDefaultTemplateOptions(tree),
9-
name: options.name || 'App_Resources',
10-
libFolderName: PluginHelpers.getLibFoldername('nativescript'),
9+
name: options.name ?? 'App_Resources',
1110
});
1211
}
1312

packages/nx/src/generators/application/application.ts

Lines changed: 91 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,94 @@
1-
import { Tree, addProjectConfiguration, generateFiles, joinPathFragments, installPackagesTask } from '@nx/devkit';
1+
import {
2+
Tree,
3+
addProjectConfiguration,
4+
generateFiles,
5+
joinPathFragments,
6+
ProjectConfiguration,
7+
runTasksInSerial,
8+
GeneratorCallback,
9+
offsetFromRoot,
10+
formatFiles,
11+
} from '@nx/devkit';
212
import { initGenerator } from '@nx/js';
3-
import { getAppName, getDefaultTemplateOptions, getFrontendFramework, getPrefix, missingArgument, PluginHelpers, prerun, updateNxProjects, updatePackageScripts } from '../../utils';
4-
import { angularVersion, nsAngularVersion, nsWebpackVersion, nsNgToolsVersion, nsCoreVersion, typescriptVersion, rxjsVersion, zonejsVersion, nsIOSRuntimeVersion, nsAndroidRuntimeVersion } from '../../utils/versions';
13+
import {
14+
getBaseName,
15+
getAppNamingConvention,
16+
getDefaultTemplateOptions,
17+
getFrontendFramework,
18+
missingArgument,
19+
preRun,
20+
updatePluginDependencies,
21+
updatePluginSettings,
22+
} from '../../utils';
23+
import {
24+
angularVersion,
25+
nsAngularVersion,
26+
nsWebpackVersion,
27+
nsNgToolsVersion,
28+
nsCoreVersion,
29+
typescriptVersion,
30+
rxjsVersion,
31+
zonejsVersion,
32+
nsIOSRuntimeVersion,
33+
nsAndroidRuntimeVersion,
34+
} from '../../utils/versions';
535
import { appResources } from '../app-resources/app-resources';
6-
import { Schema } from './schema';
36+
import { assertNotUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
37+
import { normalizeOptions } from './normalized-options';
38+
import { NormalizedSchema } from './normalized-schema';
39+
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
40+
import { logShowProjectCommand } from '@nx/devkit/src/utils/log-show-project-command';
41+
import { ApplicationSchema } from './schema';
742

8-
export async function applicationGenerator(tree: Tree, options: Schema) {
9-
if (!options.name) {
10-
throw new Error(missingArgument('name', 'Provide a name for your NativeScript app.', 'nx g @nativescript/nx:app name'));
43+
export async function applicationGenerator(tree: Tree, options: NormalizedSchema) {
44+
assertNotUsingTsSolutionSetup(tree, 'nativescript', 'application');
45+
if (!options.directory) {
46+
throw new Error(missingArgument('name', 'Provide a directory for your NativeScript app.', 'nx g @nativescript/nx:app <directory>'));
1147
}
48+
const commonOptions = preRun(tree, options, true);
49+
options = { ...options, ...getAppNamingConvention(options, 'nativescript') };
1250

13-
prerun(tree, options, true);
14-
PluginHelpers.applyAppNamingConvention(tree, options, 'nativescript');
51+
options = await normalizeOptions(tree, options);
1552

16-
await initGenerator(tree, {
17-
skipFormat: true,
18-
});
19-
addAppFiles(tree, options, options.name);
53+
const tasks: GeneratorCallback[] = [];
54+
55+
tasks.push(
56+
await initGenerator(tree, {
57+
skipFormat: true,
58+
})
59+
);
60+
61+
addBuildTargetDefaults(tree, options.buildExecutor);
62+
addProjectConfiguration(tree, options.name, getAppProjectConfiguration(options));
63+
64+
createAppFiles(tree, options);
2065
// add extra files per options
2166
if (options.routing && ['angular'].includes(options.framework)) {
22-
addAppFiles(tree, options, options.name, 'routing');
67+
createAppFiles(tree, options, 'routing');
2368
}
2469
// add app resources
2570
appResources(tree, {
26-
path: `apps/${options.directory ? options.directory + '/' : ''}${options.name}`,
71+
path: options.projectRoot,
2772
});
28-
PluginHelpers.updateRootDeps(tree, options);
29-
// PluginHelpers.updatePrettierIgnore(),
30-
// PluginHelpers.addPackageInstallTask(tree, options);
3173

32-
const directory = options.directory ? `${options.directory}/` : '';
33-
const appPath = `apps/${directory}${options.name}`;
34-
let frontendFrameworkConfig: any = {};
74+
updatePluginSettings(tree, options);
75+
tasks.push(updatePluginDependencies(tree, options));
76+
77+
if (!options.skipFormat) {
78+
await formatFiles(tree);
79+
}
80+
81+
tasks.push(() => logShowProjectCommand(options.name));
82+
83+
return runTasksInSerial(...tasks);
84+
}
85+
86+
function getFrontendFrameworkTargets(options: NormalizedSchema) {
3587
switch (options.framework) {
3688
case 'angular':
37-
frontendFrameworkConfig = {
89+
return {
3890
build: {
39-
executor: '@nativescript/nx:build',
91+
executor: options.buildExecutor,
4092
options: {
4193
noHmr: true,
4294
production: true,
@@ -56,16 +108,20 @@ export async function applicationGenerator(tree: Tree, options: Schema) {
56108
},
57109
},
58110
};
59-
break;
111+
default:
112+
return {};
60113
}
61-
addProjectConfiguration(tree, options.name, {
62-
root: `${appPath}/`,
63-
sourceRoot: `${appPath}/src`,
114+
}
115+
116+
function getAppProjectConfiguration(options: NormalizedSchema): ProjectConfiguration {
117+
return {
118+
root: options.projectRoot,
119+
sourceRoot: options.projectSourceRoot,
64120
projectType: 'application',
65121
targets: {
66-
...frontendFrameworkConfig,
122+
...getFrontendFrameworkTargets(options),
67123
ios: {
68-
executor: '@nativescript/nx:build',
124+
executor: options.buildExecutor,
69125
options: {
70126
platform: 'ios',
71127
},
@@ -79,7 +135,7 @@ export async function applicationGenerator(tree: Tree, options: Schema) {
79135
},
80136
},
81137
android: {
82-
executor: '@nativescript/nx:build',
138+
executor: options.buildExecutor,
83139
options: {
84140
platform: 'android',
85141
},
@@ -93,40 +149,27 @@ export async function applicationGenerator(tree: Tree, options: Schema) {
93149
},
94150
},
95151
clean: {
96-
executor: '@nativescript/nx:build',
152+
executor: options.buildExecutor,
97153
options: {
98154
clean: true,
99155
},
100156
},
101157
lint: {
102-
executor: '@nx/eslint:eslint',
103-
options: {
104-
lintFilePatterns: [`${appPath}/**/*.ts`, `${appPath}/src/**/*.html`],
105-
},
158+
executor: options.lintExecutor,
106159
},
107160
},
108-
});
109-
110-
return () => {
111-
installPackagesTask(tree);
112161
};
113162
}
114163

115-
function addAppFiles(tree: Tree, options: Schema, appName: string, extra: string = '') {
116-
const appname = getAppName(options, 'nativescript');
117-
const directory = options.directory ? `${options.directory}/` : '';
164+
function createAppFiles(tree: Tree, options: ApplicationSchema & Partial<NormalizedSchema>, extra: string = '') {
118165
const framework = options.framework || getFrontendFramework() || 'angular';
119166
if (typeof options.routing === 'undefined') {
120167
// ensure it's at least defined
121168
options.routing = false;
122169
}
123-
generateFiles(tree, joinPathFragments(__dirname, `files${framework ? '_' + framework : ''}${extra ? '_' + extra : ''}`), `apps/${directory}${appName}`, {
124-
...(options as any),
170+
generateFiles(tree, joinPathFragments(__dirname, `files${framework ? '_' + framework : ''}${extra ? '_' + extra : ''}`), options.projectRoot, {
171+
...options,
125172
...getDefaultTemplateOptions(tree),
126-
appname,
127-
directoryAppPath: `${directory}${options.name}`,
128-
pathOffset: directory ? '../../../' : '../../',
129-
libFolderName: PluginHelpers.getLibFoldername('nativescript'),
130173
angularVersion,
131174
nsAngularVersion,
132175
nsCoreVersion,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "extends": "<%= pathOffset %>.eslintrc.json", "ignorePatterns": ["!**/*", "references.d.ts", "node_modules/**/*", "hooks/**/*", "platforms/**/*"], "rules": {} }
1+
{ "extends": "<%= projectRootOffset %>.eslintrc.json", "ignorePatterns": ["!**/*", "references.d.ts", "node_modules/**/*", "hooks/**/*", "platforms/**/*"], "rules": {} }
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
module.exports = {
2-
preset: '<%= pathOffset %>jest.preset.js',
2+
preset: '<%= projectRootOffset %>jest.preset.js',
33
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
4-
globals: {
5-
'ts-jest': {
6-
stringifyContentPathRegex: '\\.(html|svg)$',
7-
astTransformers: ['jest-preset-angular/build/InlineFilesTransformer', 'jest-preset-angular/build/StripStylesTransformer'],
8-
tsconfig: '<rootDir>/tsconfig.spec.json',
9-
},
4+
transform: {
5+
'^.+\\.[tj]s$': [
6+
'ts-jest',
7+
{
8+
stringifyContentPathRegex: '\\.(html|svg)$',
9+
astTransformers: ['jest-preset-angular/build/InlineFilesTransformer', 'jest-preset-angular/build/StripStylesTransformer'],
10+
tsconfig: '<rootDir>/tsconfig.spec.json',
11+
},
12+
],
1013
},
11-
coverageDirectory: '<%= pathOffset %>coverage/apps/<%= directoryAppPath %>',
14+
15+
coverageDirectory: '<%= projectRootOffset %>coverage/<%= projectRoot %>',
1216

1317
displayName: 'nativescript-safety',
14-
snapshotSerializers: ['jest-preset-angular/build/serializers/no-ng-attributes', 'jest-preset-angular/build/serializers/ng-snapshot', 'jest-preset-angular/build/serializers/html-comment'],
18+
snapshotSerializers: [
19+
'jest-preset-angular/build/serializers/no-ng-attributes',
20+
'jest-preset-angular/build/serializers/ng-snapshot',
21+
'jest-preset-angular/build/serializers/html-comment',
22+
],
1523
};

packages/nx/src/generators/application/files_angular/nativescript.config.ts__tmpl__

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NativeScriptConfig } from '@nativescript/core';
22

33
export default {
4-
id: 'org.nativescript.<%= utils.sanitize(appname) %>',
4+
id: 'org.nativescript.<%= utils.sanitize(baseName) %>',
55
appResourcesPath: 'App_Resources',
66
android: {
77
v8Flags: '--expose_gc',

packages/nx/src/generators/application/files_angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "<%= utils.sanitize(appname) %>",
2+
"name": "<%= utils.sanitize(baseName) %>",
33
"main": "./src/main.ts",
44
"license": "SEE LICENSE IN <your-license-filename>",
55
"version": "0.0.0",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/// <reference path="<%= pathOffset %>node_modules/@nativescript/types-ios/index.d.ts" />
2-
/// <reference path="<%= pathOffset %>node_modules/@nativescript/types-android/lib/android-21.d.ts" />
1+
/// <reference path="<%= projectRootOffset %>node_modules/@nativescript/types-ios/index.d.ts" />
2+
/// <reference path="<%= projectRootOffset %>node_modules/@nativescript/types-android/lib/android-21.d.ts" />

packages/nx/src/generators/application/files_angular/tools/postinstall.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const path = require('path');
55
const childProcess = require('child_process');
66

77
// Copy potential hooks from root dependencies to app
8-
const hooksSrc = '<%= pathOffset %>hooks';
8+
const hooksSrc = '<%= projectRootOffset %>hooks';
99
const hooksDest = 'hooks';
1010
console.info(`Copying ${hooksSrc} -> ${hooksDest}`);
1111
try {

packages/nx/src/generators/application/files_angular/tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "<%= pathOffset %>dist/out-tsc",
4+
"outDir": "<%= projectRootOffset %>dist/out-tsc",
55
"types": []
66
},
77
"include": ["./src/environments/environment.*.ts"],

packages/nx/src/generators/application/files_angular/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "<%= pathOffset %>tsconfig.base.json",
2+
"extends": "<%= projectRootOffset %>tsconfig.base.json",
33
"files": [],
44
"include": [],
55
"references": [

packages/nx/src/generators/application/files_angular/tsconfig.spec.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "<%= pathOffset %>dist/out-tsc",
4+
"outDir": "<%= projectRootOffset %>dist/out-tsc",
55
"module": "commonjs",
66
"types": ["jest", "node"]
77
},
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
{ "extends": "<%= pathOffset %>.eslintrc.json", "ignorePatterns": ["!**/*", "references.d.ts", "node_modules/**/*", "hooks/**/*", "platforms/**/*"], "rules": {} }
1+
{
2+
"extends": "<%= projectRootOffset %>.eslintrc.json",
3+
"ignorePatterns": ["!**/*", "references.d.ts", "node_modules/**/*", "hooks/**/*", "platforms/**/*"],
4+
"rules": {}
5+
}

packages/nx/src/generators/application/files_vanilla/nativescript.config.ts__tmpl__

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NativeScriptConfig } from '@nativescript/core';
22

33
export default {
4-
id: 'org.nativescript.<%= utils.sanitize(appname) %>',
4+
id: 'org.nativescript.<%= utils.sanitize(baseName) %>',
55
appResourcesPath: 'App_Resources',
66
android: {
77
v8Flags: '--expose_gc',
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/// <reference path="<%= pathOffset %>node_modules/@nativescript/types-ios/index.d.ts" />
2-
/// <reference path="<%= pathOffset %>node_modules/@nativescript/types-android/lib/android-21.d.ts" />
1+
/// <reference path="<%= projectRootOffset %>node_modules/@nativescript/types-ios/index.d.ts" />
2+
/// <reference path="<%= projectRootOffset %>node_modules/@nativescript/types-android/lib/android-21.d.ts" />

packages/nx/src/generators/application/files_vanilla/tools/postinstall.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fs = require('fs-extra');
44
const path = require('path');
55

66
// Copy potential hooks from root dependencies to app
7-
const hooksSrc = '<%= pathOffset %>hooks';
7+
const hooksSrc = '<%= projectRootOffset %>hooks';
88
const hooksDest = 'hooks';
99
console.info(`Copying ${hooksSrc} -> ${hooksDest}`);
1010
try {

packages/nx/src/generators/application/files_vanilla/tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "<%= pathOffset %>dist/out-tsc",
4+
"outDir": "<%= projectRootOffset %>dist/out-tsc",
55
"types": []
66
},
77
"files": ["./references.d.ts", "./src/main.ts"]

0 commit comments

Comments
 (0)