import { replace } from 'lodash'; import { tmpdir } from 'os'; import { join, parse } from 'path'; import { concat, merge } from 'rxjs'; import { distinct, map, mergeMap, toArray } from 'rxjs/operators'; import { v4 } from 'uuid'; import { CreateComponentsCommand } from '../src/commands/create.components.command'; import { CreateLayoutCommand } from '../src/commands/create.layout.command'; import { Options } from '../src/options'; import { rxDeleteDir } from '../src/utils/rx.delete'; import { rxCopyDir } from './../src/utils/rx.copy'; import { expectFileExists } from './utils/expect.utils'; import { getAssetBaseDir } from './utils/test.utils'; describe('create.single.layout', () => { function findFile(aFileName: string, aArray: string[]): string | undefined { return aArray.find(name => parse(name).base === aFileName); } function findFileRegexp(aFileName: RegExp, aArray: string[]): string | undefined { return aArray.find(name => aFileName.test(name)); } it('should work with multiple layout sources', () => { const dir = tmpdir(); const name = v4(); const dstBase = join(dir, name); const assetBase = getAssetBaseDir(); const dstData = join(dir, name, 'data'); const dstSrc = dstBase; const srcSrc = join(assetBase, 'test', 'empty-project'); const srcData = join(assetBase, 'test', 'oslo'); const rxDelete = rxDeleteDir(dstBase); // copy the data const rxData = merge(rxCopyDir(srcSrc, dstSrc), rxCopyDir(srcData, dstData)); const osloSrc = join(dstSrc, 'src', 'app', 'oslo'); const stockholmSrc = join(dstSrc, 'src', 'app', 'stockholm'); // create the oslo components const cmpOpt: Options = { command: CreateComponentsCommand.COMMAND, commandType: CreateComponentsCommand.TYPE, data: dstData, src: osloSrc, verbose: false, override: true, modules: true, flat: true, skipRegistration: true, scss: true, allArgs: [] }; const cmpCmd = new CreateComponentsCommand(cmpOpt); const rxCmpCmd = cmpCmd.process(); // add an override in a different folder const layoutOpt: Options = { command: CreateLayoutCommand.COMMAND, commandType: CreateLayoutCommand.TYPE, name: 'weird name', type: 'Hero video', data: dstData, src: [stockholmSrc, osloSrc], verbose: false, override: true, skipRegistration: true, flat: true, modules: true, scss: true, allArgs: [] }; const layoutCmd = new CreateLayoutCommand(layoutOpt); const rxLayoutCmd = layoutCmd.process() const rxFiles = concat(rxCmpCmd, rxLayoutCmd).pipe( distinct(), mergeMap(expectFileExists), map(file => replace(file, /\\/g, '/')), toArray(), map(files => { // make sure we have layouts expect(findFileRegexp(/\/app\/oslo\/layouts.ts$/g, files)).toBeDefined(); expect(findFileRegexp(/\/app\/oslo\/layouts.exports.ts$/g, files)).toBeDefined(); expect(findFileRegexp(/\/app\/stockholm\/layouts.ts$/g, files)).toBeDefined(); expect(findFileRegexp(/\/app\/stockholm\/layouts.exports.ts$/g, files)).toBeDefined(); // make sure we have modules expect(findFileRegexp(/\/app\/oslo\/modules.ts$/g, files)).toBeDefined(); expect(findFileRegexp(/\/app\/oslo\/modules.exports.ts$/g, files)).toBeDefined(); expect(findFileRegexp(/\/app\/stockholm\/modules.ts$/g, files)).toBeDefined(); expect(findFileRegexp(/\/app\/stockholm\/modules.exports.ts$/g, files)).toBeDefined(); // test that we have the desired layout at the correct location expect(findFileRegexp(/\/app\/stockholm\/layouts\/weird-name\/weirdNameLayout.ts$/g, files)).toBeDefined(); expect(findFileRegexp(/\/app\/oslo\/layouts\/hero-video\/heroVideoLayout.ts$/g, files)).toBeDefined(); })); const rxTotal = concat(rxDelete, rxData, rxFiles, rxDelete); return rxTotal.toPromise(); }); it('should create a layout selecting the name automatically', () => { const dir = tmpdir(); const name = v4(); const dstBase = join(dir, name); const assetBase = getAssetBaseDir(); const dstData = join(dir, name, 'data'); const dstSrc = dstBase; const srcSrc = join(assetBase, 'test', 'empty-project'); const srcData = join(assetBase, 'test', 'oslo'); const rxDelete = rxDeleteDir(dstBase); // copy the data const rxData = merge(rxCopyDir(srcSrc, dstSrc), rxCopyDir(srcData, dstData)); // create layouts on the result const layoutOpt: Options = { command: CreateLayoutCommand.COMMAND, commandType: CreateLayoutCommand.TYPE, type: 'Hero video', data: dstData, src: join(dstSrc, 'src', 'app'), verbose: false, override: true, modules: true, skipRegistration: false, scss: true, allArgs: [] }; const layoutCmd = new CreateLayoutCommand(layoutOpt); const rxLayoutCmd = layoutCmd.process().pipe( mergeMap(expectFileExists), toArray(), map(files => { expect(findFile('layouts.ts', files)).toBeDefined(); expect(findFile('layouts.exports.ts', files)).toBeDefined(); // layout files expect(findFile('heroVideoLayout.ts', files)).toBeDefined(); expect(findFile('heroVideoLayout.scss', files)).toBeDefined(); expect(findFile('heroVideoLayout.html', files)).toBeDefined(); // component files expect(findFile('abstractHeroVideoComponent.ts', files)).toBeDefined(); expect(findFile('typeHeroVideoComponent.scss', files)).toBeDefined(); expect(findFile('typeHeroVideoComponent.ts', files)).toBeDefined(); expect(findFile('typeHeroVideoComponent.html', files)).toBeDefined(); })); const rxTotal = concat(rxDelete, rxData, rxLayoutCmd, rxDelete); return rxTotal.toPromise(); }); it('should create a layout given a predfined name', () => { const dir = tmpdir(); const name = v4(); const dstBase = join(dir, name); const assetBase = getAssetBaseDir(); const dstData = join(dir, name, 'data'); const dstSrc = dstBase; const srcSrc = join(assetBase, 'test', 'empty-project'); const srcData = join(assetBase, 'test', 'oslo'); const rxDelete = rxDeleteDir(dstSrc); // copy the data const rxData = merge(rxCopyDir(srcSrc, dstSrc), rxCopyDir(srcData, dstData)); // create layouts on the result const layoutOpt: Options = { command: CreateLayoutCommand.COMMAND, commandType: CreateLayoutCommand.TYPE, name: 'weird name', type: 'Hero video', data: dstData, src: join(dstSrc, 'src', 'app'), verbose: false, override: true, skipRegistration: false, scss: true, allArgs: [] }; const layoutCmd = new CreateLayoutCommand(layoutOpt); const rxLayoutCmd = layoutCmd.process().pipe( mergeMap(expectFileExists), toArray(), map(files => { expect(findFile('layouts.ts', files)).toBeDefined(); expect(findFile('layouts.exports.ts', files)).toBeDefined(); // layout files expect(findFile('heroVideoLayout.ts', files)).toBeDefined(); expect(findFile('heroVideoLayout.scss', files)).toBeDefined(); expect(findFile('heroVideoLayout.html', files)).toBeDefined(); // layout files expect(findFile('weirdNameLayout.ts', files)).toBeDefined(); expect(findFile('weirdNameLayout.scss', files)).toBeDefined(); expect(findFile('weirdNameLayout.html', files)).toBeDefined(); // component files expect(findFile('abstractHeroVideoComponent.ts', files)).toBeDefined(); expect(findFile('typeHeroVideoComponent.scss', files)).toBeDefined(); expect(findFile('typeHeroVideoComponent.ts', files)).toBeDefined(); expect(findFile('typeHeroVideoComponent.html', files)).toBeDefined(); })); const rxTotal = concat(rxDelete, rxData, rxLayoutCmd, rxDelete); return rxTotal.toPromise(); }); });