Skip to content

Commit

Permalink
fix: ng g application with yarn 1
Browse files Browse the repository at this point in the history
  • Loading branch information
vscaiceanu-1a committed Jan 18, 2024
1 parent 591622f commit 127c3c1
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"verdaccio:login": "yarn cpy --cwd=./.verdaccio/conf .npmrc . --rename=.npmrc-logged && npx --yes npm-cli-login -u verdaccio -p verdaccio -e test@test.com -r http://127.0.0.1:4873 --config-path \".verdaccio/conf/.npmrc-logged\"",
"verdaccio:publish": "yarn verdaccio:clean && yarn set:version 999.0.0 --include \"!**/!(dist)/package.json\" --include !package.json && yarn verdaccio:login && yarn run publish --userconfig \".verdaccio/conf/.npmrc-logged\" --tag=latest --@o3r:registry=http://127.0.0.1:4873 --@ama-sdk:registry=http://127.0.0.1:4873 --@ama-terasu:registry=http://127.0.0.1:4873",
"verdaccio:stop": "docker container stop $(docker ps -a -q --filter=\"name=verdaccio\")",
"verdaccio:all": "yarn verdaccio:stop && yarn verdaccio:start && yarn verdaccio:publish",
"watch:vscode-extension": "yarn nx run vscode-extension:compile:watch",
"workspaces:list": "yarn workspaces list --no-private --json | shx sed \"s/.*\\\"location\\\":\\\"(.*?)\\\".*/\\$1/\""
},
Expand Down
15 changes: 9 additions & 6 deletions packages/@ama-sdk/core/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {chain, type Rule} from '@angular-devkit/schematics';
import { chain, type Rule } from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import * as ts from 'typescript';
import * as path from 'node:path';
import * as ts from 'typescript';
import { NgAddSchematicsSchema } from './schema';

