Skip to content

Commit

Permalink
fix: application supports backspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Zlatin Stanimirov committed Mar 3, 2022
1 parent 5be8367 commit 81227c4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 21 deletions.
102 changes: 83 additions & 19 deletions src/lib/application/application.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ describe('Application Factory', () => {
const options: ApplicationOptions = {
name: 'project',
};
const tree: UnitTestTree = await runner.runSchematicAsync('application', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
'/project/.eslintrc.js',
Expand All @@ -35,15 +37,19 @@ describe('Application Factory', () => {
'/project/test/jest-e2e.json',
]);

expect(JSON.parse(tree.readContent('/project/package.json'))).toMatchObject({
expect(
JSON.parse(tree.readContent('/project/package.json')),
).toMatchObject({
name: 'project',
});
});
it('should manage name with dots in it', async () => {
const options: ApplicationOptions = {
name: 'project.foo.bar',
};
const tree: UnitTestTree = await runner.runSchematicAsync('application', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
`/project.foo.bar/.eslintrc.js`,
Expand All @@ -63,15 +69,19 @@ describe('Application Factory', () => {
`/project.foo.bar/test/jest-e2e.json`,
]);

expect(JSON.parse(tree.readContent('/project.foo.bar/package.json'))).toMatchObject({
expect(
JSON.parse(tree.readContent('/project.foo.bar/package.json')),
).toMatchObject({
name: 'project.foo.bar',
});
});
it('should manage name to dasherize from camel case name', async () => {
it('should manage name to normalize from camel case name', async () => {
const options: ApplicationOptions = {
name: 'awesomeProject',
};
const tree: UnitTestTree = await runner.runSchematicAsync('application', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
'/awesome-project/.eslintrc.js',
Expand All @@ -91,15 +101,51 @@ describe('Application Factory', () => {
'/awesome-project/test/jest-e2e.json',
]);

expect(JSON.parse(tree.readContent('/awesome-project/package.json'))).toMatchObject({
expect(
JSON.parse(tree.readContent('/awesome-project/package.json')),
).toMatchObject({
name: 'awesome-project',
});
});
it('should keep backspaces', async () => {
const options: ApplicationOptions = {
name: '_awesomeProject',
};
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
'/_awesome-project/.eslintrc.js',
'/_awesome-project/.gitignore',
'/_awesome-project/.prettierrc',
'/_awesome-project/README.md',
'/_awesome-project/nest-cli.json',
'/_awesome-project/package.json',
'/_awesome-project/tsconfig.build.json',
'/_awesome-project/tsconfig.json',
'/_awesome-project/src/app.controller.spec.ts',
'/_awesome-project/src/app.controller.ts',
'/_awesome-project/src/app.module.ts',
'/_awesome-project/src/app.service.ts',
'/_awesome-project/src/main.ts',
'/_awesome-project/test/app.e2e-spec.ts',
'/_awesome-project/test/jest-e2e.json',
]);

expect(
JSON.parse(tree.readContent('/_awesome-project/package.json')),
).toMatchObject({
name: '_awesome-project',
});
});
it('should manage basic name that has no scope name in it but starts with "@"', async () => {
const options: ApplicationOptions = {
name: '@/package',
};
const tree: UnitTestTree = await runner.runSchematicAsync('application', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
'/@/package/.eslintrc.js',
Expand All @@ -119,15 +165,19 @@ describe('Application Factory', () => {
'/@/package/test/jest-e2e.json',
]);

expect(JSON.parse(tree.readContent('/@/package/package.json'))).toMatchObject({
expect(
JSON.parse(tree.readContent('/@/package/package.json')),
).toMatchObject({
name: 'package',
});
});
it('should manage the name "." (ie., current working directory)', async () => {
const options: ApplicationOptions = {
name: '.',
};
const tree: UnitTestTree = await runner.runSchematicAsync('application', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
'/.eslintrc.js',
Expand Down Expand Up @@ -157,7 +207,9 @@ describe('Application Factory', () => {
const options: ApplicationOptions = {
name: '@scope/package',
};
const tree: UnitTestTree = await runner.runSchematicAsync('application', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
'/@scope/package/.eslintrc.js',
Expand All @@ -177,15 +229,19 @@ describe('Application Factory', () => {
'/@scope/package/test/jest-e2e.json',
]);

expect(JSON.parse(tree.readContent('/@scope/package/package.json'))).toMatchObject({
expect(
JSON.parse(tree.readContent('/@scope/package/package.json')),
).toMatchObject({
name: '@scope/package',
});
});
it('should manage name with blank space right after the "@" symbol', async () => {
const options: ApplicationOptions = {
name: '@ /package',
};
const tree: UnitTestTree = await runner.runSchematicAsync('application', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
'/@-/package/.eslintrc.js',
Expand All @@ -205,7 +261,9 @@ describe('Application Factory', () => {
'/@-/package/test/jest-e2e.json',
]);

expect(JSON.parse(tree.readContent('/@-/package/package.json'))).toMatchObject({
expect(
JSON.parse(tree.readContent('/@-/package/package.json')),
).toMatchObject({
name: '@-/package',
});
});
Expand All @@ -217,7 +275,9 @@ describe('Application Factory', () => {
name: 'project',
language: 'js',
};
const tree: UnitTestTree = await runner.runSchematicAsync('application', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
'/project/.babelrc',
Expand All @@ -238,16 +298,20 @@ describe('Application Factory', () => {
'/project/test/jest-e2e.json',
]);

expect(JSON.parse(tree.readContent('/project/package.json'))).toMatchObject({
name: 'project',
});
expect(JSON.parse(tree.readContent('/project/package.json'))).toMatchObject(
{
name: 'project',
},
);
});
it('should manage destination directory', async () => {
const options: ApplicationOptions = {
name: 'project',
directory: 'app',
};
const tree: UnitTestTree = await runner.runSchematicAsync('application', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
const files: string[] = tree.files;
expect(files).toEqual([
'/app/.eslintrc.js',
Expand Down
5 changes: 3 additions & 2 deletions src/lib/application/application.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
template,
url,
} from '@angular-devkit/schematics';
import { basename, parse, delimiter } from 'path';
import { basename, parse } from 'path';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import {
DEFAULT_AUTHOR,
DEFAULT_DESCRIPTION,
Expand All @@ -18,7 +19,7 @@ import {
import { ApplicationOptions } from './application.schema';

export function main(options: ApplicationOptions): Rule {
options.name = strings.dasherize(options.name);
options.name = normalizeToKebabOrSnakeCase(options.name);

const path =
!options.directory || options.directory === 'undefined'
Expand Down

0 comments on commit 81227c4

Please sign in to comment.