Skip to content

Commit 6cd2f3e

Browse files
committed
refactor(transloco): 💡 Remove all the schematics but init
This is the first commit in a series for extracting the schematics to a different lib and removing the commonjs dependecies from Transloco which aren't really used BREAKING CHANGE: 🧨 ng-add is the only available schematic in @jsverse/transloco now ✅ Closes: #768
1 parent 4fc2651 commit 6cd2f3e

32 files changed

+657
-172
lines changed

libs/transloco-schematics/src/collection.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
"extends": ["@schematics/angular"],
33
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
44
"schematics": {
5-
"ng-add": {
6-
"aliases": ["init"],
7-
"factory": "./ng-add/index",
8-
"schema": "./ng-add/schema.json",
9-
"description": "Add transloco configuration for root module."
10-
},
115
"component": {
126
"aliases": ["c"],
137
"factory": "./component/index",

libs/transloco-schematics/src/ng-add/generators/translation-files.gen.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

libs/transloco-schematics/src/scope/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import { Change } from '@schematics/angular/utility/change';
2222
import { LIB_NAME } from '../schematics.utils';
2323
import { coerceArray, stringifyList } from '../utils/array';
2424
import { findModuleFromOptions } from '../utils/find-module';
25-
import { getProject, getProjectPath } from '../utils/projects';
25+
import { getProject } from '../utils/projects';
2626
import { createTranslateFilesFromOptions } from '../utils/translations';
2727
import { getConfig } from '../utils/config';
2828

2929
import { SchemaOptions } from './schema';
30+
import { getProjectPath } from './utils';
3031

3132
function getProviderValue(options: SchemaOptions) {
3233
const name = dasherize(options.name);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Tree } from '@angular-devkit/schematics';
2+
import { WorkspaceProject } from '@schematics/angular/utility/workspace-models';
3+
4+
import { SchemaOptions } from './schema';
5+
6+
export function getProjectPath(
7+
host: Tree,
8+
project: WorkspaceProject,
9+
options: SchemaOptions,
10+
) {
11+
if (project.root.at(-1) === '/') {
12+
project.root = project.root.slice(0, project.root.length - 1);
13+
}
14+
15+
if (options.path === undefined) {
16+
const projectDirName =
17+
project.projectType === 'application' ? 'app' : 'lib';
18+
19+
return `${project.root ? `/${project.root}` : ''}/src/${projectDirName}`;
20+
}
21+
22+
return options.path;
23+
}

libs/transloco-schematics/src/utils/projects.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,3 @@ export function setEnvironments(
4848
host.overwrite(filePath, transformer(source));
4949
});
5050
}
51-
52-
export function getProjectPath(host: Tree, project, options) {
53-
if (project.root.substr(-1) === '/') {
54-
project.root = project.root.substr(0, project.root.length - 1);
55-
}
56-
57-
if (options.path === undefined) {
58-
const projectDirName =
59-
project.projectType === 'application' ? 'app' : 'lib';
60-
61-
return `${project.root ? `/${project.root}` : ''}/src/${projectDirName}`;
62-
}
63-
64-
return options.path;
65-
}

libs/transloco-utils/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/src/**/*.js
2+
/src/**/*.js.map

libs/transloco/ng-package.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
{
22
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
33
"dest": "../../dist/libs/transloco",
4-
"assets": ["README.md", "CHANGELOG.md"],
4+
"assets": [
5+
"README.md",
6+
"CHANGELOG.md",
7+
"schematics/**/*.json",
8+
"**/files/**/*"
9+
],
510
"lib": {
611
"entryFile": "src/index.ts"
7-
},
8-
"allowedNonPeerDependencies": [
9-
"fs-extra",
10-
"glob",
11-
"flat",
12-
"@jsverse/transloco-utils",
13-
"replace-in-file",
14-
"lodash.kebabcase",
15-
"ora"
16-
]
12+
}
1713
}

libs/transloco/package.json

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,16 @@
1515
"url": "https://github.com/jsverse/transloco"
1616
},
1717
"keywords": [
18-
"angular",
19-
"angular 2",
18+
"Angular",
2019
"i18n",
21-
"translate",
22-
"angular translate",
23-
"angular i18n",
24-
"transloco"
20+
"Translate",
21+
"Angular Translate",
22+
"Transloco",
23+
"Schematics",
24+
"Angular CLI"
2525
],
2626
"license": "MIT",
2727
"peerDependencies": {
2828
"@angular/core": ">=16.0.0"
29-
},
30-
"dependencies": {
31-
"fs-extra": "^11.0.0",
32-
"glob": "^10.0.0",
33-
"tslib": "^2.2.0",
34-
"@jsverse/transloco-utils": "^7.0.0",
35-
"replace-in-file": "^7.0.1",
36-
"ora": "^5.4.1",
37-
"lodash.kebabcase": "^4.1.1"
3829
}
3930
}

