Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zemd committed Sep 5, 2021
0 parents commit 052ad5c
Show file tree
Hide file tree
Showing 12 changed files with 4,923 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,ts,json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.yarn/*
!/.yarn/patches
!/.yarn/plugins
!/.yarn/releases
!/.yarn/sdks

# Swap the comments on the following lines if you don't wish to use zero-installs
# Documentation here: https://yarnpkg.com/features/zero-installs
#!/.yarn/cache
/.pnp.*
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16.8.0
631 changes: 631 additions & 0 deletions .yarn/releases/yarn-berry.cjs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
yarnPath: ".yarn/releases/yarn-berry.cjs"

plugins:
- ./bundles/@yarnpkg/plugin-wait.js
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Dmytro Zelenetskyi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# yarn-plugin-wait

> Plugin that can wait
## Usage

Waiting for specific amount of time:

```sh
yarn wait 2s
yarn wait 10m
yarn wait "15s 50ms"
```

Waiting for specific amount of time and running command at the end:

```sh
yarn wait 2m -c 'echo "Hello World!"'
```

Waiting for the command to return exit code `0` if it doesn't come try after the timeout:

```sh
yarn wait 10s -c 'docker exec -it postgres pg_isready' -w
```

## License

yarn-plugin-wait is released under the MIT license.

## Donate

[![](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://www.patreon.com/red_rabbit)
31 changes: 31 additions & 0 deletions bundles/@yarnpkg/plugin-wait.js

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "yarn-plugin-wait",
"packageManager": "yarn@3.0.2",
"author": "Dmitry Zelenetskiy <dmytro.zelenetskyi@gmail.com>",
"license": "MIT",
"main": "./sources/index.ts",
"dependencies": {
"@types/node": "^16.7.10",
"@yarnpkg/builder": "^3.0.1",
"@yarnpkg/core": "^3.0.0",
"clipanion": "^3.0.1",
"typescript": "^4.4.2"
},
"scripts": {
"build": "builder build plugin"
},
"devDependencies": {
"@types/timestring": "^6.0.2",
"chalk": "^4.1.2",
"cli-spinners": "^2.6.0",
"execa": "^5.1.1",
"log-update": "^4.0.0",
"timestring": "^6.0.0"
}
}
132 changes: 132 additions & 0 deletions sources/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { CommandContext, Plugin } from '@yarnpkg/core';
import { Command, Option } from 'clipanion';
import spinners from 'cli-spinners';
import timestring from 'timestring';
import logUpdate from 'log-update';
import chalk from 'chalk';
import execa from 'execa';
import EventEmitter from 'events';

const spinnerNames = Object.keys(spinners);

// yarn wait 1000 -c "echo Hello World;exit 0;" - waits 5s and executes command
// yarn wait 5000 - waits 5s and exits
// yarn wait 1000 -c "echo Hello World;exit 0;" -w - waits until command returns 0; with timeout 1s
class WaitCommand extends Command<CommandContext> {
static usage = Command.Usage({
description: 'Waiting for something',
details: 'This command will wait defined time',
examples: [[
'Wait 2 seconds',
'yarn wait 2s'
]],
})

command = Option.String('-c,--command', {
description: 'Command to be executed after defined time'
});

delay = Option.String({
description: 'Human readable time string, for example, 1s. If no time sign is defined, it\'s meant as milliseconds',
required: true,
});

watch = Option.Boolean('-w,--watch', false, {
description: 'Watch until command returns error code 0'
});

static paths = [[`wait`]];

private delayMs: number;
private writeAnimation: Function;
private currInterval: number;
private spinner: any;
private isRunning: boolean = false;
private eventEmitter: EventEmitter;

private showSpinner() {
const spinnerName = spinnerNames[Math.floor(Math.random() * spinnerNames.length - 1)];
this.spinner = spinners[spinnerName];

let countDown = this.delayMs;
let frameInd = 0;

this.currInterval = setInterval(() => {
const {frames} = this.spinner;
const animation = frames[frameInd = ++frameInd % frames.length];
countDown -= this.spinner.interval;
const text = ` Waiting for ${timestring(`${countDown}ms`, 's').toFixed(2)} seconds...`;
this.writeAnimation(`${animation} ${text}`);
}, this.spinner.interval);
}

private clear() {
if (this.currInterval) {
clearInterval(this.currInterval)
this.writeAnimation(``);
}
}

private wait() {
this.showSpinner();
return new Promise<void>((resolve) => {
setTimeout(() => {
this.clear();
resolve();
}, this.delayMs);
});
}

private init() {
this.eventEmitter = new EventEmitter();
this.eventEmitter.on('waitAndRun', async () => {
await this.waitAndRun(this.watch);
})

this.delayMs = timestring(this.delay, 'ms');
this.writeAnimation = logUpdate.create(this.context.stdout, {
showCursor: true
});
}

private async waitAndRun(watch = false) {
if (this.isRunning) {
return
}
this.isRunning = true;
await this.wait();
if (this.command) {
this.context.stdout.write(`> Executing ${chalk.cyan(this.command)}\n`);
this.context.stdout.write(`> Using cwd: ${chalk.blue(this.context.cwd)}\n\n`);
try {
const result = execa.commandSync(this.command.trim(), {
cwd: this.context.cwd,
stdout: this.context.stdout,
stderr: this.context.stderr,
});
if (watch && result.exitCode > 0) {
this.isRunning = false;
this.eventEmitter.emit('waitAndRun')
}
} catch(error) {
this.isRunning = false;
this.eventEmitter.emit('waitAndRun')
}
} else {
this.isRunning = false;
}
}

async execute() {
this.init();
this.eventEmitter.emit('waitAndRun')
}
}

const plugin: Plugin = {
commands: [
WaitCommand,
],
};

export default plugin;
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"module": "commonjs",
"target": "es2018",
"lib": [
"es2018"
]
},
"include": [
"sources/**/*.ts"
]
}
Loading

0 comments on commit 052ad5c

Please sign in to comment.