Skip to content

Commit d4d8dfb

Browse files
SnaveSutitgitbutler-client
authored andcommitted
🩹 Only request fs module when needed instead of immediately on installation.
1 parent cac6e4c commit d4d8dfb

20 files changed

Lines changed: 194 additions & 142 deletions

File tree

src/constants.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import PACKAGEJSON from '../package.json'
22
import { localize as translate } from './util/lang'
33
export const PACKAGE: typeof PACKAGEJSON = PACKAGEJSON
44

5-
const FS_MODULE = requireNativeModule('fs', {
6-
message: translate('require.fs'),
7-
optional: false,
8-
})!
9-
10-
export const fs = FS_MODULE
5+
let cachedFsModule: ScopedFS | null = null
6+
export function getFsModule() {
7+
cachedFsModule ??= requireNativeModule('fs', {
8+
message: translate('require.fs'),
9+
optional: false,
10+
})!
11+
return cachedFsModule
12+
}

src/formats/ajmodel/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { registerDeletableHandlerPatch } from 'blockbench-patch-manager'
22
import { mount, unmount } from 'svelte'
3-
import { fs } from '../../constants'
3+
import { getFsModule } from '../../constants'
44
import { openUnexpectedErrorDialog } from '../../dialogs/unexpectedError/unexpectedError'
55
import { localize as translate } from '../../util/lang'
66
import { sanitizeStorageKey } from '../../util/minecraftUtil'
@@ -24,9 +24,11 @@ export async function openAJModel() {
2424
}
2525

