| 
									
										
										
										
											2019-06-28 20:11:12 +08:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 00:46:29 +08:00
										 |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2019-06-28 20:11:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 00:46:29 +08:00
										 |  |  | let includeInternalScripts = false; | 
					
						
							| 
									
										
										
										
											2019-06-28 20:11:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 05:05:18 +08:00
										 |  |  | const isLinkedMode = () => { | 
					
						
							|  |  |  |   // In circleci we are in linked mode. Detect by using the circle working directory,
 | 
					
						
							|  |  |  |   // rather than the present working directory.
 | 
					
						
							|  |  |  |   const pwd = process.env.CIRCLE_WORKING_DIRECTORY || process.env.PWD; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (path.basename(pwd) === 'grafana-toolkit') { | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     return fs.lstatSync(`${pwd}/node_modules/@grafana/toolkit`.replace('~', process.env.HOME)).isSymbolicLink(); | 
					
						
							|  |  |  |   } catch { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 00:46:29 +08:00
										 |  |  | const entrypoint = () => { | 
					
						
							| 
									
										
										
										
											2020-04-15 05:05:18 +08:00
										 |  |  |   const defaultEntryPoint = `${__dirname}/../src/cli/index.js`; | 
					
						
							| 
									
										
										
										
											2020-04-15 00:46:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // IF we have a toolkit directory AND linked grafana toolkit AND the toolkit dir is a symbolic lik
 | 
					
						
							|  |  |  |   // THEN run everything in linked mode
 | 
					
						
							| 
									
										
										
										
											2020-04-15 05:05:18 +08:00
										 |  |  |   if (isLinkedMode()) { | 
					
						
							|  |  |  |     console.log('Running in local/linked mode'); | 
					
						
							|  |  |  |     // This bin is used for cli executed internally
 | 
					
						
							|  |  |  |     var tsProjectPath = path.resolve(__dirname, '../tsconfig.json'); | 
					
						
							|  |  |  |     require('ts-node').register({ | 
					
						
							|  |  |  |       project: tsProjectPath, | 
					
						
							|  |  |  |       transpileOnly: true, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     includeInternalScripts = true; | 
					
						
							|  |  |  |     return '../src/cli/index.ts'; | 
					
						
							| 
									
										
										
										
											2020-04-15 00:46:29 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // We are using npx, and a relative path does not find index.js
 | 
					
						
							|  |  |  |   if (!fs.existsSync(defaultEntryPoint) && fs.existsSync(`${__dirname}/../dist/src/cli/index.js`)) { | 
					
						
							|  |  |  |     return `${__dirname}/../dist/src/cli/index.js`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // The default entrypoint must exist, return it now.
 | 
					
						
							|  |  |  |   return defaultEntryPoint; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require(entrypoint()).run(includeInternalScripts); |