变量:
TOKEN: 获取地址
SOURCE:要下载的Deployment
SOURCE 格式类似:项目名-提交代码SHA值-域名
实现:
<?php
namespace App\Console\Commands;
use App\Models\Marketing\CouponModel;
use App\Models\Marketing\CouponReceiveModel;
use App\Models\Seller\SellerIncomeModel;
use App\Models\Seller\SellerModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class Vercel extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'vercel:source';
/**
* The console command description.
*
* @var string
*/
protected $description = '拉取vercel的source代码';
/**
* Execute the console command.
*/
public function handle()
{
$bashDir = __DIR__ . '/source';//自定义绝对路径
$this->getDir('src', $bashDir);
}
private function getDir($uri, $dir)
{
if (!file_exists($dir)) {
@mkdir($dir, 0755, true);
}
$url = 'https://vercel.com/api/file-tree/{SOURCE}?base=' . $uri;
$res = $this->request($url);
foreach ($res->json() as $item) {
if ($item['type'] == 'directory') {
$this->getDir($uri . '/' . $item['name'], $dir . '/' . $item['name']);
} else {
$this->saveFile($dir, $item['name'], $item['link']);
}
}
}
private function saveFile($dir, $name, $url)
{
$file = $dir . '/' . $name;
echo $file . PHP_EOL;
if (file_exists($file)) {
return;
}
$res = $this->request($url);
file_put_contents($file, $res->body());
}
private function request($url)
{
$http = Http::timeout(200)->withHeaders([
'Authorization' => 'bearer {TOKEN}'
]);
$res = $http->get($url);
return $res;
}
}
运行:
php artisan vercel:source