Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split out dotnet + utils to libraries in preparation for new packages #17

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(typescript): scaffold ts project
  • Loading branch information
AgentEnder committed Apr 15, 2021
commit 501bf643aee3033eb15daf911def0c35d9f774b7
14 changes: 14 additions & 0 deletions e2e/typescript-e2e/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
displayName: 'typescript-e2e',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[tj]s$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/e2e\typescript-e2e',
};
48 changes: 48 additions & 0 deletions e2e/typescript-e2e/tests/typescript.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
checkFilesExist,
ensureNxProject,
readJson,
runNxCommandAsync,
uniq,
} from '@nrwl/nx-plugin/testing';
describe('typescript e2e', () => {
it('should create typescript', async (done) => {
const plugin = uniq('typescript');
ensureNxProject('@nx-dotnet/typescript', 'dist/packages/typescript');
await runNxCommandAsync(
`generate @nx-dotnet/typescript:typescript ${plugin}`
);

const result = await runNxCommandAsync(`build ${plugin}`);
expect(result.stdout).toContain('Executor ran');

done();
});

describe('--directory', () => {
it('should create src in the specified directory', async (done) => {
const plugin = uniq('typescript');
ensureNxProject('@nx-dotnet/typescript', 'dist/packages/typescript');
await runNxCommandAsync(
`generate @nx-dotnet/typescript:typescript ${plugin} --directory subdir`
);
expect(() =>
checkFilesExist(`libs/subdir/${plugin}/src/index.ts`)
).not.toThrow();
done();
});
});

describe('--tags', () => {
it('should add tags to nx.json', async (done) => {
const plugin = uniq('typescript');
ensureNxProject('@nx-dotnet/typescript', 'dist/packages/typescript');
await runNxCommandAsync(
`generate @nx-dotnet/typescript:typescript ${plugin} --tags e2etag,e2ePackage`
);
const nxJson = readJson('nx.json');
expect(nxJson.projects[plugin].tags).toEqual(['e2etag', 'e2ePackage']);
done();
});
});
});
13 changes: 13 additions & 0 deletions e2e/typescript-e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.e2e.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
9 changes: 9 additions & 0 deletions e2e/typescript-e2e/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module.exports = {
projects: ['<rootDir>/packages/core', '<rootDir>/e2e\\core-e2e'],
projects: [
'<rootDir>/packages/core',
'<rootDir>/e2e\\core-e2e',
'<rootDir>/packages/typescript',
'<rootDir>/e2e\\typescript-e2e',
],
};
29 changes: 8 additions & 21 deletions nx.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
{
"npmScope": "nx-dotnet",
"affected": {
"defaultBase": "master"
},
"affected": { "defaultBase": "master" },
"implicitDependencies": {
"workspace.json": "*",
"package.json": {
"dependencies": "*",
"devDependencies": "*"
},
"package.json": { "dependencies": "*", "devDependencies": "*" },
"tsconfig.base.json": "*",
"tslint.json": "*",
".eslintrc.json": "*",
Expand All @@ -17,22 +12,14 @@
"tasksRunnerOptions": {
"default": {
"runner": "@nrwl/workspace/tasks-runners/default",
"options": {
"cacheableOperations": ["build", "lint", "test", "e2e"]
}
"options": { "cacheableOperations": ["build", "lint", "test", "e2e"] }
}
},
"projects": {
"core": {
"tags": []
},
"core-e2e": {
"tags": [],
"implicitDependencies": ["core"]
}
"core": { "tags": [] },
"core-e2e": { "tags": [], "implicitDependencies": ["core"] },
"typescript": { "tags": [] },
"typescript-e2e": { "tags": [], "implicitDependencies": ["typescript"] }
},
"workspaceLayout": {
"appsDir": "e2e",
"libsDir": "packages"
}
"workspaceLayout": { "appsDir": "e2e", "libsDir": "packages" }
}
3 changes: 3 additions & 0 deletions packages/typescript/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}
21 changes: 21 additions & 0 deletions packages/typescript/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"parserOptions": {
"project": ["packages/typescript/tsconfig.*?.json"]
},
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions packages/typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# typescript

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test typescript` to execute the unit tests via [Jest](https://jestjs.io).
10 changes: 10 additions & 0 deletions packages/typescript/executors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/schema",
"executors": {
"build": {
"implementation": "./src/executors/build/executor",
"schema": "./src/executors/build/schema.json",
"description": "build executor"
}
}
}
12 changes: 12 additions & 0 deletions packages/typescript/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json-schema.org/schema",
"name": "typescript",
"version": "0.0.1",
"generators": {
"typescript": {
"factory": "./src/generators/typescript/generator",
"schema": "./src/generators/typescript/schema.json",
"description": "typescript generator"
}
}
}
14 changes: 14 additions & 0 deletions packages/typescript/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
displayName: 'typescript',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/packages/typescript',
};
7 changes: 7 additions & 0 deletions packages/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@nx-dotnet/typescript",
"version": "0.0.1",
"main": "src/index.js",
"generators": "./generators.json",
"executors": "./executors.json"
}
11 changes: 11 additions & 0 deletions packages/typescript/src/executors/build/executor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BuildExecutorSchema } from './schema';
import executor from './executor';

const options: BuildExecutorSchema = {};

describe('Build Executor', () => {
it('can run', async () => {
const output = await executor(options);
expect(output.success).toBe(true);
});
});
8 changes: 8 additions & 0 deletions packages/typescript/src/executors/build/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BuildExecutorSchema } from './schema';

export default async function runExecutor(options: BuildExecutorSchema) {
console.log('Executor ran for Build', options);
return {
success: true,
};
}
1 change: 1 addition & 0 deletions packages/typescript/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface BuildExecutorSchema {} // eslint-disable-line
9 changes: 9 additions & 0 deletions packages/typescript/src/executors/build/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"title": "Build executor",
"description": "",
"type": "object",
"properties": {},
"required": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const variable = "<%= projectName %>";
20 changes: 20 additions & 0 deletions packages/typescript/src/generators/typescript/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Tree, readProjectConfiguration } from '@nrwl/devkit';

import generator from './generator';
import { TypescriptGeneratorSchema } from './schema';

describe('typescript generator', () => {
let appTree: Tree;
const options: TypescriptGeneratorSchema = { name: 'test' };

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
});

it('should run successfully', async () => {
await generator(appTree, options);
const config = readProjectConfiguration(appTree, 'test');
expect(config).toBeDefined();
});
});
73 changes: 73 additions & 0 deletions packages/typescript/src/generators/typescript/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
addProjectConfiguration,
formatFiles,
generateFiles,
getWorkspaceLayout,
names,
offsetFromRoot,
Tree,
} from '@nrwl/devkit';
import * as path from 'path';
import { TypescriptGeneratorSchema } from './schema';

interface NormalizedSchema extends TypescriptGeneratorSchema {
projectName: string;
projectRoot: string;
projectDirectory: string;
parsedTags: string[];
}

function normalizeOptions(
host: Tree,
options: TypescriptGeneratorSchema
): NormalizedSchema {
const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
: name;
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const projectRoot = `${getWorkspaceLayout(host).libsDir}/${projectDirectory}`;
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];

return {
...options,
projectName,
projectRoot,
projectDirectory,
parsedTags,
};
}

function addFiles(host: Tree, options: NormalizedSchema) {
const templateOptions = {
...options,
...names(options.name),
offsetFromRoot: offsetFromRoot(options.projectRoot),
template: '',
};
generateFiles(
host,
path.join(__dirname, 'files'),
options.projectRoot,
templateOptions
);
}

export default async function (host: Tree, options: TypescriptGeneratorSchema) {
const normalizedOptions = normalizeOptions(host, options);
addProjectConfiguration(host, normalizedOptions.projectName, {
root: normalizedOptions.projectRoot,
projectType: 'library',
sourceRoot: `${normalizedOptions.projectRoot}/src`,
targets: {
build: {
executor: '@nx-dotnet/typescript:build',
},
},
tags: normalizedOptions.parsedTags,
});
addFiles(host, normalizedOptions);
await formatFiles(host);
}
5 changes: 5 additions & 0 deletions packages/typescript/src/generators/typescript/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface TypescriptGeneratorSchema {
name: string;
tags?: string;
directory?: string;
}
29 changes: 29 additions & 0 deletions packages/typescript/src/generators/typescript/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"id": "Typescript",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use?"
},
"tags": {
"type": "string",
"description": "Add tags to the project (used for linting)",
"alias": "t"
},
"directory": {
"type": "string",
"description": "A directory where the project is placed",
"alias": "d"
}
},
"required": ["name"]
}
Empty file.
Loading