Developing Alfred JS SDK - CLI Development
I previously encapsulated the Alfred SDK, with the usage being manually installing packages in specific workflow JS files, then requiring the corresponding modules. Common script filter building, filtering, and quicklook have all been encapsulated as functions, making them fairly convenient to use. But there’s still no CLI support. To further improve efficiency, CLI development can now be driven forward. Here I’ll mark down some insights.
Solving Pain Points
- Using CLI can automatically install Alfred SDK packages
- Generate default or specified JS files, with module loading built into the code level, eliminating the need for manual writing.
This way, in the future you’ll only need npx @stacker/alfred-utils init
, then open index.js to write business logic.
CLI Principles
bin
Commands defined in the bin field of package.json will be automatically installed to the system path environment variables during package installation, and can be used directly after installation.
bin supports single shell or multiple command definitions.
When npx directly uses package bin, there are differences depending on the bin name:
- If the command name under bin is consistent with the package name, you can directly use the package name when using npx, for example
npx @stacker/alfred-utils --help
, you can see that it executes the command with the same name Note that consistency here excludes @scope, package name@stacker/alfred-utils
, bin commandalfred-utils
is considered consistent - If the command under bin is different from the package name, you need to specify the command when using, for example
npx -p @stacker/alfred-utils alfred-utils-test --help
- If the command name under bin is consistent with the package name, you can directly use the package name when using npx, for example
If you install globally, regardless of which bin it is, you can use it directly by entering the bin name.
Templates
CLI helps generate partial code, which is essentially doing scaffolding work. The basic principle is to copy sample code and make partial replacements based on input parameters.
Final Thoughts
- With the help of this CLI, developing workflows will be more efficient in the future.
- Whenever code-level repetition issues arise in the future, we just need to continuously enrich this SDK.