How do I get yeoman to auto overwrite files? - yeoman

I have a custom generator and am writing some tests for it. Before the app.run() call I already have a app.options['skip-install'] = true to prevent npm from running. But I need it to auto-overwrite files too.
Part way through the install I get a [?] Overwrite client/file.js? (Ynaxdh) and need it to auto-answer it for me.
I have tried app.options.force = true but that doesn't seem to do anything.
I'm running in app install with this:
function installApp(obj, opts, done) {
helpers.mockPrompt(obj.app, opts);
obj.app.options['skip-install'] = true;
obj.app.run({}, function () {
async.series([
function (cb) {
if (opts._extras.addPage) {
installAppPage(obj, cb);
} else {
cb();
}
}
], done);
});
}
Then I want to run a sub-generator with this:
function installAppPage(obj, done) {
helpers.mockPrompt(obj.page, {
pageName: 'webPage',
pageType: 'web'
});
obj.page.args = ['--force']; // This isn't working
obj.page.options.force; // This isn't working either
obj.page.run([], function () {
helpers.mockPrompt(obj.page, {
pageName: 'mobilePage',
pageType: 'mobile'
});
obj.page.run({}, function () {
done();
});
});
}
The sub-generator for the page modifies a file. I need to to just overwrite it so I can test it. How do I force it? I can't be prompted while running the tests, it needs to be automated.

I think you're looking for conflicter.force
obj.page.conflicter.force = true;

Related

Debugging Worker thread in electron

I'm testing out worker_thread on an electron application. I'm currently using version 11.0.2.
The code is simple and is working and returning the sample data but I cant seem to step into the code.
Main Process:
import { Worker, isMainThread, workerData } from 'worker_threads';
config.ipcMain.on('entries:search', (evt: any, opts: any) => {
if (isMainThread) {
const pathWorker = path.join(__dirname, '../data/entries_worker.js');
const worker = new Worker(pathWorker, {
workerData: opts.value,
});
worker.on('message', (data) => {
debugger;
const d = 1;
});
worker.on('error', (data) => {
debugger;
const d = 1;
});
worker.on('exit', (data) => {
debugger;
const d = 1;
});
}
});
The worker file code:
import { workerData, parentPort } from 'worker_threads';
debugger;
parentPort.postMessage({ status: 'Done' });
I'm using Visual Studio Code and I do put breakpoints and event the debugger statement but it never seems to break into the worker file.
The message event does receive the response from the script { status: 'Done' } and the exit event returns 0.
Any ideas on how I can stop at the breakpoint in the worker file entries_worker.js?
Update
Found the following link about how it's not available right now. I'm not 100% sure if it has changed
ndb allow debugger worker thread. run in develop env like this:
"electron-dev": "ndb electron ."
When you use worker thread, you can found it easy:
You can also add breakpoints debug your code:

how to combine multiple instances of navigator.serviceWorker.register('sw.js')

