Skip to content

Commit

Permalink
feat: add library generator + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok committed May 19, 2024
1 parent 2406497 commit 77dd161
Show file tree
Hide file tree
Showing 44 changed files with 962 additions and 150 deletions.
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
tmp
dist
coverage
.nx
10 changes: 10 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
semi: true,
trailingComma: 'all',
singleQuote: true,
bracketSpacing: true,
arrowParens: 'always',
printWidth: 150,
tabWidth: 2,
useTabs: false,
};
2 changes: 1 addition & 1 deletion .verdaccio/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ packages:
proxy: npmjs

# log settings
logs:
log:
type: stdout
format: pretty
level: warn
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@nx/eslint-plugin": "19.0.4",
"@nx/jest": "19.0.4",
"@nx/js": "19.0.4",
"@nx/linter": "^19.0.4",
"@nx/plugin": "19.0.4",
"@nx/workspace": "19.0.4",
"@swc-node/register": "~1.8.0",
Expand All @@ -34,6 +35,7 @@
"jest": "^29.4.1",
"jest-environment-jsdom": "^29.7.0",
"nx": "19.0.4",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"ts-node": "10.9.1",
"tsx": "^4.10.5",
Expand Down
4 changes: 2 additions & 2 deletions packages/e2e/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default {
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/e2e',
globalSetup: '<rootDir>/src/jest/startLocalRegistry.ts',
globalTeardown: '<rootDir>/src/jest/stopLocalRegistry.ts',
globalSetup: '<rootDir>/src/helper/startLocalRegistry.ts',
globalTeardown: '<rootDir>/src/helper/stopLocalRegistry.ts',
};
33 changes: 33 additions & 0 deletions packages/e2e/src/generators/library.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import { execSync } from 'child_process';
import { createTestWorkspace } from '../helper/createTestProject';
import { existsSync } from 'fs';
import { join } from 'path';

describe('Library', () => {
let workspaceRoot;

beforeAll(() => {
workspaceRoot = createTestWorkspace('library-test');
execSync('nx g @hexancore/nx:lib acme/backend --type backend', {
cwd: workspaceRoot,
stdio: 'inherit',
env: process.env,
});
});

test('should be created', () => {
expect(existsSync(join(workspaceRoot, 'libs/acme/backend', 'package.json'))).toBeTruthy();
});

test('build should pass', () => {
execSync('nx run acme-backend:build', {
cwd: workspaceRoot,
stdio: 'inherit',
env: process.env,
});

expect(existsSync(join(workspaceRoot, 'dist/libs/acme/backend/src/index.js'))).toBeTruthy();
});
});

14 changes: 14 additions & 0 deletions packages/e2e/src/generators/preset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { existsSync } from 'fs';
import { join } from 'path';

import { createTestWorkspace } from '../helper/createTestProject';

describe('Preset', () => {
let workspaceRoot: string;

test('should be installed', () => {
workspaceRoot = createTestWorkspace('preset-test');
expect(existsSync(join(workspaceRoot, 'package.json'))).toBeTruthy();
});
});

39 changes: 39 additions & 0 deletions packages/e2e/src/helper/createTestProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { execSync } from 'child_process';
import { join, dirname } from 'path';
import { mkdirSync, rmSync } from 'fs';

export function createTestWorkspace(name: string): string {
process.env['HUSKY'] = '0';

const cwd = process.cwd();
const workspaceRoot = join(cwd, 'tmp', name);

rmSync(workspaceRoot, {
recursive: true,
force: true,
});

mkdirSync(dirname(workspaceRoot), {
recursive: true,
});

process.env['PNPM_HOME'] = join(cwd, 'tmp', 'pnpm-store');
rmSync(process.env['PNPM_HOME'], {
recursive: true,
force: true,
});

const createWorkspaceScript = join(cwd, 'tools', 'scripts', 'create-workspace.mjs');
try {
execSync(`node ${createWorkspaceScript} ${name}`, {
cwd: dirname(workspaceRoot),
stdio: 'inherit',
env: process.env,
});
} catch (e) {
console.error(e);
throw e;
}

return workspaceRoot;
}
File renamed without changes.
File renamed without changes.
49 changes: 0 additions & 49 deletions packages/e2e/src/preset.test.ts

This file was deleted.

