-
-
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(workspace-tools): Added the node-library generator
- Loading branch information
1 parent
cdceef8
commit 85ada65
Showing
23 changed files
with
468 additions
and
26 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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./jest/config"; | ||
export * from "./jest/package.config"; | ||
export * from "./jest/workspace.config"; |
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 { join } from "path"; | ||
|
||
/** | ||
* Config for Jest unit tests | ||
* | ||
* https://jestjs.io/docs/configuration#projects-arraystring--projectconfig | ||
*/ | ||
|
||
/** | ||
* Config for Jest unit tests | ||
* | ||
* @remarks Please see [the Jest documentation](https://jestjs.io/docs/configuration#projects-arraystring--projectconfig) for more information. | ||
* | ||
* @param projectDir The directory of the project | ||
* @param isNode Whether the project is a Node project | ||
* @param displayName The name to display in the Jest output | ||
* @returns The Jest configuration | ||
*/ | ||
export const getJestConfig = ( | ||
projectDir: string, | ||
isNode = true, | ||
displayName?: string | ||
) => ({ | ||
displayName: displayName | ||
? displayName | ||
: projectDir.replaceAll("\\", "-").replaceAll("/", "-"), | ||
preset: "@storm-software/testing-tools/jest/preset.js", | ||
testEnvironment: isNode ? "node" : "jsdom", | ||
transform: { | ||
"^.+\\.[tj]s$": ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }] | ||
}, | ||
moduleFileExtensions: ["ts", "js", "html"], | ||
coverageDirectory: join("../../coverage", projectDir) | ||
}); |
File renamed without changes.
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"executors": { | ||
"build": { | ||
"implementation": "./executors/build/executor", | ||
"schema": "./executors/build/schema.json", | ||
"tsup": { | ||
"implementation": "./executors/tsup/executor", | ||
"schema": "./executors/tsup/schema.json", | ||
"description": "Run a build on the project using tsup configuration", | ||
"hasher": "./executors/build/hasher" | ||
"hasher": "./executors/tsup/hasher" | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from "./src/executors/tsup/executor"; | ||
export * from "./src/generators/init/init"; | ||
export * from "./src/generators/node-library/generator"; | ||
export * from "./src/generators/preset/generator"; | ||
export * from "./src/utils/versions"; |
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
30 changes: 30 additions & 0 deletions
30
packages/workspace-tools/src/generators/node-library/files/README.md
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,30 @@ | ||
<!-- START header --> | ||
<!-- END header --> | ||
|
||
# <%= name %> | ||
|
||
<!-- START doctoc --> | ||
<!-- END doctoc --> | ||
|
||
## Reduced Package Size | ||
|
||
This project uses [tsup](https://tsup.egoist.dev/) to package the source code due to its ability to remove unused code and ship smaller javascript files thanks to code splitting. This helps to greatly reduce the size of the package and to make it easier to use in other projects. | ||
|
||
## Development | ||
|
||
This project is built using [Nx](https://nx.dev). As a result, many of the usual commands are available to assist in development. | ||
|
||
### Building | ||
|
||
Run `nx build <%= name %>` to build the library. | ||
|
||
### Running unit tests | ||
|
||
Run `nx test <%= name %>` to execute the unit tests via [Jest](https://jestjs.io). | ||
|
||
### Linting | ||
|
||
Run `nx lint <%= name %>` to run [ESLint](https://eslint.org/) on the package. | ||
|
||
<!-- START footer --> | ||
<!-- END footer --> |
3 changes: 3 additions & 0 deletions
3
packages/workspace-tools/src/generators/node-library/files/jest.config.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,3 @@ | ||
import { getJestConfig } from "@storm-software/testing-tools"; | ||
|
||
export default getJestConfig("<%= projectDir %>", true, "<%= name %>"); |
1 change: 1 addition & 0 deletions
1
packages/workspace-tools/src/generators/node-library/files/src/index.ts.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 @@ | ||
const variable = "<%= name %>"; |
13 changes: 13 additions & 0 deletions
13
packages/workspace-tools/src/generators/node-library/files/tsconfig.spec.json
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,13 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "<%= offsetFromRoot %>/dist/out-tsc", | ||
"types": ["jest", "node"] | ||
}, | ||
"include": [ | ||
"jest.config.ts", | ||
"src/**/*.test.ts", | ||
"src/**/*.spec.ts", | ||
"src/**/*.d.ts" | ||
] | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/workspace-tools/src/generators/node-library/generator.spec.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,20 @@ | ||
import { Tree, readProjectConfiguration } from "@nx/devkit"; | ||
import { createTreeWithEmptyWorkspace } from "@nx/devkit/testing"; | ||
|
||
import { nodeLibraryGenerator } from "./generator"; | ||
import { NodeLibraryGeneratorSchema } from "./schema"; | ||
|
||
describe("node-library generator", () => { | ||
let tree: Tree; | ||
const options: NodeLibraryGeneratorSchema = { name: "test" }; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it("should run successfully", async () => { | ||
await nodeLibraryGenerator(tree, options); | ||
const config = readProjectConfiguration(tree, "test"); | ||
expect(config).toBeDefined(); | ||
}); | ||
}); |
176 changes: 176 additions & 0 deletions
176
packages/workspace-tools/src/generators/node-library/generator.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,176 @@ | ||
import { | ||
GeneratorCallback, | ||
Tree, | ||
addDependenciesToPackageJson, | ||
addProjectConfiguration, | ||
formatFiles, | ||
generateFiles, | ||
joinPathFragments, | ||
names, | ||
offsetFromRoot, | ||
updateJson, | ||
writeJson | ||
} from "@nx/devkit"; | ||
import { getRelativePathToRootTsConfig, tsConfigBaseOptions } from "@nx/js"; | ||
import jsInitGenerator from "@nx/js/src/generators/init/init"; | ||
import { NormalizedSchema } from "@nx/js/src/generators/library/library"; | ||
import setupVerdaccio from "@nx/js/src/generators/setup-verdaccio/generator"; | ||
import { PackageJson } from "nx/src/utils/package-json"; | ||
import { join } from "path"; | ||
import { typesNodeVersion } from "../../utils/versions"; | ||
import { normalizeOptions } from "./normalize-options"; | ||
import { NodeLibraryGeneratorSchema } from "./schema"; | ||
|
||
export async function nodeLibraryGenerator( | ||
tree: Tree, | ||
schema: NodeLibraryGeneratorSchema | ||
) { | ||
const filesDir = join(__dirname, "./files"); | ||
const options = await normalizeOptions(tree, schema); | ||
|
||
const tasks: GeneratorCallback[] = []; | ||
tasks.push( | ||
await jsInitGenerator(tree, { | ||
...options, | ||
tsConfigName: options.rootProject ? "tsconfig.json" : "tsconfig.base.json" | ||
}) | ||
); | ||
|
||
tasks.push( | ||
addDependenciesToPackageJson( | ||
tree, | ||
{}, | ||
{ | ||
"@storm-software/workspace-tools": "latest", | ||
"@storm-software/testing-tools": "latest", | ||
"@types/node": typesNodeVersion | ||
} | ||
) | ||
); | ||
|
||
if (options.publishable) { | ||
tasks.push(await setupVerdaccio(tree, { ...options, skipFormat: true })); | ||
} | ||
|
||
const { className, name, propertyName } = names( | ||
options.projectNames.projectFileName | ||
); | ||
|
||
createProjectTsConfigJson(tree, options); | ||
|
||
addProjectConfiguration(tree, options.name, { | ||
root: options.directory, | ||
projectType: "library", | ||
sourceRoot: `${options.directory}/src`, | ||
targets: { | ||
build: { | ||
executor: "@storm-software/workspace-tools:tsup", | ||
outputs: ["{options.outputPath}"], | ||
options: { | ||
outputPath: getOutputPath(options), | ||
main: join(options.projectRoot, "src/index.ts"), | ||
tsConfig: join(options.projectRoot, "tsconfig.json"), | ||
project: join(options.projectRoot, "package.json") | ||
} | ||
}, | ||
lint: {}, | ||
test: {} | ||
} | ||
}); | ||
|
||
generateFiles(tree, filesDir, options.projectRoot, { | ||
...options, | ||
dot: ".", | ||
className, | ||
name, | ||
propertyName, | ||
js: !!options.js, | ||
cliCommand: "nx", | ||
strict: undefined, | ||
tmpl: "", | ||
offsetFromRoot: offsetFromRoot(options.projectRoot), | ||
buildable: options.bundler && options.bundler !== "none", | ||
hasUnitTestRunner: options.unitTestRunner !== "none" | ||
}); | ||
|
||
const packageJsonPath = joinPathFragments( | ||
options.projectRoot, | ||
"package.json" | ||
); | ||
|
||
updateJson<PackageJson>(tree, packageJsonPath, (json: PackageJson) => { | ||
json.name = options.importPath; | ||
json.version = "0.0.1"; | ||
// If the package is publishable or root/standalone, we should remove the private field. | ||
if (json.private && (options.publishable || options.rootProject)) { | ||
delete json.private; | ||
} | ||
return { | ||
...json, | ||
files: ["build", "src"], | ||
type: "module", | ||
types: "legacy/index.d.ts", | ||
main: "legacy/index.cjs", | ||
module: "legacy/index.js", | ||
exports: { | ||
".": { | ||
import: { | ||
types: "./modern/index.d.ts", | ||
default: "./modern/index.js" | ||
}, | ||
require: { | ||
types: "./modern/index.d.cts", | ||
default: "./modern/index.cjs" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
sideEffects: false, | ||
dependencies: { | ||
...json.dependencies | ||
}, | ||
publishConfig: { | ||
access: "public" | ||
} | ||
} as unknown as PackageJson; | ||
}); | ||
|
||
await formatFiles(tree); | ||
} | ||
|
||
function getOutputPath(options: NormalizedSchema) { | ||
const parts = ["dist"]; | ||
if (options.projectRoot === ".") { | ||
parts.push(options.name); | ||
} else { | ||
parts.push(options.projectRoot); | ||
} | ||
return joinPathFragments(...parts); | ||
} | ||
|
||
function createProjectTsConfigJson(tree: Tree, options: NormalizedSchema) { | ||
const tsconfig = { | ||
extends: options.rootProject | ||
? undefined | ||
: getRelativePathToRootTsConfig(tree, options.projectRoot), | ||
compilerOptions: { | ||
...(options.rootProject ? tsConfigBaseOptions : {}), | ||
outDir: joinPathFragments( | ||
offsetFromRoot(options.projectRoot), | ||
"dist/out-tsc" | ||
), | ||
noEmit: true | ||
}, | ||
files: [], | ||
include: ["src/**/*.ts", "src/**/*.js", "bin/**/*"], | ||
exclude: ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"] | ||
}; | ||
|
||
writeJson( | ||
tree, | ||
joinPathFragments(options.projectRoot, "tsconfig.json"), | ||
tsconfig | ||
); | ||
} | ||
|
||
export default nodeLibraryGenerator; |
Oops, something went wrong.