1.0.1 β€’ Published 2 years ago

clasp-env v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Welcome to clasp-env πŸ‘‹

Version Documentation License: ISC Twitter: wildhogsm GitHub LinkedIn Wurkspaces.dev

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

Author

πŸ‘€ Dmitry Kostyuk

🀝 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