-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-cache.ts
79 lines (63 loc) · 2.96 KB
/
template-cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import globalCacheDirectory from 'global-cache-dir'
import semverSort from 'semver-sort'
import { rmSync, readdirSync, renameSync, existsSync, mkdirSync } from 'node:fs'
import { join } from 'node:path'
import { execSync } from 'node:child_process'
import { log } from './helper'
import { NativeOptions } from './types'
export const cacheDirectory = await globalCacheDirectory('numic')
const removeExpiredTemplates = (version: string) => {
let templateVersions = readdirSync(cacheDirectory)
// Don't remove requested version.
templateVersions = templateVersions.filter((template) => template !== version)
// Filter dotfiles like .DS_Store
templateVersions = templateVersions.filter((item) => !/(^|\/)\.[^/.]/g.test(item))
templateVersions = semverSort.asc(templateVersions)
// Keep only the newest two versions, plus the one being installed.
templateVersions = templateVersions.slice(0, -2)
// TODO Remove appNames if more than 10, by statSync => atimeMs (access time)
templateVersions.forEach((templateVersion) =>
rmSync(join(cacheDirectory, templateVersion), { recursive: true }),
)
}
export const cacheTemplate = (nativeOptions: NativeOptions) => {
const directory = join(cacheDirectory, nativeOptions.version, nativeOptions.appName)
removeExpiredTemplates(nativeOptions.version)
if (existsSync(directory)) {
return directory
}
// When using --directory option ini init, the directory itself cannot already exist.
mkdirSync(join(directory, '..'), { recursive: true })
// Empty package.json to bypass React Native CLI dependencies validation.
// TODO necessary? writeFileSync(join(folders.numic, 'package.json'), '{ "name": "numic-native" }')
// DOC https://github.com/react-native-community/cli/blob/master/packages/cli/src/commands/init/index.ts
try {
const usesBun = typeof Bun !== 'undefined'
execSync(
`${usesBun ? 'bunx' : 'npx'} @react-native-community/cli init ${
nativeOptions.appName
} --skip-install --install-pods false --skip-git-init true --version ${
nativeOptions.version
} --directory "${join(directory, nativeOptions.appName)}"${usesBun ? ' --pm bun' : ''}`,
{
cwd: process.cwd(),
encoding: 'utf8',
// Write output to console if in debug mode.
stdio: nativeOptions.debug ? 'inherit' : 'pipe',
},
)
} catch (error) {
log(`Failed to install React Native template.\n\n${error.stdout}`, 'warning')
}
// Only keep native assets.
renameSync(join(directory, nativeOptions.appName, 'android'), join(directory, 'android'))
renameSync(join(directory, nativeOptions.appName, 'ios'), join(directory, 'ios'))
// Remove temporary project directory.
rmSync(join(directory, nativeOptions.appName), { recursive: true })
return directory
}
export const clearTemplateCache = () => {
const templates = readdirSync(cacheDirectory)
templates.forEach((template) => rmSync(join(cacheDirectory, template), { recursive: true }))
return cacheDirectory
}