diff --git a/bin/templates/cordova/handler.js b/bin/templates/cordova/handler.js index 7a382df9..40ede40c 100644 --- a/bin/templates/cordova/handler.js +++ b/bin/templates/cordova/handler.js @@ -121,12 +121,10 @@ module.exports = { install: (asset, plugin_dir, wwwDest) => { const src = path.join(plugin_dir, asset.src); const dest = path.join(wwwDest, asset.target); + const destDir = path.parse(dest).dir; - if (fs.statSync(src).isDirectory()) { - fs.copySync(src + '/*', dest); - } else { - fs.copySync(src, dest); - } + if (destDir) fs.ensureDirSync(destDir); + fs.copySync(src, dest); }, uninstall: (asset, wwwDest, plugin_id) => { fs.removeSync(path.join(wwwDest, asset.target)); diff --git a/tests/spec/unit/handler.spec.js b/tests/spec/unit/handler.spec.js index 1babd1a0..7686bc3f 100644 --- a/tests/spec/unit/handler.spec.js +++ b/tests/spec/unit/handler.spec.js @@ -330,37 +330,46 @@ describe('Handler export', () => { const wwwDest = 'dest'; describe('Install', () => { - it('if src is a directory, should be called with cp, -Rf', () => { - const asset = { itemType: 'asset', src: 'someSrc/ServiceWorker.js', target: 'ServiceWorker.js' }; + it('should copySync with a directory path.', () => { + const asset = { + itemType: 'asset', + src: 'someSrc/ServiceWorker.js', + target: 'ServiceWorker.js' + }; // Spies - const copySyncSpy = jasmine.createSpy('copySync').and.returnValue('-Rf'); - const fsstatMock = { isDirectory: () => true }; - const statSyncSpy = jasmine.createSpy('statSync').and.returnValue(fsstatMock); + const copySyncSpy = jasmine.createSpy('copySync'); + const ensureDirSyncSpy = jasmine.createSpy('ensureDirSync').and.returnValue(true); + handler.__set__('fs', { copySync: copySyncSpy, - statSync: statSyncSpy + ensureDirSync: ensureDirSyncSpy }); handler.asset.install(asset, plugin_dir, wwwDest); + expect(ensureDirSyncSpy).toHaveBeenCalled(); expect(copySyncSpy).toHaveBeenCalledWith(jasmine.any(String), path.join('dest', asset.target)); }); - it('if src is not a directory, should be called with cp, -f', () => { - const asset = { itemType: 'asset', src: 'someSrc', target: 'ServiceWorker.js' }; - const cpPath = path.join(plugin_dir, asset.src); + it('should call copySync with a file path.', () => { + const asset = { + itemType: 'asset', + src: 'someSrc/ServiceWorker.js', + target: 'ServiceWorker.js' + }; // Spies - const copySyncSpy = jasmine.createSpy('copySync').and.returnValue('-f'); - const fsstatMock = { isDirectory: () => false }; - const statSyncSpy = jasmine.createSpy('statSync').and.returnValue(fsstatMock); + const copySyncSpy = jasmine.createSpy('copySync'); + const ensureDirSyncSpy = jasmine.createSpy('ensureDirSync'); + handler.__set__('fs', { copySync: copySyncSpy, - statSync: statSyncSpy + ensureDirSync: ensureDirSyncSpy }); handler.asset.install(asset, plugin_dir, wwwDest); - expect(copySyncSpy).toHaveBeenCalledWith(cpPath, path.join('dest', asset.target)); + expect(ensureDirSyncSpy).toHaveBeenCalled(); + expect(copySyncSpy).toHaveBeenCalledWith(jasmine.any(String), path.join('dest', asset.target)); }); });