/**
* Rule to import all the necessary dependency to run an @ama-sdk based application
* Helps to migrate from previous versions with an import replacement
* @param options schema options
*/
function ngAddFn(): Rule {
function ngAddFn(options: NgAddSchematicsSchema): Rule {

const checkSchematicsDependency: Rule = async (_, context) => {
try {
Expand Down Expand Up @@ -55,15 +57,16 @@ function ngAddFn(): Rule {
};


const addMandatoryPeerDeps: Rule = async (_, context) => {
const { getPeerDepWithPattern } = await import('@o3r/schematics');

const addMandatoryPeerDeps: Rule = async (tree, context) => {
const { getPeerDepWithPattern, getWorkspaceConfig } = await import('@o3r/schematics');
const workingDirectory = options?.projectName && getWorkspaceConfig(tree)?.projects[options.projectName]?.root || '.';
const peerDepToInstall = getPeerDepWithPattern(path.resolve(__dirname, '..', '..', 'package.json'), [
'chokidar',
'cpy',
'minimist'
]);
context.addTask(new NodePackageInstallTask({
workingDirectory,
packageName: Object.entries(peerDepToInstall.matchingPackagesVersions)
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
.map(([dependency, version]) => `${dependency}@${version || 'latest'}`)
Expand Down
6 changes: 3 additions & 3 deletions packages/@o3r/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@
"generatorDependencies": {
"@angular-eslint/eslint-plugin": "~17.2.0",
"@angular/material": "~17.0.1",
"@ngrx/router-store": "~17.0.0",
"@ngrx/effects": "~17.0.0",
"@ngrx/store-devtools": "~17.0.0",
"@ngrx/router-store": "~17.1.0",
"@ngrx/effects": "~17.1.0",
"@ngrx/store-devtools": "~17.1.0",
"@o3r/store-sync": "workspace:^",
"@types/jest": "~29.5.2",
"nx": "~17.2.0",
Expand Down
21 changes: 16 additions & 5 deletions packages/@o3r/core/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { chain, externalSchematic, noop, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { chain, externalSchematic, noop } from '@angular-devkit/schematics';
import type { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { askConfirmation } from '@angular/cli/src/utilities/prompt';
import {
AddDevInstall,
createSchematicWithMetricsIfInstalled,
displayModuleListRule,
getWorkspaceConfig,
isPackageInstalled,
registerPackageCollectionSchematics,
setupSchematicsDefaultParams
} from '@o3r/schematics';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { lastValueFrom } from 'rxjs';
import type { PackageJson } from 'type-fest';
import { getExternalPreset, presets } from '../shared/presets';
import { NgAddSchematicsSchema } from './schema';
import { askConfirmation } from '@angular/cli/src/utilities/prompt';
import { AddDevInstall, createSchematicWithMetricsIfInstalled, displayModuleListRule, isPackageInstalled, registerPackageCollectionSchematics, setupSchematicsDefaultParams } from '@o3r/schematics';
import { prepareProject } from './project-setup/index';
import { NgAddSchematicsSchema } from './schema';

/**
* Add Otter library to an Angular Project
Expand All @@ -24,13 +33,15 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
if (!options.projectName && !isPackageInstalled(workspacePackageName)) {
schematicsDependencies.push(workspacePackageName);
}

const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root || '.';
context.addTask(new AddDevInstall({
packageName: [
...schematicsDependencies.map((dependency) => dependency + o3rCoreVersion)
].join(' '),
hideOutput: false,
quiet: false,
workingDirectory,
force: options.forceInstall
} as any));
await lastValueFrom(context.engine.executePostTasks());
Expand Down
9 changes: 7 additions & 2 deletions packages/@o3r/schematics/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import * as path from 'node:path';
import { lastValueFrom } from 'rxjs';
import type { PackageJson } from 'type-fest';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import { NgAddSchematicsSchema } from './schema';

/**
* Add Otter schematics to an Angular Project
* @param options schematics options
*/
function ngAddFn(): Rule {
function ngAddFn(options: NgAddSchematicsSchema): Rule {
const schematicsDependencies = ['@angular-devkit/architect', '@angular-devkit/schematics', '@angular-devkit/core', '@schematics/angular', 'globby'];
return () => async (tree: Tree, context: SchematicContext): Promise<Rule> => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { AddDevInstall } = await import('@o3r/schematics');
const { AddDevInstall, getWorkspaceConfig } = await import('@o3r/schematics');
context.logger.info('Running ng add for schematics');
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const treePackageJson = tree.readJson('./package.json') as PackageJson;
const packageJsonContent: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, {encoding: 'utf-8'}));
const getDependencyVersion = (dependency: string) => packageJsonContent?.dependencies?.[dependency] || packageJsonContent?.peerDependencies?.[dependency];
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root || '.';
let packageName = '';
for (const dependency of schematicsDependencies) {
const version = getDependencyVersion(dependency);
Expand All @@ -28,6 +32,7 @@ function ngAddFn(): Rule {
context.addTask(new AddDevInstall({
hideOutput: false,
packageName,
workingDirectory,
quiet: false
} as any));
await lastValueFrom(context.engine.executePostTasks());
Expand Down
1 change: 1 addition & 0 deletions packages/@o3r/store-sync/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
(_, ctx) => {
const peerDepToInstall = getPeerDepWithPattern(path.resolve(__dirname, '..', '..', 'package.json'), ['fast-deep-equal']);
ctx.addTask(new NodePackageInstallTask({
workingDirectory,
packageName: Object.entries(peerDepToInstall.matchingPackagesVersions)
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
.map(([dependency, version]) => `${dependency}@${version || 'latest'}`)
Expand Down
7 changes: 5 additions & 2 deletions packages/@o3r/test-helpers/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ const doCustomAction: Rule = (tree, _context) => {
* @param options
*/
function ngAddFn(options: NgAddSchematicsSchema): Rule {
return async (_tree, context) => {
return async (tree, context) => {
try {
// use dynamic import to properly raise an exception if it is not an Otter project.
const { applyEsLintFix, install, ngAddPackages, getO3rPeerDeps } = await import('@o3r/schematics');
const { applyEsLintFix, install, ngAddPackages, getO3rPeerDeps, getWorkspaceConfig } = await import('@o3r/schematics');
// retrieve dependencies following the /^@o3r\/.*/ pattern within the peerDependencies of the current module
const depsInfo = getO3rPeerDeps(path.resolve(__dirname, '..', '..', 'package.json'));
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root || '.';
return chain([
// optional custom action dedicated to this module
doCustomAction,
Expand All @@ -27,6 +29,7 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
options.skipInstall ? noop : install,
// add the missing Otter modules in the current project
ngAddPackages(depsInfo.o3rPeerDeps, {
workingDirectory,
skipConfirmation: true,
version: depsInfo.packageVersion,
projectName: options.projectName,
Expand Down
6 changes: 3 additions & 3 deletions packages/@o3r/workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@
},
"generatorDependencies": {
"@angular/material": "~17.0.1",
"@ngrx/router-store": "~17.0.0",
"@ngrx/effects": "~17.0.0",
"@ngrx/store-devtools": "~17.0.0"
"@ngrx/router-store": "~17.1.0",
"@ngrx/effects": "~17.1.0",
"@ngrx/store-devtools": "~17.1.0"
},
"engines": {
"node": ">=18.0.0"
Expand Down

0 comments on commit 127c3c1

Please sign in to comment.