@stacksjs/utils 0.65.0 → 0.68.0

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/src/clean.ts DELETED
@@ -1,15 +0,0 @@
1
- import { path as p } from '@stacksjs/path'
2
-
3
- export async function cleanProject() {
4
- const $ = Bun.$
5
- await $`rm -rf ${p.projectPath('bun.lockb')}`
6
- await $`rm -rf ${p.projectPath('yarn.lock')}` // just in case
7
- await $`rm -rf ${p.projectPath('node_modules/')}`
8
- await $`rm -rf ${p.frameworkPath('dist')}`
9
- await $`rm -rf ${p.frameworkPath('node_modules')}`
10
- await $`rm -rf ${p.frameworkPath('**/dist')}`
11
- await $`rm -rf ${p.frameworkPath('**/node_modules')}`
12
- await $`rm -rf ${p.frameworkCloudPath('cdk.out/')}`
13
- await $`rm -rf ${p.frameworkCloudPath('cdk.context.json')}`
14
- await $`rm -rf ${p.cloudPath('dist.zip')}`
15
- }
package/src/config.ts DELETED
@@ -1,25 +0,0 @@
1
- export {
2
- defineApp,
3
- defineCache,
4
- defineCdn,
5
- defineChat,
6
- defineCli,
7
- defineCronJob,
8
- defineDatabase,
9
- defineDependencies,
10
- defineDns,
11
- defineEmail,
12
- defineEmailConfig,
13
- defineGit,
14
- defineHashing,
15
- defineJob,
16
- defineLibrary,
17
- defineNotification,
18
- definePayment,
19
- defineQueue,
20
- defineSearchEngine,
21
- defineServices,
22
- defineSms,
23
- defineStorage,
24
- defineUi,
25
- } from '@stacksjs/config'
package/src/equal.ts DELETED
@@ -1,27 +0,0 @@
1
- import { getTypeName } from '@stacksjs/types'
2
-
3
- export function isDeepEqual(value1: any, value2: any): boolean {
4
- const type1 = getTypeName(value1)
5
- const type2 = getTypeName(value2)
6
- if (type1 !== type2)
7
- return false
8
-
9
- if (type1 === 'array') {
10
- if (value1.length !== value2.length)
11
- return false
12
-
13
- return value1.every((item: any, i: number) => {
14
- return isDeepEqual(item, value2[i])
15
- })
16
- }
17
- if (type1 === 'object') {
18
- const keyArr = Object.keys(value1)
19
- if (keyArr.length !== Object.keys(value2).length)
20
- return false
21
-
22
- return keyArr.every((key: string) => {
23
- return isDeepEqual(value1[key], value2[key])
24
- })
25
- }
26
- return Object.is(value1, value2)
27
- }
@@ -1 +0,0 @@
1
- export { getExportsSize } from 'export-size'
package/src/find.ts DELETED
@@ -1,102 +0,0 @@
1
- /* eslint no-console: 0 */
2
- import type { Dirent } from 'node:fs'
3
- import fs from 'node:fs/promises'
4
- import os from 'node:os'
5
- import path from 'node:path'
6
- import { italic } from '@stacksjs/cli'
7
- import { log } from '@stacksjs/logging'
8
-
9
- const targetFileName = 'buddy' // The exact name of the target file
10
-
11
- const excludePatterns = [
12
- 'node_modules',
13
- 'dist',
14
- 'vendor',
15
- 'storage/framework',
16
- `${os.homedir()}/Documents`,
17
- `${os.homedir()}/Pictures`,
18
- `${os.homedir()}/Library`,
19
- `${os.homedir()}/.Trash`,
20
- /.*out.*/, // exclude any folder with 'out' in the name
21
- /.*\/\..*/, // exclude any folder that starts with a dot
22
- ]
23
-
24
- interface FindStacksProjectsOptions {
25
- quiet?: boolean
26
- }
27
-
28
- export async function findStacksProjects(dir?: string, options?: FindStacksProjectsOptions): Promise<string[]> {
29
- dir = dir || os.homedir()
30
-
31
- if (!options?.quiet) {
32
- log.info(`Searching for Stacks projects in: ${italic(dir)}`)
33
- log.info(`This may take a few moments...`)
34
- console.log('')
35
- console.log(italic(' Please note, while Stacks is searching for projects on your machine,'))
36
- console.log(italic(' you may be asked for permissions to scan certain directories.'))
37
- console.log('')
38
- log.debug(`Excluding directories: ${excludePatterns.join(', ')}`)
39
- }
40
-
41
- const foundProjects = await searchDirectory(dir)
42
-
43
- if (!foundProjects)
44
- throw new Error('No Stacks projects found')
45
-
46
- return foundProjects
47
- }
48
-
49
- // this searches a dir for Stacks projects which we define
50
- // as a dir that contains a 'buddy' file and a 'storage'
51
- // dir with a 'framework/core/buddy' structure
52
- async function searchDirectory(directory: string): Promise<string[]> {
53
- const foundProjects: string[] = []
54
- const isExcluded = excludePatterns.some(pattern =>
55
- typeof pattern === 'string' ? directory.includes(pattern) : pattern.test(directory),
56
- )
57
-
58
- if (isExcluded)
59
- return foundProjects
60
-
61
- let items: Dirent[]
62
- try {
63
- items = await fs.readdir(directory, { withFileTypes: true })
64
- }
65
- catch (error) {
66
- console.error(`Error reading directory ${directory}:`, error)
67
- return foundProjects
68
- }
69
-
70
- let buddyFileFound = false
71
- let storageDirFound = false
72
-
73
- for (const item of items) {
74
- if (item.isFile() && item.name === targetFileName) {
75
- buddyFileFound = true
76
- }
77
-
78
- if (item.isDirectory()) {
79
- const fullPath = path.join(directory, item.name)
80
- if (item.name === 'storage') {
81
- const storagePath = path.join(fullPath, 'framework/core/buddy')
82
- try {
83
- await fs.access(storagePath)
84
- storageDirFound = true
85
- }
86
- catch (error) {
87
- console.error(`Error accessing directory ${storagePath}:`, error)
88
- }
89
- }
90
-
91
- // Accumulate results from recursive calls
92
- const subDirProjects = await searchDirectory(fullPath)
93
- foundProjects.push(...subDirProjects)
94
- }
95
- }
96
-
97
- if (buddyFileFound && storageDirFound) {
98
- foundProjects.push(directory)
99
- }
100
-
101
- return foundProjects
102
- }
package/src/git.ts DELETED
@@ -1,13 +0,0 @@
1
- import { execSync } from 'node:child_process'
2
- import { handleError } from '@stacksjs/error-handling'
3
-
4
- export function isGitClean(): boolean {
5
- try {
6
- execSync('git diff-index --quiet HEAD --')
7
- return true
8
- }
9
- catch (error) {
10
- handleError(error)
11
- return false
12
- }
13
- }
package/src/glob.ts DELETED
@@ -1 +0,0 @@
1
- export { glob } from '@stacksjs/storage'
package/src/hash.ts DELETED
@@ -1,36 +0,0 @@
1
- import { config } from '@stacksjs/config'
2
- import { log } from '@stacksjs/logging'
3
- import { path as p } from '@stacksjs/path'
4
- import { hashPaths } from '@stacksjs/storage'
5
-
6
- export function originRequestFunctionHash() {
7
- try {
8
- return hashPaths(p.cloudPath('src/edge'))
9
- }
10
- catch (error: any) {
11
- log.error('Are we in a Docker container? Failed to hash paths. Error below:')
12
- log.error(error)
13
- return undefined
14
- }
15
- }
16
-
17
- const docsSrc = [p.projectPath('docs'), p.frameworkPath('docs/dist')]
18
-
19
- export function websiteSourceHash() {
20
- const websiteSrc = [
21
- p.projectPath('resources/views'),
22
- p.projectPath('resources/assets'),
23
- p.projectPath('resources/lang'),
24
- p.projectPath('resources/functions'),
25
- p.projectPath('resources/components'),
26
- p.projectPath('resources/modules'),
27
- p.projectPath('resources/stores'),
28
- p.frameworkPath('views/web'),
29
- ]
30
-
31
- return config.app.docMode === true ? hashPaths(docsSrc) : hashPaths(websiteSrc)
32
- }
33
-
34
- export function docsSourceHash() {
35
- return hashPaths(docsSrc)
36
- }
package/src/helpers.ts DELETED
@@ -1,186 +0,0 @@
1
- import type { NpmScript } from '@stacksjs/enums'
2
- import type { Result } from '@stacksjs/error-handling'
3
- import type { CliOptions, Manifest, Subprocess } from '@stacksjs/types'
4
- import type { AddressInfo } from 'node:net'
5
- import { runAction } from '@stacksjs/actions'
6
- import { log, runCommand } from '@stacksjs/cli'
7
- import { app, ui } from '@stacksjs/config'
8
- import { Action } from '@stacksjs/enums'
9
- import { err, handleError, ok } from '@stacksjs/error-handling'
10
- import { frameworkPath, projectPath } from '@stacksjs/path'
11
- import * as storage from '@stacksjs/storage'
12
- import { parse } from 'yaml'
13
-
14
- // import { semver } from './versions'
15
-
16
- export async function packageManager() {
17
- const { packageManager } = await storage.readPackageJson(frameworkPath('package.json'))
18
- return packageManager
19
- }
20
-
21
- export async function initProject(): Promise<Result<Subprocess, Error>> {
22
- if (app.env !== 'production')
23
- log.info('Project not yet initialized, generating application key...')
24
- else handleError('Please run `buddy key:generate` to generate an application key')
25
-
26
- const result = await runAction(Action.KeyGenerate, { cwd: projectPath() })
27
-
28
- if (result.isErr())
29
- return err(handleError(result.error))
30
-
31
- log.info('Application key generated.')
32
-
33
- return ok(result.value)
34
- }
35
-
36
- export async function ensureProjectIsInitialized() {
37
- if (storage.isFile(projectPath('.env')))
38
- return await isAppKeySet()
39
-
40
- // copy the .env.example file to .env
41
- if (storage.isFile(projectPath('.env.example')))
42
- await runCommand('cp .env.example .env', { cwd: projectPath() })
43
- else console.error('no .env.example file found')
44
-
45
- return await isAppKeySet()
46
- }
47
-
48
- export async function installIfVersionMismatch() {
49
- // const requiredBunVersion = '0.8.1'
50
- // const installedBunVersion = process.version
51
- // if (!semver.satisfies(installedBunVersion, requiredBunVersion)) {
52
- // log.warn(`Installed Bun version ${italic(installedBunVersion)} does not satisfy required version ${italic(requiredBunVersion)}. Adding it to your environment. One moment...`)
53
- // await runCommand(`tea +bun.sh${requiredBunVersion} >/dev/null 2>&1`)
54
- // }
55
- }
56
-
57
- export async function frameworkVersion(): Promise<string> {
58
- return (await storage.readPackageJson(frameworkPath('package.json'))).version
59
- }
60
-
61
- export async function isAppKeySet() {
62
- const env = await storage.readTextFile('.env', projectPath())
63
- const lines = env.data.split('\n')
64
- const appKey = lines.find(line => line.startsWith('APP_KEY='))
65
-
66
- return !!(appKey && appKey.length > 16)
67
- }
68
-
69
- /**
70
- * Determines the utilized reset preset.
71
- *
72
- * @url https://www.npmjs.com/package/@unocss/reset
73
- * @param preset
74
- */
75
- export function determineResetPreset(preset?: string) {
76
- if (ui.reset)
77
- preset = ui.reset
78
-
79
- if (preset === 'tailwind')
80
- return ['import \'@unocss/reset/tailwind.css\'']
81
-
82
- if (preset === 'normalize')
83
- return ['import \'@unocss/reset/normalize.css\'']
84
-
85
- if (preset === 'sanitize')
86
- return ['import \'@unocss/reset/sanitize/sanitize.css\'', 'import \'@unocss/reset/sanitize/assets.css']
87
-
88
- if (preset === 'eric-meyer')
89
- return ['import \'@unocss/reset/eric-meyer.css\'']
90
-
91
- if (preset === 'antfu')
92
- return ['import \'@unocss/reset/antfu.css\'']
93
-
94
- return []
95
- }
96
-
97
- /**
98
- * Determines whether the specified value is a package manifest.
99
- */
100
- export function isManifest(obj: any): obj is Manifest {
101
- return (
102
- obj
103
- && typeof obj === 'object'
104
- && isOptionalString(obj.name)
105
- && isOptionalString(obj.version)
106
- && isOptionalString(obj.description)
107
- )
108
- }
109
-
110
- /**
111
- * Determines whether the specified value is a string, null, or undefined.
112
- */
113
- export function isOptionalString(value: any): value is string | undefined {
114
- const type = typeof value
115
- return value === null || type === 'undefined' || type === 'string'
116
- }
117
-
118
- export async function setEnvValue(key: string, value: string) {
119
- const file = await storage.readTextFile(projectPath('.env'))
120
-
121
- await storage.writeTextFile({
122
- path: projectPath('.env'),
123
- data: file.data.replace(/APP_KEY=/g, `APP_KEY=${value}`), // todo: do not hardcode the APP_KEY here and instead use the key parameter
124
- })
125
- }
126
-
127
- /**
128
- * Runs the specified NPM script in the package.json file.
129
- */
130
- export async function runNpmScript(script: NpmScript, options?: CliOptions): Promise<Result<Subprocess, Error>> {
131
- const { data: manifest } = await storage.readJsonFile('package.json', frameworkPath())
132
-
133
- // simple, yet effective check to see if the script exists
134
- if (isManifest(manifest) && hasScript(manifest, script))
135
- return await runCommand(`bun run ${script}`, options)
136
-
137
- return err(handleError(`The ${script} script does not exist in the package.json file.`))
138
- }
139
-
140
- /**
141
- * Determines whether the specified NPM script exists in the given manifest.
142
- */
143
- export function hasScript(manifest: Manifest, script: NpmScript): boolean {
144
- const scripts = manifest.scripts as Record<NpmScript, string> | undefined
145
-
146
- if (scripts && typeof scripts === 'object')
147
- return Boolean(scripts[script])
148
-
149
- return false
150
- }
151
-
152
- export function parseYaml(content: any) {
153
- return parse(content)
154
- }
155
-
156
- /**
157
- * Determines the level of debugging.
158
- *
159
- * The debug level is determined by the following:
160
- * 1. The --verbose flag
161
- * 2. The debug property in the app configuration
162
- *
163
- * Currently, we don't support a custom debug level,
164
- * though, we would like to in the future.
165
- *
166
- * @param options
167
- */
168
- export function determineDebugLevel(options?: CliOptions) {
169
- if (options?.verbose === true)
170
- return true
171
-
172
- return app.debug === true
173
- }
174
-
175
- export function isIpv6(address: AddressInfo): boolean {
176
- return (
177
- address.family === 'IPv6'
178
- // in node >=18.0 <18.4 this was an integer value. This was changed in a minor version.
179
- // See: https://github.com/laravel/vite-plugin/issues/103
180
-
181
- // @ts-expect-error-next-line
182
- || address.family === 6
183
- )
184
- }
185
-
186
- export { dump as dumpYaml, load as loadYaml } from 'js-yaml'
package/src/index.ts DELETED
@@ -1,12 +0,0 @@
1
- export * from './clean'
2
-
3
- export * from './config'
4
- export * from './equal'
5
- export * from './export-size'
6
- export * from './find'
7
- export * from './git'
8
- export * from './hash'
9
- export * from './helpers'
10
- export * from './macroable'
11
- export * from './versions'
12
- export * from '@stacksjs/browser'
package/src/macroable.ts DELETED
@@ -1 +0,0 @@
1
- export { Macroable } from 'macroable'
package/src/utils.ts DELETED
@@ -1,15 +0,0 @@
1
- import { resolve } from '@stacksjs/path'
2
- import { fs } from '@stacksjs/storage'
3
- import { kebabCase } from '@stacksjs/strings'
4
-
5
- const ignore = ['readme-md']
6
-
7
- export const components = fs
8
- .readdirSync(resolve(__dirname, './resources/components'))
9
- .map(item => kebabCase(item.replace(/\.(stx|vue)/g, '')))
10
- .filter(item => !ignore.includes(item))
11
-
12
- export const functions = fs
13
- .readdirSync(resolve(__dirname, './resources/functions'))
14
- .map(item => kebabCase(item.replace(/.ts/g, '')))
15
- .filter(item => !ignore.includes(item))
package/src/versions.ts DELETED
@@ -1,6 +0,0 @@
1
- import { version } from '../package.json'
2
-
3
- // import { semver } from 'bun'
4
-
5
- export { version }
6
- // export { version, semver }