5 changes: 5 additions & 0 deletions packages/plugin/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"schema": "./src/generators/preset/schema.json",
"description": "preset generator",
"x-use-standalone-layout": true
},
"lib": {
"factory": "./src/generators/library/libraryGenerator",
"schema": "./src/generators/library/schema.json",
"description": "library generator"
}
}
}
9 changes: 9 additions & 0 deletions packages/plugin/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@
"jestConfig": "{projectRoot}/jest.config.ts",
"runInBand": true
}
},
"test-watch": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "{projectRoot}/jest.config.ts",
"runInBand": true,
"watch": true
}
}
}
}
1 change: 1 addition & 0 deletions packages/plugin/src/generators/library/LibraryType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type LibraryType = 'frontend' | 'backend' | 'shared';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# <%= project.name %>

## Building

Run `nx build <%= project.name %>` to build the library.

## Running tests

Run `nx test <%= project.name %>` to execute the tests.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import createJestConfig from "../../../jest.preset";

const jestConfig = createJestConfig(__dirname);

export default jestConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"moduleResolution": "Node16",
"module": "Node16",
"target": "es2022",
"esModuleInterop": true,
"paths": {
"<%= project.importName %>": [
"<%= project.root %>/src/index.ts"
],
"@": [
"<%= project.root %>/src"
],
"@/*": [
"<%= project.root %>/src/*"
],
"@test/*": [
"<%= project.root %>/test/helper/*"
]
}
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.test.json"
},
{
"path": "./tsconfig.lib.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"declaration": true
},
"include": ["src/**/*.ts"],
"exclude": ["test/**/*"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"types": ["jest"]
},

"files": [
"../../../node_modules/jest-expect-message/types/index.d.ts",
"../../../node_modules/@hexancore/common/lib/cjs/Test/Matchers.d.ts"
],
"include": [
"src/**/*.ts",
"test/**/*.test.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @group unit
*/
describe('unit sample test', () => {
test('sample', () => {
expect(1).toBe(1);
});
});
34 changes: 34 additions & 0 deletions packages/plugin/src/generators/library/libraryGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
Tree,
updateJson
} from '@nx/devkit';
import * as path from 'path';
import { HcNxHelper } from '../../util/HcNxHelper';
import { ProjectMeta, ProjectPackageJsonGenerator } from '../../util';
import { LibraryGeneratorSchema } from './schema';

export async function libraryGenerator(tree: Tree, options: LibraryGeneratorSchema): Promise<void> {

const helper = new HcNxHelper(tree);
const project: ProjectMeta = helper.createProjectMeta(options.directory, false);

helper.addProjectConfiguration(project);
ProjectPackageJsonGenerator.run(tree, { project });

const filesPath = path.join(__dirname, 'files');
helper.generateProjectFiles(project, path.join(filesPath, 'common'), '');

updateJson(helper.tree, 'tsconfig.base.json', (v) => {
v.compilerOptions.paths = v.paths ?? {};
v.compilerOptions.paths[project.importName] = [`${project.root}/src/index.ts`];
return v;
});

tree.write(path.join(project.root, 'src/index.ts'), 'export const INITIAL = 1;');
tree.write(path.join(project.root, 'test/helper/.gitkeep'), '');
tree.write(path.join(project.root, 'test/unit/.gitkeep'), '');
tree.write(path.join(project.root, 'test/integration/.gitkeep'), '');
helper.generateProjectFiles(project, path.join(filesPath, 'test'), 'test/unit');
}

export default libraryGenerator;
6 changes: 6 additions & 0 deletions packages/plugin/src/generators/library/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { LibraryType } from "./LibraryType";

export interface LibraryGeneratorSchema {
directory: string;
type: LibraryType | 'backend' | 'frontend' | 'shared';
}
34 changes: 34 additions & 0 deletions packages/plugin/src/generators/library/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "https://json-schema.org/schema",
"$id": "Library",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use?"
},
"directory": {
"type": "string",
"description": "A directory where the lib is placed.",
"x-priority": "important",
"$default": {
"$source": "argv",
"index": 0
},
},
"type": {
"description": "The library type.",
"type": "string",
"enum": ["frontend", "backend", "shared"],
"x-prompt": "Which library type would you like to use ? Choose 'none' to skip build setup.",
"x-priority": "important"
}
},
"required": ["name", "type"]
}
Loading

0 comments on commit 77dd161

Please sign in to comment.