Skip to content

Commit

Permalink
Merge pull request #6 from kreuzerk/feature/updateCommands
Browse files Browse the repository at this point in the history
Feature/update commands
  • Loading branch information
nivekcode authored May 1, 2020
2 parents 09ffe8b + faeede0 commit b572dfb
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 52 deletions.
13 changes: 13 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# Ignores TypeScript files, but keeps definitions.
!*.d.ts

*.ts(!**/files/**/*.{ts,json})

.idea

!package.json
jest.config.js
coverage/
tsconfig.json
tslint.json
.travis.yml
.prettierrc
.prettierignore
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/subentry/files/**/*
14 changes: 7 additions & 7 deletions src/collection.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"ng-samurai": {
"description": "A blank schematic.",
"factory": "./ng-samurai/index#ngSamurai"
"split-lib": {
"description": "Automatically split your library into multiple chungks",
"factory": "./library-split/index#splitLib"
},
"submodule": {
"description": "A schematic to generate tree-shakeable sub-module in Angular library (ng-packagr secondary entry point).",
"factory": "./submodule/index#submodule",
"schema": "./submodule/schema.json"
"generate-subentry": {
"description": "A schematic to generate tree-shakeable sub-entry in Angular library (ng-packagr secondary entry point).",
"factory": "./subentry/index#generateSubentry",
"schema": "./subentry/schema.json"
}
}
}
42 changes: 21 additions & 21 deletions src/ng-samurai/index.spec.ts → src/library-split/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const collectionPath = path.join(__dirname, '../collection.json');
const runner = new SchematicTestRunner('schematics', collectionPath);
let appTree: UnitTestTree;

describe('ng-samurai', () => {
describe('split', () => {
beforeEach(async () => {
console.log = () => {};

Expand Down Expand Up @@ -101,7 +101,7 @@ describe('ng-samurai', () => {
}

it('should export foo and bar from the public-api', async () => {
const updatedTree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const updatedTree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
const topLevelPublicAPIContent = updatedTree.readContent(
'/projects/some-lib/src/public-api.ts'
);
Expand All @@ -124,22 +124,22 @@ describe('ng-samurai', () => {
}

it('should add a public_api to foo module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/foo/public-api.ts')).toBe(true);
});

it('should add a public_api to bar module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/public-api.ts')).toBe(true);
});

it('should not add a public_api to baz module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/baz/public-api.ts')).not.toBe(true);
});

it('should export foo.component.ts and foo.module.ts from foos public-api', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
const publicAPI = tree.read('/projects/some-lib/src/lib/foo/public-api.ts').toString();
const expectedFilesIncludedInPublicAPI = ['foo.module', 'foo.component', 'foo.service'];
const expectedFileContent = expectedSubentryPublicAPIContent(
Expand All @@ -150,7 +150,7 @@ describe('ng-samurai', () => {
});

it('should export bar.component.ts, bar.module.ts, bar.model and baz.component.ts from bars public-api', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
const publicAPI = tree.read('/projects/some-lib/src/lib/bar/public-api.ts').toString();
const expectedFilesIncludedInPublicAPI = [
'bar.module',
Expand All @@ -169,43 +169,43 @@ describe('ng-samurai', () => {

describe('index.ts', () => {
it('should add an index.ts to foo module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/foo/index.ts')).toBe(true);
});

it('should add export everything from public-api inside the index.ts of foo', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.read('/projects/some-lib/src/lib/foo/index.ts').toString()).toEqual(
"export * from './public-api';\n"
);
});

it('should add an index.ts bar module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/index.ts')).toBe(true);
});

it('should add export everything from public-api inside the index.ts of bar', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.read('/projects/some-lib/src/lib/bar/index.ts').toString()).toEqual(
"export * from './public-api';\n"
);
});

it('should not add an index.ts to baz module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/baz/index.ts')).not.toBe(true);
});
});

describe('package.json', () => {
it('should add an index.ts to foo module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/foo/package.json')).toBe(true);
});

