diff --git a/src/copy.spec.ts b/src/copy.spec.ts new file mode 100644 index 00000000..a6bbae0a --- /dev/null +++ b/src/copy.spec.ts @@ -0,0 +1,54 @@ +import * as copy from './copy'; + +import * as config from './util/config'; + +describe('copy task', () => { + describe('copyConfigToWatchConfig', () => { + it('should do something', () => { + // arrange + const context = { }; + const configFile = 'configFile'; + const sampleConfig: copy.CopyConfig = { + copyAssets: { + src: ['{{SRC}}/assets/**/*'], + dest: '{{WWW}}/assets' + }, + copyIndexContent: { + src: ['{{SRC}}/index.html', '{{SRC}}/manifest.json', '{{SRC}}/service-worker.js'], + dest: '{{WWW}}' + }, + copyFonts: { + src: ['{{ROOT}}/node_modules/ionicons/dist/fonts/**/*', '{{ROOT}}/node_modules/ionic-angular/fonts/**/*'], + dest: '{{WWW}}/assets/fonts' + }, + copyPolyfills: { + src: ['{{ROOT}}/node_modules/ionic-angular/polyfills/polyfills.js'], + dest: '{{BUILD}}' + }, + someOtherOption: { + src: ['{{ROOT}}/whatever'], + dest: '{{BUILD}}' + } + }; + let combinedSource: string[] = []; + Object.keys(sampleConfig).forEach(entry => combinedSource = combinedSource.concat(sampleConfig[entry].src)); + + spyOn(config, config.generateContext.name).and.returnValue(context); + spyOn(config, config.getUserConfigFile.name).and.returnValue(configFile); + spyOn(config, config.fillConfigDefaults.name).and.returnValue(sampleConfig); + + // act + const result = copy.copyConfigToWatchConfig(null); + + // assert + expect(config.generateContext).toHaveBeenCalledWith(null); + expect(config.getUserConfigFile).toHaveBeenCalledWith(context, copy.taskInfo, ''); + expect(config.fillConfigDefaults).toHaveBeenCalledWith(configFile, copy.taskInfo.defaultConfigFile); + (result.paths as string[]).forEach(glob => { + expect(combinedSource.indexOf(glob)).not.toEqual(-1); + }); + expect(result.callback).toBeDefined(); + expect(result.options).toBeDefined(); + }); + }); +}); \ No newline at end of file diff --git a/src/copy.ts b/src/copy.ts index fc2841e4..f5d51060 100644 --- a/src/copy.ts +++ b/src/copy.ts @@ -1,7 +1,7 @@ import { mkdirpSync } from 'fs-extra'; import { dirname as pathDirname, join as pathJoin, relative as pathRelative, resolve as pathResolve } from 'path'; import { Logger } from './logger/logger'; -import { fillConfigDefaults, getUserConfigFile, replacePathVars } from './util/config'; +import { fillConfigDefaults, generateContext, getUserConfigFile, replacePathVars } from './util/config'; import { emit, EventType } from './util/events'; import { generateGlobTasks, globAll, GlobObject, GlobResult } from './util/glob-util'; import { copyFileAsync, rimRafAsync, unlinkAsync } from './util/helpers'; @@ -241,6 +241,9 @@ function cleanConfigContent(dictionaryKeys: string[], copyConfig: CopyConfig, co } export function copyConfigToWatchConfig(context: BuildContext): Watcher { + if (!context) { + context = generateContext(context); + } const configFile = getUserConfigFile(context, taskInfo, ''); const copyConfig: CopyConfig = fillConfigDefaults(configFile, taskInfo.defaultConfigFile); let results: GlobObject[] = []; @@ -274,7 +277,7 @@ interface CopySrcToDestResult { errorMessage: string; } -const taskInfo: TaskInfo = { +export const taskInfo: TaskInfo = { fullArg: '--copy', shortArg: '-y', envVar: 'IONIC_COPY',