This repository is a basic Schematic implementation that serves as a starting point to create and publish Schematics to NPM.
More info on this sandbox approach is detailed in this blog post kevinschuchard.com/blog/2018-11-20-schematic-sandbox/
contains the necessary scripts to build & publish a schematic
example schematic that uses the Angular Component Schematic to generate a new component similar to ng generate component NAME
. Also adds a license to each Typescript file with name
provided by the CLI prompts. I have an article on CLI prompts here.
⚙ Yarn is required for the scripts. Make sure it's installed on your machine. If you'd like to use NPM, update the package.json scripts from yarn
to npm run
and the link command from yarn
to npm
.
⬇ Install the dependencies for the schematic and the sandbox application
yarn && cd sandbox && yarn && cd ..
🖇 Link the schematic in the sandbox to run locally
yarn link:schematic
🏃 Run the schematic
yarn dev
# or
yarn build:clean:launch
To execute an example schematic, make sure you've ran the Install and Link steps above. Then run the following and inspect the changed files.
git checkout component-license && yarn dev
Execute the schematic against the sandbox. Then run linting, unit & e2e tests and a prod build in the sandbox.
yarn test
Run the unit tests using Jasmine as a runner and test framework.
yarn test:unit
Running the schematic locally makes file system changes. The sandbox is version controlled so that viewing a diff of the changes is trivial. After the schematic has run locally, reset the sandbox with the following.
yarn clean
Note: if you're using the schematics
CLI mentioned below, you can execute the schematic in a dry run mode.
- do a global search and replace for
schematic-starter
andschematicStarter
with the new name. - change the folder name from
./src/schematic-starter/...
to./src/NEW_NAME/...
- run
yarn link:schematic
to set up local package linking for the sandbox
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
schematics --help
The publish
package.json script is setup to build the schematic. Additional testing scripts could be added to ensure stable releases.
Before publishing:
- change the package.json
name
value - if using a scope package name (
@username/package-name
), ensure the following is in your package.json.
{
"name": "@username/package-name",
"publishConfig": {
"access": "public"
}
}