-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/add install packages schematic #93
Closed
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
118bf88
feat: add install-packages schematic
neodmy f29816a
refactor: use install-packages
neodmy dfe455b
refactor: use nest cli to generate nest components
neodmy 434191a
refactor: skip installation if no package.json
neodmy 6ac40dc
Merge branch 'main' into feat/add-install-packages-schematic
neodmy 4df31d3
docs: remove reference to NestJs
neodmy 7321700
Merge remote-tracking branch 'origin' into feat/add-install-packages-…
neodmy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
packages/@guidesmiths/cuckoojs-cli/src/commands/generate.command.ts
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
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
23 changes: 23 additions & 0 deletions
23
packages/@guidesmiths/cuckoojs-schematics/src/install-packages/README.md
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,23 @@ | ||
# install-packages | ||
|
||
## Description | ||
|
||
This schematic install packages with your project's package manager by exploring the _lock_ file in your project or your | ||
package manager globally install. | ||
|
||
It is an adaptation of [detect-package-manager](https://github.com/egoist/detect-package-manager) to work within the context | ||
of schematics and its `Tree`. | ||
|
||
## Options | ||
|
||
| Option | Description | Requiered | Type | Default | | ||
|---------------|--------------------------------------------------------------------|---|---|---------| | ||
| `directory` | Root folder of your project | false | string | `.` | | ||
|
||
## How to use it within a project | ||
|
||
Add it to your project running: | ||
|
||
```bash | ||
schematics @guidesmiths/cuckoojs-schematics:install-packages --directory=. | ||
``` |
55 changes: 55 additions & 0 deletions
55
packages/@guidesmiths/cuckoojs-schematics/src/install-packages/install-packages.factory.ts
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,55 @@ | ||
import { | ||
type Rule, | ||
type SchematicContext, | ||
type Tree, | ||
} from '@angular-devkit/schematics'; | ||
import {normalize} from '@angular-devkit/core'; | ||
import {execSync} from 'child_process'; | ||
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks' | ||
|
||
interface Options { | ||
directory: string; | ||
} | ||
|
||
type PackageManager = 'npm' | 'pnmp' | 'yarn'; | ||
|
||
export function main(options: Options): Rule { | ||
return function (tree: Tree, context: SchematicContext) { | ||
context.logger.info('Installing packages...'); | ||
const packageManager = getPackageManager(tree, options.directory); | ||
return installDependencies(packageManager) | ||
}; | ||
} | ||
|
||
// inspired by https://github.com/egoist/detect-package-manager | ||
function getFromLockFile(tree: Tree, directory: string): PackageManager | undefined { | ||
if(tree.exists(normalize(`${directory}/package-json.lock`))) return 'npm'; | ||
if(tree.exists(normalize(`${directory}/yarn.lock`))) return 'yarn'; | ||
if(tree.exists(normalize(`${directory}/pnpm-lock.yaml`))) return 'pnmp'; | ||
} | ||
|
||
// inspired by https://github.com/egoist/detect-package-manager | ||
function getFromGlobalInstallation(): PackageManager { | ||
try { | ||
execSync('yarn --version', {stdio: 'pipe'}); | ||
return 'yarn' | ||
} catch(e) { | ||
try { | ||
execSync('pnpm --version', {stdio: 'pipe'}); | ||
return 'pnmp'; | ||
} catch(e) { | ||
return 'npm'; | ||
} | ||
} | ||
} | ||
|
||
function getPackageManager(tree: Tree, directory: string) { | ||
return getFromLockFile(tree, directory) ?? getFromGlobalInstallation(); | ||
} | ||
|
||
function installDependencies(packageManager: PackageManager): Rule { | ||
return (tree: Tree, context: SchematicContext) => { | ||
context.addTask(new NodePackageInstallTask({ packageManager })); | ||
return tree; | ||
}; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/@guidesmiths/cuckoojs-schematics/src/install-packages/schema.json
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,13 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"$id": "SchematicsInstallPackages", | ||
"title": "Install packages with your package manager", | ||
"type": "object", | ||
"properties": { | ||
"directory": { | ||
"type": "string", | ||
"description": "root folder of your project.", | ||
"default": "." | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that by default it will use npm if it doesn't find anything. What happens if npm is not installed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And one more question. What happens if several of them are installed? We're giving preference to
yarn
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preference is based on the lock file. If there is no lock file, it will explore the globally installed ones. In that case,
yarn
takes preference indeed. We can change it.If the package manager is not installed, it will fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind the order. I was just wondering if it's useful to have an optional config parameter to set the preference. Or perhaps I'm suggesting nonsense! 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean to overwrite the discovered package manager?