clasp-env
v1.0.1
Published
CLI clasp environment switcher
Downloads
28
Maintainers
Readme
Welcome to clasp-env 👋
CLI clasp environment switcher 🏠 Homepage
Usage
Run the following command (without prior installation):
npx clasp-env --folder <path_to_folder> --scriptId <apps_script_project_id>
Run Tests
npm run test
Project Description
The Strategy
clasp-env
is a command-line utility that allows you to switch between different Google Apps Script environments. When you write your code locally and deploy your Google Apps Script code with clasp
, it creates a .clasp.json
file that contains the scriptId
property. This tells clasp which project it needs to push the code to.
When working in a team and/or with a client, you want to have multiple environments. At minimum, you probably want a dev environment (or multiple ones) in which you are working, and a test environment in which the client or your team can run acceptance tests before production. Of course, they must both be separate from the production environment.
clasp-env
allows you to update the scriptId
property in the .clasp.json
file so that the code gets pushed to the intented project environment. The <path>
indicates the path to the folder where the .clasp.json
file is located.
It takes two arguments, <path_to_folder>
and <apps_script_project_id>
, both of which are mandatory. It then looks up the .clasp.json file in the folder and sets its scriptId to the one specified in the parameter.
Include in package.json
Scripts
You don’t want to type out the folder paths and IDs every time you want to change the scripts, so it’s best to save them in the package.json
's scripts
property like so:
{
"scripts": {
"clasp/dev": "npx clasp-env --folder <path_to_folder> --scriptId <dev_apps_script_project_id>",
"clasp/prod": "npx clasp-env --folder <path_to_folder> --scriptId <prod_apps_script_project_id>"
}
}
To switch to the prod environment, simply run npm run clasp/prod
in the terminal and the scriptId
property will be updated.
It’s also a good idea to combine clasp-env
with the clasp push
command. Accordingly, as soon as you switch environments, clasp
will begin pushing your code to the project. The -w
option at the end stands for “watch,” meaning that clasp
will push your code every time you save a file:
{
"scripts": {
"push/dev": "npx clasp-env --folder <path_to_folder> --scriptId <dev_apps_script_project_id> && cd <path_to_folder> && clasp push -w",
"push/prod": "npx clasp-env --folder <path_to_folder> --scriptId <prod_apps_script_project_id> && cd <path_to_folder> && clasp push -w"
}
}
To switch to the prod environment and push your code there, run npm run push/prod
in the terminal.
Note that the folder must be a relative path from the project root. A typical project structure may look something like this:
my-app/
├─ node_modules/
├─ gas/
│ ├─ Code.js
│ ├─ appsscript.json
│ ├─ .clasp.json
├─ src/
│ ├─ index.js
├─ .gitignore
├─ package.json
├─ package-lock.json
├─ README.md
The Google Apps Script files, including .clasp.json
, are in the gas
folder; thus, the command should look like this:
npx clasp-env --folder gas –-scriptId 1k1bnhRoqrESBvS95ZJncRYpsgSdM4anxwo3yI2Egs_Q
.gitignore
If you use git with your projects, you’ll also want to add .clasp.json
files to .gitignore
so that it’s not considered a change in the source code every time you switch environments. You can accomplish this by adding this line to the .gitignore
file: **/.clasp.json
Related Articles
- How to Write Google Apps Script Code Locally in VS Code and Deploy It With clasp
- The ULTIMATE Guide to NPM Modules in Google Apps Script
Author
👤 Dmitry Kostyuk
- Website: https://www.wurkspaces.dev
- LinkedIn: @dmitrykostyuk
- Medium: @dmitry-kostyuk
- Github: @WildH0g
- Stack Overflow: Dmitry Kostyuk
- Twitter: @wildhogsm
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Show your support
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by readme-md-generator