* change the package.json of a component to include postInstall script. * @see postInstallTemplate() JSDoc to understand better why this postInstall script is needed
(component: Component, postInstallLinks = [], postInstallSymlinks = [])
| 361 | * @see postInstallTemplate() JSDoc to understand better why this postInstall script is needed |
| 362 | */ |
| 363 | function generatePostInstallScript(component: Component, postInstallLinks = [], postInstallSymlinks = []): LinkFile { |
| 364 | // $FlowFixMe todo: is it possible that writtenPath is empty here? |
| 365 | // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! |
| 366 | const componentDir: string = component.writtenPath; |
| 367 | // convert from array to object for easier parsing in the postinstall script |
| 368 | const linkPathsObject = postInstallLinks.reduce((acc, val) => { |
| 369 | // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! |
| 370 | // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! |
| 371 | acc[val.filePath] = val.content; |
| 372 | return acc; |
| 373 | }, {}); |
| 374 | const symlinkPathsObject = postInstallSymlinks.reduce((acc, val) => { |
| 375 | // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! |
| 376 | // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! |
| 377 | acc[val.dest] = val.source; |
| 378 | return acc; |
| 379 | }, {}); |
| 380 | const postInstallCode = postInstallTemplate(JSON.stringify(linkPathsObject), JSON.stringify(symlinkPathsObject)); |
| 381 | const POST_INSTALL_FILENAME = '.bit.postinstall.js'; |
| 382 | const postInstallFilePath = path.join(componentDir, POST_INSTALL_FILENAME); |
| 383 | const postInstallScript = `node ${POST_INSTALL_FILENAME}`; |
| 384 | if (!component.packageJsonFile) throw new Error(`packageJsonFile is missing for ${component.id.toString()}`); |
| 385 | component.packageJsonFile.addOrUpdateProperty('scripts', { postinstall: postInstallScript }); |
| 386 | const postInstallFile = LinkFile.load({ filePath: postInstallFilePath, content: postInstallCode, override: true }); |
| 387 | return postInstallFile; |
| 388 | } |
| 389 | |
| 390 | function addCustomResolveAliasesToPackageJson(component: Component, links: LinkFileType[]): boolean { |
| 391 | const resolveAliases = links.reduce((acc, link: LinkFileType) => { |
no test coverage detected