Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,31 @@
}
}
}
},
"packages-schematics": {
"root": "packages/schematics",
"sourceRoot": "packages/schematics/src",
"projectType": "library",
"prefix": "sp",
"architect": {
"build": {
"builder": "@nrwl/builders:run-commands",
"options": {
"commands": [
{ "command": "node_modules/.bin/tsc -p packages/schematics/tsconfig.lib.json" },
{ "command": "cp packages/schematics/.npmignore packages/schematics/README.md packages/schematics/package.json packages/schematics/dist" },
{ "command": "cp packages/schematics/src/collection.json packages/schematics/src/migration.json packages/schematics/dist/src" }
]
}
},
"test": {
"builder": "@nrwl/builders:jest",
"options": {
"jestConfig": "packages/schematics/jest.config.js",
"tsConfig": "packages/schematics/tsconfig.spec.json"
}
}
}
}
},
"defaultProject": "sparkles",
Expand All @@ -594,6 +619,9 @@
"unitTestRunner": "jest",
"e2eTestRunner": "cypress"
},
"@nrwl/schematics:component": {
"styleext": "scss"
},
"@nrwl/schematics:node-application": {
"framework": "express"
}
Expand Down
3 changes: 2 additions & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = {
'reframed',
'shared',
'styles',
'testing'
'testing',
'schematics'
]
],
'scope-empty': [1, 'never'],
Expand Down
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
},
"demos-app": {
"tags": []
},
"packages-schematics": {
"tags": []
}
},
"implicitDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
"devDependencies": {
"@angular-devkit/build-angular": "~0.13.1",
"@angular-devkit/build-ng-packagr": "~0.13.0",
"@angular-devkit/core": "^7.3.8",
"@angular-devkit/schematics": "^7.3.8",
"@angular-devkit/schematics-cli": "^0.13.8",
"@angular/cli": "7.3.8",
"@angular/compiler-cli": "7.2.13",
"@angular/language-service": "7.2.13",
Expand All @@ -74,6 +77,7 @@
"@ngrx/schematics": "7.4.0",
"@nrwl/builders": "7.7.2",
"@nrwl/schematics": "7.7.2",
"@types/fs-extra": "^5.0.5",
"@types/jasmine": "~3.3.0",
"@types/jasminewd2": "~2.0.3",
"@types/jest": "24.0.11",
Expand Down
20 changes: 20 additions & 0 deletions packages/schematics/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Outputs
src/**/*.js
src/**/*.js.map
src/**/*.d.ts

# IDEs
.idea/
jsconfig.json
.vscode/

# Misc
node_modules/
npm-debug.log*
yarn-error.log*

# Mac OSX Finder files.
**/.DS_Store
.DS_Store

dist/
3 changes: 3 additions & 0 deletions packages/schematics/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignores TypeScript files, but keeps definitions.
*.ts
!*.d.ts
34 changes: 34 additions & 0 deletions packages/schematics/DEVELOP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Getting Started With Schematics

This repository is a basic Schematic implementation that serves as a starting point to create and publish Schematics to NPM.

### Testing

To test locally, install `@angular-devkit/schematics-cli` globally and use the `schematics` command line tool. That tool acts the same as the `generate` command of the Angular CLI, but also has a debug mode.

Check the documentation with
```bash
schematics --help
```

### Unit Testing

`npm run test` will run the unit tests, using Jasmine as a runner and test framework.

### Publishing

To publish, simply do:

```bash
npm run build
npm publish
```

That's it!


#### Futher Reading

- https://brianflove.com/2018/12/11/angular-schematics-tutorial/
- `ng update --registry http://myregistry.org`: https://github.com/angular/angular-cli/issues/10624
- ng update command: https://github.com/angular/angular-cli/blob/master/docs/specifications/update.md#library-developers
9 changes: 9 additions & 0 deletions packages/schematics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@sparkles/schematics
====================

```bash
$ ng add @sparkles/schematics

```

https://github.com/nrwl/nx/blob/master/packages/schematics/migrations/update-7-0-0/update-7-0-0.ts
9 changes: 9 additions & 0 deletions packages/schematics/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
name: 'packages-schematics',
preset: '../../jest.config.js',
coverageDirectory: '../../coverage/packages/schematics',
snapshotSerializers: [
'jest-preset-angular/AngularSnapshotSerializer.js',
'jest-preset-angular/HTMLCommentSerializer.js'
]
};
27 changes: 27 additions & 0 deletions packages/schematics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@sparkles/schematics",
"version": "1.0.0-1",
"description": "Sample schematics",
"license": "MIT",
"scripts": {
"build": "../../node_modules/.bin/tsc -p tsconfig.lib.json",
"test": "npm run build && jasmine src/**/*_spec.js"
},
"keywords": [
"schematics"
],
"schematics": "./src/collection.json",
"ng-update": {
"migrations": "./src/migration.json",
"packageGroup": [
"@sparkles/components",
"@sparkles/styles",
"@sparkles/schematics"
],
"requirements": {
"@sparkles/schematics": ">= 1"
}
},
"dependencies": {
}
}
18 changes: 18 additions & 0 deletions packages/schematics/src/collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
"extends": "@nrwl/schematics",
"schematics": {
"ng-add": {
"description": "Adds the sparkles workspace to the application",
"factory": "./collection/ng-add/index",
"aliases": ["install"],
"hidden": true
},
"init-workspace": {
"description": "Initializes a workspace",
"factory": "./collection/init/index",
"aliases": ["init"],
"hidden": true
}
}
}
15 changes: 15 additions & 0 deletions packages/schematics/src/collection/init/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Rule, externalSchematic } from '@angular-devkit/schematics';

