@stacksjs/utils 0.56.34 → 0.57.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base.d.ts +3 -0
- package/dist/base.mjs +12 -0
- package/dist/config.d.ts +28 -0
- package/dist/config.mjs +79 -0
- package/dist/currency.d.ts +3 -0
- package/dist/currency.mjs +28 -0
- package/dist/debounce.d.ts +1 -0
- package/dist/debounce.mjs +1 -0
- package/dist/delete.d.ts +1 -0
- package/dist/delete.mjs +1 -0
- package/dist/equal.d.ts +1 -0
- package/dist/equal.mjs +23 -0
- package/dist/function.d.ts +18 -0
- package/dist/function.mjs +7 -0
- package/dist/guards.d.ts +28 -0
- package/dist/guards.mjs +12 -0
- package/dist/helpers.d.ts +46 -0
- package/dist/helpers.mjs +92 -0
- package/dist/index.d.ts +15 -377
- package/dist/index.mjs +15 -401
- package/dist/math.d.ts +3 -0
- package/dist/math.mjs +28 -0
- package/dist/p.d.ts +41 -0
- package/dist/p.mjs +74 -0
- package/dist/promise.d.ts +65 -0
- package/dist/promise.mjs +57 -0
- package/dist/throttle.d.ts +19 -0
- package/dist/throttle.mjs +20 -0
- package/dist/vendors.d.ts +3 -0
- package/dist/vendors.mjs +210 -0
- package/dist/versions.d.ts +1 -0
- package/dist/versions.mjs +1 -0
- package/package.json +18 -18
package/dist/base.d.ts
ADDED
package/dist/base.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function toString(v) {
|
|
2
|
+
return Object.prototype.toString.call(v);
|
|
3
|
+
}
|
|
4
|
+
export function loop(times, callback) {
|
|
5
|
+
[...Array(times)].forEach((item, i) => callback(i));
|
|
6
|
+
}
|
|
7
|
+
export function getTypeName(v) {
|
|
8
|
+
if (v === null)
|
|
9
|
+
return "null";
|
|
10
|
+
const type = toString(v).slice(8, -1).toLowerCase();
|
|
11
|
+
return typeof v === "object" || typeof v === "function" ? type : typeof v;
|
|
12
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AppOptions, CacheOptions, CdnOptions, CronJobOptions, DatabaseOptions, DebugOptions, DnsOptions, EmailOptions, Events, GitOptions, HashingOptions, JobOptions, LibraryOptions, Model, NotificationOptions, PagesOption, PaymentOptions, QueueOptions, SearchEngineOptions, ServicesOptions, StorageOptions, UiOptions, UserCliOptions } from '@stacksjs/types';
|
|
2
|
+
export { loadStacksConfig as config } from '@stacksjs/config';
|
|
3
|
+
export declare function defineApp(config: Partial<AppOptions>): AppOptions;
|
|
4
|
+
export declare function defineCache(config: Partial<CacheOptions>): CacheOptions;
|
|
5
|
+
export declare function defineCdn(config: Partial<CdnOptions>): CdnOptions;
|
|
6
|
+
export declare function defineCli(config: Partial<UserCliOptions>): UserCliOptions;
|
|
7
|
+
export declare function defineCronJobsConfig(config: Partial<CronJobOptions>[]): CronJobOptions[];
|
|
8
|
+
export declare function defineJobsConfig(config: Partial<JobOptions>[]): JobOptions[];
|
|
9
|
+
export declare function defineDatabase(config: Partial<DatabaseOptions>): DatabaseOptions;
|
|
10
|
+
export declare function defineDebugConfig(config: Partial<DebugOptions>): DebugOptions;
|
|
11
|
+
export declare function defineDns(config: Partial<DnsOptions>): DnsOptions;
|
|
12
|
+
export declare function defineEmailConfig(config: Partial<EmailOptions>): EmailOptions;
|
|
13
|
+
export declare function defineEvents(config: Partial<Events>): Events;
|
|
14
|
+
export declare function defineGit(config: Partial<GitOptions>): GitOptions;
|
|
15
|
+
export declare function defineHashing(config: Partial<HashingOptions>): HashingOptions;
|
|
16
|
+
export declare function defineLibrary(config: Partial<LibraryOptions>): LibraryOptions;
|
|
17
|
+
export declare function defineModel(config: Partial<Model>): Model;
|
|
18
|
+
export declare function defineNotification(config: Partial<NotificationOptions>): NotificationOptions;
|
|
19
|
+
export declare function defineSms(config: Partial<NotificationOptions['sms']>): NotificationOptions;
|
|
20
|
+
export declare function defineEmail(config: Partial<NotificationOptions['email']>): NotificationOptions;
|
|
21
|
+
export declare function defineChat(config: Partial<NotificationOptions['chat']>): NotificationOptions;
|
|
22
|
+
export declare function definePage(config: Partial<PagesOption>): PagesOption;
|
|
23
|
+
export declare function definePayment(config: Partial<PaymentOptions>): PaymentOptions;
|
|
24
|
+
export declare function defineQueue(config: Partial<QueueOptions>): QueueOptions;
|
|
25
|
+
export declare function defineSearchEngine(config: Partial<SearchEngineOptions>): SearchEngineOptions;
|
|
26
|
+
export declare function defineServices(config: Partial<ServicesOptions>): ServicesOptions;
|
|
27
|
+
export declare function defineStorage(config: Partial<StorageOptions>): StorageOptions;
|
|
28
|
+
export declare function defineUi(config: Partial<UiOptions>): UiOptions;
|
package/dist/config.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export { loadStacksConfig as config } from "@stacksjs/config";
|
|
2
|
+
export function defineApp(config) {
|
|
3
|
+
return config;
|
|
4
|
+
}
|
|
5
|
+
export function defineCache(config) {
|
|
6
|
+
return config;
|
|
7
|
+
}
|
|
8
|
+
export function defineCdn(config) {
|
|
9
|
+
return config;
|
|
10
|
+
}
|
|
11
|
+
export function defineCli(config) {
|
|
12
|
+
return config;
|
|
13
|
+
}
|
|
14
|
+
export function defineCronJobsConfig(config) {
|
|
15
|
+
return config;
|
|
16
|
+
}
|
|
17
|
+
export function defineJobsConfig(config) {
|
|
18
|
+
return config;
|
|
19
|
+
}
|
|
20
|
+
export function defineDatabase(config) {
|
|
21
|
+
return config;
|
|
22
|
+
}
|
|
23
|
+
export function defineDebugConfig(config) {
|
|
24
|
+
return config;
|
|
25
|
+
}
|
|
26
|
+
export function defineDns(config) {
|
|
27
|
+
return config;
|
|
28
|
+
}
|
|
29
|
+
export function defineEmailConfig(config) {
|
|
30
|
+
return config;
|
|
31
|
+
}
|
|
32
|
+
export function defineEvents(config) {
|
|
33
|
+
return config;
|
|
34
|
+
}
|
|
35
|
+
export function defineGit(config) {
|
|
36
|
+
return config;
|
|
37
|
+
}
|
|
38
|
+
export function defineHashing(config) {
|
|
39
|
+
return config;
|
|
40
|
+
}
|
|
41
|
+
export function defineLibrary(config) {
|
|
42
|
+
return config;
|
|
43
|
+
}
|
|
44
|
+
export function defineModel(config) {
|
|
45
|
+
return config;
|
|
46
|
+
}
|
|
47
|
+
export function defineNotification(config) {
|
|
48
|
+
return config;
|
|
49
|
+
}
|
|
50
|
+
export function defineSms(config) {
|
|
51
|
+
return config;
|
|
52
|
+
}
|
|
53
|
+
export function defineEmail(config) {
|
|
54
|
+
return config;
|
|
55
|
+
}
|
|
56
|
+
export function defineChat(config) {
|
|
57
|
+
return config;
|
|
58
|
+
}
|
|
59
|
+
export function definePage(config) {
|
|
60
|
+
return config;
|
|
61
|
+
}
|
|
62
|
+
export function definePayment(config) {
|
|
63
|
+
return config;
|
|
64
|
+
}
|
|
65
|
+
export function defineQueue(config) {
|
|
66
|
+
return config;
|
|
67
|
+
}
|
|
68
|
+
export function defineSearchEngine(config) {
|
|
69
|
+
return config;
|
|
70
|
+
}
|
|
71
|
+
export function defineServices(config) {
|
|
72
|
+
return config;
|
|
73
|
+
}
|
|
74
|
+
export function defineStorage(config) {
|
|
75
|
+
return config;
|
|
76
|
+
}
|
|
77
|
+
export function defineUi(config) {
|
|
78
|
+
return config;
|
|
79
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { add, allocate, compare, convert, equal, greaterThan, greaterThanOrEqual, hasSubUnits, isZero, lessThan, lessThanOrEqual, maximum, minimum, multiply, normalizeScale, dinero, dinero as money, subtract, toSnapshot, toUnits, toDecimal, trimScale, haveSameCurrency, haveSameAmount, dinero as currency, } from 'dinero.js';
|
|
2
|
+
export { USD } from '@dinero.js/currencies';
|
|
3
|
+
export type { Dinero, Currency } from 'dinero.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export {
|
|
2
|
+
add,
|
|
3
|
+
allocate,
|
|
4
|
+
compare,
|
|
5
|
+
convert,
|
|
6
|
+
equal,
|
|
7
|
+
greaterThan,
|
|
8
|
+
greaterThanOrEqual,
|
|
9
|
+
hasSubUnits,
|
|
10
|
+
isZero,
|
|
11
|
+
lessThan,
|
|
12
|
+
lessThanOrEqual,
|
|
13
|
+
maximum,
|
|
14
|
+
minimum,
|
|
15
|
+
multiply,
|
|
16
|
+
normalizeScale,
|
|
17
|
+
dinero,
|
|
18
|
+
dinero as money,
|
|
19
|
+
subtract,
|
|
20
|
+
toSnapshot,
|
|
21
|
+
toUnits,
|
|
22
|
+
toDecimal,
|
|
23
|
+
trimScale,
|
|
24
|
+
haveSameCurrency,
|
|
25
|
+
haveSameAmount,
|
|
26
|
+
dinero as currency
|
|
27
|
+
} from "dinero.js";
|
|
28
|
+
export { USD } from "@dinero.js/currencies";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { debounce } from 'perfect-debounce';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { debounce } from "perfect-debounce";
|
package/dist/delete.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { rimraf, rimraf as del } from 'rimraf';
|
package/dist/delete.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { rimraf, rimraf as del } from "rimraf";
|
package/dist/equal.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isDeepEqual(value1: any, value2: any): boolean;
|
package/dist/equal.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { getTypeName } from "./base.mjs";
|
|
2
|
+
export function isDeepEqual(value1, value2) {
|
|
3
|
+
const type1 = getTypeName(value1);
|
|
4
|
+
const type2 = getTypeName(value2);
|
|
5
|
+
if (type1 !== type2)
|
|
6
|
+
return false;
|
|
7
|
+
if (type1 === "array") {
|
|
8
|
+
if (value1.length !== value2.length)
|
|
9
|
+
return false;
|
|
10
|
+
return value1.every((item, i) => {
|
|
11
|
+
return isDeepEqual(item, value2[i]);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
if (type1 === "object") {
|
|
15
|
+
const keyArr = Object.keys(value1);
|
|
16
|
+
if (keyArr.length !== Object.keys(value2).length)
|
|
17
|
+
return false;
|
|
18
|
+
return keyArr.every((key) => {
|
|
19
|
+
return isDeepEqual(value1[key], value2[key]);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return Object.is(value1, value2);
|
|
23
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Fn, Nullable } from '@stacksjs/types';
|
|
2
|
+
/**
|
|
3
|
+
* Call every function in an array
|
|
4
|
+
*/
|
|
5
|
+
export declare function batchInvoke(functions: Nullable<Fn>[]): void;
|
|
6
|
+
/**
|
|
7
|
+
* Pass the value through the callback, and return the value
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```
|
|
11
|
+
* function createUser(name: string): User {
|
|
12
|
+
* return tap(new User, user => {
|
|
13
|
+
* user.name = name
|
|
14
|
+
* })
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function tap<T>(value: T, callback: (value: T) => void): T;
|
package/dist/guards.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard to filter out null-ish values
|
|
3
|
+
*
|
|
4
|
+
* @category Guards
|
|
5
|
+
* @example array.filter(notNullish)
|
|
6
|
+
*/
|
|
7
|
+
export declare function notNullish<T>(v: T | null | undefined): v is NonNullable<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Type guard to filter out null values
|
|
10
|
+
*
|
|
11
|
+
* @category Guards
|
|
12
|
+
* @example array.filter(noNull)
|
|
13
|
+
*/
|
|
14
|
+
export declare function noNull<T>(v: T | null): v is Exclude<T, null>;
|
|
15
|
+
/**
|
|
16
|
+
* Type guard to filter out null-ish values
|
|
17
|
+
*
|
|
18
|
+
* @category Guards
|
|
19
|
+
* @example array.filter(notUndefined)
|
|
20
|
+
*/
|
|
21
|
+
export declare function notUndefined<T>(v: T): v is Exclude<T, undefined>;
|
|
22
|
+
/**
|
|
23
|
+
* Type guard to filter out falsy values
|
|
24
|
+
*
|
|
25
|
+
* @category Guards
|
|
26
|
+
* @example array.filter(isTruthy)
|
|
27
|
+
*/
|
|
28
|
+
export declare function isTruthy<T>(v: T): v is NonNullable<T>;
|
package/dist/guards.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { AddressInfo } from 'node:net';
|
|
3
|
+
import type { CliOptions, CommandResult, Manifest, NpmScript } from '@stacksjs/types';
|
|
4
|
+
export declare function isProjectCreated(): Promise<boolean>;
|
|
5
|
+
export declare function installIfVersionMismatch(): Promise<void>;
|
|
6
|
+
export declare function frameworkVersion(): Promise<string>;
|
|
7
|
+
export declare function isAppKeySet(): Promise<boolean>;
|
|
8
|
+
/**
|
|
9
|
+
* Determines the utilized reset preset.
|
|
10
|
+
*
|
|
11
|
+
* @url https://www.npmjs.com/package/@unocss/reset
|
|
12
|
+
* @param preset
|
|
13
|
+
*/
|
|
14
|
+
export declare function determineResetPreset(preset?: string): string[];
|
|
15
|
+
/**
|
|
16
|
+
* Determines whether the specified value is a package manifest.
|
|
17
|
+
*/
|
|
18
|
+
export declare function isManifest(obj: any): obj is Manifest;
|
|
19
|
+
/**
|
|
20
|
+
* Determines whether the specified value is a string, null, or undefined.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isOptionalString(value: any): value is string | undefined;
|
|
23
|
+
export declare function setEnvValue(key: string, value: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Runs the specified NPM script in the package.json file.
|
|
26
|
+
*/
|
|
27
|
+
export declare function runNpmScript(script: NpmScript, options?: CliOptions): Promise<CommandResult>;
|
|
28
|
+
/**
|
|
29
|
+
* Determines whether the specified NPM script exists in the given manifest.
|
|
30
|
+
*/
|
|
31
|
+
export declare function hasScript(manifest: Manifest, script: NpmScript): boolean;
|
|
32
|
+
export declare function parseYaml(content: any): any;
|
|
33
|
+
/**
|
|
34
|
+
* Determines the level of debugging.
|
|
35
|
+
*
|
|
36
|
+
* The debug level is determined by the following:
|
|
37
|
+
* 1. The --verbose flag
|
|
38
|
+
* 2. The debug property in the app configuration
|
|
39
|
+
*
|
|
40
|
+
* Currently, we don't support a custom debug level,
|
|
41
|
+
* though, we would like to in the future.
|
|
42
|
+
*
|
|
43
|
+
* @param options
|
|
44
|
+
*/
|
|
45
|
+
export declare function determineDebugLevel(options?: CliOptions): boolean;
|
|
46
|
+
export declare function isIpv6(address: AddressInfo): boolean;
|
package/dist/helpers.mjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { frameworkPath, projectPath } from "@stacksjs/path";
|
|
2
|
+
import { parse } from "yaml";
|
|
3
|
+
import { execSync, log, runCommand, spawn } from "@stacksjs/cli";
|
|
4
|
+
import { app, ui } from "@stacksjs/config";
|
|
5
|
+
import * as storage from "@stacksjs/storage";
|
|
6
|
+
import { semver } from "./versions.mjs";
|
|
7
|
+
export async function isProjectCreated() {
|
|
8
|
+
if (storage.isFile(".env"))
|
|
9
|
+
return await isAppKeySet();
|
|
10
|
+
if (storage.isFile(".env.example"))
|
|
11
|
+
await spawn("cp .env.example .env", { stdio: "inherit", cwd: projectPath() });
|
|
12
|
+
return await isAppKeySet();
|
|
13
|
+
}
|
|
14
|
+
export async function installIfVersionMismatch() {
|
|
15
|
+
const dependenciesYaml = storage.fs.readFileSync(projectPath("tea.yaml"), "utf8");
|
|
16
|
+
const dependencies = parseYaml(dependenciesYaml).dependencies;
|
|
17
|
+
const requiredNodeVersion = dependencies["nodejs.org"];
|
|
18
|
+
const requiredPnpmVersion = dependencies["pnpm.io"];
|
|
19
|
+
const installedNodeVersion = process.version;
|
|
20
|
+
const installedPnpmVersion = execSync("pnpm -v").trim();
|
|
21
|
+
if (!semver.satisfies(installedNodeVersion, requiredNodeVersion)) {
|
|
22
|
+
log.error(`Installed Node.js version (${installedNodeVersion}) does not satisfy required version (${requiredNodeVersion}). One moment...`);
|
|
23
|
+
await runCommand(`tea +nodejs.org${requiredNodeVersion} >/dev/null 2>&1`);
|
|
24
|
+
}
|
|
25
|
+
if (!semver.satisfies(installedPnpmVersion, requiredPnpmVersion)) {
|
|
26
|
+
log.error(`Installed pnpm version (${installedPnpmVersion}) does not satisfy required version (${requiredPnpmVersion}). One moment...`);
|
|
27
|
+
await runCommand(`tea +pnpm.io${requiredPnpmVersion} >/dev/null 2>&1`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export async function frameworkVersion() {
|
|
31
|
+
return (await storage.readPackageJson(frameworkPath("package.json"))).version;
|
|
32
|
+
}
|
|
33
|
+
export async function isAppKeySet() {
|
|
34
|
+
const env = await storage.readTextFile(".env", projectPath());
|
|
35
|
+
const lines = env.data.split("\n");
|
|
36
|
+
const appKey = lines.find((line) => line.startsWith("APP_KEY="));
|
|
37
|
+
return !!(appKey && appKey.length > 16);
|
|
38
|
+
}
|
|
39
|
+
export function determineResetPreset(preset) {
|
|
40
|
+
if (ui.reset)
|
|
41
|
+
preset = ui.reset;
|
|
42
|
+
if (preset === "tailwind")
|
|
43
|
+
return ["import '@unocss/reset/tailwind.css'"];
|
|
44
|
+
if (preset === "normalize")
|
|
45
|
+
return ["import '@unocss/reset/normalize.css'"];
|
|
46
|
+
if (preset === "sanitize")
|
|
47
|
+
return ["import '@unocss/reset/sanitize/sanitize.css'", "import '@unocss/reset/sanitize/assets.css"];
|
|
48
|
+
if (preset === "eric-meyer")
|
|
49
|
+
return ["import '@unocss/reset/eric-meyer.css'"];
|
|
50
|
+
if (preset === "antfu")
|
|
51
|
+
return ["import '@unocss/reset/antfu.css'"];
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
export function isManifest(obj) {
|
|
55
|
+
return obj && typeof obj === "object" && isOptionalString(obj.name) && isOptionalString(obj.version) && isOptionalString(obj.description);
|
|
56
|
+
}
|
|
57
|
+
export function isOptionalString(value) {
|
|
58
|
+
const type = typeof value;
|
|
59
|
+
return value === null || type === "undefined" || type === "string";
|
|
60
|
+
}
|
|
61
|
+
export async function setEnvValue(key, value) {
|
|
62
|
+
const file = await storage.readTextFile(projectPath(".env"));
|
|
63
|
+
await storage.writeTextFile({
|
|
64
|
+
path: projectPath(".env"),
|
|
65
|
+
data: file.data.replace(/APP_KEY=/g, `APP_KEY=${value}`)
|
|
66
|
+
// todo: do not hardcode the APP_KEY here and instead use the key parameter
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
export async function runNpmScript(script, options) {
|
|
70
|
+
const { data: manifest } = await storage.readJsonFile("package.json", frameworkPath());
|
|
71
|
+
if (isManifest(manifest) && hasScript(manifest, script))
|
|
72
|
+
return await runCommand(`pnpm run ${script}`, options);
|
|
73
|
+
log.error(`The specified npm script "${script}" does not exist in the package.json file`);
|
|
74
|
+
process.exit();
|
|
75
|
+
}
|
|
76
|
+
export function hasScript(manifest, script) {
|
|
77
|
+
const scripts = manifest.scripts;
|
|
78
|
+
if (scripts && typeof scripts === "object")
|
|
79
|
+
return Boolean(scripts[script]);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
export function parseYaml(content) {
|
|
83
|
+
return parse(content);
|
|
84
|
+
}
|
|
85
|
+
export function determineDebugLevel(options) {
|
|
86
|
+
if (options?.verbose === true)
|
|
87
|
+
return true;
|
|
88
|
+
return app.debug === true;
|
|
89
|
+
}
|
|
90
|
+
export function isIpv6(address) {
|
|
91
|
+
return address.family === "IPv6" || address.family === 6;
|
|
92
|
+
}
|