generated from codinsonn/universal-app-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added an "add-dependencies" script / generator
- Loading branch information
Showing
14 changed files
with
330 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-disable import/no-anonymous-default-export */ | ||
import { PlopTypes } from '@turbo/gen' | ||
import { getWorkspaceOptions, parseWorkspaces } from '../scripts/helpers/scriptUtils' | ||
import { execSync } from 'child_process' | ||
import fs from 'fs' | ||
|
||
/* --- Disclaimer ------------------------------------------------------------------------------ */ | ||
|
||
// Learn more about Turborepo Generators at: | ||
// https://turbo.build/repo/docs/core-concepts/monorepos/code-generation | ||
|
||
/* --- Constants ------------------------------------------------------------------------------- */ | ||
|
||
const workspaceOptions = getWorkspaceOptions('') | ||
|
||
/** --- Dependency Installer ------------------------------------------------------------------- */ | ||
/** -i- Install Expo SDK compatible dependencies in your workspace of choice */ | ||
export const registerDependencyGenerator = (plop: PlopTypes.NodePlopAPI) => { | ||
plop.setGenerator('add-dependencies', { | ||
description: 'Install Expo SDK compatible dependencies in your workspace of choice', | ||
prompts: [ | ||
{ | ||
type: 'confirm', | ||
name: 'confirm', | ||
message: 'This will install Expo SDK compatible dependencies in a workspace of your choice. Continue?', | ||
validate: (value) => !!value || 'Please confirm to continue', | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'dependencies', | ||
message: "Which dependencies should we install?", | ||
validate: (value) => !!value, | ||
}, | ||
{ | ||
type: 'list', | ||
name: 'workspaceTarget', | ||
message: 'Which workspace should we install them in?', | ||
choices: Object.keys(workspaceOptions), | ||
}, | ||
], | ||
actions: (data) => { | ||
// Args | ||
const { workspaceTarget } = data! | ||
const dependencies = data!.dependencies.split(' ') | ||
const depList = dependencies.join(' ') | ||
|
||
// Figure out workspace | ||
const { workspaceImports } = parseWorkspaces('./') | ||
const workspacePath = workspaceOptions[workspaceTarget as keyof typeof workspaceOptions] | ||
const workspaceName = workspaceImports[workspacePath] | ||
|
||
// Log out the dependencies | ||
console.log('\n', `> Installing Expo SDK compatible packages ${depList} in '${workspaceName}' workspace`) // prettier-ignore | ||
|
||
// Read the @app/expo package json | ||
const originalExpoPackageJsonFile = fs.readFileSync(`apps/expo/package.json`, 'utf-8') | ||
const originalExpoPackageJson = JSON.parse(originalExpoPackageJsonFile) // prettier-ignore | ||
const originalDeps = originalExpoPackageJson.dependencies | ||
|
||
// Install the new dependencies in @app/expo | ||
const output = execSync(`npm -w @app/expo run add-dependencies ${dependencies.join(' ')}`) // prettier-ignore | ||
const loggableOutput = output.toString().split('\n').slice(0, 9).join('\n') | ||
console.log(loggableOutput) | ||
|
||
// Extract the new dependencies from the package json | ||
const newExpoPackageJson = JSON.parse(fs.readFileSync(`apps/expo/package.json`, 'utf-8')) // prettier-ignore | ||
const newDeps = Object.entries(newExpoPackageJson.dependencies).filter(([key]) => !originalDeps[key]) // prettier-ignore | ||
|
||
// Restore the old package json | ||
fs.writeFileSync(`apps/expo/package.json`, originalExpoPackageJsonFile) | ||
|
||
// Add the new dependencies to the chosen workspace | ||
const installStatements = newDeps.map(([key, value]) => `${key}@${value}`).join(' ') | ||
console.log(`> Moving ${installStatements} to '${workspaceName}' workspace`) | ||
execSync(`npm -w ${workspaceName} install ${installStatements}`) | ||
console.log(`> Install successfull`, '\n') | ||
|
||
// Log out the dependency list | ||
const lsOutput = execSync(`npm ls ${depList}`) | ||
console.log(lsOutput.toString()) | ||
|
||
// Actions | ||
return [] | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.