export function initWorkspace(): Rule {

return (host, context) => {
context.logger.info(`ng add @nrwl/schematics`);

// ng add @nrwl/schematics
return externalSchematic('@nrwl/schematics', 'ng-add', {});
};
}

export default function schematics(_options: any): Rule {
return initWorkspace();
}
14 changes: 14 additions & 0 deletions packages/schematics/src/collection/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import * as path from 'path';

const collectionPath = path.join(__dirname, '../collection.json');

describe('schematics', () => {
it('works', () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = runner.runSchematic('schematics', {}, Tree.empty());

expect(tree.files).toEqual([]);
});
});
43 changes: 43 additions & 0 deletions packages/schematics/src/collection/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Rule, SchematicContext, Tree, externalSchematic, chain } from '@angular-devkit/schematics';
import { NodePackageInstallTask, RunSchematicTask } from '@angular-devkit/schematics/tasks';
import { updateJsonInTree } from '../../utils/json';
import { VERSIONS } from '../../versions';

function addNxToPackageJsonDependencies(): Rule {

return updateJsonInTree('package.json', (json, context) => {
context.logger.info(`Updating package.json...`);
if (!json['devDependencies']) {
json['devDependencies'] = {};
}
json['devDependencies']['@nrwl/schematics'] = VERSIONS.NX;

return json;
});
}

function installDependencies(): Rule {
return (host: Tree, context: SchematicContext) => {
context.addTask(new NodePackageInstallTask());
context.logger.info(`🔍 Installing packages...`);

return host;
};
}

function scheduleNxAdd(): Rule {
return (host: Tree, context: SchematicContext) => {
context.addTask(new RunSchematicTask('@sparkles/schematics', 'init', {}));

return host;
};
}

// schematic function for `ng add @sparkles/schematics`
export default function schematics(_options: any): Rule {
return chain([
addNxToPackageJsonDependencies(),
installDependencies(),
scheduleNxAdd(),
]);
}
10 changes: 10 additions & 0 deletions packages/schematics/src/migration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"migration-v1": {
"version": "1",
"description": "Updates Sparkles to v1",
"factory": "./migration/index#migrateToV1"
}
}
}
1 change: 1 addition & 0 deletions packages/schematics/src/migration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { migrateToV1 } from './migration-v1/migration-v1';
67 changes: 67 additions & 0 deletions packages/schematics/src/migration/migration-v1/migration-v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Rule, SchematicContext, Tree, externalSchematic, chain } from '@angular-devkit/schematics';
import { NodePackageInstallTask, RunSchematicTask } from '@angular-devkit/schematics/tasks';
import { readJsonInTree, updateJsonInTree } from '../../utils/json';

function updateOrAddNx(): Rule {

return (tree: Tree, context: SchematicContext) => {
const packageJson = readJsonInTree(tree, 'package.json');

const inDevDependencies = packageJson['devDependencies'] ? packageJson['devDependencies']['@nrwl/schematics'] : null;
const inDependencies = packageJson['devDependencies'] ? packageJson['devDependencies']['@nrwl/schematics'] : null;
if (inDevDependencies || inDependencies) {
const fromVersion = inDevDependencies || inDependencies;
const toVersion = '7.7.0';
context.logger.info(`Migrating @nrwl/schematis from already installed version=${fromVersion} to ${toVersion}`);

return externalSchematic('@schematics/update', 'update', {
packages: ['@nrwl/schematics'],
from: fromVersion,
to: toVersion,
force: true
})
} else {
// ng add @nrwl/schematics
return externalSchematic('@nrwl/schematics', 'ng-add', {});
}
};
}

function addNxToPackageJsonDependencies(): Rule {

return updateJsonInTree('package.json', (json, context) => {
context.logger.info(`Updating package.json...`);
if (!json['devDependencies']) {
json['devDependencies'] = {};
}
json['devDependencies']['@nrwl/schematics'] = '7.7.0';

return json;
});
}

function installDependencies(): Rule {
return (host: Tree, context: SchematicContext) => {
context.addTask(new NodePackageInstallTask());
context.logger.info(`🔍 Installing packages...`);

return host;
};
}

function scheduleNxAdd(): Rule {
return (host: Tree, context: SchematicContext) => {
context.addTask(new RunSchematicTask('@sparkles/schematics', 'init', {}));

return host;
};
}


export function migrateToV1(_options: any): Rule {
return chain([
addNxToPackageJsonDependencies(),
installDependencies(),
scheduleNxAdd()
]);
}
8 changes: 8 additions & 0 deletions packages/schematics/src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as fs from 'fs';
import * as path from 'path';
import { ensureDirSync } from 'fs-extra';

export function writeToFile(filePath: string, str: string) {
ensureDirSync(path.dirname(filePath));
fs.writeFileSync(filePath, str);
}
Loading