-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add library generator + refactor
- Loading branch information
Showing
44 changed files
with
962 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
tmp | ||
dist | ||
coverage | ||
.nx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ packages: | |
proxy: npmjs | ||
|
||
# log settings | ||
logs: | ||
log: | ||
type: stdout | ||
format: pretty | ||
level: warn | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type LibraryType = 'frontend' | 'backend' | 'shared'; |
5 changes: 5 additions & 0 deletions
5
packages/plugin/src/generators/library/files/common/.eslintrc.json__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"] | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
packages/plugin/src/generators/library/files/common/README.md.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
5 changes: 5 additions & 0 deletions
5
packages/plugin/src/generators/library/files/common/jest.config.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
33 changes: 33 additions & 0 deletions
33
packages/plugin/src/generators/library/files/common/tsconfig.json.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/plugin/src/generators/library/files/common/tsconfig.lib.json__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**/*"] | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/plugin/src/generators/library/files/common/tsconfig.test.json__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/plugin/src/generators/library/files/test/sample.test.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
packages/plugin/src/generators/library/libraryGenerator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Oops, something went wrong.