Skip to content
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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@guidesmiths/cuckoojs-schematics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ That's it!

- [`nestjs-config`](/packages/@guidesmiths/cuckoojs-schematics/src/nestjs-config/README.md)
- [`basic-tooling`](/packages/@guidesmiths/cuckoojs-schematics/src/basic-tooling/README.md)
- [`install-packages`](/packages/@guidesmiths/cuckoojs-schematics/src/install-packages/README.md)

### Pull Request Template

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import {
} from '@angular-devkit/schematics';
import {schematic} from '@angular-devkit/schematics';
import {normalize} from '@angular-devkit/core';
import {execSync} from 'child_process';
import {resolve} from 'path';
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks'

interface Options {
directory: string;
Expand All @@ -20,26 +17,20 @@ export function main(options: Options): Rule {
return (tree: Tree, context: SchematicContext) => {
context.logger.info('Adding husky, commitlint, gitignore, nvmrc...');

if (!tree.exists(normalize(`${options.directory}/package.json`))) {
let existPackageJson = true;
if (!tree.exists(normalize(`./package.json`))) {
context.logger.warn(
'package.json file not found. Initializing package.json',
'package.json file not found. Skipping installation',
);
execSync('npm init --y', {cwd: resolve(options.directory)});
existPackageJson = false;
}

return chain([
schematic('husky', options),
schematic('commitlint', options),
schematic('gitignore', options),
schematic('nvmrc', options),
options.skipInstall ? noop() : installDependencies(),
options.skipInstall || !existPackageJson ? noop() : schematic('install-packages', options),
]);
};
}

function installDependencies(): Rule {
return (tree: Tree, context: SchematicContext) => {
context.addTask(new NodePackageInstallTask());
return tree;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
"description": "Add basic tooling to the project",
"factory": "./basic-tooling/basic-tooling.factory#main",
"schema": "./basic-tooling/schema.json"
},
"install-packages": {
"description": "Install packages in your project",
"factory": "./install-packages/install-packages.factory#main",
"schema": "./install-packages/schema.json"
}
}
}
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=.
```
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';
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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! 😬

Copy link
Contributor Author

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?

}
}
}

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;
};
}
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": "."
}
}
}