| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Canonical path provides a consistent path (i.e. always forward slashes) across different OSes
 | 
					
						
							|  |  |  | const path = require('canonical-path'); | 
					
						
							|  |  |  | const archiver = require('archiver'); | 
					
						
							|  |  |  | const fs = require('fs-extra'); | 
					
						
							|  |  |  | const globby = require('globby'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const regionExtractor = require('../transforms/examples-package/services/region-parser'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  | const EXAMPLE_CONFIG_NAME = 'example-config.json'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  | class ExampleZipper { | 
					
						
							|  |  |  |   constructor(sourceDirName, outputDirName) { | 
					
						
							|  |  |  |     this.examplesPackageJson = path.join(__dirname, '../examples/shared/package.json'); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |     this.examplesSystemjsConfig = path.join(__dirname, '../examples/shared/boilerplate/systemjs/src/systemjs.config.js'); | 
					
						
							|  |  |  |     this.examplesSystemjsLoaderConfig = path.join(__dirname, '../examples/shared/boilerplate/systemjs/src/systemjs-angular-loader.js'); | 
					
						
							|  |  |  |     this.exampleTsconfig = path.join(__dirname, '../examples/shared/boilerplate/systemjs/src/tsconfig.json'); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 18:08:28 +01:00
										 |  |  |     let gpathStackblitz = path.join(sourceDirName, '**/*stackblitz.json'); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |     let gpathZipper = path.join(sourceDirName, '**/zipper.json'); | 
					
						
							| 
									
										
										
										
											2017-11-03 18:08:28 +01:00
										 |  |  |     let configFileNames = globby.sync([gpathStackblitz, gpathZipper], { ignore: ['**/node_modules/**'] }); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |     configFileNames.forEach((configFileName) => { | 
					
						
							|  |  |  |       this._zipExample(configFileName, sourceDirName, outputDirName); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   _changeTypeRoots(tsconfig) { | 
					
						
							|  |  |  |     return tsconfig.replace('../../../', '../'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   _createZipArchive(zipFileName) { | 
					
						
							|  |  |  |     let dirName = path.dirname(zipFileName); | 
					
						
							|  |  |  |     fs.ensureDirSync(dirName); | 
					
						
							|  |  |  |     let output = fs.createWriteStream(zipFileName); | 
					
						
							|  |  |  |     let archive = archiver('zip'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     archive.on('error', function (err) { | 
					
						
							|  |  |  |       throw err; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     archive.pipe(output); | 
					
						
							|  |  |  |     return archive; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |   _getExampleType(sourceFolder) { | 
					
						
							|  |  |  |     const filePath = path.join(sourceFolder, EXAMPLE_CONFIG_NAME); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       return require(filePath, 'utf-8').projectType || 'cli'; | 
					
						
							|  |  |  |     } catch (err) { // empty file, so it is cli
 | 
					
						
							|  |  |  |       return 'cli'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 15:02:49 +01:00
										 |  |  |   // rename a custom main.ts or index.html file
 | 
					
						
							| 
									
										
										
										
											2018-05-02 07:13:32 -05:00
										 |  |  |   _renameFile(file, exampleType) { | 
					
						
							|  |  |  |     if (/src\/main[-.]\w+\.ts$/.test(file) && exampleType !== 'universal') { | 
					
						
							| 
									
										
										
										
											2017-12-14 15:02:49 +01:00
										 |  |  |       return 'src/main.ts'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (/src\/index[-.]\w+\.html$/.test(file)) { | 
					
						
							|  |  |  |       return 'src/index.html'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return file; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |   _zipExample(configFileName, sourceDirName, outputDirName) { | 
					
						
							|  |  |  |     let json = require(configFileName, 'utf-8'); | 
					
						
							|  |  |  |     const basePath = json.basePath || ''; | 
					
						
							|  |  |  |     const jsonFileName = configFileName.replace(/^.*[\\\/]/, ''); | 
					
						
							|  |  |  |     let relativeDirName = path.dirname(path.relative(sourceDirName, configFileName)); | 
					
						
							|  |  |  |     let exampleZipName; | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |     const exampleType = this._getExampleType(path.join(sourceDirName, relativeDirName)); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |     if (relativeDirName.indexOf('/') !== -1) { // Special example
 | 
					
						
							| 
									
										
										
										
											2017-11-22 15:58:21 +02:00
										 |  |  |       exampleZipName = relativeDirName.split('/').join('-'); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2017-11-03 18:08:28 +01:00
										 |  |  |       exampleZipName = jsonFileName.replace(/(stackblitz|zipper).json/, relativeDirName); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const exampleDirName = path.dirname(configFileName); | 
					
						
							|  |  |  |     const outputFileName = path.join(outputDirName, relativeDirName, exampleZipName + '.zip'); | 
					
						
							| 
									
										
										
										
											2019-07-15 23:07:25 +05:30
										 |  |  |     let defaultIncludes = ['**/*.ts', '**/*.js', '**/*.es6', '**/*.css', '**/*.html', '**/*.md', '**/*.json', '**/*.png', '**/*.svg']; | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |     let alwaysIncludes = [ | 
					
						
							|  |  |  |       '.editorconfig', | 
					
						
							|  |  |  |       '.gitignore', | 
					
						
							| 
									
										
										
										
											2019-08-01 00:10:52 +03:00
										 |  |  |       'angular.json', | 
					
						
							|  |  |  |       'browserslist', | 
					
						
							|  |  |  |       'bs-config.json', | 
					
						
							|  |  |  |       'karma.conf.js', | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |       'karma-test-shim.js', | 
					
						
							| 
									
										
										
										
											2020-07-22 22:23:55 +03:00
										 |  |  |       'package.json', | 
					
						
							| 
									
										
										
										
											2019-08-01 00:10:52 +03:00
										 |  |  |       'tsconfig.*', | 
					
						
							|  |  |  |       'tslint.*', | 
					
						
							|  |  |  |       'e2e/protractor.conf.js', | 
					
						
							|  |  |  |       'e2e/tsconfig.json', | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |       'src/favicon.ico', | 
					
						
							| 
									
										
										
										
											2017-12-14 15:02:49 +01:00
										 |  |  |       'src/polyfills.ts', | 
					
						
							| 
									
										
										
										
											2018-05-16 15:30:39 -05:00
										 |  |  |       'src/test.ts', | 
					
						
							| 
									
										
										
										
											2017-12-14 15:02:49 +01:00
										 |  |  |       'src/environments/**/*', | 
					
						
							| 
									
										
										
										
											2019-08-01 00:10:52 +03:00
										 |  |  |       'src/testing/**/*', | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |     ]; | 
					
						
							| 
									
										
										
										
											2017-12-14 15:02:49 +01:00
										 |  |  |     var alwaysExcludes = [ | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |       '!**/bs-config.e2e.json', | 
					
						
							| 
									
										
										
										
											2017-11-03 18:08:28 +01:00
										 |  |  |       '!**/*stackblitz.*', | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |       '!**/*zipper.*', | 
					
						
							|  |  |  |       '!**/systemjs.config.js', | 
					
						
							|  |  |  |       '!**/npm-debug.log', | 
					
						
							|  |  |  |       '!**/example-config.json', | 
					
						
							|  |  |  |       '!**/wallaby.js', | 
					
						
							| 
									
										
										
										
											2020-03-16 16:32:31 +02:00
										 |  |  |       '!**/e2e/protractor-puppeteer.conf.js', | 
					
						
							| 
									
										
										
										
											2020-02-02 20:49:28 -05:00
										 |  |  |       // AOT related files
 | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |       '!**/aot/**/*.*', | 
					
						
							|  |  |  |       '!**/*-aot.*' | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (json.files) { | 
					
						
							|  |  |  |       if (json.files.length > 0) { | 
					
						
							|  |  |  |         json.files = json.files.map(file => { | 
					
						
							|  |  |  |           if (file.startsWith('!')) { | 
					
						
							|  |  |  |             if (file.startsWith('!**')) { | 
					
						
							|  |  |  |               return file; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return '!' + basePath + file.substr(1); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return basePath + file; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 16:17:32 +02:00
										 |  |  |         if (json.files[0][0] === '!') { | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |           json.files = defaultIncludes.concat(json.files); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       json.files = defaultIncludes; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     json.files = json.files.concat(alwaysIncludes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let gpaths = json.files.map((fileName) => { | 
					
						
							|  |  |  |       fileName = fileName.trim(); | 
					
						
							| 
									
										
										
										
											2020-03-19 16:17:32 +02:00
										 |  |  |       if (fileName[0] === '!') { | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |         return '!' + path.join(exampleDirName, fileName.substr(1)); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         return path.join(exampleDirName, fileName); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 16:17:32 +02:00
										 |  |  |     gpaths.push(...alwaysExcludes); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 16:17:32 +02:00
										 |  |  |     let fileNames = globby.sync(gpaths, { ignore: ['**/node_modules/**'] }); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let zip = this._createZipArchive(outputFileName); | 
					
						
							|  |  |  |     fileNames.forEach((fileName) => { | 
					
						
							|  |  |  |       let relativePath = path.relative(exampleDirName, fileName); | 
					
						
							| 
									
										
										
										
											2018-05-02 07:13:32 -05:00
										 |  |  |       relativePath = this._renameFile(relativePath, exampleType); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |       let content = fs.readFileSync(fileName, 'utf8'); | 
					
						
							|  |  |  |       let extn = path.extname(fileName).substr(1); | 
					
						
							|  |  |  |       // if we don't need to clean up the file then we can do the following.
 | 
					
						
							|  |  |  |       // zip.append(fs.createReadStream(fileName), { name: relativePath });
 | 
					
						
							|  |  |  |       let output = regionExtractor()(content, extn).contents; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 16:17:32 +02:00
										 |  |  |       zip.append(output, { name: relativePath } ); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // also a systemjs config
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |     if (exampleType === 'systemjs') { | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |       zip.append(fs.readFileSync(this.examplesSystemjsConfig, 'utf8'), { name: 'src/systemjs.config.js' }); | 
					
						
							|  |  |  |       zip.append(fs.readFileSync(this.examplesSystemjsLoaderConfig, 'utf8'), { name: 'src/systemjs-angular-loader.js' }); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |       // a modified tsconfig
 | 
					
						
							|  |  |  |       let tsconfig = fs.readFileSync(this.exampleTsconfig, 'utf8'); | 
					
						
							|  |  |  |       zip.append(this._changeTypeRoots(tsconfig), {name: 'src/tsconfig.json'}); | 
					
						
							| 
									
										
										
										
											2017-04-16 23:40:47 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     zip.finalize(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ExampleZipper; |