Skip to content

Commit

Permalink
fix(generators): sandbox setup and mode for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWalker committed Mar 12, 2019
1 parent 529cd6c commit 77246e8
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { Component } from '@angular/core';

<% if (setupSandbox) { %>import { RouterExtensions } from 'nativescript-angular/router';<% } %>
import { BaseComponent } from '@<%= npmScope %>/core';

@Component({
moduleId: module.id,
selector: '<%= prefix %>-home',
templateUrl: './home.component.html'
})
export class HomeComponent extends BaseComponent {}
export class HomeComponent extends BaseComponent {
<% if (setupSandbox) { %>
constructor(private _routerExt: RouterExtensions) {

}

// for quick sandbox feature creation
goTo(route: string) {
this._routerExt.navigate([route]);
}
<% } %>
}
8 changes: 6 additions & 2 deletions src/app.nativescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ export default function (options: ApplicationOptions) {
(tree: Tree) => {
const scripts = {};
const platformApp = options.name.replace('-', '.');
// standard apps don't have hmr on by default since results can vary
// more reliable to leave off by default for now
// however, sandbox setup due to its simplicity uses hmr by default
let hmr = options.setupSandbox ? ' --hmr' : '';
scripts[`clean`] = `npx rimraf -- hooks node_modules package-lock.json && npm i`;
scripts[`start.${platformApp}.ios`] = `cd apps/${options.name} && tns run ios --emulator --bundle`;
scripts[`start.${platformApp}.android`] = `cd apps/${options.name} && tns run android --emulator --bundle`;
scripts[`start.${platformApp}.ios`] = `cd apps/${options.name} && tns run ios --emulator --bundle${hmr}`;
scripts[`start.${platformApp}.android`] = `cd apps/${options.name} && tns run android --emulator --bundle${hmr}`;
scripts[`clean.${platformApp}`] = `cd apps/${options.name} && npx rimraf -- hooks node_modules platforms package-lock.json && npm i && npx rimraf -- package-lock.json`;
return updatePackageScripts(tree, scripts);
},
Expand Down
160 changes: 82 additions & 78 deletions src/utils/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,11 @@ export function updateIDESettings(
: "/var/local/Code/User/settings.json";
const windowsHome = process.env.APPDATA;
if (windowsHome) {
userSettingsVSCodePath = join(windowsHome, "Code/User/settings.json");
userSettingsVSCodePath = join(windowsHome, "Code", "User", "settings.json");
}
// console.log('userSettingsVSCodePath:',userSettingsVSCodePath);
const isVsCode = fs.existsSync(userSettingsVSCodePath);

let vscodeCreateSettingsNote = `It's possible you don't have a user settings.json yet. If so, open VS Code User settings and save any kind of setting to have it created.`;
// console.log('isVsCode:',isVsCode);
if (isVsCode) {
const userSettings = fs.readFileSync(userSettingsVSCodePath, "UTF-8");
Expand Down Expand Up @@ -1035,90 +1035,94 @@ export function updateIDESettings(
userSettingsVSCodePath,
JSON.stringify(userSettingsJson, null, 2)
);
} else {
console.warn(`Warning: xplat could not read your VS Code settings.json file therefore development mode has not been set. ${vscodeCreateSettingsNote}`);
}
} else {
console.log(`Note to VS Code users: no development mode set. xplat could not find any VS Code settings in the standard location: ${userSettingsVSCodePath} ${vscodeCreateSettingsNote}`)
}

// WebStorm support
let isWebStorm = false;
// list preferences to get correct webstorm prefs file
let preferencesFolder = isMac
? process.env.HOME +
`/Library/Preferences`
: __dirname;
if (windowsHome) {
preferencesFolder = windowsHome;
}
const prefs = fs.readdirSync(preferencesFolder).filter(f => fs.statSync(join(preferencesFolder, f)).isDirectory());
// let preferencesFolder = isMac
// ? process.env.HOME +
// `/Library/Preferences`
// : __dirname;
// if (windowsHome) {
// preferencesFolder = windowsHome;
// }
// const prefs = fs.readdirSync(preferencesFolder).filter(f => fs.statSync(join(preferencesFolder, f)).isDirectory());
// find first one
// TODO: user may have multiple version installed (or at least older versions) so may need to handle if multiples
let webStormPrefFolderName = prefs.find(f => f.indexOf('WebStorm20') > -1);
if (webStormPrefFolderName) {
isWebStorm = true;
webStormPrefFolderName = webStormPrefFolderName.split('/').slice(-1)[0];
// console.log('webStormPrefFolderName:',webStormPrefFolderName);
// let webStormPrefFolderName = prefs.find(f => f.indexOf('WebStorm20') > -1);
// if (webStormPrefFolderName) {
// isWebStorm = true;
// webStormPrefFolderName = webStormPrefFolderName.split('/').slice(-1)[0];
// // console.log('webStormPrefFolderName:',webStormPrefFolderName);

// ensure folders are excluded from project view
let projectViewWebStormPath =
isMac
? process.env.HOME +
`/Library/Preferences/${webStormPrefFolderName}/options/projectView.xml`
: join(__dirname, webStormPrefFolderName, 'config');
if (windowsHome) {
projectViewWebStormPath = join(windowsHome, webStormPrefFolderName, 'config');
}

let projectView = fs.readFileSync(projectViewWebStormPath, "UTF-8");
if (projectView) {
// console.log('projectView:', projectView);
xml2js.parseString(projectView, (err, settings) => {
// console.log(util.inspect(settings, false, null));
if (settings && settings.application && settings.application.component && settings.application.component.length) {
const builder = new xml2js.Builder({ headless: true });

const sharedSettingsIndex = (<Array<any>>settings.application.component).findIndex(c => c.$.name === 'ProjectViewSharedSettings');
if (sharedSettingsIndex > -1) {
const sharedSettings = settings.application.component[sharedSettingsIndex];
if (sharedSettings.option && sharedSettings.option.length) {
const showExcludedFilesIndex = sharedSettings.option.findIndex(o => o.$.name === 'showExcludedFiles');
if (showExcludedFilesIndex > -1) {
settings.application.component[sharedSettingsIndex].option[showExcludedFilesIndex].$.value = `${!isExcluding}`;
} else {
settings.application.component[sharedSettingsIndex].option.push(webStormExcludedViewNode(isExcluding));
}
} else {
settings.application.component[sharedSettingsIndex].option = [
webStormExcludedViewNode(isExcluding)
];
}
settings = builder.buildObject(settings);
} else {
(<Array<any>>settings.application.component).push({
$: 'ProjectViewSharedSettings',
option: [
webStormExcludedViewNode(isExcluding)
]
});
settings = builder.buildObject(settings);
}
} else {
// create projectView.xml
settings = createWebStormProjectView(isExcluding);
}
// modify projectView
// console.log('settings:', settings);
fs.writeFileSync(
projectViewWebStormPath,
settings
);
});
} else {
// create projectView.xml
fs.writeFileSync(
projectViewWebStormPath,
createWebStormProjectView(isExcluding)
);
}
}
// // ensure folders are excluded from project view
// let projectViewWebStormPath =
// isMac
// ? process.env.HOME +
// `/Library/Preferences/${webStormPrefFolderName}/options/projectView.xml`
// : join(__dirname, webStormPrefFolderName, 'config');
// if (windowsHome) {
// projectViewWebStormPath = join(windowsHome, webStormPrefFolderName, 'config');
// }

// let projectView = fs.readFileSync(projectViewWebStormPath, "UTF-8");
// if (projectView) {
// // console.log('projectView:', projectView);
// xml2js.parseString(projectView, (err, settings) => {
// // console.log(util.inspect(settings, false, null));
// if (settings && settings.application && settings.application.component && settings.application.component.length) {
// const builder = new xml2js.Builder({ headless: true });

// const sharedSettingsIndex = (<Array<any>>settings.application.component).findIndex(c => c.$.name === 'ProjectViewSharedSettings');
// if (sharedSettingsIndex > -1) {
// const sharedSettings = settings.application.component[sharedSettingsIndex];
// if (sharedSettings.option && sharedSettings.option.length) {
// const showExcludedFilesIndex = sharedSettings.option.findIndex(o => o.$.name === 'showExcludedFiles');
// if (showExcludedFilesIndex > -1) {
// settings.application.component[sharedSettingsIndex].option[showExcludedFilesIndex].$.value = `${!isExcluding}`;
// } else {
// settings.application.component[sharedSettingsIndex].option.push(webStormExcludedViewNode(isExcluding));
// }
// } else {
// settings.application.component[sharedSettingsIndex].option = [
// webStormExcludedViewNode(isExcluding)
// ];
// }
// settings = builder.buildObject(settings);
// } else {
// (<Array<any>>settings.application.component).push({
// $: 'ProjectViewSharedSettings',
// option: [
// webStormExcludedViewNode(isExcluding)
// ]
// });
// settings = builder.buildObject(settings);
// }
// } else {
// // create projectView.xml
// settings = createWebStormProjectView(isExcluding);
// }
// // modify projectView
// // console.log('settings:', settings);
// fs.writeFileSync(
// projectViewWebStormPath,
// settings
// );
// });
// } else {
// // create projectView.xml
// fs.writeFileSync(
// projectViewWebStormPath,
// createWebStormProjectView(isExcluding)
// );
// }
// }

if (!devMode) {
// only when not specifying a dev mode
Expand Down Expand Up @@ -1168,7 +1172,7 @@ export function updateIDESettings(
};

if (isVsCode) {
const workspaceSettingsPath = join(cwd, ".vscode/settings.json");
const workspaceSettingsPath = join(cwd, ".vscode", "settings.json");
// console.log('workspaceSettingsPath:',workspaceSettingsPath);
let workspaceSettingsJson: any = {};
if (fs.existsSync(workspaceSettingsPath)) {
Expand Down

0 comments on commit 77246e8

Please sign in to comment.