Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #119 from swarmion/feat/generators/directory-option
Browse files Browse the repository at this point in the history
Add directory option to generators
  • Loading branch information
guillaumeduboc authored Mar 21, 2022
2 parents 1a5d3d9 + 8f8bad7 commit 229b2cf
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 53 deletions.
7 changes: 3 additions & 4 deletions tools/generators/helpers/createFiles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateFiles, names, offsetFromRoot, Tree } from '@nrwl/devkit';
import { generateFiles, names, Tree } from '@nrwl/devkit';
import { join } from 'path';

import { NormalizedSchema } from '../types';
Expand All @@ -10,7 +10,7 @@ export const createFiles = (
): void => {
const { className, name, propertyName } = names(options.name);

generateFiles(tree, sourcePath, options.projectRoot, {
generateFiles(tree, sourcePath, options.packageRoot, {
...options,
dot: '.',
className,
Expand All @@ -19,13 +19,12 @@ export const createFiles = (
cliCommand: 'nx',
strict: undefined,
tmpl: '',
offsetFromRoot: offsetFromRoot(options.projectRoot),
hasUnitTestRunner: options.unitTestRunner !== 'none',
});

if (options.unitTestRunner === 'none') {
tree.delete(
join(options.projectRoot, 'src/lib', `${options.fileName}.spec.ts`),
join(options.packageRoot, 'src/lib', `${options.fileName}.spec.ts`),
);
}
};
12 changes: 5 additions & 7 deletions tools/generators/helpers/normalizeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Tree,
} from '@nrwl/devkit';
import { Linter } from '@nrwl/linter';
import { relative } from 'path';

import {
GeneratorType,
Expand All @@ -19,11 +20,7 @@ export const normalizeOptions = (
generatorType: GeneratorType,
): NormalizedSchema => {
const name = names(options.name).fileName;
options.directory =
options.directory !== undefined && options.directory !== ''
? options.directory
: GeneratorTypeToDirectory[generatorType];
const projectRoot = joinPathFragments(
const packageRoot = joinPathFragments(
names(options.directory).fileName,
name,
);
Expand All @@ -38,7 +35,7 @@ export const normalizeOptions = (
pascalCaseFiles: false,
});
const { npmScope } = getWorkspaceLayout(tree);

const offsetFromRoot = relative(packageRoot, tree.root);
const importPath = formatImportPath(generatorType, projectName);

return {
Expand All @@ -48,7 +45,8 @@ export const normalizeOptions = (
importPath,
linter,
name: projectName,
projectRoot,
packageRoot,
offsetFromRoot,
unitTestRunner,
workspaceName: npmScope,
};
Expand Down
12 changes: 8 additions & 4 deletions tools/generators/helpers/packageGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface PackageGeneratorParams {
sourcePath: string;
packageJson: (options: NormalizedSchema) => PackageJson;
packageProjectJson: (root: string) => ProjectConfiguration;
packageTsConfig: TsConfig;
packageTsConfig: (options: NormalizedSchema) => TsConfig;
}

export const packageGenerator = ({
Expand All @@ -31,13 +31,17 @@ export const packageGenerator = ({

writeJson(
tree,
join(options.projectRoot, `package.json`),
join(options.packageRoot, `package.json`),
packageJson(options),
);

writeJson(tree, join(options.projectRoot, `tsconfig.json`), packageTsConfig);
writeJson(
tree,
join(options.packageRoot, `tsconfig.json`),
packageTsConfig(options),
);

const projectConfiguration = packageProjectJson(options.projectRoot);
const projectConfiguration = packageProjectJson(options.packageRoot);
addProjectConfiguration(tree, options.importPath, projectConfiguration);

updateCodeWorkspace(tree, options);
Expand Down
16 changes: 6 additions & 10 deletions tools/generators/helpers/symlink.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Tree } from '@nrwl/devkit';
import { symlinkSync } from 'fs';
import { join, relative } from 'path';
import { join } from 'path';

import { NormalizedSchema } from '../types';

export const symlinkVsCodeConfiguration = (
tree: Tree,
options: NormalizedSchema,
): void => {
const relativePath = relative(
options.projectRoot,
join(tree.root, 'commonConfiguration/.vscode'),
export const symlinkVsCodeConfiguration = (options: NormalizedSchema): void => {
const relativePath = join(
options.offsetFromRoot,
'commonConfiguration/.vscode',
);
symlinkSync(relativePath, join(options.projectRoot, '.vscode'), 'dir');
symlinkSync(relativePath, join(options.packageRoot, '.vscode'), 'dir');
};
2 changes: 1 addition & 1 deletion tools/generators/helpers/updateCodeWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const updateCodeWorkspace = (
`${options.workspaceName}.code-workspace`,
(json: CodeWorkspaceType) => {
json.folders.push({
path: options.projectRoot,
path: options.packageRoot,
name: formatFolderName(options),
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const generateImportOrderRule = require('../../commonConfiguration/generateImportOrderRule');
const generateImportOrderRule = require('<%= offsetFromRoot %>/commonConfiguration/generateImportOrderRule');

module.exports = {
rules: generateImportOrderRule(__dirname),
Expand Down
2 changes: 1 addition & 1 deletion tools/generators/library/files/.lintstagedrc.js__tmpl__
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const baseConfig = require('../../.lintstagedrc.js');
const baseConfig = require('<%= offsetFromRoot %>/.lintstagedrc.js');
module.exports = baseConfig;
3 changes: 0 additions & 3 deletions tools/generators/library/files/babel.config.js

This file was deleted.

3 changes: 3 additions & 0 deletions tools/generators/library/files/babel.config.js__tmpl__
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const commonBabelConfig = require('<%= offsetFromRoot %>/commonConfiguration/babel.config');

module.exports = commonBabelConfig();
4 changes: 2 additions & 2 deletions tools/generators/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export default async (tree: Tree, schema: Schema): Promise<() => void> => {
});
writeJson(
tree,
join(options.projectRoot, `tsconfig.build.json`),
join(options.packageRoot, `tsconfig.build.json`),
packageBuildTsConfig,
);
await formatFiles(tree);

return () => {
symlinkVsCodeConfiguration(tree, options);
symlinkVsCodeConfiguration(options);
installPackagesTask(tree, true);
};
};
6 changes: 5 additions & 1 deletion tools/generators/library/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "library-generator",
"type": "object",
Expand All @@ -11,6 +10,11 @@
"$source": "argv",
"index": 0
}
},
"directory": {
"type": "string",
"description": "Directory of the new library",
"default": "packages"
}
},
"required": ["name"]
Expand Down
2 changes: 1 addition & 1 deletion tools/generators/library/typed-json-config/package.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const packageJson = (options: NormalizedSchema): PackageJson => ({
nx: {
targets: {
package: {
outputs: [join(options.projectRoot, 'dist')],
outputs: [join(options.packageRoot, 'dist')],
},
},
},
Expand Down
10 changes: 6 additions & 4 deletions tools/generators/library/typed-json-config/tsconfig.json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { TsConfig } from '../../types';
import { joinPathFragments } from '@nrwl/devkit';

export const packageTsConfig: TsConfig = {
extends: '../../tsconfig.json',
import { NormalizedSchema, TsConfig } from '../../types';

export const packageTsConfig = (options: NormalizedSchema): TsConfig => ({
extends: joinPathFragments(options.offsetFromRoot, 'tsconfig.json'),
compilerOptions: {
baseUrl: 'src',
composite: true,
Expand All @@ -12,4 +14,4 @@ export const packageTsConfig: TsConfig = {
},
exclude: ['./dist'],
include: ['./**/*.ts'],
};
});
2 changes: 1 addition & 1 deletion tools/generators/service/files/.eslintrc.js__tmpl__
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const generateImportOrderRule = require('../../commonConfiguration/generateImportOrderRule');
const generateImportOrderRule = require('<%= offsetFromRoot %>/commonConfiguration/generateImportOrderRule');

module.exports = {
rules: generateImportOrderRule(__dirname),
Expand Down
2 changes: 1 addition & 1 deletion tools/generators/service/files/.lintstagedrc.js__tmpl__
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const baseConfig = require('../../.lintstagedrc.js');
const baseConfig = require('<%= offsetFromRoot %>/.lintstagedrc.js');
module.exports = baseConfig;
2 changes: 1 addition & 1 deletion tools/generators/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default async (tree: Tree, schema: Schema): Promise<() => void> => {
await formatFiles(tree);

return () => {
symlinkVsCodeConfiguration(tree, options);
symlinkVsCodeConfiguration(options);
installPackagesTask(tree, true);
};
};
6 changes: 5 additions & 1 deletion tools/generators/service/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "service-generator",
"type": "object",
Expand All @@ -11,6 +10,11 @@
"$source": "argv",
"index": 0
}
},
"directory": {
"type": "string",
"description": "Directory of the new service",
"default": "backend"
}
},
"required": ["name"]
Expand Down
38 changes: 30 additions & 8 deletions tools/generators/service/typed-json-config/tsconfig.json.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import { TsConfig } from '../../types';
import { joinPathFragments } from '@nrwl/devkit';

export const packageTsConfig: TsConfig = {
extends: '../../tsconfig.json',
import { NormalizedSchema, TsConfig } from '../../types';

export const packageTsConfig = (options: NormalizedSchema): TsConfig => ({
extends: joinPathFragments(options.offsetFromRoot, 'tsconfig.json'),
compilerOptions: {
preserveSymlinks: true,
baseUrl: '.',
esModuleInterop: true,
},
references: [
{ path: '../../contracts/core-contracts/tsconfig.build.json' },
{ path: '../../packages/configuration/tsconfig.build.json' },
{ path: '../../packages/serverless-configuration/tsconfig.build.json' },
{ path: '../../packages/serverless-helpers/tsconfig.build.json' },
{
path: joinPathFragments(
options.offsetFromRoot,
'contracts/core-contracts/tsconfig.build.json',
),
},
{
path: joinPathFragments(
options.offsetFromRoot,
'packages/configuration/tsconfig.build.json',
),
},
{
path: joinPathFragments(
options.offsetFromRoot,
'packages/serverless-configuration/tsconfig.build.json',
),
},
{
path: joinPathFragments(
options.offsetFromRoot,
'packages/serverless-helpers/tsconfig.build.json',
),
},
],
include: ['./**/*.ts'],
'ts-node': {
files: true,
},
};
});
5 changes: 3 additions & 2 deletions tools/generators/types/Shema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GeneratorType } from './GeneratorType';

export interface Schema {
name: string;
directory?: string;
directory: string;
skipJestConfig?: boolean;
}

Expand All @@ -14,7 +14,8 @@ export interface NormalizedSchema extends Schema {
importPath: string;
linter: Linter;
name: string;
projectRoot: string;
packageRoot: string;
offsetFromRoot: string;
unitTestRunner: 'jest' | 'none';
workspaceName: string;
}

0 comments on commit 229b2cf

Please sign in to comment.