Skip to content

Commit 58a4b7d

Browse files
committed
feat(builder): creating
- adding ts-node for running jasmine in ts - add builder and schema
1 parent 437ca77 commit 58a4b7d

File tree

7 files changed

+172
-28
lines changed

7 files changed

+172
-28
lines changed

builders.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"builders": {
3+
"svg-icons-builder": {
4+
"implementation": "./svg-icons-builder",
5+
"schema": "./svg-icons-builder/schema.json",
6+
"description": "Build SVG icons."
7+
}
8+
}
9+
}

package-lock.json

Lines changed: 72 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
{
2-
"name": "svg-icons-builder",
2+
"name": "@angular-extensions/svg-icons-builder",
33
"version": "0.0.0",
44
"description": "A blank schematics",
55
"scripts": {
6-
"build": "tsc -p tsconfig.json",
7-
"copy:files": "cp ../**/*.json dist/",
8-
"test": "npm run build && npm run copy:files && jasmine dist/**/*_spec.js"
6+
"build": "tsc -p tsconfig.json && npm run copy:files",
7+
"build:watch": "tsc -p tsconfig.json --watch && npm run copy:files",
8+
"copy:files": "cp ./builders.json ./package.json dist/ && cp ./src/*.json dist/ && cp ./src/svg-icons-builder/*.json ./dist/svg-icons-builder/",
9+
"test": "ts-node node_modules/jasmine/bin/jasmine src/**/*_spec.ts"
910
},
1011
"keywords": [
1112
"schematics"
1213
],
1314
"author": "",
1415
"license": "MIT",
15-
"schematics": "./src/collection.json",
16+
"schematics": "./collection.json",
17+
"builders": "./builders.json",
1618
"dependencies": {
1719
"@angular-devkit/core": "^10.1.6",
1820
"@angular-devkit/schematics": "^10.1.6",
1921
"typescript": "~4.0.2"
2022
},
2123
"devDependencies": {
22-
"@types/node": "^12.11.1",
24+
"@angular-devkit/architect": "^0.1001.7",
2325
"@types/jasmine": "~3.5.0",
24-
"jasmine": "^3.5.0"
26+
"@types/node": "^12.11.1",
27+
"jasmine": "^3.5.0",
28+
"ts-node": "^9.0.0"
2529
}
2630
}

src/collection.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
{
22
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
33
"schematics": {
4-
"svg-icons-builder": {
5-
"description": "A blank schematic.",
6-
"factory": "./svg-icons-builder/index#svgIconsBuilder"
7-
},
84
"ng-add": {
95
"description": "A blank schematic.",
106
"factory": "./ng-add/index#ngAdd"

src/svg-icons-builder/index.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
1+
2+
import { BuilderOutput, createBuilder } from '@angular-devkit/architect';
3+
import * as childProcess from 'child_process';
4+
import { JsonObject } from '@angular-devkit/core';
25

3-
4-
// You don't have to export the function as default. You can also have more than one rule factory
5-
// per file.
6-
export function svgIconsBuilder(_options: any): Rule {
7-
return (tree: Tree, _context: SchematicContext) => {
8-
return tree;
9-
};
6+
interface Options extends JsonObject {
7+
command: string;
8+
args: string[];
109
}
10+
11+
export default createBuilder<Options>((options, context) => {
12+
console.log("options, context", options)
13+
return new Promise<BuilderOutput>((resolve, reject) => {
14+
context.reportStatus(`Executing "${options.command}"...`);
15+
const child = childProcess.spawn(options.command, options.args, { stdio: 'pipe' });
16+
17+
child.stdout.on('data', (data) => {
18+
context.logger.info(data.toString());
19+
});
20+
child.stderr.on('data', (data) => {
21+
context.logger.error(data.toString());
22+
reject();
23+
});
24+
25+
context.reportStatus(`Done.`);
26+
child.on('close', code => {
27+
resolve({ success: code === 0 });
28+
});
29+
});
30+
});
Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
1-
import { Tree } from '@angular-devkit/schematics';
2-
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
3-
import * as path from 'path';
1+
import { Architect } from '@angular-devkit/architect';
2+
import { TestingArchitectHost } from '@angular-devkit/architect/testing';
3+
import { logging, schema } from '@angular-devkit/core';
4+
const { join } = require('path');
45

6+
describe('svg-icons-builder', () => {
7+
let architect: Architect;
8+
let architectHost: TestingArchitectHost;
59

6-
const collectionPath = path.join(__dirname, '../collection.json');
10+
beforeEach(async () => {
11+
const registry = new schema.CoreSchemaRegistry();
12+
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
13+
const workspaceRoot = join(__dirname, '..', '..');
714

15+
// TestingArchitectHost() takes workspace and current directories.
16+
// Since we don't use those, both are the same in this case.
17+
architectHost = new TestingArchitectHost(__dirname, __dirname);
18+
architect = new Architect(architectHost, registry);
19+
20+
// This will either take a Node package name, or a path to the directory
21+
// for the package.json file.
22+
await architectHost.addBuilderFromPackage(workspaceRoot);
23+
console.log('#', Array.from((architectHost as any)._builderMap.keys()))
24+
});
825

9-
describe('svg-icons-builder', () => {
1026
it('works', async () => {
11-
const runner = new SchematicTestRunner('schematics', collectionPath);
12-
const tree = await runner.runSchematicAsync('svg-icons-builder', {}, Tree.empty()).toPromise();
27+
const logger = new logging.Logger('');
28+
const logs: string[] = [];
29+
logger.subscribe(ev => logs.push(ev.message));
30+
31+
// A "run" can have multiple outputs, and contains progress information.
32+
const run = await architect.scheduleBuilder('@angular-extensions/svg-icons-builder:svg-icons-builder', {
33+
command: 'node',
34+
args: ['--print', '\'foo\''],
35+
}, { logger }); // We pass the logger for checking later.
36+
37+
// The "result" member (of type BuilderOutput) is the next output.
38+
await run.result;
39+
40+
// Stop the builder from running. This stops Architect from keeping
41+
// the builder-associated states in memory, since builders keep waiting
42+
// to be scheduled.
43+
await run.stop();
1344

14-
expect(tree.files).toEqual([]);
45+
// Expect that foo was logged
46+
expect(logs).toContain('foo');
1547
});
1648
});

src/svg-icons-builder/schema.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"type": "object",
4+
"properties": {
5+
"conversionType": {
6+
"type": "string",
7+
"description": "Converting your icons to a single object, converting your icons to constants or converting your icons to single files.",
8+
"default": "constants",
9+
"enum": ["object", "constants", "files"]
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)