it('should add the correct config to the package.json of foo subentry', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
const expectedSubentryConfig = {
ngPackage: {
lib: {
Expand All @@ -220,12 +220,12 @@ describe('ng-samurai', () => {
});

it('should add an packag.json to bar module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/package.json')).toBe(true);
});

it('should add the correct config to the package.json of bar subentry', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
const expectedSubentryConfig = {
ngPackage: {
lib: {
Expand All @@ -240,7 +240,7 @@ describe('ng-samurai', () => {
});

it('should not add a package.json to baz module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/baz/package.json')).not.toBe(true);
});
});
Expand Down Expand Up @@ -283,7 +283,7 @@ describe('ng-samurai', () => {
const expectedModuleContent = getExpectedBarModuleContent();
updateBarModuleContent();

const updatedTree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const updatedTree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
const moduleContentAfterSchematics = updatedTree.readContent(
'/projects/some-lib/src/lib/bar/bar.module.ts'
);
Expand All @@ -296,7 +296,7 @@ describe('ng-samurai', () => {
const expectedComponentContent = getExpectedBazComponentContent();
updateBazComponentContent();

const updatedTree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const updatedTree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
const componentContentAfterSchematics = updatedTree.readContent(
'/projects/some-lib/src/lib/bar/baz/baz.component.ts'
);
Expand All @@ -313,7 +313,7 @@ describe('ng-samurai', () => {
}

it('should update the paths in the tsconfig.json', async () => {
const updatedTree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const updatedTree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
const tsconfigContent = JSON.parse(updatedTree.readContent('tsconfig.json'));
const expectedPaths = {
'some-lib': ['dist/some-lib/some-lib', 'dist/some-lib'],
Expand All @@ -326,7 +326,7 @@ describe('ng-samurai', () => {

it('should add paths to the tsconfig.json even if no path exist', async () => {
deletePathsFromTsconfig();
const updatedTree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
const updatedTree = await runner.runSchematicAsync('split-lib', {}, appTree).toPromise();
const tsconfigContent = JSON.parse(updatedTree.readContent('tsconfig.json'));
const expectedPaths = {
'some-lib/*': ['projects/some-lib/*', 'projects/some-lib']
Expand Down
12 changes: 6 additions & 6 deletions src/ng-samurai/index.ts → src/library-split/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { chain, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';

import { submodule } from '../submodule/index';
import { getLibRootPath, getModuleName } from '../shared/pathHelper';
import { generateSubentry } from '../subentry/index';
import { getFolderPath, getLibRootPath, getModuleName, resolvePath } from '../shared/path-helper';
import { addTsconfigPaths } from '../rules/add-tsconfig-paths.rule';
import { updateImportPaths } from '../rules/update-import-paths.rule';
import { updateSubentryPublicAPI } from '../rules/update-public-api/update-subentry-public-api.rule';
import { updateTopLevelPublicAPI } from '../rules/update-public-api/update-top-level-public-api.rule';
import { logWelcomeMessage } from '../shared/log-helper';

export function ngSamurai(_options: any): Rule {
export function splitLib(_options: any): Rule {
logWelcomeMessage();

return (tree: Tree, _context: SchematicContext) => {
Expand All @@ -25,10 +25,10 @@ export function ngSamurai(_options: any): Rule {
modulePaths.push(filePath);

rules.push(
submodule({
generateSubentry({
name: getModuleName(filePath),
filesPath: '../submodule/files',
path: libRootPath,
filesPath: '../subentry/files',
path: resolvePath(getFolderPath(filePath), '..'),
generateComponent: false,
generateModule: false
})
Expand Down
2 changes: 1 addition & 1 deletion src/rules/update-import-paths.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
convertModulePathToPublicAPIImport,
convertToAbsolutPath,
getFolderPath
} from '../shared/pathHelper';
} from '../shared/path-helper';
import { logError } from '../shared/log-helper';

interface Modification {
Expand Down
1 change: 1 addition & 0 deletions src/rules/update-public-api/update-public-api.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export function updatePublicAPI(path: string, paths: string[]): Rule {
const publicAPIFile = path + '/public-api.ts';
tree.overwrite(publicAPIFile, generatePublicAPIcontent(paths));
} catch (e) {
console.error(e);
logError(`Something went wrong: Do you have multiple modules in ${path}`);
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Rule, Tree } from '@angular-devkit/schematics';
import { buildRelativePath } from '@schematics/angular/utility/find-module';

import { getFileDirectoryPath } from '../../shared/pathHelper';
import { getFileDirectoryPath } from '../../shared/path-helper';

import { updatePublicAPI } from './update-public-api.rule';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rule, Tree } from '@angular-devkit/schematics';

import { convertModulePathToPublicAPIImport, getSourceRootPath } from '../../shared/pathHelper';
import { convertModulePathToPublicAPIImport, getSourceRootPath } from '../../shared/path-helper';

import { updatePublicAPI } from './update-public-api.rule';

Expand Down
5 changes: 1 addition & 4 deletions src/shared/log-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,5 @@ export function logWelcomeMessage() {
}

export function logError(error: string) {
console.log(boxen(`${chalk.blue('Ng-samurai: ')} ${chalk.red(error)}`), {
padding: 2,
borderColor: 'red'
});
console.log(`${chalk.blue('Ng-samurai: ')} ${chalk.red(error)}`);
}
Loading

0 comments on commit b572dfb

Please sign in to comment.