Commit ded16dd
2021-05-03 11:35:36
Changed files (17)
.github/ISSUE_TEMPLATE.md
@@ -0,0 +1,16 @@
+## Expected Behavior
+
+
+## Actual Behavior
+
+
+## Steps to Reproduce the Problem
+
+1.
+1.
+1.
+
+## Specifications
+
+- Version:
+- Platform:
\ No newline at end of file
.github/PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,6 @@
+Fixes #<issue_number_goes_here>
+
+> It's a good idea to open an issue first for discussion.
+
+- [ ] Tests pass
+- [ ] Appropriate changes to README are included in PR
\ No newline at end of file
docs/code-of-conduct.md
@@ -0,0 +1,93 @@
+# Code of Conduct
+
+## Our Pledge
+
+In the interest of fostering an open and welcoming environment, we as
+contributors and maintainers pledge to making participation in our project and
+our community a harassment-free experience for everyone, regardless of age, body
+size, disability, ethnicity, gender identity and expression, level of
+experience, education, socio-economic status, nationality, personal appearance,
+race, religion, or sexual identity and orientation.
+
+## Our Standards
+
+Examples of behavior that contributes to creating a positive environment
+include:
+
+* Using welcoming and inclusive language
+* Being respectful of differing viewpoints and experiences
+* Gracefully accepting constructive criticism
+* Focusing on what is best for the community
+* Showing empathy towards other community members
+
+Examples of unacceptable behavior by participants include:
+
+* The use of sexualized language or imagery and unwelcome sexual attention or
+ advances
+* Trolling, insulting/derogatory comments, and personal or political attacks
+* Public or private harassment
+* Publishing others' private information, such as a physical or electronic
+ address, without explicit permission
+* Other conduct which could reasonably be considered inappropriate in a
+ professional setting
+
+## Our Responsibilities
+
+Project maintainers are responsible for clarifying the standards of acceptable
+behavior and are expected to take appropriate and fair corrective action in
+response to any instances of unacceptable behavior.
+
+Project maintainers have the right and responsibility to remove, edit, or reject
+comments, commits, code, wiki edits, issues, and other contributions that are
+not aligned to this Code of Conduct, or to ban temporarily or permanently any
+contributor for other behaviors that they deem inappropriate, threatening,
+offensive, or harmful.
+
+## Scope
+
+This Code of Conduct applies both within project spaces and in public spaces
+when an individual is representing the project or its community. Examples of
+representing a project or community include using an official project e-mail
+address, posting via an official social media account, or acting as an appointed
+representative at an online or offline event. Representation of a project may be
+further defined and clarified by project maintainers.
+
+This Code of Conduct also applies outside the project spaces when the Project
+Steward has a reasonable belief that an individual's behavior may have a
+negative impact on the project or its community.
+
+## Conflict Resolution
+
+We do not believe that all conflict is bad; healthy debate and disagreement
+often yield positive results. However, it is never okay to be disrespectful or
+to engage in behavior that violates the project’s code of conduct.
+
+If you see someone violating the code of conduct, you are encouraged to address
+the behavior directly with those involved. Many issues can be resolved quickly
+and easily, and this gives people more control over the outcome of their
+dispute. If you are unable to resolve the matter for any reason, or if the
+behavior is threatening or harassing, report it. We are dedicated to providing
+an environment where participants feel welcome and safe.
+
+Reports should be directed to *[PROJECT STEWARD NAME(s) AND EMAIL(s)]*, the
+Project Steward(s) for *[PROJECT NAME]*. It is the Project Steward’s duty to
+receive and address reported violations of the code of conduct. They will then
+work with a committee consisting of representatives from the Open Source
+Programs Office and the Google Open Source Strategy team. If for any reason you
+are uncomfortable reaching out to the Project Steward, please email
+opensource@google.com.
+
+We will investigate every complaint, but you may not receive a direct response.
+We will use our discretion in determining when and how to follow up on reported
+incidents, which may range from not taking action to permanent expulsion from
+the project and project-sponsored spaces. We will notify the accused of the
+report and provide them an opportunity to discuss it before any action is taken.
+The identity of the reporter will be omitted from the details of the report
+supplied to the accused. In potentially harmful situations, such as ongoing
+harassment or threats to anyone's safety, we may take action without notice.
+
+## Attribution
+
+This Code of Conduct is adapted from the Contributor Covenant, version 1.4,
+available at
+https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
docs/contributing.md
@@ -0,0 +1,28 @@
+# How to Contribute
+
+We'd love to accept your patches and contributions to this project. There are
+just a few small guidelines you need to follow.
+
+## Contributor License Agreement
+
+Contributions to this project must be accompanied by a Contributor License
+Agreement. You (or your employer) retain the copyright to your contribution;
+this simply gives us permission to use and redistribute your contributions as
+part of the project. Head over to <https://cla.developers.google.com/> to see
+your current agreements on file or to sign a new one.
+
+You generally only need to submit a CLA once, so if you've already submitted one
+(even if it was for a different project), you probably don't need to do it
+again.
+
+## Code Reviews
+
+All submissions, including submissions by project members, require review. We
+use GitHub pull requests for this purpose. Consult
+[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
+information on using pull requests.
+
+## Community Guidelines
+
+This project follows [Google's Open Source Community
+Guidelines](https://opensource.google/conduct/).
examples/backup-github.mjs
@@ -0,0 +1,35 @@
+#!/usr/bin/env zx
+
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+let username = await question('What is your GitHub username? ')
+let token = await question('Do you have GitHub token in env? ', {
+ choices: Object.keys(process.env)
+})
+
+let headers = {}
+if (process.env[token]) {
+ headers = {
+ Authorization: `token ${process.env[token]}`
+ }
+}
+let res = await fetch(`https://api.github.com/users/${username}/repos`, {headers})
+let data = await res.json()
+let urls = data.map(x => x.git_url)
+
+await $`mkdir -p backups`
+cd('./backups')
+
+await Promise.all(urls.map(url => $`git clone ${url}`))
examples/basics.mjs
@@ -0,0 +1,26 @@
+#!/usr/bin/env zx
+
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+await $`# Hello world!
+date
+`
+
+let answer = await question('What is your name? ')
+await $`echo "Hello, ${answer}!"`
+
+if (test('-f package.json')) {
+ console.log('Yes')
+}
examples/cjs.js
@@ -0,0 +1,23 @@
+#!/usr/bin/env zx
+
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// In CommonJS module we can't use top-level await,
+// so wrap in in async function.
+
+void async function () {
+ await $`echo "Hello, CommonJS!"`
+}()
+ .catch(console.error)
examples/parallel.mjs
@@ -0,0 +1,21 @@
+#!/usr/bin/env zx
+
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+await Promise.all([
+ $`sleep 1; echo 1`,
+ $`sleep 2; echo 2`,
+ $`sleep 3; echo 3`,
+])
index.d.ts
@@ -0,0 +1,37 @@
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+interface $ {
+ (pieces: TemplateStringsArray, ...args: string[]): Promise<ProcessOutput>
+ verbose: boolean
+ shell: string
+ cwd: string
+}
+
+export const $: $
+
+export function cd(path: string)
+
+export function test(cmd: string): boolean
+
+type QuestionOptions = { choices: string[] }
+
+export function question(query: string, options?: QuestionOptions): Promise<string>
+
+export class ProcessOutput {
+ readonly exitCode: number
+ readonly stdout: string
+ readonly stderr: string
+ toString(): string
+}
index.mjs
@@ -0,0 +1,151 @@
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import {existsSync} from 'fs'
+import {exec, execSync} from 'child_process'
+import {promisify} from 'util'
+import {createInterface} from 'readline'
+import {default as nodeFetch} from 'node-fetch'
+import chalk from 'chalk'
+
+export {chalk}
+
+function colorize(cmd) {
+ return cmd.replace(/^\w+\s/, substr => {
+ return chalk.greenBright(substr)
+ })
+}
+
+export function $(pieces, ...args) {
+ let __from = (new Error().stack.split('at ')[2]).trim()
+ let cmd = pieces[0], i = 0
+ for (; i < args.length; i++) cmd += args[i] + pieces[i + 1]
+ for (++i; i < pieces.length; i++) cmd += pieces[i]
+
+ if ($.verbose) console.log('$', colorize(cmd))
+
+ return new Promise((resolve, reject) => {
+ let options = {
+ windowsHide: true,
+ }
+ if (typeof $.shell !== 'undefined') options.shell = $.shell
+ if (typeof $.cwd !== 'undefined') options.cwd = $.cwd
+
+ let child = exec(cmd, options), stdout = '', stderr = '', combined = ''
+ child.stdout.on('data', data => {
+ if ($.verbose) process.stdout.write(data)
+ stdout += data
+ combined += data
+ })
+ child.stderr.on('data', data => {
+ if ($.verbose) process.stderr.write(data)
+ stderr += data
+ combined += data
+ })
+ child.on('exit', code => {
+ (code === 0 ? resolve : reject)(
+ new ProcessOutput({code, stdout, stderr, combined, __from})
+ )
+ })
+ })
+}
+
+$.verbose = true
+$.shell = undefined
+$.cwd = undefined
+
+export function cd(path) {
+ if ($.verbose) console.log('$', colorize(`cd ${path}`))
+ if (!existsSync(path)) {
+ let __from = (new Error().stack.split('at ')[2]).trim()
+ console.error(`cd: ${path}: No such directory`)
+ console.error(` at ${__from}`)
+ process.exit(1)
+ }
+ $.cwd = path
+}
+
+export function test(cmd) {
+ if ($.verbose) console.log('$', colorize(`test ${cmd}`))
+ try {
+ execSync(`test ${cmd}`)
+ return true
+ } catch (e) {
+ return false
+ }
+}
+
+export async function question(query, options) {
+ let completer = undefined
+ if (Array.isArray(options?.choices)) {
+ completer = function completer(line) {
+ const completions = options.choices
+ const hits = completions.filter((c) => c.startsWith(line))
+ return [hits.length ? hits : completions, line]
+ }
+ }
+ const rl = createInterface({
+ input: process.stdin,
+ output: process.stdout,
+ completer,
+ })
+ const question = promisify(rl.question).bind(rl)
+ let answer = await question(query)
+ rl.close()
+ return answer
+}
+
+export async function fetch(url, init) {
+ if ($.verbose) {
+ if (typeof init !== 'undefined') console.log('$', colorize(`fetch ${url}`), init)
+ else console.log('$', colorize(`fetch ${url}`))
+ }
+ return nodeFetch(url, init)
+}
+
+export class ProcessOutput {
+ #code = 0
+ #stdout = ''
+ #stderr = ''
+ #combined = ''
+ #__from = ''
+
+ constructor({code, stdout, stderr, combined, __from}) {
+ this.#code = code
+ this.#stdout = stdout
+ this.#stderr = stderr
+ this.#combined = combined
+ this.#__from = __from
+ }
+
+ toString() {
+ return this.#combined.replace(/\n$/, '')
+ }
+
+ get stdout() {
+ return this.#stdout.replace(/\n$/, '')
+ }
+
+ get stderr() {
+ return this.#stderr.replace(/\n$/, '')
+ }
+
+ get exitCode() {
+ return this.#code
+ }
+
+ get __from() {
+ return this.#__from
+ }
+}
LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
package-lock.json
@@ -0,0 +1,155 @@
+{
+ "name": "zx",
+ "version": "1.0.1",
+ "lockfileVersion": 2,
+ "requires": true,
+ "packages": {
+ "": {
+ "version": "1.0.1",
+ "license": "Apache-2.0",
+ "dependencies": {
+ "chalk": "^4.1.1",
+ "node-fetch": "^2.6.1",
+ "uuid": "^8.3.2"
+ },
+ "bin": {
+ "zx": "zx.mjs"
+ }
+ },
+ "node_modules/ansi-styles": {
+ "version": "4.3.0",
+ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
+ "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
+ "dependencies": {
+ "color-convert": "^2.0.1"
+ },
+ "engines": {
+ "node": ">=8"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/ansi-styles?sponsor=1"
+ }
+ },
+ "node_modules/chalk": {
+ "version": "4.1.1",
+ "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz",
+ "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==",
+ "dependencies": {
+ "ansi-styles": "^4.1.0",
+ "supports-color": "^7.1.0"
+ },
+ "engines": {
+ "node": ">=10"
+ },
+ "funding": {
+ "url": "https://github.com/chalk/chalk?sponsor=1"
+ }
+ },
+ "node_modules/color-convert": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
+ "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
+ "dependencies": {
+ "color-name": "~1.1.4"
+ },
+ "engines": {
+ "node": ">=7.0.0"
+ }
+ },
+ "node_modules/color-name": {
+ "version": "1.1.4",
+ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
+ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
+ },
+ "node_modules/has-flag": {
+ "version": "4.0.0",
+ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
+ "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/node-fetch": {
+ "version": "2.6.1",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
+ "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==",
+ "engines": {
+ "node": "4.x || >=6.0.0"
+ }
+ },
+ "node_modules/supports-color": {
+ "version": "7.2.0",
+ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
+ "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
+ "dependencies": {
+ "has-flag": "^4.0.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/uuid": {
+ "version": "8.3.2",
+ "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
+ "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
+ "bin": {
+ "uuid": "dist/bin/uuid"
+ }
+ }
+ },
+ "dependencies": {
+ "ansi-styles": {
+ "version": "4.3.0",
+ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
+ "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
+ "requires": {
+ "color-convert": "^2.0.1"
+ }
+ },
+ "chalk": {
+ "version": "4.1.1",
+ "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz",
+ "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==",
+ "requires": {
+ "ansi-styles": "^4.1.0",
+ "supports-color": "^7.1.0"
+ }
+ },
+ "color-convert": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
+ "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
+ "requires": {
+ "color-name": "~1.1.4"
+ }
+ },
+ "color-name": {
+ "version": "1.1.4",
+ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
+ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
+ },
+ "has-flag": {
+ "version": "4.0.0",
+ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
+ "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="
+ },
+ "node-fetch": {
+ "version": "2.6.1",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
+ "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
+ },
+ "supports-color": {
+ "version": "7.2.0",
+ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
+ "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
+ "requires": {
+ "has-flag": "^4.0.0"
+ }
+ },
+ "uuid": {
+ "version": "8.3.2",
+ "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
+ "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
+ }
+ }
+}
package.json
@@ -0,0 +1,21 @@
+{
+ "name": "zx",
+ "version": "1.0.1",
+ "description": "Replaces bash with js",
+ "main": "index.mjs",
+ "types": "index.d.ts",
+ "bin": {
+ "zx": "zx.mjs"
+ },
+ "scripts": {
+ "test": "zx test.mjs"
+ },
+ "dependencies": {
+ "chalk": "^4.1.1",
+ "node-fetch": "^2.6.1",
+ "uuid": "^8.3.2"
+ },
+ "repository": "google/zx",
+ "author": "Anton Medvedev <anton@medv.io>",
+ "license": "Apache-2.0"
+}
README.md
@@ -0,0 +1,223 @@
+# 🐚 zx
+
+```js
+#!/usr/bin/env zx
+
+await $`cat package.json | grep name`
+
+let branch = await $`git branch --show-current`
+await $`dep deploy --branch=${branch}`
+
+await Promise.all([
+ $`sleep 1; echo 1`,
+ $`sleep 2; echo 2`,
+ $`sleep 3; echo 3`,
+])
+
+await $`ssh medv.io uptime`
+```
+
+Bash is great, but when it comes to writing scripts,
+people usually choose a more convenient programming languages.
+JavaScript is a perfect choose, but standard Node.js library
+requires additional hassle before using. `zx` package provides
+useful wrappers around `child_process` and gives sensible defaults.
+
+## Install
+
+```bash
+npm i -g zx
+```
+
+## Documentation
+
+Write your scripts in a file with `.mjs` extension in order to
+be able to use `await` on top level. In you prefer `.js` extension,
+wrap your script in something like `void async function () {...}()`.
+
+Add next shebang at the beginning of your script:
+```bash
+#!/usr/bin/env zx
+```
+
+Now you will be able to run your script as:
+```bash
+chmod +x ./script.mjs
+./script.mjs
+```
+
+Or via `zx` bin:
+
+```bash
+zx ./script.mjs
+```
+
+Then using `zx` bin or via shebang, all `$`, `cd`, `fetch`, etc
+available without imports.
+
+### `$`
+
+Executes given string using `exec` function
+from `child_process` package and returns `Promise<ProcessOutput>`.
+
+```js
+let count = parseInt(await $`ls -1 | wc -l`)
+console.log(`Files count: ${count}`)
+```
+
+Example. Upload files in parallel:
+
+```js
+let hosts = [...]
+await Promise.all(hosts.map(host =>
+ $`rsync -azP ./src ${host}:/var/www`
+))
+```
+
+```ts
+class ProcessOutput {
+ readonly exitCode: number
+ readonly stdout: string
+ readonly stderr: string
+ toString(): string
+}
+```
+
+If executed program returns non-zero exit code, `ProcessOutput` will be thrown.
+
+```js
+try {
+ await $`exit 1`
+} catch (p) {
+ console.log(`Exit code: ${p.exitCode}`)
+ console.log(`Error: ${p.stderr}`)
+}
+```
+
+### `cd`
+
+Changes working directory.
+
+```js
+cd('/tmp')
+await $`pwd` // outputs /tmp
+```
+
+### `test`
+
+Executes `test` command using `execSync` and returns `true` or `false`.
+
+```js
+if (test('-f package.json')) {
+ console.log('Yes')
+}
+```
+
+This is equivalent of next bash code:
+
+```bash
+if test -f package.json; then
+ echo Yes;
+fi
+```
+
+### `fetch`
+
+This is a wrapper around [node-fetch](https://www.npmjs.com/package/node-fetch) package.
+```js
+let resp = await fetch('http://wttr.in')
+if (resp.ok) {
+ console.log(await resp.text())
+}
+```
+
+### `question`
+
+This is a wrapper around [readline](https://nodejs.org/api/readline.html) package.
+
+```ts
+type QuestionOptions = { choices: string[] }
+
+function question(query: string, options?: QuestionOptions): Promise<string>
+```
+
+Usage:
+
+```js
+let username = await question('What is your username? ')
+let token = await question('Choose env variable: ', {
+ choices: Object.keys(process.env)
+})
+```
+
+
+
+### `chalk`
+
+The [chalk](https://www.npmjs.com/package/chalk) package available without
+importing inside scripts.
+
+```js
+console.log(chalk.blue('Hello world!'))
+```
+
+### `fs`
+
+The [fx](https://nodejs.org/api/fs.html) package available without importing
+inside scripts.
+
+```js
+let content = await fs.readFile('./package.json')
+```
+
+Promisified version imported by default. Same as if you write:
+
+```js
+import {promises as fs} from 'fs'
+```
+
+### `os`
+
+The [os](https://nodejs.org/api/os.html) package available without importing
+inside scripts.
+
+```js
+await $`cd ${os.homedir()} && mkdir example`
+```
+
+### `$.shell`
+
+Specifies what shell is used. Default is `/bin/sh`.
+
+```js
+$.shell = '/bin/bash'
+```
+
+### `$.verbose`
+
+Specifies verbosity. Default: `true`.
+
+In verbose mode prints executed commands with outputs of it. Same as
+`set -x` in bash.
+
+### Importing
+
+It's possible to use `$` and others with explicit import.
+
+```js
+#!/usr/bin/env node
+import {$} from 'zx'
+await $`date`
+```
+
+### Executing remote scripts
+
+If arg to `zx` bin starts with `https://`, it will be downloaded and executed.
+
+```bash
+zx https://medv.io/example-script.mjs
+```
+
+## License
+
+[Apache-2.0](LICENSE)
test.mjs
@@ -0,0 +1,19 @@
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+await Promise.all([
+ $`sleep 1; echo 1`,
+ $`sleep 2; echo 2`,
+ $`sleep 3; echo 3`,
+])
version.js
@@ -0,0 +1,15 @@
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+module.exports.version = require('./package.json').version
zx.mjs
@@ -0,0 +1,100 @@
+#!/usr/bin/env node
+
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import {join, basename} from 'path'
+import os, {tmpdir} from 'os'
+import {promises as fs} from 'fs'
+import {v4 as uuid} from 'uuid'
+import {$, test, cd, question, fetch, chalk, ProcessOutput} from './index.mjs'
+import {version} from './version.js'
+
+Object.assign(global, {
+ $,
+ cd,
+ test,
+ fetch,
+ question,
+ chalk,
+ fs,
+ os,
+})
+
+try {
+ let firstArg = process.argv[2]
+
+ if (['-v', '-V', '--version'].includes(firstArg)) {
+ console.log(`zx version ${version}`)
+ process.exit(0)
+ }
+
+ if (typeof firstArg === 'undefined') {
+ let ok = await scriptFromStdin()
+ if (!ok) {
+ console.log(`usage: zx <script>`)
+ process.exit(2)
+ }
+ } else if (firstArg.startsWith('http://') || firstArg.startsWith('https://')) {
+ await scriptFromHttp(firstArg)
+ } else {
+ await import(join(process.cwd(), firstArg))
+ }
+
+} catch (p) {
+ if (p instanceof ProcessOutput) {
+ console.error(' at ' + p.__from)
+ process.exit(1)
+ } else {
+ throw p
+ }
+}
+
+async function scriptFromStdin() {
+ let script = ''
+ if (!process.stdin.isTTY) {
+ process.stdin.setEncoding('utf8')
+ for await (const chunk of process.stdin) {
+ script += chunk
+ }
+
+ if (script.length > 0) {
+ let filepath = join(tmpdir(), uuid() + '.mjs')
+ await writeAndImport(filepath, script)
+ return true
+ }
+ }
+ return false
+}
+
+async function scriptFromHttp(firstArg) {
+ let res = await fetch(firstArg)
+ if (!res.ok) {
+ console.error(`Error: Can't get ${firstArg}`)
+ process.exit(1)
+ }
+ let script = await res.text()
+ let filepath = join(tmpdir(), basename(firstArg))
+ await writeAndImport(filepath, script)
+}
+
+async function writeAndImport(filepath, script) {
+ await fs.mkdtemp(filepath)
+ try {
+ await fs.writeFile(filepath, script)
+ await import(filepath)
+ } finally {
+ await fs.rm(filepath)
+ }
+}