| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  | const fs = require('fs-extra'); | 
					
						
							|  |  |  | const glob = require('glob'); | 
					
						
							|  |  |  | const path = require('canonical-path'); | 
					
						
							|  |  |  | const shelljs = require('shelljs'); | 
					
						
							|  |  |  | const yargs = require('yargs'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const SHARED_PATH = path.resolve(__dirname, 'shared'); | 
					
						
							|  |  |  | const SHARED_NODE_MODULES_PATH = path.resolve(SHARED_PATH, 'node_modules'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 14:40:03 +03:00
										 |  |  | const BOILERPLATE_BASE_PATH = path.resolve(SHARED_PATH, 'boilerplate'); | 
					
						
							|  |  |  | const BOILERPLATE_CLI_PATH = path.resolve(BOILERPLATE_BASE_PATH, 'cli'); | 
					
						
							|  |  |  | const BOILERPLATE_COMMON_PATH = path.resolve(BOILERPLATE_BASE_PATH, 'common'); | 
					
						
							|  |  |  | const BOILERPLATE_VIEWENGINE_PATH = path.resolve(BOILERPLATE_BASE_PATH, 'viewengine'); | 
					
						
							| 
									
										
										
										
											2020-04-06 22:57:58 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 14:40:03 +03:00
										 |  |  | const EXAMPLES_BASE_PATH = path.resolve(__dirname, '../../content/examples'); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  | const EXAMPLE_CONFIG_FILENAME = 'example-config.json'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ExampleBoilerPlate { | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Add boilerplate files to all the examples | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-04-06 22:57:58 +03:00
										 |  |  |   add(viewengine = false) { | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |     // Get all the examples folders, indicated by those that contain a `example-config.json` file
 | 
					
						
							| 
									
										
										
										
											2019-03-20 13:47:58 +00:00
										 |  |  |     const exampleFolders = | 
					
						
							|  |  |  |         this.getFoldersContaining(EXAMPLES_BASE_PATH, EXAMPLE_CONFIG_FILENAME, 'node_modules'); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-05 21:20:05 +00:00
										 |  |  |     if (!fs.existsSync(SHARED_NODE_MODULES_PATH)) { | 
					
						
							| 
									
										
										
										
											2019-03-20 13:47:58 +00:00
										 |  |  |       throw new Error( | 
					
						
							|  |  |  |           `The shared node_modules folder for the examples (${SHARED_NODE_MODULES_PATH}) is missing.\n` + | 
					
						
							| 
									
										
										
										
											2020-04-09 11:42:19 +03:00
										 |  |  |           'Perhaps you need to run "yarn example-use-npm" or "yarn example-use-local" to install the dependencies?'); | 
					
						
							| 
									
										
										
										
											2019-02-05 21:20:05 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 22:57:58 +03:00
										 |  |  |     if (!viewengine) { | 
					
						
							| 
									
										
										
										
											2019-10-16 23:07:01 +03:00
										 |  |  |       shelljs.exec(`yarn --cwd ${SHARED_PATH} ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points`); | 
					
						
							| 
									
										
										
										
											2019-02-05 21:20:05 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     exampleFolders.forEach(exampleFolder => { | 
					
						
							|  |  |  |       const exampleConfig = this.loadJsonFile(path.resolve(exampleFolder, EXAMPLE_CONFIG_FILENAME)); | 
					
						
							| 
									
										
										
										
											2017-10-06 14:48:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |       // Link the node modules - requires admin access (on Windows) because it adds symlinks
 | 
					
						
							|  |  |  |       const destinationNodeModules = path.resolve(exampleFolder, 'node_modules'); | 
					
						
							|  |  |  |       fs.ensureSymlinkSync(SHARED_NODE_MODULES_PATH, destinationNodeModules); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  |       const boilerPlateType = exampleConfig.projectType || 'cli'; | 
					
						
							|  |  |  |       const boilerPlateBasePath = path.resolve(BOILERPLATE_BASE_PATH, boilerPlateType); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 14:40:03 +03:00
										 |  |  |       // All example types other than `cli` and `systemjs` are based on `cli`. Copy over the `cli`
 | 
					
						
							|  |  |  |       // boilerplate files first.
 | 
					
						
							|  |  |  |       // (Some of these files might be later overwritten by type-specific files.)
 | 
					
						
							|  |  |  |       if (boilerPlateType !== 'cli' && boilerPlateType !== 'systemjs') { | 
					
						
							|  |  |  |         this.copyDirectoryContents(BOILERPLATE_CLI_PATH, exampleFolder); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 14:40:03 +03:00
										 |  |  |       // Copy the type-specific boilerplate files.
 | 
					
						
							|  |  |  |       this.copyDirectoryContents(boilerPlateBasePath, exampleFolder); | 
					
						
							| 
									
										
										
										
											2019-05-12 20:12:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 14:40:03 +03:00
										 |  |  |       // Copy the common boilerplate files (unless explicitly not used).
 | 
					
						
							|  |  |  |       if (exampleConfig.useCommonBoilerplate !== false) { | 
					
						
							|  |  |  |         this.copyDirectoryContents(BOILERPLATE_COMMON_PATH, exampleFolder); | 
					
						
							| 
									
										
										
										
											2019-03-20 15:10:47 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-02-05 21:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 22:57:58 +03:00
										 |  |  |       // Copy ViewEngine (pre-Ivy) specific files
 | 
					
						
							|  |  |  |       if (viewengine) { | 
					
						
							|  |  |  |         const veBoilerPlateType = boilerPlateType === 'systemjs' ? 'systemjs' : 'cli'; | 
					
						
							| 
									
										
										
										
											2020-07-22 14:40:03 +03:00
										 |  |  |         const veBoilerPlateBasePath = path.resolve(BOILERPLATE_VIEWENGINE_PATH, veBoilerPlateType); | 
					
						
							|  |  |  |         this.copyDirectoryContents(veBoilerPlateBasePath, exampleFolder); | 
					
						
							| 
									
										
										
										
											2019-02-05 21:20:05 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Remove all the boilerplate files from all the examples | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-03-20 13:47:58 +00:00
										 |  |  |   remove() { shelljs.exec('git clean -xdfq', {cwd: EXAMPLES_BASE_PATH}); } | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   main() { | 
					
						
							| 
									
										
										
										
											2019-03-20 13:47:58 +00:00
										 |  |  |     yargs.usage('$0 <cmd> [args]') | 
					
						
							| 
									
										
										
										
											2020-04-06 22:57:58 +03:00
										 |  |  |         .command('add', 'add the boilerplate to each example', yrgs => this.add(yrgs.argv.viewengine)) | 
					
						
							| 
									
										
										
										
											2019-03-20 13:47:58 +00:00
										 |  |  |         .command('remove', 'remove the boilerplate from each example', () => this.remove()) | 
					
						
							|  |  |  |         .demandCommand(1, 'Please supply a command from the list above') | 
					
						
							|  |  |  |         .argv; | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getFoldersContaining(basePath, filename, ignore) { | 
					
						
							|  |  |  |     const pattern = path.resolve(basePath, '**', filename); | 
					
						
							|  |  |  |     const ignorePattern = path.resolve(basePath, '**', ignore, '**'); | 
					
						
							| 
									
										
										
										
											2019-03-20 13:47:58 +00:00
										 |  |  |     return glob.sync(pattern, {ignore: [ignorePattern]}).map(file => path.dirname(file)); | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 13:47:58 +00:00
										 |  |  |   loadJsonFile(filePath) { return fs.readJsonSync(filePath, {throws: false}) || {}; } | 
					
						
							| 
									
										
										
										
											2017-10-30 23:39:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 14:40:03 +03:00
										 |  |  |   copyDirectoryContents(srcDir, dstDir) { | 
					
						
							|  |  |  |     shelljs.ls('-Al', srcDir).forEach(stat => { | 
					
						
							|  |  |  |       const srcPath = path.resolve(srcDir, stat.name); | 
					
						
							|  |  |  |       const dstPath = path.resolve(dstDir, stat.name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (stat.isDirectory()) { | 
					
						
							|  |  |  |         // `srcPath` is a directory: Recursively copy it to `dstDir`.
 | 
					
						
							|  |  |  |         shelljs.mkdir('-p', dstPath); | 
					
						
							|  |  |  |         return this.copyDirectoryContents(srcPath, dstPath); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         // `srcPath` is a file: Copy it to `dstDir`.
 | 
					
						
							|  |  |  |         // (Also make the file non-writable to avoid accidental editing of boilerplate files).
 | 
					
						
							|  |  |  |         if (shelljs.test('-f', dstPath)) { | 
					
						
							|  |  |  |           // If the file already exists, ensure it is writable (so it can be overwritten).
 | 
					
						
							|  |  |  |           shelljs.chmod(666, dstPath); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         shelljs.cp(srcPath, dstDir); | 
					
						
							|  |  |  |         shelljs.chmod(444, dstPath); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-30 23:39:58 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-07-27 08:29:17 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = new ExampleBoilerPlate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // If this file was run directly then run the main function,
 | 
					
						
							|  |  |  | if (require.main === module) { | 
					
						
							|  |  |  |   module.exports.main(); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:31:15 +02:00
										 |  |  | } |