for example in my index.html I have a code to detect an update for service worker and the code is like this:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js').then(reg => {
reg.addEventListener('updatefound', () => {
// A wild service worker has appeared in reg.installing!
newWorker = reg.installing;
newWorker.addEventListener('statechange', () => {
// Has network.state changed?
switch (newWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
// new update available
showUpdateBar();
}
// No update available
break;
}
});
});
});
let refreshing;
navigator.serviceWorker.addEventListener('controllerchange', function () {
if (refreshing) return;
window.location.reload();
refreshing = true;
});
}
then in pushManager.js the code is like:
navigator.serviceWorker.register('sw.js')
.then(function (registration) {
messaging.useServiceWorker(registration);
// Request for permission
messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
// TODO(developer): Retrieve an Instance ID token for use with FCM.
messaging.getToken()
.then(function(currentToken) {
if (currentToken) {
console.log('Token: ' + currentToken)
sendTokenToServer(currentToken);
// updateSubscriptionOnServer(currentToken);
} else {
console.log('No Instance ID token available. Request permission to generate one.');
setTokenSentToServer(false);
}
})
......
The pushManagr.js is included in both login/index.html and index.html.
I think that calling navigator.serviceWorker.register at multiple places will have adverse effect.
so how I can combine the two instances into one.
If possible, remove the registration of the serviceworker from pushManager.
If you want the registration instance of the serviceworker,use the api getRegistration()
navigator.serviceWorker.getRegistration(/*scope*/).then(function(registration) {
if(registration){
// Move all your firebase messaging code to here
messaging.useServiceWorker(registration);
}
});

Electron: Minimize the application when launched after computer is restarted

I am using node-auto-launch to launch my application after computer is restarted. This application is only for windows. I want this application by default to be launched minimized as it works in the background. HOw can I achieve this?
let bizAnalystAutoLauncher = new AutoLaunch({
name: 'BizAnalystDesktop'
});
bizAnalystAutoLauncher.enable();
bizAnalystAutoLauncher.isEnabled()
.then(function (isEnabled: boolean) {
if (isEnabled) {
return;
}
bizAnalystAutoLauncher.enable();
})
.catch(function (err: any) {
// handle error
console.log(err);
});
I don't want the application to be hidden. The application icon should be visible in the system tray in the taskbar.
So you want to have some kind of "minimize to tray" behaviour.
Initialize your app the usual way but instead of mainWindow.show() you call mainWindow.minimize() after initializing the mainWindow, then add EventListeners for the mainWiondw's minimize and restore events to hide or show the taskbar icon for your app via mainWindow.setSkipTaskbar():
...
mainWindow.on('restore', () => {
mainWindow.setSkipTaskbar(false)
})
mainWindow.on('minimize', () => {
mainWindow.setSkipTaskbar(true)
})
...
Add a Tray menu like in the documentation but make sure you add a menu item to restore the app window, otherwise you will end up with an app that is not accessible after it is minimized:
...
const trayMenu = Menu.buildFromTemplate([
{
label: 'Show',
click: () => {
mainWindow.restore()
}
},
{
label: 'Quit',
role: 'quit'
}
])
tray.setContextMenu(trayMenu)
...
The way I would do it is I would create a shortcut in the start menu Programs > startup with an argument instead of using node-auto-launch. Then when the app runs check for that argument in process.argv.
So to create a start menu shortcut with an argument of startMinimized you can use this module called windows-shortcuts
require('windows-shortcuts').create(
'%APPDATA%/Microsoft/Windows/Start Menu/Programs/Startup/myApp.lnk', {
target: process.execPath,
args: 'startMinimized',
icon: 'path/to/icon'
}, function (err) {
if (err) {
throw Error(err);
}
}
);
Then you could write some script like this to minimize the window at startup:
var startMinimized = false;
if (process.argv[2] && process.argv[2].indexOf('startMinimized') != -1) {
startMinimized = true;
}
var mainWindow = new BrowserWindow({show: !startMinimized});
if (startMinimized) {
mainWindow.minimize();
}
process.argv is an array of arguments the app starts with. The first one is the .exe path. The second is the squirrel argument.

Capybara or Jasmine?

I need to write a test (in Rails) that just shows a function was called. I am not sure to approach this in Jasmine or in Capybara?
My click event is:
<button class="player_vs_cpu_intro" onclick="playerVsCpu()">Player Vs. CPU's Selection</button>
My function is:
function playerVsCpu() {
alert("Hello World");
//other code is in here as well
}
Also, if I had a function within playerVsCpu();, can I test that as well?
Any advice would be appreciated!
You can use jasmine for that. To check if a function has been called you need to use spyOn().
describe("button click", function() {
var btn;
beforeEach(function() {
btn = $('.player_vs_cpu_intro')[0];
});
it("should call playerVsCpu", function() {
spyOn(window, 'playerVsCpu');
$(btn).click();
expect(window.playerVsCpu).toHaveBeenCalled();
});
});
You can also test if a function has been called inside playerVsCpu(). Let's say you call a function anotherFunction() inside playerVsCpu() and you want to assert that.
describe("playerVsCpu", function() {
it("should call anotherFunction", function() {
spyOn(window, 'anotherFunction');
playerVsCpu();
expect(window.anotherFunction).toHaveBeenCalled();
});
});

Set folder metadata doesn't work on Phonegap 3.3.0 / iOS

I'm trying to use the method setMetadata, using the File plugin, but it seems does not work.
No Success or Fail callback is executed. When I use console.log(entry.setMetadata), it prints the correct method. I use the File plugin to access, create and delete files and folders without problems. Only the setMetadata doesn't work.
Example:
localFileSystem = LocalFileSystem.PERSISTENT;
subFolder = "Backups";
metadataKey = "com.apple.MobileBackup";
metadataValue = 1;
window.requestFileSystem(localFileSystem, 0, function(fileSystem) {
fileSystem.root.getDirectory(subFolder, {create: true, exclusive: false}, function(parent) {
var data = {};
data[metadataKey] = metadataValue;
console.log(data); // OK
console.log(parent); // OK
parent.setMetadata(function() {
console.log("success setting metadata"); // Nothing
}, function() {
console.log("error setting metadata"); // Nothing
}, data);
}, function() {
console.log("error getting dir"); // Nothing, directory is OK
});
}, function(error) {
console.log(error.code); // No error here
});
It was a bug on the File plugin. I checked with the developers on Github:
https://github.com/apache/cordova-plugin-file/pull/39
Just waiting for changes on the Phonegap site.

Resources