2626
export function convertAJModelToBlueprint(path: string) {
27+
const { readFileSync } = getFsModule()
28+
2729
try {
2830
console.log(`Converting .ajmodel: ${path}`)
29-
const blueprint = upgradeAnimatedJavaBlueprint(JSON.parse(fs.readFileSync(path, 'utf8')))
31+
const blueprint = upgradeAnimatedJavaBlueprint(JSON.parse(readFileSync(path, 'utf8')))
3032

3133
const codec = BLUEPRINT_CODEC.get()
3234
if (!codec) throw new Error('Animated Java Blueprint codec is not registered!')

src/formats/blueprint/codec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type IBlueprintFormatJSON,
77
type ICollectionJSON,
88
} from '.'
9-
import { PACKAGE, fs } from '../../constants'
9+
import { PACKAGE, getFsModule } from '../../constants'
1010
import { localize as translate } from '../../util/lang'
1111
import { sanitizeStorageKey } from '../../util/minecraftUtil'
1212
import { Variant } from '../../variants'
@@ -98,17 +98,18 @@ export const BLUEPRINT_CODEC = registerDeletableHandlerPatch({
9898
if (model.textures) {
9999
for (const texture of model.textures) {
100100
const newTexture = new Texture(texture, texture.uuid).add(false)
101+
const { existsSync } = getFsModule()
101102
if (texture.relative_path && Project.save_path) {
102103
const resolvedPath = PathModule.resolve(
103104
Project.save_path,
104105
texture.relative_path
105106
)
106-
if (fs.existsSync(resolvedPath)) {
107+
if (existsSync(resolvedPath)) {
107108
newTexture.fromPath(resolvedPath)
108109
continue
109110
}
110111
}
111-
if (texture.path && fs.existsSync(texture.path) && !model.meta?.backup) {
112+
if (texture.path && existsSync(texture.path) && !model.meta?.backup) {
112113
newTexture.fromPath(texture.path)
113114
} else if (texture.source?.startsWith('data:')) {
114115
newTexture.fromDataURL(texture.source)

src/formats/blueprint/settings.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { join } from 'node:path'
2-
import { fs } from '../../constants'
2+
import { getFsModule } from '../../constants'
33
import type { ValueCheckResult } from '../../svelteComponents/sidebarDialogItems/sidebarDialogTypes'
44
import { getVersionById } from '../../systems/minecraft/versionManager'
55
import { resolvePath } from '../../util/fileUtil'
@@ -203,28 +203,30 @@ export function validateResourcePackFolder(value: string): ValueCheckResult {
203203
}
204204
}
205205

206-
if (!fs.existsSync(path)) {
206+
const { existsSync, statSync } = getFsModule()
207+
208+
if (!existsSync(path)) {
207209
return {
208210
type: 'error',
209211
message: localize('resource_pack.folder.error.does_not_exist'),
210212
}
211213
}
212214

213-
if (!fs.statSync(path).isDirectory()) {
215+
if (!statSync(path).isDirectory()) {
214216
return {
215217
type: 'error',
216218
message: localize('resource_pack.folder.error.not_a_dir'),
217219
}
218220
}
219221

220-
if (!fs.existsSync(join(path, 'pack.mcmeta'))) {
222+
if (!existsSync(join(path, 'pack.mcmeta'))) {
221223
return {
222224
type: 'error',
223225
message: localize('resource_pack.folder.error.no_pack_mcmeta'),
224226
}
225227
}
226228

227-
if (!fs.existsSync(join(path, 'assets'))) {
229+
if (!existsSync(join(path, 'assets'))) {
228230
return {
229231
type: 'warning',
230232
message: localize('resource_pack.folder.warning.no_assets'),
@@ -251,28 +253,30 @@ export function validateDataPackFolder(value: string): ValueCheckResult {
251253
}
252254
}
253255

254-
if (!fs.existsSync(path)) {
256+
const { existsSync, statSync } = getFsModule()
257+
258+
if (!existsSync(path)) {
255259
return {
256260
type: 'error',
257261
message: localize('data_pack.folder.error.does_not_exist'),
258262
}
259263
}
260264

261-
if (!fs.statSync(path).isDirectory()) {
265+
if (!statSync(path).isDirectory()) {
262266
return {
263267
type: 'error',
264268
message: localize('data_pack.folder.error.not_a_dir'),
265269
}
266270
}
267271

268-
if (!fs.existsSync(join(path, 'pack.mcmeta'))) {
272+
if (!existsSync(join(path, 'pack.mcmeta'))) {
269273
return {
270274
type: 'error',
271275
message: localize('data_pack.folder.error.no_pack_mcmeta'),
272276
}
273277
}
274278

275-
if (!fs.existsSync(join(path, 'data'))) {
279+
if (!existsSync(join(path, 'data'))) {
276280
return {
277281
type: 'warning',
278282
message: localize('data_pack.folder.warning.no_data'),

src/mods/actions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { registerPropertyOverridePatch } from 'blockbench-patch-manager'
2-
import { fs } from '../constants'
2+
import { getFsModule } from '../constants'
33
import { openBlueprintSettings } from '../dialogs/blueprintSettings/blueprintSettings'
44
import { activeProjectIsBlueprintFormat, saveBlueprint } from '../formats/blueprint'
55
import { BLUEPRINT_CODEC } from '../formats/blueprint/codec'
@@ -58,7 +58,8 @@ registerPropertyOverridePatch({
5858

5959
const path = Project.save_path || Project.export_path
6060
if (path) {
61-
if (fs.existsSync(PathModule.dirname(path))) {
61+
const { existsSync } = getFsModule()
62+
if (existsSync(PathModule.dirname(path))) {
6263
Project.save_path = path
6364
codec.write(codec.compile(), path)
6465
} else {

src/systems/cleaner.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fs } from '../constants'
1+
import { getFsModule } from '../constants'
22
import { isFunctionTagPath } from '../util/fileUtil'
33
import { type FunctionTagJSON, parseDataPackPath } from '../util/minecraftUtil'
44
import { getExportPaths } from './exporter'
@@ -14,6 +14,8 @@ export async function cleanupExportedFiles() {
1414
// modelExportFolder,
1515
// displayItemPath,
1616
} = getExportPaths()
17+
const { existsSync, promises } = getFsModule()
18+
const { unlink, mkdir, copyFile, readFile, writeFile, readdir, rm } = promises
1719

1820
if (aj.resource_pack_export_mode === 'folder') {
1921
const assetsMetaPath = PathModule.join(resourcePackFolder, 'assets.ajmeta')
@@ -31,32 +33,32 @@ export async function cleanupExportedFiles() {
3133
const removedFolders = new Set<string>()
3234
for (const file of assetsMeta.previousVersionedFiles) {
3335
if (!isFunctionTagPath(file)) {
34-
if (fs.existsSync(file)) await fs.promises.unlink(file)
36+
if (existsSync(file)) await unlink(file)
3537
} else if (aj.blueprint_id !== Project!.last_used_blueprint_id) {
3638
const resourceLocation = parseDataPackPath(file)!.resourceLocation
3739
if (
3840
resourceLocation.startsWith(
3941
`animated_java:${Project!.last_used_blueprint_id}/`
4042
) &&
41-
fs.existsSync(file)
43+
existsSync(file)
4244
) {
4345
const newPath = replacePathPart(
4446
file,
4547
Project!.last_used_blueprint_id,
4648
aj.blueprint_id
4749
)
48-
await fs.promises.mkdir(PathModule.dirname(newPath), { recursive: true })
49-
await fs.promises.copyFile(file, newPath)
50-
await fs.promises.unlink(file)
50+
await mkdir(PathModule.dirname(newPath), { recursive: true })
51+
await copyFile(file, newPath)
52+
await unlink(file)
5153
}
5254
}
5355
let folder = PathModule.dirname(file)
5456
while (
5557
!removedFolders.has(folder) &&
56-
fs.existsSync(folder) &&
57-
(await fs.promises.readdir(folder)).length === 0
58+
existsSync(folder) &&
59+
(await readdir(folder)).length === 0
5860
) {
59-
await fs.promises.rm(folder, { recursive: true })
61+
await rm(folder, { recursive: true })
6062
removedFolders.add(folder)
6163
folder = PathModule.dirname(folder)
6264
if (PathModule.basename(folder) === 'assets') break
@@ -82,7 +84,7 @@ export async function cleanupExportedFiles() {
8284
// MAX_PROGRESS.set(dataMeta.oldFiles.size)
8385
const removedFolders = new Set<string>()
8486
for (const file of [...dataMeta.previousCoreFiles, ...dataMeta.previousVersionedFiles]) {
85-
if (isFunctionTagPath(file) && fs.existsSync(file)) {
87+
if (isFunctionTagPath(file) && existsSync(file)) {
8688
if (aj.blueprint_id !== Project!.last_used_blueprint_id) {
8789
const resourceLocation = parseDataPackPath(file)!.resourceLocation
8890
if (
@@ -95,33 +97,31 @@ export async function cleanupExportedFiles() {
9597
Project!.last_used_blueprint_id,
9698
aj.blueprint_id
9799
)
98-
await fs.promises.mkdir(PathModule.dirname(newPath), { recursive: true })
99-
await fs.promises.copyFile(file, newPath)
100-
await fs.promises.unlink(file)
100+
await mkdir(PathModule.dirname(newPath), { recursive: true })
101+
await copyFile(file, newPath)
102+
await unlink(file)
101103
}
102104
}
103105
// Remove mentions of the export namespace from the file
104-
const content: FunctionTagJSON = JSON.parse(
105-
(await fs.promises.readFile(file)).toString()
106-
)
106+
const content: FunctionTagJSON = JSON.parse((await readFile(file)).toString())
107107
content.values = content.values.filter(
108108
v =>
109109
typeof v === 'string' &&
110110
(!v.startsWith(`animated_java:${aj.blueprint_id}/`) ||
111111
!v.startsWith(`animated_java:${Project!.last_used_blueprint_id}/`))
112112
)
113-
await fs.promises.writeFile(file, autoStringify(content))
113+
await writeFile(file, autoStringify(content))
114114
} else {
115115
// Delete the file
116-
if (fs.existsSync(file)) await fs.promises.unlink(file)
116+
if (existsSync(file)) await unlink(file)
117117
}
118118
let folder = PathModule.dirname(file)
119119
while (
120120
!removedFolders.has(folder) &&
121-
fs.existsSync(folder) &&
122-
(await fs.promises.readdir(folder)).length === 0
121+
existsSync(folder) &&
122+
(await readdir(folder)).length === 0
123123
) {
124-
await fs.promises.rm(folder, { recursive: true })
124+
await rm(folder, { recursive: true })
125125
removedFolders.add(folder)
126126
folder = PathModule.dirname(folder)
127127
if (PathModule.basename(folder) === 'data') break

0 commit comments

Comments
 (0)