import { accessSync, F_OK, readFileSync, writeFileSync } from 'fs'; import { dirname, join } from 'path'; import * as glob from 'glob'; import { dest, src, start, task } from 'gulp'; import * as gulpif from 'gulp-if'; import * as watch from 'gulp-watch'; import { template } from 'lodash'; import * as rollup from 'rollup'; import * as nodeResolve from 'rollup-plugin-node-resolve'; import * as commonjs from 'rollup-plugin-commonjs'; import * as runSequence from 'run-sequence'; import { obj } from 'through2'; import * as VinylFile from 'vinyl'; import { DIST_E2E_COMPONENTS_ROOT, DIST_E2E_ROOT, DIST_NAME, E2E_NAME, ES5, ES_2015, LOCAL_SERVER_PORT, PROJECT_ROOT, SCRIPTS_ROOT, SRC_COMPONENTS_ROOT, SRC_ROOT } from '../constants'; import { createTempTsConfig, deleteFiles, getFolderInfo, runNgc } from '../util'; task('e2e.prod', e2eBuild); function e2eBuild(done: (err: any) => void) { runSequence( 'e2e.clean', 'e2e.polyfill', 'e2e.copySource', 'e2e.compileTests', 'e2e.copyExternalDependencies', 'e2e.sass', 'e2e.fonts', 'e2e.bundleProd', done); } task('e2e.copySource', (done: Function) => { const buildConfig = require('../../build/config'); const stream = src([`${SRC_ROOT}/**/*`, `!${SRC_ROOT}/**/*.spec.ts`]) .pipe(gulpif(/app.module.ts$/, createIndexHTML())) .pipe(gulpif(/e2e.ts$/, createPlatformTests())) .pipe(dest(DIST_E2E_ROOT)); stream.on('end', done); function createIndexHTML() { const indexTemplate = readFileSync(`${SCRIPTS_ROOT}/${E2E_NAME}/e2e.template.prod.html`); const indexTs = readFileSync(`${SCRIPTS_ROOT}/${E2E_NAME}/entry.ts`); return obj(function (file, enc, next) { this.push(new VinylFile({ base: file.base, contents: new Buffer(indexTemplate), path: join(dirname(file.path), 'index.html'), })); this.push(new VinylFile({ base: file.base, contents: new Buffer(indexTs), path: join(dirname(file.path), 'entry.ts'), })); next(null, file); }); } // TODO this is almost the same as dev, diff and combine function createPlatformTests() { let platforms = [ 'android', 'ios', 'windows' ]; let testTemplate = template(readFileSync(`${SCRIPTS_ROOT}/${E2E_NAME}/e2e.template.js`).toString()); return obj(function (file, enc, next) { let self = this; let relativePath = dirname(file.path.replace(/^.*?src(\/|\\)/, '')); let contents = file.contents.toString(); platforms.forEach(function (platform) { let platformContents = testTemplate({ contents: contents, buildConfig: buildConfig, relativePath: relativePath, platform: platform }); self.push(new VinylFile({ base: file.base, contents: new Buffer(platformContents), path: file.path.replace(/e2e.ts$/, platform + '.e2e.js') })); }); next(); }); } }); task('e2e.compileTests', (done: Function) => { let folderInfo = getFolderInfo(); buildE2ETests(folderInfo, done); }); function buildE2ETests(folderInfo: any, done: Function) { let includeGlob = ['./components/*/test/*/app.module.ts', './components/*/test/*/entry.ts']; if (folderInfo.componentName && folderInfo.componentTest) { includeGlob = [ `./components/${folderInfo.componentName}/test/${folderInfo.componentTest}/app.module.ts`, `./components/${folderInfo.componentName}/test/${folderInfo.componentTest}/entry.ts`, ]; } createTempTsConfig(includeGlob, ES5, ES_2015, `${PROJECT_ROOT}/tsconfig.json`, `${DIST_E2E_ROOT}/tsconfig.json`); runNgc(`${DIST_E2E_ROOT}/tsconfig.json`, (err) => { if (err) { done(err); return; } // clean up any .ts files that remain deleteFiles([`${DIST_E2E_ROOT}/**/*.ts`, `!${DIST_E2E_ROOT}/**/*.ngfactory.ts`, `!${DIST_E2E_ROOT}/**/*.d.ts`], done); }); } task('e2e.bundleProd', (done) => { let includeGlob = `${DIST_E2E_ROOT}/components/*/test/*/entry.js`; let folderInfo = getFolderInfo(); if (folderInfo.componentName && folderInfo.componentTest) { includeGlob = `${DIST_E2E_ROOT}/components/${folderInfo.componentName}/test/${folderInfo.componentTest}/entry.js`; } glob(includeGlob, {}, function (er, files) { var directories = files.map(function (file) { return dirname(file); }); let indexFileContents = directories.map(function (dir) { let testName = dir.replace(`${DIST_E2E_ROOT}/components/`, ''); let fileName = dir.replace(`${PROJECT_ROOT}`, ''); return `
`; }, []); writeFileSync(`${DIST_E2E_ROOT}/index.html`, '\n' + indexFileContents.join('\n') + '' ); createBundles(files).then(() => { done(); }).catch(err => { done(err); }); }); }); function createBundles(files: string[]) { let start; if (!files) { return Promise.reject(new Error('list of files is null')); } else if (files.length === 0) { return Promise.resolve(); } else { const outputFileName = join(dirname(files[0]), 'app.bundle.js'); start = Date.now(); return bundle(files[0], outputFileName).then(() => { const end = Date.now(); const seconds = (end - start) / 1000; console.log(`Took ${seconds} seconds to process ${files[0]}`); const remainingFiles = files.concat(); remainingFiles.shift(); return createBundles(remainingFiles); }).catch(err => { return Promise.reject(err); }); } } function bundle(inputFile: string, outputFile: string): Promise