v6
1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import * as globbyModule from 'globby'
16import minimist from 'minimist'
17import { setTimeout as sleep } from 'node:timers/promises'
18import nodeFetch, { RequestInfo, RequestInit } from 'node-fetch'
19import { getCtx, getRootCtx } from './context.js'
20import { colorize } from './print.js'
21import { log } from './print.js'
22
23export { default as chalk } from 'chalk'
24export { default as fs } from 'fs-extra'
25export { default as which } from 'which'
26export { default as YAML } from 'yaml'
27export { default as path } from 'node:path'
28export { default as os } from 'node:os'
29export { sleep }
30
31export const argv = minimist(process.argv.slice(2))
32
33export const globby = Object.assign(function globby(
34 patterns: string | readonly string[],
35 options?: globbyModule.Options
36) {
37 return globbyModule.globby(patterns, options)
38},
39globbyModule)
40
41export const glob = globby
42
43export async function fetch(url: RequestInfo, init?: RequestInit) {
44 log(
45 { scope: 'fetch' },
46 '$',
47 colorize(`fetch ${url}`),
48 init && JSON.stringify(init, null, 2)
49 )
50
51 return nodeFetch(url, init)
52}
53
54export function cd(path: string) {
55 if (getCtx().verbose) console.log('$', colorize(`cd ${path}`))
56 process.chdir(path)
57 getRootCtx().cwd = getCtx().cwd = process.cwd()
58}