firebase-tools-with-isolate 12.9.1-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (594) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -0
  3. package/lib/accountExporter.js +207 -0
  4. package/lib/accountImporter.js +305 -0
  5. package/lib/api.js +84 -0
  6. package/lib/apiv2.js +353 -0
  7. package/lib/appdistribution/client.js +193 -0
  8. package/lib/appdistribution/distribution.js +51 -0
  9. package/lib/appdistribution/options-parser-util.js +51 -0
  10. package/lib/archiveDirectory.js +120 -0
  11. package/lib/auth.js +535 -0
  12. package/lib/bin/firebase.js +132 -0
  13. package/lib/checkMinRequiredVersion.js +14 -0
  14. package/lib/checkValidTargetFilters.js +46 -0
  15. package/lib/command.js +250 -0
  16. package/lib/commands/appdistribution-distribute.js +121 -0
  17. package/lib/commands/appdistribution-group-create.js +19 -0
  18. package/lib/commands/appdistribution-group-delete.js +24 -0
  19. package/lib/commands/appdistribution-testers-add.js +24 -0
  20. package/lib/commands/appdistribution-testers-remove.js +40 -0
  21. package/lib/commands/apps-android-sha-create.js +29 -0
  22. package/lib/commands/apps-android-sha-delete.js +16 -0
  23. package/lib/commands/apps-android-sha-list.js +42 -0
  24. package/lib/commands/apps-create.js +166 -0
  25. package/lib/commands/apps-list.js +53 -0
  26. package/lib/commands/apps-sdkconfig.js +103 -0
  27. package/lib/commands/auth-export.js +44 -0
  28. package/lib/commands/auth-import.js +114 -0
  29. package/lib/commands/crashlytics-mappingfile-generateid.js +26 -0
  30. package/lib/commands/crashlytics-mappingfile-upload.js +46 -0
  31. package/lib/commands/crashlytics-symbols-upload.js +78 -0
  32. package/lib/commands/database-get.js +122 -0
  33. package/lib/commands/database-import.js +85 -0
  34. package/lib/commands/database-instances-create.js +29 -0
  35. package/lib/commands/database-instances-list.js +53 -0
  36. package/lib/commands/database-profile.js +46 -0
  37. package/lib/commands/database-push.js +63 -0
  38. package/lib/commands/database-remove.js +42 -0
  39. package/lib/commands/database-rules-canary.js +24 -0
  40. package/lib/commands/database-rules-get.js +22 -0
  41. package/lib/commands/database-rules-list.js +36 -0
  42. package/lib/commands/database-rules-release.js +24 -0
  43. package/lib/commands/database-rules-stage.js +26 -0
  44. package/lib/commands/database-set.js +68 -0
  45. package/lib/commands/database-settings-get.js +44 -0
  46. package/lib/commands/database-settings-set.js +46 -0
  47. package/lib/commands/database-update.js +69 -0
  48. package/lib/commands/deploy.js +101 -0
  49. package/lib/commands/emulators-exec.js +19 -0
  50. package/lib/commands/emulators-export.js +14 -0
  51. package/lib/commands/emulators-start.js +115 -0
  52. package/lib/commands/experimental-functions-shell.js +13 -0
  53. package/lib/commands/experiments-describe.js +31 -0
  54. package/lib/commands/experiments-disable.js +28 -0
  55. package/lib/commands/experiments-enable.js +28 -0
  56. package/lib/commands/experiments-list.js +29 -0
  57. package/lib/commands/ext-configure.js +110 -0
  58. package/lib/commands/ext-dev-deprecate.js +68 -0
  59. package/lib/commands/ext-dev-init.js +148 -0
  60. package/lib/commands/ext-dev-list.js +47 -0
  61. package/lib/commands/ext-dev-publish.js +32 -0
  62. package/lib/commands/ext-dev-register.js +50 -0
  63. package/lib/commands/ext-dev-undeprecate.js +57 -0
  64. package/lib/commands/ext-dev-upload.js +88 -0
  65. package/lib/commands/ext-dev-usage.js +141 -0
  66. package/lib/commands/ext-export.js +68 -0
  67. package/lib/commands/ext-info.js +118 -0
  68. package/lib/commands/ext-install.js +167 -0
  69. package/lib/commands/ext-list.js +18 -0
  70. package/lib/commands/ext-uninstall.js +29 -0
  71. package/lib/commands/ext-update.js +105 -0
  72. package/lib/commands/ext.js +41 -0
  73. package/lib/commands/firestore-databases-create.js +55 -0
  74. package/lib/commands/firestore-databases-delete.js +39 -0
  75. package/lib/commands/firestore-databases-get.js +25 -0
  76. package/lib/commands/firestore-databases-list.js +24 -0
  77. package/lib/commands/firestore-databases-update.js +59 -0
  78. package/lib/commands/firestore-delete.js +88 -0
  79. package/lib/commands/firestore-indexes-list.js +38 -0
  80. package/lib/commands/firestore-locations.js +24 -0
  81. package/lib/commands/frameworks-backends-create.js +14 -0
  82. package/lib/commands/frameworks-backends-delete.js +98 -0
  83. package/lib/commands/frameworks-backends-get.js +80 -0
  84. package/lib/commands/frameworks-backends-list.js +68 -0
  85. package/lib/commands/functions-config-clone.js +53 -0
  86. package/lib/commands/functions-config-export.js +118 -0
  87. package/lib/commands/functions-config-get.js +35 -0
  88. package/lib/commands/functions-config-set.js +43 -0
  89. package/lib/commands/functions-config-unset.js +42 -0
  90. package/lib/commands/functions-delete.js +95 -0
  91. package/lib/commands/functions-deletegcfartifacts.js +44 -0
  92. package/lib/commands/functions-list.js +47 -0
  93. package/lib/commands/functions-log.js +34 -0
  94. package/lib/commands/functions-secrets-access.js +22 -0
  95. package/lib/commands/functions-secrets-destroy.js +64 -0
  96. package/lib/commands/functions-secrets-get.js +28 -0
  97. package/lib/commands/functions-secrets-prune.js +57 -0
  98. package/lib/commands/functions-secrets-set.js +108 -0
  99. package/lib/commands/functions-shell.js +15 -0
  100. package/lib/commands/help.js +27 -0
  101. package/lib/commands/hosting-channel-create.js +78 -0
  102. package/lib/commands/hosting-channel-delete.js +47 -0
  103. package/lib/commands/hosting-channel-deploy.js +146 -0
  104. package/lib/commands/hosting-channel-list.js +40 -0
  105. package/lib/commands/hosting-channel-open.js +48 -0
  106. package/lib/commands/hosting-clone.js +103 -0
  107. package/lib/commands/hosting-disable.js +31 -0
  108. package/lib/commands/hosting-sites-create.js +34 -0
  109. package/lib/commands/hosting-sites-delete.js +39 -0
  110. package/lib/commands/hosting-sites-get.js +27 -0
  111. package/lib/commands/hosting-sites-list.js +28 -0
  112. package/lib/commands/index.js +191 -0
  113. package/lib/commands/init.js +195 -0
  114. package/lib/commands/internaltesting-frameworks-compose.js +22 -0
  115. package/lib/commands/internaltesting-functions-discover.js +25 -0
  116. package/lib/commands/login-add.js +32 -0
  117. package/lib/commands/login-ci.js +25 -0
  118. package/lib/commands/login-list.js +28 -0
  119. package/lib/commands/login-use.js +24 -0
  120. package/lib/commands/login.js +57 -0
  121. package/lib/commands/logout.js +76 -0
  122. package/lib/commands/open.js +100 -0
  123. package/lib/commands/projects-addfirebase.js +19 -0
  124. package/lib/commands/projects-create.js +37 -0
  125. package/lib/commands/projects-list.js +61 -0
  126. package/lib/commands/remoteconfig-get.js +68 -0
  127. package/lib/commands/remoteconfig-rollback.js +48 -0
  128. package/lib/commands/remoteconfig-versions-list.js +34 -0
  129. package/lib/commands/serve.js +60 -0
  130. package/lib/commands/setup-emulators-database.js +12 -0
  131. package/lib/commands/setup-emulators-firestore.js +12 -0
  132. package/lib/commands/setup-emulators-pubsub.js +12 -0
  133. package/lib/commands/setup-emulators-storage.js +12 -0
  134. package/lib/commands/setup-emulators-ui.js +12 -0
  135. package/lib/commands/target-apply.js +24 -0
  136. package/lib/commands/target-clear.js +20 -0
  137. package/lib/commands/target-remove.js +20 -0
  138. package/lib/commands/target.js +34 -0
  139. package/lib/commands/use.js +162 -0
  140. package/lib/config.js +227 -0
  141. package/lib/configstore.js +6 -0
  142. package/lib/crashlytics/buildToolsJarHelper.js +51 -0
  143. package/lib/database/api.js +19 -0
  144. package/lib/database/import.js +218 -0
  145. package/lib/database/listRemote.js +37 -0
  146. package/lib/database/metadata.js +67 -0
  147. package/lib/database/remove.js +88 -0
  148. package/lib/database/removeRemote.js +51 -0
  149. package/lib/database/rulesConfig.js +79 -0
  150. package/lib/database/settings.js +62 -0
  151. package/lib/defaultCredentials.js +80 -0
  152. package/lib/deploy/database/deploy.js +5 -0
  153. package/lib/deploy/database/index.js +9 -0
  154. package/lib/deploy/database/prepare.js +54 -0
  155. package/lib/deploy/database/release.js +30 -0
  156. package/lib/deploy/extensions/args.js +2 -0
  157. package/lib/deploy/extensions/deploy.js +49 -0
  158. package/lib/deploy/extensions/deploymentSummary.js +56 -0
  159. package/lib/deploy/extensions/errors.js +31 -0
  160. package/lib/deploy/extensions/index.js +9 -0
  161. package/lib/deploy/extensions/planner.js +157 -0
  162. package/lib/deploy/extensions/prepare.js +110 -0
  163. package/lib/deploy/extensions/release.js +57 -0
  164. package/lib/deploy/extensions/secrets.js +150 -0
  165. package/lib/deploy/extensions/tasks.js +156 -0
  166. package/lib/deploy/extensions/v2FunctionHelper.js +53 -0
  167. package/lib/deploy/extensions/validate.js +17 -0
  168. package/lib/deploy/firestore/deploy.js +44 -0
  169. package/lib/deploy/firestore/index.js +9 -0
  170. package/lib/deploy/firestore/prepare.js +59 -0
  171. package/lib/deploy/firestore/release.js +22 -0
  172. package/lib/deploy/functions/args.js +2 -0
  173. package/lib/deploy/functions/backend.js +316 -0
  174. package/lib/deploy/functions/build.js +311 -0
  175. package/lib/deploy/functions/cache/applyHash.js +22 -0
  176. package/lib/deploy/functions/cache/hash.js +30 -0
  177. package/lib/deploy/functions/cel.js +365 -0
  178. package/lib/deploy/functions/checkIam.js +183 -0
  179. package/lib/deploy/functions/containerCleaner.js +305 -0
  180. package/lib/deploy/functions/deploy.js +108 -0
  181. package/lib/deploy/functions/ensure.js +105 -0
  182. package/lib/deploy/functions/ensureCloudBuildEnabled.js +1 -0
  183. package/lib/deploy/functions/functionsDeployHelper.js +134 -0
  184. package/lib/deploy/functions/index.js +9 -0
  185. package/lib/deploy/functions/params.js +517 -0
  186. package/lib/deploy/functions/prepare.js +304 -0
  187. package/lib/deploy/functions/prepareFunctionsUpload.js +113 -0
  188. package/lib/deploy/functions/pricing.js +171 -0
  189. package/lib/deploy/functions/prompts.js +157 -0
  190. package/lib/deploy/functions/release/executor.js +51 -0
  191. package/lib/deploy/functions/release/fabricator.js +584 -0
  192. package/lib/deploy/functions/release/index.js +93 -0
  193. package/lib/deploy/functions/release/planner.js +205 -0
  194. package/lib/deploy/functions/release/reporter.js +196 -0
  195. package/lib/deploy/functions/release/sourceTokenScraper.js +67 -0
  196. package/lib/deploy/functions/release/timer.js +14 -0
  197. package/lib/deploy/functions/runtimes/discovery/index.js +85 -0
  198. package/lib/deploy/functions/runtimes/discovery/mockDiscoveryServer.js +14 -0
  199. package/lib/deploy/functions/runtimes/discovery/parsing.js +95 -0
  200. package/lib/deploy/functions/runtimes/discovery/v1alpha1.js +296 -0
  201. package/lib/deploy/functions/runtimes/index.js +59 -0
  202. package/lib/deploy/functions/runtimes/node/extractTriggers.js +23 -0
  203. package/lib/deploy/functions/runtimes/node/index.js +173 -0
  204. package/lib/deploy/functions/runtimes/node/parseRuntimeAndValidateSDK.js +57 -0
  205. package/lib/deploy/functions/runtimes/node/parseTriggers.js +332 -0
  206. package/lib/deploy/functions/runtimes/node/triggerParser.js +72 -0
  207. package/lib/deploy/functions/runtimes/node/validate.js +34 -0
  208. package/lib/deploy/functions/runtimes/node/versioning.js +95 -0
  209. package/lib/deploy/functions/runtimes/python/index.js +154 -0
  210. package/lib/deploy/functions/services/auth.js +95 -0
  211. package/lib/deploy/functions/services/database.js +14 -0
  212. package/lib/deploy/functions/services/firebaseAlerts.js +14 -0
  213. package/lib/deploy/functions/services/firestore.js +17 -0
  214. package/lib/deploy/functions/services/index.js +117 -0
  215. package/lib/deploy/functions/services/remoteConfig.js +14 -0
  216. package/lib/deploy/functions/services/storage.js +44 -0
  217. package/lib/deploy/functions/services/testLab.js +14 -0
  218. package/lib/deploy/functions/triggerRegionHelper.js +16 -0
  219. package/lib/deploy/functions/validate.js +198 -0
  220. package/lib/deploy/hosting/args.js +2 -0
  221. package/lib/deploy/hosting/context.js +2 -0
  222. package/lib/deploy/hosting/convertConfig.js +181 -0
  223. package/lib/deploy/hosting/deploy.js +85 -0
  224. package/lib/deploy/hosting/hashcache.js +48 -0
  225. package/lib/deploy/hosting/index.js +9 -0
  226. package/lib/deploy/hosting/prepare.js +162 -0
  227. package/lib/deploy/hosting/release.js +40 -0
  228. package/lib/deploy/hosting/uploader.js +207 -0
  229. package/lib/deploy/index.js +127 -0
  230. package/lib/deploy/lifecycleHooks.js +151 -0
  231. package/lib/deploy/remoteconfig/deploy.js +4 -0
  232. package/lib/deploy/remoteconfig/functions.js +69 -0
  233. package/lib/deploy/remoteconfig/index.js +9 -0
  234. package/lib/deploy/remoteconfig/prepare.js +23 -0
  235. package/lib/deploy/remoteconfig/release.js +14 -0
  236. package/lib/deploy/storage/deploy.js +12 -0
  237. package/lib/deploy/storage/index.js +9 -0
  238. package/lib/deploy/storage/prepare.js +52 -0
  239. package/lib/deploy/storage/release.js +27 -0
  240. package/lib/deploymentTool.js +22 -0
  241. package/lib/detectProjectRoot.js +28 -0
  242. package/lib/downloadUtils.js +37 -0
  243. package/lib/dynamicImport.js +22 -0
  244. package/lib/emulator/ExpressBasedEmulator.js +92 -0
  245. package/lib/emulator/adminSdkConfig.js +65 -0
  246. package/lib/emulator/auth/apiSpec.js +8138 -0
  247. package/lib/emulator/auth/cloudFunctions.js +70 -0
  248. package/lib/emulator/auth/errors.js +77 -0
  249. package/lib/emulator/auth/handlers.js +281 -0
  250. package/lib/emulator/auth/index.js +146 -0
  251. package/lib/emulator/auth/operations.js +2145 -0
  252. package/lib/emulator/auth/schema.js +2 -0
  253. package/lib/emulator/auth/server.js +474 -0
  254. package/lib/emulator/auth/state.js +668 -0
  255. package/lib/emulator/auth/types.js +2 -0
  256. package/lib/emulator/auth/utils.js +84 -0
  257. package/lib/emulator/auth/widget_ui.js +620 -0
  258. package/lib/emulator/commandUtils.js +380 -0
  259. package/lib/emulator/constants.js +111 -0
  260. package/lib/emulator/controller.js +654 -0
  261. package/lib/emulator/databaseEmulator.js +172 -0
  262. package/lib/emulator/dns.js +49 -0
  263. package/lib/emulator/download.js +88 -0
  264. package/lib/emulator/downloadableEmulators.js +384 -0
  265. package/lib/emulator/emulatorLogger.js +237 -0
  266. package/lib/emulator/env.js +37 -0
  267. package/lib/emulator/eventarcEmulator.js +143 -0
  268. package/lib/emulator/eventarcEmulatorUtils.js +62 -0
  269. package/lib/emulator/events/types.js +28 -0
  270. package/lib/emulator/extensions/postinstall.js +41 -0
  271. package/lib/emulator/extensions/validation.js +72 -0
  272. package/lib/emulator/extensionsEmulator.js +254 -0
  273. package/lib/emulator/firestoreEmulator.js +96 -0
  274. package/lib/emulator/functionsEmulator.js +1038 -0
  275. package/lib/emulator/functionsEmulatorRuntime.js +730 -0
  276. package/lib/emulator/functionsEmulatorShared.js +322 -0
  277. package/lib/emulator/functionsEmulatorShell.js +88 -0
  278. package/lib/emulator/functionsEmulatorUtils.js +91 -0
  279. package/lib/emulator/functionsRuntimeWorker.js +314 -0
  280. package/lib/emulator/hostingEmulator.js +40 -0
  281. package/lib/emulator/hub.js +162 -0
  282. package/lib/emulator/hubClient.js +53 -0
  283. package/lib/emulator/hubExport.js +208 -0
  284. package/lib/emulator/loggingEmulator.js +118 -0
  285. package/lib/emulator/portUtils.js +323 -0
  286. package/lib/emulator/pubsubEmulator.js +186 -0
  287. package/lib/emulator/registry.js +122 -0
  288. package/lib/emulator/shared/request.js +19 -0
  289. package/lib/emulator/storage/apis/firebase.js +500 -0
  290. package/lib/emulator/storage/apis/gcloud.js +405 -0
  291. package/lib/emulator/storage/apis/shared.js +44 -0
  292. package/lib/emulator/storage/cloudFunctions.js +88 -0
  293. package/lib/emulator/storage/crc.js +33 -0
  294. package/lib/emulator/storage/errors.js +9 -0
  295. package/lib/emulator/storage/files.js +410 -0
  296. package/lib/emulator/storage/index.js +98 -0
  297. package/lib/emulator/storage/metadata.js +349 -0
  298. package/lib/emulator/storage/multipart.js +63 -0
  299. package/lib/emulator/storage/persistence.js +92 -0
  300. package/lib/emulator/storage/rules/config.js +53 -0
  301. package/lib/emulator/storage/rules/expressionValue.js +2 -0
  302. package/lib/emulator/storage/rules/manager.js +88 -0
  303. package/lib/emulator/storage/rules/runtime.js +363 -0
  304. package/lib/emulator/storage/rules/types.js +19 -0
  305. package/lib/emulator/storage/rules/utils.js +60 -0
  306. package/lib/emulator/storage/server.js +113 -0
  307. package/lib/emulator/storage/upload.js +143 -0
  308. package/lib/emulator/types.js +200 -0
  309. package/lib/emulator/ui.js +52 -0
  310. package/lib/emulator/workQueue.js +114 -0
  311. package/lib/ensureApiEnabled.js +110 -0
  312. package/lib/ensureCloudResourceLocation.js +12 -0
  313. package/lib/error.js +29 -0
  314. package/lib/errorOut.js +23 -0
  315. package/lib/experiments.js +153 -0
  316. package/lib/extensions/askUserForEventsConfig.js +97 -0
  317. package/lib/extensions/askUserForParam.js +338 -0
  318. package/lib/extensions/billingMigrationHelper.js +61 -0
  319. package/lib/extensions/change-log.js +95 -0
  320. package/lib/extensions/checkProjectBilling.js +82 -0
  321. package/lib/extensions/diagnose.js +56 -0
  322. package/lib/extensions/displayExtensionInfo.js +175 -0
  323. package/lib/extensions/emulator/optionsHelper.js +73 -0
  324. package/lib/extensions/emulator/specHelper.js +102 -0
  325. package/lib/extensions/emulator/triggerHelper.js +105 -0
  326. package/lib/extensions/etags.js +28 -0
  327. package/lib/extensions/export.js +64 -0
  328. package/lib/extensions/extensionsApi.js +361 -0
  329. package/lib/extensions/extensionsHelper.js +862 -0
  330. package/lib/extensions/listExtensions.js +53 -0
  331. package/lib/extensions/localHelper.js +67 -0
  332. package/lib/extensions/manifest.js +205 -0
  333. package/lib/extensions/metricsTypeDef.js +2 -0
  334. package/lib/extensions/metricsUtils.js +98 -0
  335. package/lib/extensions/paramHelper.js +140 -0
  336. package/lib/extensions/provisioningHelper.js +109 -0
  337. package/lib/extensions/publishHelpers.js +8 -0
  338. package/lib/extensions/publisherApi.js +214 -0
  339. package/lib/extensions/refs.js +67 -0
  340. package/lib/extensions/resolveSource.js +24 -0
  341. package/lib/extensions/secretsUtils.js +60 -0
  342. package/lib/extensions/tos.js +90 -0
  343. package/lib/extensions/types.js +25 -0
  344. package/lib/extensions/updateHelper.js +127 -0
  345. package/lib/extensions/utils.js +63 -0
  346. package/lib/extensions/versionHelper.js +14 -0
  347. package/lib/extensions/warnings.js +44 -0
  348. package/lib/fetchMOTD.js +46 -0
  349. package/lib/fetchWebSetup.js +75 -0
  350. package/lib/filterTargets.js +34 -0
  351. package/lib/firebaseConfig.js +2 -0
  352. package/lib/firebaseConfigValidate.js +29 -0
  353. package/lib/firestore/api-sort.js +118 -0
  354. package/lib/firestore/api-spec.js +2 -0
  355. package/lib/firestore/api-types.js +60 -0
  356. package/lib/firestore/api.js +566 -0
  357. package/lib/firestore/checkDatabaseType.js +18 -0
  358. package/lib/firestore/delete.js +347 -0
  359. package/lib/firestore/encodeFirestoreValue.js +59 -0
  360. package/lib/firestore/fsConfig.js +74 -0
  361. package/lib/firestore/options.js +2 -0
  362. package/lib/firestore/util.js +39 -0
  363. package/lib/firestore/validator.js +38 -0
  364. package/lib/frameworks/angular/index.js +193 -0
  365. package/lib/frameworks/angular/interfaces.js +2 -0
  366. package/lib/frameworks/angular/utils.js +431 -0
  367. package/lib/frameworks/astro/index.js +73 -0
  368. package/lib/frameworks/astro/utils.js +40 -0
  369. package/lib/frameworks/compose/discover/filesystem.js +52 -0
  370. package/lib/frameworks/compose/discover/frameworkMatcher.js +76 -0
  371. package/lib/frameworks/compose/discover/frameworkSpec.js +39 -0
  372. package/lib/frameworks/compose/discover/index.js +32 -0
  373. package/lib/frameworks/compose/discover/runtime/node.js +149 -0
  374. package/lib/frameworks/compose/discover/types.js +2 -0
  375. package/lib/frameworks/compose/driver/docker.js +187 -0
  376. package/lib/frameworks/compose/driver/hooks.js +22 -0
  377. package/lib/frameworks/compose/driver/index.js +16 -0
  378. package/lib/frameworks/compose/driver/local.js +51 -0
  379. package/lib/frameworks/compose/index.js +31 -0
  380. package/lib/frameworks/compose/interfaces.js +21 -0
  381. package/lib/frameworks/constants.js +40 -0
  382. package/lib/frameworks/docs/index.js +2 -0
  383. package/lib/frameworks/express/index.js +102 -0
  384. package/lib/frameworks/flutter/constants.js +72 -0
  385. package/lib/frameworks/flutter/index.js +58 -0
  386. package/lib/frameworks/flutter/utils.js +11 -0
  387. package/lib/frameworks/frameworks.js +31 -0
  388. package/lib/frameworks/index.js +421 -0
  389. package/lib/frameworks/interfaces.js +2 -0
  390. package/lib/frameworks/lit/index.js +23 -0
  391. package/lib/frameworks/next/constants.js +12 -0
  392. package/lib/frameworks/next/index.js +403 -0
  393. package/lib/frameworks/next/interfaces.js +2 -0
  394. package/lib/frameworks/next/utils.js +197 -0
  395. package/lib/frameworks/nuxt/index.js +92 -0
  396. package/lib/frameworks/nuxt/interfaces.js +2 -0
  397. package/lib/frameworks/nuxt/utils.js +23 -0
  398. package/lib/frameworks/nuxt2/index.js +79 -0
  399. package/lib/frameworks/preact/index.js +23 -0
  400. package/lib/frameworks/react/index.js +23 -0
  401. package/lib/frameworks/svelte/index.js +23 -0
  402. package/lib/frameworks/sveltekit/index.js +54 -0
  403. package/lib/frameworks/sveltekit/interfaces.js +2 -0
  404. package/lib/frameworks/utils.js +260 -0
  405. package/lib/frameworks/vite/index.js +99 -0
  406. package/lib/fsAsync.js +43 -0
  407. package/lib/fsutils.js +35 -0
  408. package/lib/functional.js +87 -0
  409. package/lib/functions/constants.js +14 -0
  410. package/lib/functions/ensureTargeted.js +21 -0
  411. package/lib/functions/env.js +246 -0
  412. package/lib/functions/events/index.js +7 -0
  413. package/lib/functions/events/v1.js +6 -0
  414. package/lib/functions/events/v2.js +25 -0
  415. package/lib/functions/functionslog.js +35 -0
  416. package/lib/functions/projectConfig.js +68 -0
  417. package/lib/functions/python.js +23 -0
  418. package/lib/functions/runtimeConfigExport.js +141 -0
  419. package/lib/functions/secrets.js +239 -0
  420. package/lib/functionsConfig.js +157 -0
  421. package/lib/functionsConfigClone.js +68 -0
  422. package/lib/functionsShellCommandAction.js +112 -0
  423. package/lib/gcp/artifactregistry.js +16 -0
  424. package/lib/gcp/auth.js +16 -0
  425. package/lib/gcp/cloudbilling.js +25 -0
  426. package/lib/gcp/cloudbuild.js +50 -0
  427. package/lib/gcp/cloudfunctions.js +351 -0
  428. package/lib/gcp/cloudfunctionsv2.js +389 -0
  429. package/lib/gcp/cloudlogging.js +25 -0
  430. package/lib/gcp/cloudmonitoring.js +77 -0
  431. package/lib/gcp/cloudscheduler.js +155 -0
  432. package/lib/gcp/cloudtasks.js +162 -0
  433. package/lib/gcp/docker.js +80 -0
  434. package/lib/gcp/eventarc.js +42 -0
  435. package/lib/gcp/firestore.js +47 -0
  436. package/lib/gcp/frameworks.js +40 -0
  437. package/lib/gcp/iam.js +76 -0
  438. package/lib/gcp/identityPlatform.js +44 -0
  439. package/lib/gcp/index.js +11 -0
  440. package/lib/gcp/location.js +44 -0
  441. package/lib/gcp/proto.js +130 -0
  442. package/lib/gcp/pubsub.js +34 -0
  443. package/lib/gcp/resourceManager.js +74 -0
  444. package/lib/gcp/rules.js +156 -0
  445. package/lib/gcp/run.js +157 -0
  446. package/lib/gcp/runtimeconfig.js +127 -0
  447. package/lib/gcp/secretManager.js +207 -0
  448. package/lib/gcp/serviceusage.js +24 -0
  449. package/lib/gcp/storage.js +111 -0
  450. package/lib/getDefaultDatabaseInstance.js +10 -0
  451. package/lib/getDefaultHostingSite.js +32 -0
  452. package/lib/getProjectNumber.js +15 -0
  453. package/lib/handlePreviewToggles.js +38 -0
  454. package/lib/hosting/api.js +289 -0
  455. package/lib/hosting/cloudRunProxy.js +52 -0
  456. package/lib/hosting/config.js +189 -0
  457. package/lib/hosting/expireUtils.js +33 -0
  458. package/lib/hosting/functionsProxy.js +42 -0
  459. package/lib/hosting/implicitInit.js +58 -0
  460. package/lib/hosting/initMiddleware.js +63 -0
  461. package/lib/hosting/interactive.js +75 -0
  462. package/lib/hosting/options.js +3 -0
  463. package/lib/hosting/proxy.js +143 -0
  464. package/lib/hosting/runTags.js +114 -0
  465. package/lib/index.js +71 -0
  466. package/lib/init/features/account.js +62 -0
  467. package/lib/init/features/database.js +133 -0
  468. package/lib/init/features/emulators.js +102 -0
  469. package/lib/init/features/extensions/index.js +16 -0
  470. package/lib/init/features/firestore/index.js +36 -0
  471. package/lib/init/features/firestore/indexes.js +71 -0
  472. package/lib/init/features/firestore/rules.js +80 -0
  473. package/lib/init/features/frameworks/constants.js +7 -0
  474. package/lib/init/features/frameworks/index.js +116 -0
  475. package/lib/init/features/frameworks/repo.js +125 -0
  476. package/lib/init/features/functions/index.js +165 -0
  477. package/lib/init/features/functions/javascript.js +46 -0
  478. package/lib/init/features/functions/npm-dependencies.js +37 -0
  479. package/lib/init/features/functions/python.js +48 -0
  480. package/lib/init/features/functions/typescript.js +61 -0
  481. package/lib/init/features/hosting/github.js +397 -0
  482. package/lib/init/features/hosting/index.js +183 -0
  483. package/lib/init/features/index.js +27 -0
  484. package/lib/init/features/project.js +101 -0
  485. package/lib/init/features/remoteconfig.js +32 -0
  486. package/lib/init/features/storage.js +27 -0
  487. package/lib/init/index.js +45 -0
  488. package/lib/listFiles.js +15 -0
  489. package/lib/loadCJSON.js +17 -0
  490. package/lib/localFunction.js +222 -0
  491. package/lib/logError.js +29 -0
  492. package/lib/logger.js +31 -0
  493. package/lib/management/apps.js +280 -0
  494. package/lib/management/database.js +184 -0
  495. package/lib/management/projects.js +312 -0
  496. package/lib/metaprogramming.js +2 -0
  497. package/lib/monospace/index.js +82 -0
  498. package/lib/monospace/interfaces.js +2 -0
  499. package/lib/operation-poller.js +59 -0
  500. package/lib/options.js +2 -0
  501. package/lib/parseBoltRules.js +29 -0
  502. package/lib/profileReport.js +577 -0
  503. package/lib/profiler.js +99 -0
  504. package/lib/projectPath.js +16 -0
  505. package/lib/projectUtils.js +59 -0
  506. package/lib/prompt.js +50 -0
  507. package/lib/rc.js +165 -0
  508. package/lib/remoteconfig/get.js +49 -0
  509. package/lib/remoteconfig/interfaces.js +17 -0
  510. package/lib/remoteconfig/rollback.js +22 -0
  511. package/lib/remoteconfig/versionslist.js +33 -0
  512. package/lib/requireAuth.js +81 -0
  513. package/lib/requireConfig.js +13 -0
  514. package/lib/requireDatabaseInstance.js +26 -0
  515. package/lib/requireHostingSite.js +12 -0
  516. package/lib/requireInteractive.js +12 -0
  517. package/lib/requirePermissions.js +30 -0
  518. package/lib/responseToError.js +58 -0
  519. package/lib/rtdb.js +35 -0
  520. package/lib/rulesDeploy.js +180 -0
  521. package/lib/scopes.js +10 -0
  522. package/lib/serve/functions.js +64 -0
  523. package/lib/serve/hosting.js +115 -0
  524. package/lib/serve/index.js +53 -0
  525. package/lib/shortenUrl.js +29 -0
  526. package/lib/throttler/errors/retries-exhausted-error.js +11 -0
  527. package/lib/throttler/errors/task-error.js +9 -0
  528. package/lib/throttler/errors/timeout-error.js +9 -0
  529. package/lib/throttler/queue.js +23 -0
  530. package/lib/throttler/stack.js +28 -0
  531. package/lib/throttler/throttler.js +230 -0
  532. package/lib/track.js +196 -0
  533. package/lib/unzip.js +126 -0
  534. package/lib/utils.js +488 -0
  535. package/package.json +239 -0
  536. package/schema/extension-yaml.json +432 -0
  537. package/schema/firebase-config.json +2619 -0
  538. package/standalone/check.js +6 -0
  539. package/standalone/config.template.js +19 -0
  540. package/standalone/firepit.js +1074 -0
  541. package/standalone/package-lock.json +7019 -0
  542. package/standalone/package.json +36 -0
  543. package/standalone/runtime.js +122 -0
  544. package/standalone/welcome.js +178 -0
  545. package/templates/_gitignore +66 -0
  546. package/templates/banner.txt +6 -0
  547. package/templates/emulators/default_storage.rules +8 -0
  548. package/templates/extensions/CL-template.md +2 -0
  549. package/templates/extensions/POSTINSTALL.md +29 -0
  550. package/templates/extensions/PREINSTALL.md +22 -0
  551. package/templates/extensions/extension.yaml +61 -0
  552. package/templates/extensions/integration-test.env +2 -0
  553. package/templates/extensions/integration-test.json +14 -0
  554. package/templates/extensions/javascript/WELCOME.md +18 -0
  555. package/templates/extensions/javascript/_gitignore +1 -0
  556. package/templates/extensions/javascript/index.js +25 -0
  557. package/templates/extensions/javascript/integration-test.js +13 -0
  558. package/templates/extensions/javascript/package.lint.json +25 -0
  559. package/templates/extensions/javascript/package.nolint.json +19 -0
  560. package/templates/extensions/typescript/WELCOME.md +22 -0
  561. package/templates/extensions/typescript/_gitignore +8 -0
  562. package/templates/extensions/typescript/_mocharc +10 -0
  563. package/templates/extensions/typescript/index.ts +26 -0
  564. package/templates/extensions/typescript/integration-test.ts +13 -0
  565. package/templates/extensions/typescript/package.lint.json +31 -0
  566. package/templates/extensions/typescript/package.nolint.json +24 -0
  567. package/templates/extensions/typescript/tsconfig.dev.json +5 -0
  568. package/templates/extensions/typescript/tsconfig.json +15 -0
  569. package/templates/firebase.json +9 -0
  570. package/templates/hosting/init.js +36 -0
  571. package/templates/init/firestore/firestore.indexes.json +26 -0
  572. package/templates/init/firestore/firestore.rules +16 -0
  573. package/templates/init/functions/javascript/_eslintrc +28 -0
  574. package/templates/init/functions/javascript/_gitignore +1 -0
  575. package/templates/init/functions/javascript/index.js +19 -0
  576. package/templates/init/functions/javascript/package.lint.json +26 -0
  577. package/templates/init/functions/javascript/package.nolint.json +23 -0
  578. package/templates/init/functions/python/_gitignore +0 -0
  579. package/templates/init/functions/python/main.py +13 -0
  580. package/templates/init/functions/python/requirements.txt +1 -0
  581. package/templates/init/functions/typescript/_eslintrc +32 -0
  582. package/templates/init/functions/typescript/_gitignore +9 -0
  583. package/templates/init/functions/typescript/index.ts +19 -0
  584. package/templates/init/functions/typescript/package.lint.json +31 -0
  585. package/templates/init/functions/typescript/package.nolint.json +25 -0
  586. package/templates/init/functions/typescript/tsconfig.dev.json +5 -0
  587. package/templates/init/functions/typescript/tsconfig.json +15 -0
  588. package/templates/init/hosting/404.html +33 -0
  589. package/templates/init/hosting/index.html +89 -0
  590. package/templates/init/storage/storage.rules +12 -0
  591. package/templates/loginFailure.html +34 -0
  592. package/templates/loginSuccess.html +32 -0
  593. package/templates/loginSuccessGithub.html +32 -0
  594. package/templates/setup/web.js +5 -0
