Skip to content

Commit

Permalink
feat: add lerna at the root of a new otter project
Browse files Browse the repository at this point in the history
  • Loading branch information
vscaiceanu-1a committed Jul 15, 2024
1 parent 9e984e8 commit 9c5807d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/@o3r/workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@
"@angular/material": "~18.0.0",
"@ngrx/router-store": "~18.0.0",
"@ngrx/effects": "~18.0.0",
"@ngrx/store-devtools": "~18.0.0"
"@ngrx/store-devtools": "~18.0.0",
"lerna": "^8.1.5"
},
"engines": {
"node": "^18.19.1 || ^20.11.1 || >=22.0.0"
Expand Down
41 changes: 41 additions & 0 deletions packages/@o3r/workspace/schematics/ng-add/helpers/npm-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,44 @@ export function filterPackageJsonScripts(tree: Tree, _context: SchematicContext)
tree.overwrite(rootPackageJsonPath, JSON.stringify(rootPackageJsonObject, null, 2));
return tree;
}

/**
* The supported monorepo managers
*/
export type MonorepoManager = 'lerna';

/**
* Add a monorepo manager at the root of the project
* @param o3rWorkspacePackageJson the @o3r/workspace package.json
* @param manager the monorepo manager
*/
export function addMonorepoManager(o3rWorkspacePackageJson: PackageJson & { generatorDependencies: Record<string, string> }, manager: MonorepoManager): Rule {
return (tree: Tree, _context: SchematicContext) => {
if (manager === 'lerna') {
const rootPackageJsonPath = '/package.json';
if (!tree.exists(rootPackageJsonPath)) {
throw new SchematicsException('Root package.json does not exist');
}

const rootPackageJsonObject = tree.readJson(rootPackageJsonPath) as PackageJson;
rootPackageJsonObject.devDependencies = {
...rootPackageJsonObject.devDependencies,
'lerna': o3rWorkspacePackageJson.generatorDependencies.lerna
};
rootPackageJsonObject.scripts = {
...rootPackageJsonObject.scripts,
'build': 'lerna run build',
'test': 'lerna run test',
'lint': 'lerna run lint'
};

tree.create('/lerna.json', JSON.stringify({
'$schema': 'https://github.com/lerna/lerna/blob/main/packages/lerna/schemas/lerna-schema.json',
'version': rootPackageJsonObject.version || '0.0.0-placeholder'
}, null, 2));

tree.overwrite(rootPackageJsonPath, JSON.stringify(rootPackageJsonObject, null, 2));
}
return tree;
};
}
8 changes: 5 additions & 3 deletions packages/@o3r/workspace/schematics/ng-add/project-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import type { DependencyToAdd } from '@o3r/schematics';
import { NodeDependencyType } from '@schematics/angular/utility/dependencies';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { addWorkspacesToProject, filterPackageJsonScripts } from './helpers/npm-workspace';
import { addMonorepoManager, addWorkspacesToProject, filterPackageJsonScripts } from './helpers/npm-workspace';
import { generateRenovateConfig } from './helpers/renovate';
import type { NgAddSchematicsSchema } from './schema';
import { shouldOtterLinterBeInstalled } from './helpers/linter';
import { updateGitIgnore } from './helpers/gitignore-update';
import type { PackageJson } from 'type-fest';

/**
* Enable all the otter features requested by the user
Expand All @@ -34,7 +35,7 @@ export const prepareProject = (options: NgAddSchematicsSchema): Rule => {
const ownSchematicsFolder = path.resolve(__dirname, '..');
const ownPackageJsonPath = path.resolve(ownSchematicsFolder, '..', 'package.json');
const depsInfo = getO3rPeerDeps(ownPackageJsonPath);
const ownPackageJsonContent = JSON.parse(fs.readFileSync(ownPackageJsonPath, { encoding: 'utf-8' }));
const ownPackageJsonContent = JSON.parse(fs.readFileSync(ownPackageJsonPath, { encoding: 'utf-8' })) as PackageJson & { generatorDependencies: Record<string, string> };

return async (tree, context) => {
if (!ownPackageJsonContent) {
Expand Down Expand Up @@ -74,7 +75,8 @@ export const prepareProject = (options: NgAddSchematicsSchema): Rule => {
ngAddToRun: internalPackagesToInstallWithNgAdd
}),
!options.skipLinter && installOtterLinter ? applyEsLintFix() : noop(),
addWorkspacesToProject()
addWorkspacesToProject(),
addMonorepoManager(ownPackageJsonContent, 'lerna')
])(tree, context);
};
};
2 changes: 1 addition & 1 deletion packages/@o3r/workspace/src/cli/set-version.cts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as path from 'node:path';
import * as winston from 'winston';
import { clean } from 'semver';

const defaultIncludedFiles = ['**/package.json', '!/**/templates/**/package.json', '!**/node_modules/**/package.json'];
const defaultIncludedFiles = ['**/package.json', '!/**/templates/**/package.json', '!**/node_modules/**/package.json', '**/lerna.json'];

const collect = (pattern: string, patterns: string[]) => {
if (patterns === defaultIncludedFiles && pattern) {
Expand Down

0 comments on commit 9c5807d

Please sign in to comment.