Skip to content

Commit

Permalink
feat(nativescript): support configuration for environment handling (#216
Browse files Browse the repository at this point in the history
)
  • Loading branch information
NathanWalker authored Oct 7, 2020
1 parent a3c7d5a commit 0ae270e
Show file tree
Hide file tree
Showing 20 changed files with 204 additions and 86 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
"@angular/service-worker": "~10.1.0",
"@angular/upgrade": "~10.1.0",
"@cypress/webpack-preprocessor": "^5.4.2",
"@nrwl/angular": "~10.2.0",
"@nrwl/express": "~10.2.0",
"@nrwl/nest": "~10.2.0",
"@nrwl/node": "~10.2.0",
"@nrwl/react": "~10.2.0",
"@nrwl/web": "~10.2.0",
"@nrwl/workspace": "~10.2.0",
"@nrwl/angular": "~10.3.0",
"@nrwl/express": "~10.3.0",
"@nrwl/nest": "~10.3.0",
"@nrwl/node": "~10.3.0",
"@nrwl/react": "~10.3.0",
"@nrwl/web": "~10.3.0",
"@nrwl/workspace": "~10.3.0",
"@schematics/angular": "~10.1.0",
"@types/express": "~4.17.0",
"@types/jasmine": "~3.5.11",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"migrations": "./migrations.json"
},
"dependencies": {
"@nrwl/angular": "^10.2.0"
"@nrwl/angular": "^10.3.0"
},
"peerDependencies": {
"@nstudio/xplat": "*"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const environment = {
production: true
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const environment = {
production: false,
api_url: 'http://127.0.0.1:4000',
baseRoutePath: ''
production: false
};
2 changes: 1 addition & 1 deletion packages/angular/src/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const xplatVersion = '*';
export const nxVersion = '~10.2.0';
export const nxVersion = '~10.3.0';
export const angularVersion = '~10.1.0';
export const angularDevkitVersion = '~0.1001.0';
export const ngxTranslateVersion = '~13.0.0';
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-angular/src/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const xplatVersion = '*';
export const nxVersion = '~10.2.0';
export const nxVersion = '~10.3.0';
27 changes: 6 additions & 21 deletions packages/focus/src/schematics/mode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
supportedPlatformsWithNx,
getJsonFromFile,
isXplatWorkspace,
getAppPaths,
} from '@nstudio/xplat-utils';
import { Schema } from './schema';
import { FocusHelpers } from '../../utils';
Expand Down Expand Up @@ -43,14 +44,12 @@ export default function (options: Schema) {
// update IDE settings
(tree: Tree, context: SchematicContext) => {
// apps
const appsDir = tree.getDir('apps');
const appPaths = getAppPaths(tree);
const allApps = [];
if (appsDir && appsDir.subdirs) {
const appFolders = appsDir.subdirs;
for (const dir of appFolders) {
allApps.push(`**${appsDir.path}/${dir}`);
}
for (const appPath of appPaths) {
allApps.push(`**${appPath}`);
}
// console.log('allApps:', allApps);

// libs
const libsDir = tree.getDir('libs');
Expand Down Expand Up @@ -81,21 +80,7 @@ export default function (options: Schema) {
// 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}`;
}
focusOnApps[i] = `**/apps/${projectName}`;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/// <reference path="../../node_modules/@nativescript/types/index.d.ts" />
/// <reference path="<%= pathOffset %>node_modules/@nativescript/types/index.d.ts" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["<%= pathOffset %>libs/core/environments/*.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,6 @@ describe('app', () => {

const packageFile = getFileContent(tree, 'package.json');
// console.log(packageFile)
expect(
packageFile.indexOf('start.foo.nativescript.ios')
).toBeGreaterThanOrEqual(0);
});

describe('useXplat false', () => {
Expand Down
46 changes: 32 additions & 14 deletions packages/nativescript-angular/src/schematics/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getDefaultTemplateOptions,
XplatHelpers,
updateTsConfig,
output,
} from '@nstudio/xplat';
import {
prerun,
Expand Down Expand Up @@ -125,15 +126,9 @@ export default function (options: Schema) {
scripts[
`clean`
] = `npx rimraf -- hooks node_modules package-lock.json && npm i`;
scripts[
`start.${platformApp}.ios`
] = `cd apps/${directory}${options.name} && ns debug ios --no-hmr --emulator`;
scripts[
`start.${platformApp}.android`
] = `cd apps/${directory}${options.name} && ns debug android --no-hmr --emulator`;
scripts[
`clean.${platformApp}`
] = `cd apps/${directory}${options.name} && npx rimraf -- hooks node_modules platforms package-lock.json && npm i && npx rimraf -- package-lock.json`;
] = `cd apps/${directory}${options.name} && ns clean && npm i && npx rimraf -- package-lock.json`;
return updatePackageScripts(tree, scripts);
},
(tree: Tree, context: SchematicContext) => {
Expand All @@ -151,30 +146,43 @@ export default function (options: Schema) {
},
},
architect: {
default: {
builder: '@nrwl/workspace:run-commands',
configurations: {
production: {
fileReplacements: [
{
replace: 'libs/core/environments/environment.ts',
with: 'libs/core/environments/environment.prod.ts',
},
],
},
},
},
ios: {
builder: '@nrwl/workspace:run-commands',
options: {
commands: ['ns debug ios --no-hmr'],
commands: [
`ns debug ios --no-hmr --env.configuration={args.configuration} --env.projectName=${options.name}`,
],
cwd: `apps/${directory}${options.name}`,
parallel: false,
},
},
android: {
builder: '@nrwl/workspace:run-commands',
options: {
commands: ['ns debug android --no-hmr'],
commands: [
`ns debug android --no-hmr --env.configuration={args.configuration} --env.projectName=${options.name}`,
],
cwd: `apps/${directory}${options.name}`,
parallel: false,
},
},
clean: {
builder: '@nrwl/workspace:run-commands',
options: {
commands: [
'npx rimraf hooks node_modules platforms package-lock.json',
'npm i',
'npx rimraf package-lock.json',
],
commands: ['ns clean', 'npm i', 'npx rimraf package-lock.json'],
cwd: `apps/${directory}${options.name}`,
parallel: false,
},
Expand All @@ -190,6 +198,16 @@ export default function (options: Schema) {
};
return updateNxProjects(tree, projects);
},
(tree: Tree) => {
output.log({
title: 'You will be able to run your app with:',
bodyLines: [
`nx run ${options.name}:ios`,
` `,
`nx run ${options.name}:android`,
],
});
},
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ describe('app', () => {

const packageFile = getFileContent(tree, 'package.json');
// console.log(packageFile)
expect(
packageFile.indexOf('start.foo.nativescript.ios')
).toBeGreaterThanOrEqual(0);
});

it('should create all files for app in directory', async () => {
Expand Down
21 changes: 1 addition & 20 deletions packages/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,7 @@
"migrations": "./migrations/migrations.json"
},
"dependencies": {
"@nrwl/angular": "~10.2.0",
"@nrwl/angular": "~10.3.0",
"@nstudio/xplat-utils": "*"
},
"devDependencies": {
"@angular-devkit/core": "~10.1.0",
"@angular-devkit/schematics": "~10.1.0",
"@nrwl/workspace": "~10.1.0",
"@nstudio/angular": "*",
"@nstudio/xplat": "*",
"cosmiconfig": "~6.0.0",
"fs-extra": "~9.0.0",
"ignore": "~5.1.8",
"npm-run-all": "~4.1.5",
"opn": "~6.0.0",
"rxjs": "~6.6.0",
"semver": "~7.3.2",
"strip-json-comments": "~3.1.1",
"tmp": "~0.2.1",
"yargs-parser": "~18.1.3",
"yargs": "~15.4.1",
"prettier": "~2.0.5"
}
}
2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"migrations": "./migrations.json"
},
"dependencies": {
"@nrwl/web": "^10.2.0"
"@nrwl/web": "^10.3.0"
},
"peerDependencies": {
"@nstudio/xplat": "*"
Expand Down
3 changes: 0 additions & 3 deletions packages/xplat-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
"main": "index.js",
"types": "index.d.ts",
"dependencies": {
"@angular-devkit/core": "~10.1.0",
"@angular-devkit/schematics": "~10.1.0",
"@nrwl/workspace": "~10.2.0",
"chalk": "~4.1.0",
"strip-json-comments": "~3.1.1"
},
Expand Down
74 changes: 74 additions & 0 deletions packages/xplat-utils/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,80 @@ export function checkRootTsConfig(tree: Tree) {
return tree;
}

export function getAppPaths(
tree: Tree,
type?: PlatformTypes // by default, will return all app paths (considering folder nesting)
): Array<string> {
const appsDir = tree.getDir('apps');
const appPaths: Array<string> = [];

const checkIfPlatform = (dirPath: string) => {
let packagePath = `${dirPath}/package.json`;
// check for platform via it's package (web is only app type that doesn't have a package)
switch (type) {
case 'nativescript':
if (tree.exists(`${dirPath}/nativescript.config.ts`)) {
appPaths.push(dirPath);
}
break;
case 'ionic':
if (tree.exists(packagePath)) {
const packageData = getJsonFromFile(tree, packagePath);
if (
packageData.dependencies &&
packageData.dependencies['@capacitor/core']
) {
appPaths.push(dirPath);
}
}
break;
case 'electron':
let embeddedPackage = `${dirPath}/src/package.json`;
if (tree.exists(embeddedPackage)) {
const packageData = getJsonFromFile(tree, embeddedPackage);
if (packageData.build && packageData.build.appId) {
appPaths.push(dirPath);
}
}
break;
case 'web':
if (!tree.exists(packagePath)) {
// web app when no package is present
appPaths.push(dirPath);
}
break;
}
};
for (const dir of appsDir.subdirs) {
let tsconfigPath = `${appsDir.path}/${dir}/tsconfig.json`;

if (tree.exists(tsconfigPath)) {
// this is an app directory
if (type) {
checkIfPlatform(`${appsDir.path}/${dir}`);
} else {
appPaths.push(`${appsDir.path}/${dir}`);
}
} else {
// apps in nested folders
const subDirs = tree.getDir(`${appsDir.path}/${dir}`).subdirs;
for (const subDir of subDirs) {
tsconfigPath = `${appsDir.path}/${dir}/${subDir}/tsconfig.json`;
if (tree.exists(tsconfigPath)) {
// this is an app directory
if (type) {
checkIfPlatform(`${appsDir.path}/${dir}/${subDir}`);
} else {
appPaths.push(`${appsDir.path}/${dir}/${subDir}`);
}
}
}
}
}

return appPaths;
}

export function prerun(options?: any, init?: boolean) {
return (tree: Tree) => {
const nxJson = getNxWorkspaceConfig(tree);
Expand Down
5 changes: 5 additions & 0 deletions packages/xplat/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"version": "10.2.0",
"description": "Migrate workspace to 10.2.0",
"factory": "./migrations/update-10-2-0/update-10-2-0"
},
"update-to-10.3.0": {
"version": "10.3.0",
"description": "Migrate workspace to 10.3.0",
"factory": "./migrations/update-10-3-0/update-10-3-0"
}
}
}
Loading

0 comments on commit 0ae270e

Please sign in to comment.