@@ -0,0 +1,1074 @@
1
+ /*
2
+ -------------------------------------
3
+ Introduction
4
+ -------------------------------------
5
+
6
+ "This is probably the scariest 1000 lines of code I have ever seen" - Sam Stern
7
+
8
+ Welcome to Firepit! This script (and it's siblings) is a bundle of magical
9
+ code which allow the firebase-tools package to run on a developer's machine without
10
+ a dependency on Node.js as a single, standalone binary.
11
+
12
+ If firebase-tools was a simpler tool, Firepit would also be simpler, however... it's
13
+ not. The "firebase" command relies on a few patterns which make bundling it without
14
+ Node.js particularly difficult, specifically it enjoys shelling out to npm / node.
15
+ Most of the work in this package is to properly ensure that those commands (npm, node)
16
+ exist and function as expected even when deep in multiple layers of shelling.
17
+
18
+ Some examples of how shelling is used...
19
+
20
+ 1) Running any "firebase" command will automatically call npm to check is the "firebase-tools"
21
+ package itself is outdated.
22
+
23
+ 2) Running "firebase deploy --only functions" uses npm to build and prepare the developer's
24
+ Cloud Functions code.
25
+
26
+ 3) Developer's Cloud Functions may require being built with Typescript or other tools which require
27
+ access to Node / npm
28
+
29
+ The majority of firebase-tools commands work perfectly with minimal effort from Firepit,
30
+ specifically any JavaScript-only commands (which are most) work totally fine. Most of the
31
+ complexity is related to building and deploying Cloud Functions.
32
+
33
+ Firepit's job isn't *just* to ensure all commands work, it also simplifies the getting
34
+ started flows for developers by offering a "hand-holding" setup (see welcome.js) and
35
+ improving what we call the "double-click" experience (when a developer downloads the file and
36
+ clicks it to run).
37
+
38
+ Beyond that Firepit also puts extra effort into ensuring that *any* "firebase" related command
39
+ will still function if copy/pasted from existing tutorials. Specifically, if the internet says
40
+ running "npm update -g firebase-tools" will update your CLI, then the internet must be right and
41
+ we need to support that.
42
+
43
+ This code is generally very carefully written with special care given to cross platform compatibility.
44
+ We avoid many cross-platform problems by getting *back* into Node as soon as possible. We'll talk
45
+ more about this below, but most code which helps Firepit work cross-platform is not platform-specific
46
+ code, but in fact uses Node's natural cross-platform tools / libraries to help out as much as possible.
47
+ We'll discuss this more in detail below.
48
+
49
+ Ready? Let's go!
50
+ */
51
+
52
+ /*
53
+ -------------------------------------
54
+ Globals
55
+ -------------------------------------
56
+
57
+ Our dependencies are largely uninteresting, we use "user-home" to know where to install our scripts
58
+ and files to, we use "chalk" for nice colors, and we use a handful of built in libraries for
59
+ their intended purposes.
60
+
61
+ The most interesting dep is "shelljs". This library is a collection of Unix-style commands like
62
+ (cat, ls, mkdir, etc) which are reimplemented in cross-platform JavaScript. They function
63
+ identically across platforms and help us whenever we're dealing with the filesystem. The names
64
+ are universal and easy to understand for anyone with a *nix background.
65
+
66
+ We also include our own package.json so we can report the Firepit version to Google Analytics.
67
+ */
68
+
69
+ const fs = require("fs");
70
+ const path = require("path");
71
+ const { fork, spawn } = require("child_process");
72
+ const homePath = require("user-home");
73
+ const chalk = require("chalk");
74
+ const shell = require("shelljs");
75
+ shell.config.silent = true;
76
+ const version = require("./package.json").version;
77
+
78
+ /*
79
+ Our only other require, the "./runtime.js" file, is worth discussing in detail. The script itself
80
+ is documented in itself, so you're welcome to read that, however the more important topic is the
81
+ general structure Firepit uses.
82
+
83
+ Firepit loops back into itself constantly and is essentially a router which ensures that incoming
84
+ invocations end up calling the correct scripts using the embedded Node runtime. A Firepit binary
85
+ doesn't include *just* the "firebase" command, it also includes "npm" and "node" because these
86
+ are needed by "firebase-tools" to be fully functional. When running in headful (double-click)
87
+ mode these commands are exposed to the developer, they can run "npm" just like they would with
88
+ a normal Node install, however internally it's not *really* npm, they're invoking a shell script
89
+ which comes back into a new Firepit process and is then routed to the npm scripts.
90
+
91
+ When you're not running Firepit in headful mode, these sub-commands can still be accessed via
92
+ hidden flags...
93
+
94
+ firebase is:npm install -g chalk // Calls npm
95
+ firebase is:node ./script.js // Calls node
96
+ firebase --help // Calls firebase-tools
97
+
98
+ These hidden flags aren't intended to be used by end-developers, they're needed because we're
99
+ constantly hoping out of the Firepit process. For example Firepit spawn a shell, the shell calls
100
+ "npm" (which is actually a new Firepit process) which calls the npm scripts which invokes a user's
101
+ build script which spawns a node process (which is actually a new Firepit process) and so on.
102
+
103
+ We use these special flags to give context between invocations and ask Firepit to imitate whatever
104
+ tool the user wants to call. (See Imitate*() functions)
105
+
106
+ In order to allow ensure that the "node", "npm", and "firebase" commands exist through all
107
+ these processes we can do two things.
108
+
109
+ 1) We can modify env variables like PATH to place our scripts in place of actual tools
110
+ 2) We can pass special flags to the tools we're pretending to be so they tell their children
111
+ that the world is how we want them to think it is.
112
+
113
+ Technically (and on a high level) When a developer runs Firepit we go through a series of steps.
114
+
115
+ 1) If needed, extract the copy of "firebase-tools" which is embedded in the binary file
116
+ (see SetupFirebaseTools())
117
+
118
+ 2) Generate a series of "runtime" scripts which get called from other processes. These scripts
119
+ look to the developer like the "npm" or "node" commands, but actually route back into Firepit
120
+ and are redirected to the embedded tools.
121
+ (see createRuntimeBinaries())
122
+
123
+ 3) Determine how we can access our embedded NodeJS runtime
124
+ (see VerifyNodePath())
125
+
126
+ 4) Modify the developers env variables to include the "runtime" scripts and other changes
127
+ (see firepit())
128
+
129
+ 5) Route the invocation to the correct command (firebase, npm, or node).
130
+ 6) Exit with the correct code and go to bed.
131
+
132
+ The "runtime.js" script contains two functions. In createRuntimeBinaries() we call .toString()
133
+ on these functions and write them to files (which later act like commands on the user's path).
134
+
135
+ The functions in "runtime.js" are not meant to be invoked from Firepit, but are standalone scripts
136
+ which get ran *through* Firepit when it is imitating Node.js.
137
+ */
138
+ const runtime = require("./runtime");
139
+
140
+
141
+ /*
142
+ We use a configuration file (see config.template.js) which is generated by our build pipeline to
143
+ determine if we're running in headless or headful mode.
144
+ */
145
+ let config;
146
+ try {
147
+ config = require("./config");
148
+ } catch (err) {
149
+ console.warn("Invalid Firepit configuration, this may be a broken build.");
150
+ process.exit(2);
151
+ }
152
+
153
+ const isWindows = process.platform === "win32";
154
+
155
+ /*
156
+ The installPath is where we'll place our extracted firebase-tools scripts.
157
+ The runtimeBinsPath is where we place our "npm" and "node" shell scripts which route back into
158
+ Firepit.
159
+ */
160
+ const installPath = path.join(homePath, ".cache", "firebase", "tools");
161
+ let runtimeBinsPath = path.join(homePath, ".cache", "firebase", "runtime");
162
+
163
+ /*
164
+ As I mentioned above, one of the ways we can control the detached children processes which get
165
+ created when using Firepit is to pass special arguments when we're pretending to be them.
166
+
167
+ In this case, when a user calls "npm" (and it routes to Firepit, pretending to be npm) we tack
168
+ on a few scripts which change the global config file to point to our custom installPath and
169
+ we supply a special "script shell".
170
+
171
+ This "script shell" is normally something like "bash" or "cmd.exe", however in our case, we want
172
+ to inject Firepit into there again to ensure everyone thinks the commands we're exposing still exist.
173
+
174
+ You can see the implementation of this script in runtime.js/Script_ShellJS().
175
+
176
+ When npm invokes a script on behalf of the developer (like when they run "npm run build") this
177
+ command is then spawned in npm as "$SCRIPT_SHELL $USER_SCRIPT" so by replacing the this shell
178
+ we can set up env variables / PATHs / etc then spawn the $USER_SCRIPT manually so the behavior
179
+ looks no different.
180
+
181
+ We use these base npmArgs every time we pretend to be npm. They can be overwritten by a user if
182
+ they manually specify any of these flags and that would produce unexpected behavior.
183
+ */
184
+ const npmArgs = [
185
+ `--script-shell=${runtimeBinsPath}/shell${isWindows ? ".bat" : ""}`,
186
+ `--globalconfig=${path.join(runtimeBinsPath, "npmrc")}`,
187
+ `--scripts-prepend-node-path=auto`
188
+ ];
189
+
190
+ /*
191
+ Windows is terrible and through-out Firepit you'll see references to "safe" and "unsafe" paths.
192
+ Unsafe paths, on Windows, are ones with things like spaces in them - yes spaces break stuff.
193
+
194
+ There is debate about who is at fault. It may be npm, it may be Node, it may be Microsoft, regardless
195
+ if your username on Windows (for example) has a space in it, it'll break everything.
196
+
197
+ Luckily because we control the universe in Firepit, we can use a crazy hack to replace any
198
+ evil (i.e. space-inclusive) paths with DOS (yes DOS) style paths. See getSafeCrossPlatformPath()
199
+
200
+ For example:
201
+
202
+ unsafePath: C:\Program Files\Java\jdk1.6.0_22
203
+ safePath: C:\PROGRA~1\Java\JDK16~1.0_2
204
+
205
+ We use the safePath when needed (specifically when passing them through cmd.exe) to reduce the
206
+ chances of space-related bugs.
207
+
208
+ This is needed *all* the time, but it's pretty common in here.
209
+ */
210
+ let safeNodePath;
211
+ const unsafeNodePath = process.argv[0];
212
+
213
+ /*
214
+ Firepit supports some additional flags that the firebase command does not. These flags are
215
+ generally used internally when Firepit invokes Firepit (for example, during welcome.js).
216
+
217
+ If you want to run any of these flags, invoke Firepit with --tool:$COMMAND
218
+ */
219
+ const flagDefinitions = [
220
+ "file-debug", // --tool:file-debug - Write log to a file
221
+ "log-debug", // --tool:log-debug - Write log to stdout
222
+ "disable-write", // --tool:disable-write - Do not write runtime scripts to filesystem
223
+ "runtime-check", // --tool:runtime-check - Determine if firepit binary is node or not (see VerifyNodePath())
224
+ "setup-check", // --tool:setup-check - Check if firebase-tools is set up
225
+ "force-setup", // --tool:force-setup - Force Firepit to go through setup
226
+ "force-update", // --tool:force-update - Aggressively clear npm cache and re-setup
227
+ "ignore-embedded-cache" // --tool:ignore-embedded-cache - Setup from online, do not use embedded firebase-tools
228
+ ];
229
+
230
+ /*
231
+ This script parses our flagDefinitions and returns a map like {file-debug: false, ...}
232
+ */
233
+ const flags = flagDefinitions.reduce((flags, name) => {
234
+ flags[name] = process.argv.indexOf(`--tool:${name}`) !== -1;
235
+ if (flags[name]) {
236
+ process.argv.splice(process.argv.indexOf(`--tool:${name}`), 1);
237
+ }
238
+
239
+ return flags;
240
+ }, {});
241
+
242
+ /*
243
+ We use @zeit/pkg to actually bundle our JavaScript with the NodeJS runtime to produce our binaries.
244
+ In general if you're running your code inside of pkg and you attempt to spawn the pkg binary which
245
+ you invoked to run your code (i.e. firepit.exe invokes firepit.exe) what you'll actually be
246
+ invoking is the underlying Node.js binary which is embedded is the binary.
247
+
248
+ This works well, albeit it may be a bit unexpected, however due to the nature of Firepit,
249
+ there's no assurance that we'll actually be in the same process at any given time.
250
+
251
+ For example, if we invoke "./firepit" and Firepit spawns a shell and that shell is used to call "firebase"
252
+ we're now in a situation where invoking "./firepit" from "firebase" will act as a fresh call to
253
+ Firepit, resulting it in running through the setup and such.
254
+
255
+ In another example, if we invoke "./firepit" and it immediately spawns "./firepit" then it'll
256
+ be spawning a node process.
257
+
258
+ I know this is confusing, but the moral is that we can be sure at any moment if spawning "./firepit"
259
+ will provide us with this file running in Node or just a Node runtime.
260
+
261
+ To detect what the "firepit" binary is we run "./firepit check.js --tool:runtime-check" in
262
+ VerifyNodePath(). If "./firepit" is acting as Firepit, this conditional will flip and we'll
263
+ just exit out. If "./firepit" is acting as a Node runtime, it'll invoke check.js and return
264
+ a unicode ✓. This allows us to know if we can safely invoke Node scripts by calling ourselves
265
+ or if we must call "./firepit is:node ./script" to force it to manually imitate Node.
266
+ */
267
+ if (flags["runtime-check"]) {
268
+ console.log(`firepit invoked for runtime check, exiting subpit.`);
269
+ return;
270
+ }
271
+
272
+ debug(`Welcome to firepit v${version}!`);
273
+
274
+ /*
275
+
276
+ -------------------------------------
277
+ The Main Path
278
+ -------------------------------------
279
+
280
+ When running Firepit, we start here. This async closure handles checking most of the --tool flags
281
+ and ensuring that Firepit is setup and in-place before running firepit()
282
+ */
283
+ (async () => {
284
+ /*
285
+ Any time we invoke a child process from Firepit, we tack on a FIREPIT_VERSION env variable.
286
+ This is useful here so we can detect if we are the "top level" Firepit instance.
287
+
288
+ For example, if you are running Firepit in headful mode then the first instance of Firepit
289
+ spawns you a command prompt window. In that command prompt we go through welcome.js then
290
+ you're given access to the "firebase" command.
291
+
292
+ When you run "firebase", if we didn't know if we were top-level then you'd just spawn another
293
+ command prompt window - clearly not what we want. So we look for the env variable set by the
294
+ process which spawned the window. If it exists, we functionally fall into "headless" mode
295
+ and act like a normal Firebase CLI.
296
+ */
297
+ const isTopLevel = !process.env.FIREPIT_VERSION;
298
+
299
+ /*
300
+ As I mentioned above, we make heavy use of this function to DOS-isy paths to avoid space-issues.
301
+ In this case, we're using process.argv[0] (always a reference to the node binary which spawned
302
+ this script) and turning it safe so we have an invokable Node.js runtime for later.
303
+ */
304
+ safeNodePath = await getSafeCrossPlatformPath(isWindows, process.argv[0]);
305
+ /*
306
+ If the user has ever had an older version of Firepit, clear it out and replace it with us.
307
+ */
308
+ uninstallLegacyFirepit();
309
+
310
+ /*
311
+ --tool:setup-check is used by welcome.js and returns out a JSON list of binaries for the "firebase"
312
+ command. It's essentially a check to see if we can find a copy of "firebase" to invoke.
313
+
314
+ The FindTool function looks in several places for where it thinks our firebase script might be
315
+ and returns as many as it fins. We almost always use the 0th one.
316
+ */
317
+ if (flags["setup-check"]) {
318
+ const bins = FindTool("firebase-tools/lib/bin/firebase");
319
+
320
+ for (const bin of bins) {
321
+ bins[bin] = await getSafeCrossPlatformPath(bins[bin]);
322
+ }
323
+
324
+ console.log(JSON.stringify({ bins }));
325
+ return;
326
+ }
327
+
328
+
329
+ /*
330
+ --tool:force-update is never used internally, but can be useful for EAPs where version numbers
331
+ may be incorrect. This manually clear NPMs cache and then flips the flags "ignore-embedded-cache"
332
+ and "force-setup" to tell Firepit to install itself from the remote package (either a link to a
333
+ tgz or just firebase-tools@latest).
334
+ */
335
+ if (flags["force-update"]) {
336
+ console.log(`Please wait while we clear npm's cache...`);
337
+
338
+ /*
339
+ This is the first instance of invoking one of the Imitate*() methods. These methods are
340
+ the methods which "route" to the underlying scripts for each command. As you'd expect
341
+ ImitateNPM forces the process to act just like npm.
342
+
343
+ By replacing the process.argv before calling ImitateNPM(), we're rewriting what the
344
+ command was which called Firepit. For example, this snippet creates the following command...
345
+
346
+ /blah/blah/node ./firepit.js is:npm cache clean --force
347
+
348
+ As far as Firepit is concerned, this looks just like invoking it with is:npm from the top.
349
+
350
+ It may be cleaner to have Imitate*() take an array of command strings instead of modifying
351
+ process.argv, but for now I'll leave it like this.
352
+ */
353
+ process.argv = [
354
+ ...process.argv.slice(0, 2),
355
+ "is:npm",
356
+ "cache",
357
+ "clean",
358
+ "--force"
359
+ ];
360
+
361
+ /*
362
+ The Imitate*() methods also always return codes (0, 1, 2) from the underlying script. We
363
+ need to make sure we bubble these up because incorrect handling of exit codes will create
364
+ unexpected behavior in scripts.
365
+ */
366
+ const code = await ImitateNPM();
367
+
368
+ if (code) {
369
+ console.log("NPM cache clearing failed, can't update.");
370
+ process.exit(code);
371
+ }
372
+
373
+ flags["ignore-embedded-cache"] = true;
374
+ flags["force-setup"] = true;
375
+ console.log(`Clearing out your firebase-tools setup...`);
376
+
377
+ /*
378
+ Here's a handy use of shelljs. It's stupidly hard to recursively remove a directory with
379
+ Node's standard libs. Shelljs makes it trivial.
380
+ */
381
+ shell.rm("-rf", installPath);
382
+ }
383
+
384
+ /*
385
+ Every time Firepit is invoked it recreates the runtime binaries (node, npm, shell) because
386
+ these binaries need to know the current location of the Firepit binary. See the function
387
+ comments for more.
388
+ */
389
+ await createRuntimeBinaries();
390
+
391
+ /*
392
+ If we're in --tool:force-setup then extract or remotely install firebase-tools then exit out.
393
+ */
394
+ if (flags["force-setup"]) {
395
+ debug("Forcing setup...");
396
+ await SetupFirebaseTools();
397
+ console.log("firebase-tools setup complete.");
398
+ return;
399
+ }
400
+
401
+ /*
402
+ As I mentioned above, isTopLevel is basically the same as headless mode. There's an entire flow
403
+ here which revolves around invoking "./welcome.js" See that script for more details
404
+ */
405
+ if (isTopLevel && !config.headless) {
406
+ const welcome_path = await getSafeCrossPlatformPath(
407
+ isWindows,
408
+ path.join(__dirname, "/welcome.js")
409
+ );
410
+
411
+ const firebaseToolsCommand = await getFirebaseToolsCommand();
412
+
413
+ /*
414
+ This function adds a directory onto the PATH env variable. On Windows they're ; separated
415
+ and *nix they're : seperated.
416
+ */
417
+ appendToPath(isWindows, [path.join(installPath, "bin"), runtimeBinsPath]);
418
+
419
+ /*
420
+ As I mentioned above, we set the FIREPIT_VERSION env variable so that the shell we spawn
421
+ doesn't spawn another window and it instead acts as a headless firepit.
422
+ */
423
+ const shellEnv = {
424
+ FIREPIT_VERSION: version,
425
+ ...process.env
426
+ };
427
+
428
+ if (isWindows) {
429
+ /*
430
+ This is some of the only platform specific bits we have here. On Windows, headful mode spawns
431
+ a custom cmd.exe prompt with doskey (alias) commands called to expose the "firebase" and "npm"
432
+ commands. We also set the prompt to a neat yellow ">" then invoke the welcome script.
433
+
434
+ This top level Firepit script sits open until the developer closes that terminal.
435
+ */
436
+ const shellConfig = {
437
+ stdio: "inherit",
438
+ env: shellEnv
439
+ };
440
+
441
+ spawn(
442
+ "cmd",
443
+ [
444
+ "/k",
445
+ [
446
+ `doskey firebase=${firebaseToolsCommand} $*`,
447
+ `doskey npm=${firebaseToolsCommand} is:npm $*`,
448
+ `set prompt=${chalk.yellow("$G")}`,
449
+ `${firebaseToolsCommand} is:node ${welcome_path} ${firebaseToolsCommand}`
450
+ ].join(" & ")
451
+ ],
452
+ shellConfig
453
+ );
454
+
455
+ process.on("SIGINT", () => {
456
+ debug("Received SIGINT. Refusing to close top-level shell.");
457
+ });
458
+ } else {
459
+ /*
460
+ If we're not on Windows, then we can technically perform headful mode on Mac. By default double-clicking
461
+ a binary on Mac will pop up a terminal, so we just invoke the welcome screen and set the bash prompt.
462
+ */
463
+ process.argv = [
464
+ ...process.argv.slice(0, 2),
465
+ "is:node",
466
+ welcome_path,
467
+ firebaseToolsCommand
468
+ ];
469
+ const code = await ImitateNode();
470
+
471
+ if (code) {
472
+ console.log("Node failed to run welcome script.");
473
+ process.exit(code);
474
+ }
475
+
476
+ spawn("bash", {
477
+ env: { ...shellEnv, PS1: "\\e[0;33m> \\e[m" },
478
+ stdio: "inherit"
479
+ });
480
+ }
481
+ } else {
482
+ /*
483
+ In the case that Firepit is not in headful mode (or it was loaded in headful more, but is
484
+ not the top level process), then we jump into the actual firepit() method which takes care
485
+ of routing the is:npm, is:node, or other core modes.
486
+ */
487
+ SetWindowTitle("Firebase CLI");
488
+ await firepit();
489
+ }
490
+
491
+ if (flags["file-debug"]) {
492
+ fs.writeFileSync("firepit-log.txt", debug.log.join("\n"));
493
+ }
494
+ })().catch(err => {
495
+ /*
496
+ Note we have a high-level catch here which attempts to catch any crazy firepit errors. This is
497
+ rarely hit, but it will produce a firepit-log.txt when some internal errors occur.
498
+ */
499
+ debug(err.toString());
500
+ console.log(
501
+ `This tool has encountered an error. Please file a bug on Github (https://github.com/firebase/firebase-tools/) and include firepit-log.txt`
502
+ );
503
+ fs.writeFileSync("firepit-log.txt", debug.log.join("\n"));
504
+ });
505
+
506
+
507
+ async function firepit() {
508
+ /*
509
+ When running inside Node, the "node" binary is stored in many places. As I mentioned earlier,
510
+ it's the 0th item of process.argv and it's also in a couple other places. To be safe we
511
+ get a "safe" version of the Node runtime path and replace all known references with this.
512
+ */
513
+ runtimeBinsPath = await getSafeCrossPlatformPath(isWindows, runtimeBinsPath);
514
+
515
+ // TODO: I'm not sure this is needed, more testing would be useful.
516
+ process.argv[0] = safeNodePath;
517
+ process.env.NODE = safeNodePath;
518
+ process.env._ = safeNodePath;
519
+
520
+ debug(safeNodePath);
521
+ debug(process.argv);
522
+
523
+ // TODO: This may not be needed since we invoke createRuntimeBinaries() earlier
524
+ await createRuntimeBinaries();
525
+ appendToPath(isWindows, [runtimeBinsPath]);
526
+
527
+ /*
528
+ We check for the is:npm and is:node flags and if either exist, we opt ot imitate that process
529
+ and then exit out when done.
530
+ */
531
+ if (process.argv.indexOf("is:npm") !== -1) {
532
+ const code = await ImitateNPM();
533
+ process.exit(code);
534
+ }
535
+
536
+ if (process.argv.indexOf("is:node") !== -1) {
537
+ const code = await ImitateNode();
538
+ process.exit(code);
539
+ }
540
+
541
+ /*
542
+ If Firepit was invoked in headless mode, there is a chance that firebase-tools has not been set
543
+ up yet (since the welcome screen was never shown and that script is what calls --tool:forces-setup.
544
+
545
+ To be sure, we attempt to find the firebase-tools script and if it's not found, we attempt a setup.
546
+
547
+ After the setup, if the script still isn't found then something is wrong and we die.
548
+ */
549
+ let firebaseBins = FindTool("firebase-tools/lib/bin/firebase");
550
+ if (!firebaseBins.length) {
551
+ debug(`CLI not found! Invoking setup...`);
552
+ await SetupFirebaseTools();
553
+ firebaseBins = FindTool("firebase-tools/lib/bin/firebase");
554
+ }
555
+
556
+ /*
557
+ Assuming we've gotten this far, we've found the CLI and we're ready to run firebase-tools.
558
+ That was easy, huh?
559
+ */
560
+ const firebaseBin = firebaseBins[0];
561
+ debug(`CLI install found at "${firebaseBin}", starting fork...`);
562
+ const code = await ImitateFirebaseTools(firebaseBin);
563
+ process.exit(code);
564
+ }
565
+
566
+ /*
567
+ -------------------------------------
568
+ Imitate*()
569
+ -------------------------------------
570
+
571
+ All of the Imitate*() methods are very similar. For is:npm and is:node we break process.argv
572
+ based on that string and then pass everything on the right to the script, which is forked from
573
+ the main Node process. We create a promise (which can be awaited) and then resolve when the
574
+ command is done.
575
+ */
576
+
577
+ function ImitateNPM() {
578
+ debug("Detected is:npm flag, calling NPM");
579
+ const breakerIndex = process.argv.indexOf("is:npm") + 1;
580
+ const args = [...npmArgs, ...process.argv.slice(breakerIndex)];
581
+ debug(args.join(" "));
582
+ return new Promise(resolve => {
583
+ const cmd = fork(FindTool("npm/bin/npm-cli")[0], args, {
584
+ stdio: "inherit",
585
+ env: process.env
586
+ });
587
+ cmd.on("close", code => {
588
+ debug(`faux-npm done.`);
589
+ resolve(code);
590
+ });
591
+ });
592
+ }
593
+
594
+ function ImitateNode() {
595
+ debug("Detected is:node flag, calling node");
596
+ const breakerIndex = process.argv.indexOf("is:node") + 1;
597
+ const nodeArgs = [...process.argv.slice(breakerIndex)];
598
+ return new Promise(resolve => {
599
+ const cmd = fork(nodeArgs[0], nodeArgs.slice(1), {
600
+ stdio: "inherit",
601
+ env: process.env
602
+ });
603
+ cmd.on("close", code => {
604
+ debug(`faux-node done.`);
605
+ resolve(code);
606
+ });
607
+ });
608
+ }
609
+
610
+ function ImitateFirebaseTools(binPath) {
611
+ debug("Detected no special flags, calling firebase-tools");
612
+ return new Promise(resolve => {
613
+ const cmd = fork(binPath, process.argv.slice(2), {
614
+ stdio: "inherit",
615
+ env: { ...process.env, FIREPIT_VERSION: version }
616
+ });
617
+ cmd.on("close", code => {
618
+ debug(`firebase-tools is done.`);
619
+ resolve(code);
620
+ });
621
+ });
622
+ }
623
+
624
+ /*
625
+ -------------------------------------
626
+ Core Functions
627
+ -------------------------------------
628
+ */
629
+
630
+ async function createRuntimeBinaries() {
631
+ /*
632
+ As discussed in the introduction, Firepit isn't *just* firebase-tools, it's also npm and node.
633
+ We need it to act as several CLI tools in order to support firebase-tools because it shells out
634
+ to these other commands in some situations.
635
+
636
+ In order to support this we add a few special scripts onto the users's path so when a user (or
637
+ script) invokes "npm" or "node" it redirects back into Firepit so we can control the environment
638
+ regardless of how that command was invoked.
639
+
640
+ To do this cross-platform, we need to create both shell and batch scripts (for nix / windows).
641
+ These scripts are kept very minimal, as you can see in runtimeBins, they're mostly one line or
642
+ two.
643
+
644
+ Each of the platform-specific scripts like "shell" or "node.bat" do the absolute minimum work
645
+ needed to act as an executable binary, then immediately redirect the arguments passed to it
646
+ back into Firepit via the "shell.js" or "node.js" scripts. (See runtime.js for contents). These
647
+ two scripts do the majority of heavy lifting in terms of imitating npm or node.
648
+
649
+ Originally, we implemented the node / npm stand-ins in pure bash or batch, however there was
650
+ way too much platform specific code, by redirecting us back into Firepit (and Node) we add
651
+ another process, but we also dramatically reduce per-platform code. The Node code is
652
+ cross-platform and works perfectly everywhere. It's also easier to test because any *nix
653
+ machine can functionally test the same code that would run on Windows or vice-versa.
654
+ */
655
+ const runtimeBins = {
656
+ /* Linux / OSX */
657
+ shell: `"${unsafeNodePath}" ${runtimeBinsPath}/shell.js "$@"`,
658
+ node: `"${unsafeNodePath}" ${runtimeBinsPath}/node.js "$@"`,
659
+ npm: `"${unsafeNodePath}" "${
660
+ FindTool("npm/bin/npm-cli")[0]
661
+ }" ${npmArgs.join(" ")} "$@"`,
662
+
663
+ /* Windows */
664
+ "node.bat": `@echo off
665
+ "${unsafeNodePath}" ${runtimeBinsPath}\\node.js %*`,
666
+ "shell.bat": `@echo off
667
+ "${unsafeNodePath}" ${runtimeBinsPath}\\shell.js %*`,
668
+ "npm.bat": `@echo off
669
+ node "${FindTool("npm/bin/npm-cli")[0]}" ${npmArgs.join(" ")} %*`,
670
+
671
+ /* Runtime scripts */
672
+ "shell.js": `${appendToPath.toString()}\n${getSafeCrossPlatformPath.toString()}\n(${runtime.Script_ShellJS.toString()})()`,
673
+ "node.js": `(${runtime.Script_NodeJS.toString()})()`,
674
+
675
+ /* Config files */
676
+ npmrc: `prefix = ${installPath}`
677
+ };
678
+
679
+ /*
680
+ We handle creating the runtimeBins files by looping through and writing files. There's nothing
681
+ special or interesting here.
682
+ */
683
+
684
+ try {
685
+ shell.mkdir("-p", runtimeBinsPath);
686
+ } catch (err) {
687
+ debug(err);
688
+ }
689
+
690
+ if (!flags["disable-write"]) {
691
+ Object.keys(runtimeBins).forEach(filename => {
692
+ const runtimeBinPath = path.join(runtimeBinsPath, filename);
693
+ try {
694
+ shell.rm("-rf", runtimeBinPath);
695
+ } catch (err) {
696
+ debug(err);
697
+ }
698
+ fs.writeFileSync(runtimeBinPath, runtimeBins[filename]);
699
+ shell.chmod("+x", runtimeBinPath);
700
+ });
701
+ }
702
+ debug("Runtime binaries created.");
703
+ }
704
+
705
+
706
+ async function SetupFirebaseTools() {
707
+ /*
708
+ Firepit supports "setting up" (that is, installing) firebase-tools in two ways.
709
+
710
+ 1) Use the copy of firebase-tools which is stored inside the firepit binary at
711
+ join(__dirname, "vendor/node_modules/firebase-tools")
712
+ 2) Use a copy of firebase-tools installed via npm via the internet.
713
+ */
714
+ debug(`Attempting to install to "${installPath}"`);
715
+
716
+ const original_argv = [...process.argv];
717
+ const nodeModulesPath = path.join(installPath, "lib");
718
+ const binPath = path.join(installPath, "bin");
719
+ debug(shell.mkdir("-p", nodeModulesPath).toString());
720
+ debug(shell.mkdir("-p", binPath).toString());
721
+
722
+ /*
723
+ In general, we use the embedded version of firebase-tools. Once installed, this version can be
724
+ upgraded via npm, however it's important to skip npm for the initial setup as it's dramatically
725
+ faster.
726
+ */
727
+
728
+ if (!flags["ignore-embedded-cache"]) {
729
+ /*
730
+ When doing the embedded install, the setup is as simple as cp -R'ing the JavaScript files
731
+ to the right place then linking the script to a bin folder (see below).
732
+ */
733
+ debug("Using embedded cache for quick install...");
734
+ debug(
735
+ shell
736
+ .cp("-R", path.join(__dirname, "vendor/*"), nodeModulesPath)
737
+ .toString()
738
+ );
739
+ } else {
740
+ /*
741
+ When doing a remote install, we ImitateNPM and run a normal npm install. Note that we're
742
+ installing both firebase-tools and "npm" because this will upgrade the copy of npm used
743
+ by Firepit. Better up-to-date than sorry!
744
+ */
745
+ debug("Using remote for slow install...");
746
+ // Install remotely
747
+ process.argv = [
748
+ ...process.argv.slice(0, 2),
749
+ "is:npm",
750
+ "install",
751
+ "-g",
752
+ "npm",
753
+ config.firebase_tools_package
754
+ ];
755
+ const code = await ImitateNPM();
756
+ if (code) {
757
+ console.log("Setup from remote host failed due to npm error.");
758
+ process.exit(code);
759
+ }
760
+ }
761
+
762
+ /*
763
+ When installing remotely, npm automatically links the firebase-tools script to a binary folder,
764
+ however sometimes this doesn't happen as expected, so we manually call shell.ln (link) to create
765
+ a symlink regardless of the install type.
766
+
767
+ This step ensures that whether the firebase-tools install was created from the remote or
768
+ local install that the binary still exists in the same place.
769
+
770
+ Note we can not simply move firebase.js because it uses imports relative to it's position in
771
+ the node_modules tree.
772
+ */
773
+ debug(
774
+ shell
775
+ .ln(
776
+ "-sf",
777
+ path.join(
778
+ nodeModulesPath,
779
+ "node_modules/firebase-tools/lib/bin/firebase.js"
780
+ ),
781
+ path.join(binPath, "firebase")
782
+ )
783
+ .toString()
784
+ );
785
+
786
+ /*
787
+ Finally we check to make sure we now have a copy of the "firebase" command which is findable
788
+ and then restore the original process.argv before finishing the setup.
789
+ */
790
+ if (!FindTool("firebase-tools/lib/bin/firebase").length) {
791
+ console.warn(`firebase-tools setup failed.`);
792
+ process.exit(2);
793
+ }
794
+
795
+ process.argv = original_argv;
796
+ }
797
+
798
+ /*
799
+ -------------------------------------
800
+ Other / Helper Functions
801
+ -------------------------------------
802
+ */
803
+
804
+ function uninstallLegacyFirepit() {
805
+ /*
806
+ There are two situations where we should trash the Firepit install directory.
807
+
808
+ 1) We're using an old firepit version where the "cli" folder exists
809
+ 2) We're using an old firebase-tools version where the version is different than ours.
810
+ */
811
+
812
+ /*
813
+ To detect an old-style Firepit install, we look for the "cli" folder, a folder which has
814
+ been renmaed in new Firepit builds.
815
+ */
816
+ const isLegacyFirepit = !shell.ls(
817
+ path.join(homePath, ".cache", "firebase", "cli")
818
+ ).code;
819
+
820
+ /*
821
+ To check for mismatched firebase-tools versions, we find the package.json and read the version
822
+ manually then compare it to ours.
823
+ */
824
+ let installedFirebaseToolsPackage = {};
825
+ const installedFirebaseToolsPackagePath = path.join(
826
+ homePath,
827
+ ".cache/firebase/tools/lib/node_modules/firebase-tools/package.json"
828
+ );
829
+ const firepitFirebaseToolsPackagePath = path.join(
830
+ __dirname,
831
+ "vendor/node_modules/firebase-tools/package.json"
832
+ );
833
+ debug(`Doing JSON parses for version checks at ${firepitFirebaseToolsPackagePath}`);
834
+ debug(shell.ls(path.join(__dirname, "vendor/node_modules/")));
835
+ const firepitFirebaseToolsPackage = JSON.parse(
836
+ shell.cat(firepitFirebaseToolsPackagePath)
837
+ );
838
+ try {
839
+ installedFirebaseToolsPackage = JSON.parse(
840
+ shell.cat(installedFirebaseToolsPackagePath)
841
+ );
842
+ } catch (err) {
843
+ debug("No existing firebase-tools install found.");
844
+ }
845
+
846
+ debug(
847
+ `Installed ft@${installedFirebaseToolsPackage.version ||
848
+ "none"} and packaged ft@${firepitFirebaseToolsPackage.version}`
849
+ );
850
+
851
+ const isLegacyFirebaseTools =
852
+ installedFirebaseToolsPackage.version !==
853
+ firepitFirebaseToolsPackage.version;
854
+
855
+ /*
856
+ If either of these conditions are true, we just delete the whole cache and start over fresh.
857
+ */
858
+
859
+ if (!isLegacyFirepit && !isLegacyFirebaseTools) return;
860
+ debug("Legacy firepit / firebase-tools detected, clearing it out...");
861
+ debug(shell.rm("-rf", path.join(homePath, ".cache", "firebase")));
862
+ }
863
+
864
+ async function getFirebaseToolsCommand() {
865
+ /*
866
+ This helper function produces an absolute, cross-platform "firebase" command reference.
867
+
868
+ It outputs either "c:\path\to\firebase.exe" or "c:\path\to\firebase.exe path\to\firebase.js"
869
+ As discussed above, whether running the firepit binary results in a Node.js runtime or the
870
+ "firebase" command can change (seemingly randomly, but it's not) depending on if we're
871
+ inside of an existing pkg process. Doing this check ensures that we get a command which
872
+ when ran results in "firebase" being ran regardless of environment.
873
+ */
874
+ const isRuntime = await VerifyNodePath(safeNodePath);
875
+ debug(`Node path ${safeNodePath} is runtime? ${isRuntime}`);
876
+
877
+ let firebase_command;
878
+ if (isRuntime) {
879
+ const script_path = await getSafeCrossPlatformPath(
880
+ isWindows,
881
+ path.join(__dirname, "/firepit.js")
882
+ );
883
+ //TODO: We should store this as an array to prevent issues with spaces
884
+ firebase_command = `${safeNodePath} ${script_path}`;
885
+ } else {
886
+ firebase_command = safeNodePath;
887
+ }
888
+ debug(firebase_command);
889
+ return firebase_command;
890
+ }
891
+
892
+ async function VerifyNodePath(nodePath) {
893
+ /*
894
+ VerifyNodePath invokes the firepit binary with two flags...
895
+
896
+ ./firepit check.js --tool:runtime-check
897
+
898
+ This allows us to determine if the current environment is internal to pkg or not. When it's
899
+ internal, meaning that the invocation of firepit is a direct child of another firepit process
900
+ then ./firepit will invoke the node runtime which is bundled within the firepit binary.
901
+
902
+ When it's not internal, it will run the firepit scripts.
903
+
904
+ This check works because with these flags ./firepit call will run check.js and return a
905
+ checkmark if it's acting as the Node runtime and if it's not it will just log something
906
+ else and exit.
907
+
908
+ We use this to ensure that we can always build a command which invokes the Firebase CLI
909
+ regardless of where the process is actually being spawned.
910
+ */
911
+ const runtimeCheckPath = await getSafeCrossPlatformPath(
912
+ isWindows,
913
+ path.join(__dirname, "check.js")
914
+ );
915
+ return new Promise(resolve => {
916
+ const cmd = spawn(nodePath, [runtimeCheckPath, "--tool:runtime-check"], {
917
+ shell: true
918
+ });
919
+
920
+ let result = "";
921
+ cmd.on("error", error => {
922
+ throw error;
923
+ });
924
+
925
+ cmd.stderr.on("data", stderr => {
926
+ debug(`STDERR: ${stderr.toString()}`);
927
+ });
928
+
929
+ cmd.stdout.on("data", stdout => {
930
+ debug(`STDOUT: ${stdout.toString()}`);
931
+ result += stdout.toString();
932
+ });
933
+
934
+ cmd.on("close", code => {
935
+ debug(
936
+ `[VerifyNodePath] Expected "✓" from runtime got code ${code} with output "${result}"`
937
+ );
938
+ if (code === 0) {
939
+ if (result.indexOf("✓") >= 0) {
940
+ resolve(true);
941
+ } else {
942
+ resolve(false);
943
+ }
944
+ } else {
945
+ resolve(false);
946
+ }
947
+ });
948
+ });
949
+ }
950
+
951
+ function FindTool(bin) {
952
+ /*
953
+ This method returns a list of files which match the script name provided. We use this to
954
+ locate npm, firebase-tools, etc.
955
+ */
956
+
957
+ const potentialPaths = [
958
+ path.join(installPath, "lib/node_modules", bin),
959
+ path.join(installPath, "node_modules", bin),
960
+ path.join(__dirname, "node_modules", bin)
961
+ ];
962
+
963
+ return potentialPaths
964
+ .map(path => {
965
+ debug(`Checking for ${bin} install at ${path}`);
966
+ if (shell.ls(path + ".js").code === 0) {
967
+ debug(`Found ${bin} install.`);
968
+ return path;
969
+ }
970
+ })
971
+ .filter(p => p);
972
+ }
973
+
974
+ function SetWindowTitle(title) {
975
+ /*
976
+ This method *attempts* to set the terminal window title to something pretty so it doesn't
977
+ show the internal shell'ing we do. It kinda works, but fails silently, so I've left it in.
978
+ */
979
+ if (isWindows) {
980
+ process.title = title;
981
+ }
982
+ }
983
+
984
+
985
+ /*
986
+ -------------------------------------
987
+ Shared Functions
988
+ -------------------------------------
989
+
990
+ These methods are very special and should be edited carefully. They must be pure JavaScript
991
+ functions which do not rely on any global state or imports.
992
+
993
+ If you look at createRuntimeBinaries() and see the runtimeBins scripts, you'll see that we
994
+ call getSafeCrossPlatformPath.toString() and appendToPath.toString() and put them into the
995
+ scripts which we place on the filesystem. We do this because the scripts in ./runtime.js
996
+ depend on these functions and since we need to create single JavaScript files to drop onto
997
+ the user's filesystem, we concat them together.
998
+
999
+ This is fairly dangerous, but we don't have many options.
1000
+ */
1001
+
1002
+ async function getSafeCrossPlatformPath(isWin, path) {
1003
+ /*
1004
+ This function generates "safe" DOS style file paths on Windows.
1005
+
1006
+ For example:
1007
+
1008
+ unsafePath: C:\Program Files\Java\jdk1.6.0_22
1009
+ safePath: C:\PROGRA~1\Java\JDK16~1.0_2
1010
+
1011
+ These paths remove spaces and special characters which could interfere with the terminal.
1012
+ In theory, it should be possible to avoid this, but because of issues in npm, we need to be
1013
+ extra safe about spaces.
1014
+ */
1015
+ if (!isWin) return path;
1016
+
1017
+ /*
1018
+ This is perhaps the biggest hack in Firepit, we shell out to command and run a small script
1019
+ which returns the DOS-formatted version of a path. This is not fast, but it's (apparently)
1020
+ the only way to fetch the safe version of a path
1021
+ */
1022
+ let command = `for %I in ("${path}") do echo %~sI`;
1023
+ return new Promise(resolve => {
1024
+ const cmd = require("child_process").spawn(`cmd`, ["/c", command], {
1025
+ shell: true
1026
+ });
1027
+
1028
+ let result = "";
1029
+ cmd.on("error", error => {
1030
+ throw error;
1031
+ });
1032
+ cmd.stdout.on("data", stdout => {
1033
+ result += stdout.toString();
1034
+ });
1035
+
1036
+ cmd.on("close", code => {
1037
+ if (code === 0) {
1038
+ const lines = result.split("\r\n").filter(line => line);
1039
+ const path = lines.slice(-1)[0];
1040
+ resolve(path.trim());
1041
+ } else {
1042
+ throw `Attempt to dosify path failed with code ${code}`;
1043
+ }
1044
+ });
1045
+ });
1046
+ }
1047
+
1048
+ function appendToPath(isWin, pathsToAppend) {
1049
+ /*
1050
+ This method handles appending a folder to the user's PATH directory in a cross-platform way.
1051
+
1052
+ Windows uses ";" to delimit paths and *nix uses ":"
1053
+ */
1054
+ const PATH = process.env.PATH;
1055
+ const pathSeperator = isWin ? ";" : ":";
1056
+
1057
+ process.env.PATH = [
1058
+ ...pathsToAppend,
1059
+ ...PATH.split(pathSeperator).filter(folder => folder)
1060
+ ].join(pathSeperator);
1061
+ }
1062
+
1063
+ function debug(...msg) {
1064
+ /*
1065
+ This method creates a debug log which can go to stdout or a file depending on --tool: flags.
1066
+ */
1067
+ if (!debug.log) debug.log = [];
1068
+
1069
+ if (flags["log-debug"]) {
1070
+ msg.forEach(m => console.log(m));
1071
+ } else {
1072
+ msg.forEach(m => debug.log.push(m));
1073
+ }
1074
+ }