uniwind-pro 1.0.0-beta.9 → 1.0.0-rc.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uniwind-pro",
3
- "version": "1.0.0-beta.9",
3
+ "version": "1.0.0-rc.1",
4
4
  "type": "module",
5
5
  "description": "Uniwind Pro CLI",
6
6
  "keywords": [
@@ -0,0 +1,67 @@
1
+ import { mkdirSync, writeFileSync, readdirSync, cpSync, rmSync, existsSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+ import { homedir } from 'node:os'
4
+
5
+ const CACHE_ROOT = join(homedir(), '.uniwind', 'cache', 'pro')
6
+
7
+ const getVersionCacheRoot = (packageVersion) => {
8
+ return join(CACHE_ROOT, packageVersion)
9
+ }
10
+
11
+ const getCacheDir = (packageVersion) => getVersionCacheRoot(packageVersion)
12
+
13
+ export const copyDirectoryContents = (fromDir, toDir) => {
14
+ for (const file of readdirSync(fromDir)) {
15
+ cpSync(join(fromDir, file), join(toDir, file), { recursive: true })
16
+ }
17
+ }
18
+
19
+ export const enforceCachePolicy = (packageVersion) => {
20
+ try {
21
+ mkdirSync(CACHE_ROOT, { recursive: true })
22
+
23
+ const gitIgnorePath = join(CACHE_ROOT, '.gitignore')
24
+
25
+ if (!existsSync(gitIgnorePath)) {
26
+ writeFileSync(gitIgnorePath, '*\n!.gitignore\n', 'utf8')
27
+ }
28
+ } catch {
29
+ return
30
+ }
31
+
32
+ try {
33
+ for (const entry of readdirSync(CACHE_ROOT, { withFileTypes: true })) {
34
+ if (entry.name !== packageVersion && entry.name !== '.gitignore') {
35
+ rmSync(join(CACHE_ROOT, entry.name), { recursive: true, force: true })
36
+ }
37
+ }
38
+ } catch {}
39
+ }
40
+
41
+ export const restoreFromCache = (packageVersion, targetDir) => {
42
+ const cacheDir = getCacheDir(packageVersion)
43
+
44
+ if (!existsSync(cacheDir)) {
45
+ return false
46
+ }
47
+
48
+ const cacheFiles = readdirSync(cacheDir)
49
+
50
+ if (cacheFiles.length === 0) {
51
+ return false
52
+ }
53
+
54
+ copyDirectoryContents(cacheDir, targetDir)
55
+
56
+ return true
57
+ }
58
+
59
+ export const saveToCache = (packageVersion, sourceDir) => {
60
+ try {
61
+ const cacheDir = getCacheDir(packageVersion)
62
+
63
+ rmSync(cacheDir, { recursive: true, force: true })
64
+ mkdirSync(cacheDir, { recursive: true })
65
+ copyDirectoryContents(sourceDir, cacheDir)
66
+ } catch {}
67
+ }
@@ -1,14 +1,21 @@
1
1
  import chalk from 'chalk'
2
- import { readFileSync, mkdirSync, readdirSync, cpSync, rmSync, existsSync } from 'node:fs'
2
+ import { readFileSync, mkdirSync, readdirSync, rmSync, existsSync } from 'node:fs'
3
3
  import { join, dirname } from 'node:path'
4
4
  import { fileURLToPath } from 'node:url'
5
5
  import { pipeline } from 'node:stream'
6
6
  import { promisify } from 'node:util'
7
7
  import { x as extract } from 'tar'
8
8
  import { INSTALLATION_URL, SERVICE } from '../consts.js'
9
+ import {
10
+ copyDirectoryContents,
11
+ enforceCachePolicy,
12
+ restoreFromCache,
13
+ saveToCache,
14
+ } from './cache.js'
9
15
 
10
16
  const __dirname = dirname(fileURLToPath(import.meta.url))
11
- const pkgPath = join(__dirname, '../../package.json')
17
+ const PKG_ROOT = join(__dirname, '../../')
18
+ const pkgPath = join(PKG_ROOT, 'package.json')
12
19
  const streamPipeline = promisify(pipeline)
13
20
 
14
21
  const getVersion = () => {
@@ -97,6 +104,15 @@ const getProjectName = () => {
97
104
 
98
105
  export const downloadFromCI = async (licenseUUID) => {
99
106
  const packageVersion = getVersion()
107
+
108
+ enforceCachePolicy(packageVersion)
109
+
110
+ if (restoreFromCache(packageVersion, PKG_ROOT)) {
111
+ console.log(chalk.gray(`Using cached v${packageVersion} package...\n`))
112
+
113
+ return
114
+ }
115
+
100
116
  const cdnUrl = `${INSTALLATION_URL}/ci/${packageVersion}/${licenseUUID}`
101
117
  const response = await fetch(cdnUrl)
102
118
 
@@ -104,20 +120,28 @@ export const downloadFromCI = async (licenseUUID) => {
104
120
  throw new Error(`Download failed: ${response.status} ${response.statusText}`)
105
121
  }
106
122
 
107
- return await install(response.body)
123
+ return await install(response.body, packageVersion)
108
124
  }
109
125
 
110
126
  export const downloadFromCLI = async ({ account: handle, password: token }) => {
111
127
  const packageVersion = getVersion()
112
128
  const projectName = getProjectName()
113
129
 
130
+ enforceCachePolicy(packageVersion)
131
+
132
+ if (restoreFromCache(packageVersion, PKG_ROOT)) {
133
+ console.log(chalk.gray(`Using cached v${packageVersion} for project "${projectName}".\n`))
134
+
135
+ return
136
+ }
137
+
114
138
  console.log(chalk.gray(`Fetching v${packageVersion} for project "${projectName}"...\n`))
115
139
 
116
140
  const cdnUrl = `${INSTALLATION_URL}/cli/${handle}/${encodeURIComponent(projectName)}/${packageVersion}/${token}`
117
141
  const response = await fetch(cdnUrl)
118
142
 
119
143
  if (response.ok) {
120
- return await install(response.body)
144
+ return await install(response.body, packageVersion)
121
145
  }
122
146
 
123
147
  if (response.status === 401) {
@@ -134,8 +158,8 @@ export const downloadFromCLI = async ({ account: handle, password: token }) => {
134
158
  }
135
159
  }
136
160
 
137
- const install = async (stream) => {
138
- const TMP_DIR = join(__dirname, '../../_package')
161
+ const install = async (stream, packageVersion) => {
162
+ const TMP_DIR = join(PKG_ROOT, '_package')
139
163
 
140
164
  mkdirSync(TMP_DIR, { recursive: true })
141
165
 
@@ -147,9 +171,8 @@ const install = async (stream) => {
147
171
  })
148
172
  )
149
173
 
150
- for (const file of readdirSync(TMP_DIR)) {
151
- cpSync(join(TMP_DIR, file), join(__dirname, '../../', file), { recursive: true })
152
- }
174
+ copyDirectoryContents(TMP_DIR, PKG_ROOT)
175
+ saveToCache(packageVersion, TMP_DIR)
153
176
 
154
177
  rmSync(TMP_DIR, { recursive: true, force: true })
155
178
  }