libs/transloco/project.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"prefix": "transloco-nx-shell",
77
"tags": [],
88
"targets": {
9-
"build": {
9+
"build-package": {
1010
"dependsOn": ["copy-readme"],
1111
"executor": "@nx/angular:package",
1212
"outputs": ["{workspaceRoot}/dist/libs/transloco"],
@@ -23,6 +23,21 @@
2323
},
2424
"defaultConfiguration": "production"
2525
},
26+
"build": {
27+
"executor": "nx:run-commands",
28+
"options": {
29+
"parallel": false,
30+
"commands": [
31+
{
32+
"command": "nx build-package transloco"
33+
},
34+
{
35+
"command": "npx tsc -p libs/transloco/tsconfig.schematics.json"
36+
}
37+
]
38+
},
39+
"outputs": ["{workspaceRoot}/dist/libs/transloco"]
40+
},
2641
"test": {
2742
"executor": "@angular-devkit/build-angular:karma",
2843
"options": {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export { stringifyList, coerceArray } from './utils/collections';
2+
export { getProject, setEnvironments } from './utils/workspace';
3+
export { createTranslocoConfig } from './utils/transloco';
4+
export {
5+
createTranslateFiles,
6+
checkIfTranslationFilesExist,
7+
} from './utils/translation';
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
2+
import type { Schema as WorkspaceSchema } from '@schematics/angular/workspace/schema';
3+
import {
4+
Style,
5+
ViewEncapsulation,
6+
Schema as ApplicationSchema,
7+
} from '@schematics/angular/application/schema';
8+
import type { Schema as LibrarySchema } from '@schematics/angular/library/schema';
9+
10+
export const defaultWorkspaceOptions: WorkspaceSchema = {
11+
name: 'workspace',
12+
newProjectRoot: 'projects',
13+
version: '16.0.4',
14+
minimal: true,
15+
};
16+
17+
export const defaultAppOptions: ApplicationSchema = {
18+
name: 'bar',
19+
inlineStyle: false,
20+
inlineTemplate: false,
21+
viewEncapsulation: ViewEncapsulation.Emulated,
22+
routing: false,
23+
style: Style.Css,
24+
skipTests: false,
25+
};
26+
27+
const defaultLibOptions = {
28+
name: 'baz',
29+
};
30+
31+
export function createWorkspace(
32+
schematicRunner: SchematicTestRunner,
33+
options: {
34+
workspaceOptions?: Partial<WorkspaceSchema>;
35+
appOptions?: Partial<ApplicationSchema>;
36+
libOptions?: Partial<LibrarySchema>;
37+
} = {},
38+
) {
39+
const appOptions = { ...defaultAppOptions, ...options.appOptions };
40+
const workspaceOptions = {
41+
...defaultWorkspaceOptions,
42+
...options.workspaceOptions,
43+
};
44+
const libOptions = { ...defaultLibOptions, ...options.libOptions };
45+
const angularSchematic = '@schematics/angular';
46+
47+
return schematicRunner
48+
.runExternalSchematic(angularSchematic, 'workspace', workspaceOptions)
49+
.then((tree) =>
50+
schematicRunner.runExternalSchematic(
51+
angularSchematic,
52+
'application',
53+
appOptions,
54+
tree,
55+
),
56+
)
57+
.then((tree) =>
58+
schematicRunner.runExternalSchematic(
59+
angularSchematic,
60+
'library',
61+
libOptions,
62+
tree,
63+
),
64+
);
65+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './create-workspace';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function stringifyList<T>(list: T[], separator = ', '): string {
2+
return list.map((item) => `'${item}'`).join(separator);
3+
}
4+
5+
export function coerceArray<T>(value: T | T[]): T[];
6+
export function coerceArray<T>(value: T | readonly T[]): readonly T[];
7+
export function coerceArray<T>(value: T | T[]): T[] {
8+
return Array.isArray(value) ? value : [value];
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as nodePath from 'node:path';
2+
3+
import { PathFragment } from '@angular-devkit/core';
4+
import { DirEntry, Tree } from '@angular-devkit/schematics';
5+
6+
export function getJsonFileContent(fileName: PathFragment, dir: DirEntry) {
7+
const content = dir.file(fileName)!.content.toString('utf-8');
8+
return JSON.parse(content);
9+
}
10+
11+
export function writeToJson(
12+
host: Tree,
13+
dirPath: string,
14+
fileName: PathFragment,
15+
content: unknown,
16+
) {
17+
return host.overwrite(
18+
nodePath.join(dirPath, fileName),
19+
JSON.stringify(content, null, 2),
20+
);
21+
}
22+
23+
export function hasSubdirs(dir: DirEntry): boolean {
24+
return dir.subdirs && dir.subdirs.length > 0;
25+
}
26+
27+
export function hasFiles(dir: DirEntry): boolean {
28+
return dir.subfiles && dir.subfiles.length > 0;
29+
}

0 commit comments

Comments
 (0)